1 package org.onap.sdc.workflow.api;
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;
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.services.types.Workflow;
36 import org.onap.sdc.workflow.services.WorkflowManager;
37 import org.onap.sdc.workflow.services.types.Page;
38 import org.onap.sdc.workflow.services.types.PagingRequest;
39 import org.onap.sdc.workflow.services.types.RequestSpec;
40 import org.onap.sdc.workflow.services.types.Sort;
41 import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
42 import org.springframework.test.web.servlet.MockMvc;
43 import org.springframework.test.web.servlet.ResultActions;
44 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
46 @RunWith(MockitoJUnitRunner.class)
47 public class WorkflowControllerTest {
49 private static final String USER_ID = "userId";
50 private static final String MISSING_USER_HEADER_ERROR =
51 "Missing request header 'USER_ID' for method parameter of type String";
52 private static final Gson GSON = new Gson();
53 private static final String DEFAULT_SORT_VALUE = "name:asc";
55 private MockMvc mockMvc;
58 private WorkflowManager workflowManagerMock;
60 private ArgumentCaptor<RequestSpec> requestSpecArg;
62 private WorkflowController workflowController;
66 mockMvc = MockMvcBuilders.standaloneSetup(workflowController)
67 .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
68 .setControllerAdvice(new ExceptionsHandler()).build();
72 public void shouldReturnErrorWhenMissingUserIdInGetReqHeader() throws Exception {
73 Workflow workflowMock = createWorkflow(1, true);
74 mockMvc.perform(get(RestPath.getWorkflowPath(workflowMock.getId())).contentType(APPLICATION_JSON))
75 .andDo(print()).andExpect(status().isBadRequest())
76 .andExpect(jsonPath("$.message", is(MISSING_USER_HEADER_ERROR)));
80 public void shouldReturnWorkflowDataWhenRequestPathIsOk() throws Exception {
81 Workflow workflowMock = createWorkflow(1, true);
82 doReturn(workflowMock).when(workflowManagerMock).get(any(Workflow.class));
83 mockMvc.perform(get(RestPath.getWorkflowPath(workflowMock.getId())).header(USER_ID_HEADER, USER_ID)
84 .contentType(APPLICATION_JSON))
85 .andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.id", is(workflowMock.getId())))
86 .andExpect(jsonPath("$.name", is(workflowMock.getName())));
90 public void shouldReturnErrorWhenMissingUserIdInListReqHeader() throws Exception {
91 mockMvc.perform(get(RestPath.getWorkflowsPath()).contentType(APPLICATION_JSON)).andDo(print())
92 .andExpect(status().isBadRequest()).andExpect(jsonPath("$.message", is(MISSING_USER_HEADER_ERROR)));
96 public void listWhenExist() throws Exception {
98 ResultActions result = mockMvc.perform(
99 get(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON))
100 .andDo(print()).andExpect(status().isOk())
101 .andExpect(jsonPath("$.items", hasSize(3)));
102 for (int i = 0; i < 3; i++) {
103 result.andExpect(jsonPath(String.format("$.items[%s].id", i), is(String.valueOf(i + 1))));
106 verify(workflowManagerMock).list(any(), requestSpecArg.capture());
107 assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, DEFAULT_LIMIT, Collections.emptyList());
111 public void listWhenPagingAndSortingAreSet() throws Exception {
113 mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "1"))
114 .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print())
115 .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
116 verify(workflowManagerMock).list(any(), requestSpecArg.capture());
117 assertRequestSpec(requestSpecArg.getValue(), 1, 2, Collections.singletonList(new Sort("name", true)));
121 public void shouldReturnResultsWithDefaultWhenLimitIsNegative() throws Exception {
123 mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "-2", "1"))
124 .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print())
125 .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
126 verify(workflowManagerMock).list(any(), requestSpecArg.capture());
127 assertRequestSpec(requestSpecArg.getValue(), 1, DEFAULT_LIMIT,
128 Collections.singletonList(new Sort("name", true)));
132 public void shouldFallbackOnDefaultOffsetWhenOffsetIsNegative() throws Exception {
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(), DEFAULT_OFFSET, 2,
139 Collections.singletonList(new Sort("name", true)));
143 public void shouldFallbackOnDefaultLimitWhenLimitIsNotAnInteger() throws Exception {
145 mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "abc", "0"))
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(), 0, DEFAULT_LIMIT,
150 Collections.singletonList(new Sort("name", true)));
154 public void shouldFallbackOnDefaultOffsetWhenOffsetIsNotAnInteger() throws Exception {
156 mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "abc"))
157 .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)).andDo(print())
158 .andExpect(jsonPath("$.items", hasSize(3)));
159 verify(workflowManagerMock).list(any(), requestSpecArg.capture());
160 assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 2,
161 Collections.singletonList(new Sort("name", true)));
165 public void shouldReturnDefaultLimitOffsetAppliedWorkflowsWhenLimitIsNotSpecified() throws Exception {
167 mockMvc.perform(get(RestPath.getWorkflowsPathNoSortAndLimit("1")).header(USER_ID_HEADER, USER_ID)
168 .contentType(APPLICATION_JSON)).andDo(print())
169 .andExpect(jsonPath("$.items", hasSize(3)));
170 verify(workflowManagerMock).list(any(), requestSpecArg.capture());
171 assertRequestSpec(requestSpecArg.getValue(), 1, DEFAULT_LIMIT, Collections.emptyList());
175 public void shouldReturnDefaultOffsetAppliedWorkflowsWhenOffsetIsNotSpecified() throws Exception {
177 mockMvc.perform(get(RestPath.getWorkflowsPathNoSortAndOffset("1")).header(USER_ID_HEADER, USER_ID)
178 .contentType(APPLICATION_JSON)).andDo(print())
179 .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
180 verify(workflowManagerMock).list(any(), requestSpecArg.capture());
181 assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 1, Collections.emptyList());
185 public void shouldCreateWorkflowWhenCallingPostRESTRequest() throws Exception {
186 Item item = new Item();
187 item.setId(new Id("abc"));
188 Workflow reqWorkflow = createWorkflow(1, false);
189 mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
190 .content(GSON.toJson(reqWorkflow))).andDo(print())
191 .andExpect(status().isCreated());
192 verify(workflowManagerMock).create(reqWorkflow);
196 public void shouldThrowExceptionWhenWorkflowNameInvalid() throws Exception {
197 Workflow reqWorkflow = new Workflow();
198 reqWorkflow.setName("Invalid workflow name %");
199 mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
200 .content(GSON.toJson(reqWorkflow))).andDo(print())
201 .andExpect(status().isBadRequest())
202 .andExpect(jsonPath("$.message", is("Workflow name must contain only letters, digits and underscores")));
205 private void mockManagerList3() {
206 doReturn(new Page<>(Arrays.asList(createWorkflow(1, true), createWorkflow(2, true), createWorkflow(3, true)),
207 new PagingRequest(DEFAULT_OFFSET, DEFAULT_LIMIT), 3)).when(workflowManagerMock).list(any(), any());
210 private static void assertRequestSpec(RequestSpec actual, int expectedOffset, int expectedLimit,
211 List<Sort> expectedSorts) {
212 assertEquals(Integer.valueOf(expectedOffset), actual.getPaging().getOffset());
213 assertEquals(Integer.valueOf(expectedLimit), actual.getPaging().getLimit());
214 if (expectedSorts.isEmpty()) {
215 assertEquals(expectedSorts, actual.getSorting().getSorts());
217 for (int i = 0; i < expectedSorts.size(); i++) {
218 assertEquals(expectedSorts.get(i), actual.getSorting().getSorts().get(i));