Version Artifact API
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / test / java / org / onap / sdc / workflow / api / WorkflowVersionControllerTest.java
1 package org.onap.sdc.workflow.api;
2
3 import static org.hamcrest.Matchers.equalTo;
4 import static org.hamcrest.Matchers.is;
5 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
6 import static org.junit.Assert.assertEquals;
7 import static org.mockito.Mockito.doReturn;
8 import static org.mockito.Mockito.times;
9 import static org.mockito.Mockito.verify;
10 import static org.springframework.http.MediaType.APPLICATION_JSON;
11 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
12 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
13 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
14 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
15 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
16 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
17
18 import com.google.gson.Gson;
19 import java.util.Arrays;
20 import java.util.List;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.onap.sdc.workflow.RestPath;
28 import org.onap.sdc.workflow.api.impl.WorkflowVersionControllerImpl;
29 import org.onap.sdc.workflow.api.types.VersionRequestDto;
30 import org.onap.sdc.workflow.persistence.types.WorkflowVersion;
31 import org.onap.sdc.workflow.services.WorkflowVersionManager;
32 import org.openecomp.sdc.versioning.dao.types.Version;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.mock.web.MockHttpServletResponse;
35 import org.springframework.test.web.servlet.MockMvc;
36 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class WorkflowVersionControllerTest {
40
41     private static final String USER_ID = "cs0008";
42     private static final String ITEM1_ID = "item_id_1";
43     private static final String VERSION1_ID = "version_id_1";
44     private static final String VERSION2_ID = "version_id_2";
45     private List<Version> versionList;
46
47     private static final Gson GSON = new Gson();
48
49     private MockMvc mockMvc;
50
51     @Mock
52     private WorkflowVersionManager workflowVersionManagerMock;
53
54     @InjectMocks
55     private WorkflowVersionControllerImpl workflowVersionController;
56
57     @Before
58     public void setUp() {
59         versionList = Arrays.asList( new Version(VERSION1_ID),new Version(VERSION2_ID));
60         mockMvc = MockMvcBuilders.standaloneSetup(workflowVersionController).build();
61     }
62
63     @Test
64     public void shouldReturnWorkflowVersionListWhenCallingVersionGetREST() throws Exception {
65
66         doReturn(versionList).when(workflowVersionManagerMock).list(ITEM1_ID);
67         mockMvc.perform(get(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID)
68                                                                    .contentType(APPLICATION_JSON)).andExpect(status().isOk())
69                .andExpect(jsonPath("$.results", hasSize(2)))
70                .andExpect(jsonPath("$.results[0].id", equalTo(VERSION1_ID)))
71                .andExpect(jsonPath("$.results[1].id", equalTo(VERSION2_ID)));
72
73         verify(workflowVersionManagerMock, times(1)).list(ITEM1_ID);
74     }
75
76
77     @Test
78     public void shouldCreateWorkflowVersionWhenCallingVersionsPostREST() throws Exception {
79
80         VersionRequestDto version = new VersionRequestDto();
81         version.setDescription("VersionDescription");
82         mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID)
83                                                                     .contentType(APPLICATION_JSON)
84                                                                     .content(GSON.toJson(version)))
85                .andExpect(status().isCreated());
86
87         verify(workflowVersionManagerMock, times(1)).create(ITEM1_ID, version);
88     }
89
90
91     @Test
92     public void shouldReturnWorkflowVersionWhenExists() throws Exception {
93         WorkflowVersion version = new WorkflowVersion(VERSION1_ID);
94         doReturn(version).when(workflowVersionManagerMock).get(ITEM1_ID, VERSION1_ID);
95         mockMvc.perform(
96                 get(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID)
97                                                                        .contentType(APPLICATION_JSON)).andDo(print())
98                .andExpect(status().isOk()).andExpect(jsonPath("$.id", is(version.getId())));
99         verify(workflowVersionManagerMock, times(1)).get(ITEM1_ID, VERSION1_ID);
100     }
101
102     @Test
103     public void shouldUpdateWorkflowVersionWhenCallingPutREST() throws Exception {
104         WorkflowVersion version = new WorkflowVersion();
105         version.setDescription("Updated");
106
107         MockHttpServletResponse result = mockMvc.perform(
108                 put(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID)
109                                                                        .contentType(APPLICATION_JSON)
110                                                                        .content(GSON.toJson(version))).andReturn()
111                                                 .getResponse();
112
113         assertEquals(HttpStatus.OK.value(), result.getStatus());
114         version.setId(VERSION1_ID);
115         verify(workflowVersionManagerMock, times(1)).update(ITEM1_ID, version);
116
117     }
118
119 }