f8d2aec8c6049bf83a9df8f2e9d7e44f95797203
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / test / java / org / onap / sdc / workflow / api / WorkflowControllerTest.java
1 package org.onap.sdc.workflow.api;
2
3 import static org.hamcrest.Matchers.is;
4 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
5 import static org.junit.Assert.assertEquals;
6 import static org.mockito.ArgumentMatchers.any;
7 import static org.mockito.Mockito.doReturn;
8 import static org.mockito.Mockito.verify;
9 import static org.onap.sdc.workflow.TestUtil.createWorkflow;
10 import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER;
11 import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_LIMIT;
12 import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_OFFSET;
13 import static org.springframework.http.MediaType.APPLICATION_JSON;
14 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
15 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
16 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
17 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
18 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
19
20 import com.amdocs.zusammen.datatypes.Id;
21 import com.amdocs.zusammen.datatypes.item.Item;
22 import com.google.gson.Gson;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.Captor;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.onap.sdc.workflow.RestPath;
35 import org.onap.sdc.workflow.api.exceptionshandlers.CustomizedResponseEntityExceptionHandler;
36 import org.onap.sdc.workflow.persistence.types.Workflow;
37 import org.onap.sdc.workflow.services.WorkflowManager;
38 import org.onap.sdc.workflow.services.types.Page;
39 import org.onap.sdc.workflow.services.types.PagingRequest;
40 import org.onap.sdc.workflow.services.types.RequestSpec;
41 import org.onap.sdc.workflow.services.types.Sort;
42 import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
43 import org.springframework.mock.web.MockHttpServletResponse;
44 import org.springframework.test.web.servlet.MockMvc;
45 import org.springframework.test.web.servlet.ResultActions;
46 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class WorkflowControllerTest {
50
51
52     private static final String MISSING_REQUEST_HEADER_ERRROR_FORMAT =
53             "Missing request header '%s' for method parameter of type String";
54     private static final String USER_ID = "userId";
55     private static final Gson GSON = new Gson();
56     private static final String DEFAULT_SORT_VALUE = "name:asc";
57
58     private MockMvc mockMvc;
59
60
61     @Mock
62     private WorkflowManager workflowManagerMock;
63     @Captor
64     private ArgumentCaptor<RequestSpec> requestSpecArg;
65     @InjectMocks
66     private WorkflowController workflowController;
67
68
69     @Before
70     public void setUp() {
71         mockMvc = MockMvcBuilders.standaloneSetup(workflowController)
72                                  .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
73                                  .setControllerAdvice(new CustomizedResponseEntityExceptionHandler()).build();
74     }
75
76     @Test
77     public void shouldReturnErrorWhenMissingUserIdInGetReqHeader() throws Exception {
78         Workflow workflowMock = createWorkflow(1, true);
79         MockHttpServletResponse response =
80                 mockMvc.perform(get(RestPath.getWorkflowPath(workflowMock.getId())).contentType(APPLICATION_JSON))
81                        .andDo(print()).andExpect(status().isBadRequest()).andExpect(status().is(400)).andReturn()
82                        .getResponse();
83         assertEquals(String.format(MISSING_REQUEST_HEADER_ERRROR_FORMAT, USER_ID_HEADER),
84                 response.getContentAsString());
85     }
86
87     @Test
88     public void shouldReturnWorkflowDataWhenRequestPathIsOk() throws Exception {
89         Workflow workflowMock = createWorkflow(1, true);
90         doReturn(workflowMock).when(workflowManagerMock).get(any(Workflow.class));
91         mockMvc.perform(get(RestPath.getWorkflowPath(workflowMock.getId())).header(USER_ID_HEADER, USER_ID)
92                                                                            .contentType(APPLICATION_JSON))
93                .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.id", is(workflowMock.getId())))
94                .andExpect(jsonPath("$.name", is(workflowMock.getName())));
95     }
96
97     @Test
98     public void shouldReturnErrorWhenMissingUserIdInListReqHeader() throws Exception {
99         MockHttpServletResponse response =
100                 mockMvc.perform(get(RestPath.getWorkflowsPath()).contentType(APPLICATION_JSON)).andDo(print())
101                        .andExpect(status().isBadRequest()).andExpect(status().is(400)).andReturn().getResponse();
102         assertEquals(String.format(MISSING_REQUEST_HEADER_ERRROR_FORMAT, USER_ID_HEADER),
103                 response.getContentAsString());
104     }
105
106     @Test
107     public void listWhenExist() throws Exception {
108         mockManagerList3();
109         ResultActions result = mockMvc.perform(
110                 get(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON))
111                                       .andDo(print()).andExpect(status().isOk())
112                                       .andExpect(jsonPath("$.items", hasSize(3)));
113         for (int i = 0; i < 3; i++) {
114             result.andExpect(jsonPath(String.format("$.items[%s].id", i), is(String.valueOf(i + 1))));
115         }
116
117         verify(workflowManagerMock).list(any(), requestSpecArg.capture());
118         assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, DEFAULT_LIMIT, Collections.emptyList());
119     }
120
121     @Test
122     public void listWhenPagingAndSortingAreSet() throws Exception {
123         mockManagerList3();
124         mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "1"))
125                                 .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print())
126                .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
127         verify(workflowManagerMock).list(any(), requestSpecArg.capture());
128         assertRequestSpec(requestSpecArg.getValue(), 1, 2, Collections.singletonList(new Sort("name", true)));
129     }
130
131     @Test
132     public void shouldReturnResultsWithDefaultWhenLimitIsNegative() throws Exception {
133         mockManagerList3();
134         mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "-2", "1"))
135                                 .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print())
136                .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
137         verify(workflowManagerMock).list(any(), requestSpecArg.capture());
138         assertRequestSpec(requestSpecArg.getValue(), 1, DEFAULT_LIMIT,
139                 Collections.singletonList(new Sort("name", true)));
140     }
141
142     @Test
143     public void shouldFallbackOnDefaultOffsetWhenOffsetIsNegative() throws Exception {
144         mockManagerList3();
145         mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "-1"))
146                                 .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print())
147                .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
148         verify(workflowManagerMock).list(any(), requestSpecArg.capture());
149         assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 2,
150                 Collections.singletonList(new Sort("name", true)));
151     }
152
153     @Test
154     public void shouldFallbackOnDefaultLimitWhenLimitIsNotAnInteger() throws Exception {
155         mockManagerList3();
156         mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "abc", "0"))
157                                 .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print())
158                .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
159         verify(workflowManagerMock).list(any(), requestSpecArg.capture());
160         assertRequestSpec(requestSpecArg.getValue(), 0, DEFAULT_LIMIT,
161                 Collections.singletonList(new Sort("name", true)));
162     }
163
164     @Test
165     public void shouldFallbackOnDefaultOffsetWhenOffsetIsNotAnInteger() throws Exception {
166         mockManagerList3();
167         mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "abc"))
168                                 .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print())
169                .andExpect(jsonPath("$.items", hasSize(3)));
170         verify(workflowManagerMock).list(any(), requestSpecArg.capture());
171         assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 2,
172                 Collections.singletonList(new Sort("name", true)));
173     }
174
175     @Test
176     public void shouldReturnDefaultLimitOffsetAppliedWorkflowsWhenLimitIsNotSpecified() throws Exception {
177         mockManagerList3();
178         mockMvc.perform(get(RestPath.getWorkflowsPathNoSortAndLimit("1")).header(USER_ID_HEADER, USER_ID)
179                                                                          .contentType(APPLICATION_JSON)).andDo(print())
180                .andExpect(jsonPath("$.items", hasSize(3)));
181         verify(workflowManagerMock).list(any(), requestSpecArg.capture());
182         assertRequestSpec(requestSpecArg.getValue(), 1, DEFAULT_LIMIT, Collections.emptyList());
183     }
184
185     @Test
186     public void shouldReturnDefaultOffsetAppliedWorkflowsWhenOffsetIsNotSpecified() throws Exception {
187         mockManagerList3();
188         mockMvc.perform(get(RestPath.getWorkflowsPathNoSortAndOffset("1")).header(USER_ID_HEADER, USER_ID)
189                                                                           .contentType(APPLICATION_JSON)).andDo(print())
190                .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
191         verify(workflowManagerMock).list(any(), requestSpecArg.capture());
192         assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 1, Collections.emptyList());
193     }
194
195     @Test
196     public void shouldCreateWorkflowWhenCallingPostRESTRequest() throws Exception {
197         Item item = new Item();
198         item.setId(new Id("abc"));
199         Workflow reqWorkflow = createWorkflow(1, false);
200         mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
201                                                          .content(GSON.toJson(reqWorkflow))).andDo(print())
202                .andExpect(status().isCreated());
203         verify(workflowManagerMock).create(reqWorkflow);
204     }
205
206     @Test
207     public void shouldThrowExceptionWhenWorkflowNameInvalid() throws Exception {
208
209         Workflow reqWorkflow = new Workflow();
210         reqWorkflow.setName("Invalid workflow name %");
211         MockHttpServletResponse response = mockMvc.perform(
212                 post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
213                                                  .content(GSON.toJson(reqWorkflow))).andDo(print())
214                                                   .andExpect(status().isBadRequest()).andReturn().getResponse();
215         assertEquals("Workflow name must contain only letters, digits and underscores", response.getContentAsString());
216     }
217
218     private void mockManagerList3() {
219         doReturn(new Page<>(Arrays.asList(
220                 createWorkflow(1, true),
221                 createWorkflow(2, true),
222                 createWorkflow(3, true)),
223                 new PagingRequest(DEFAULT_OFFSET, DEFAULT_LIMIT), 3))
224                 .when(workflowManagerMock).list(any(), any());
225     }
226
227     private static void assertRequestSpec(RequestSpec actual, int expectedOffset, int expectedLimit,
228             List<Sort> expectedSorts) {
229         assertEquals(Integer.valueOf(expectedOffset), actual.getPaging().getOffset());
230         assertEquals(Integer.valueOf(expectedLimit), actual.getPaging().getLimit());
231         if (expectedSorts.isEmpty()) {
232             assertEquals(expectedSorts, actual.getSorting().getSorts());
233         } else {
234             for (int i = 0; i < expectedSorts.size(); i++) {
235                 assertEquals(expectedSorts.get(i), actual.getSorting().getSorts().get(i));
236             }
237         }
238     }
239 }