aboutsummaryrefslogtreecommitdiffstats
path: root/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java
blob: 5e0f61d04df2a0cfd19604107f93cb35fd02511b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.sdc.workflow.api;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.onap.sdc.workflow.TestUtil.createWorkflow;
import static org.onap.sdc.workflow.api.RestParams.USER_ID_HEADER;
import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_LIMIT;
import static org.onap.sdc.workflow.services.types.PagingConstants.DEFAULT_OFFSET;
import static org.onap.sdc.workflow.services.types.WorkflowValidationConstants.MAX_LENGTH;
import static org.onap.sdc.workflow.services.types.WorkflowValidationConstants.MIN_LENGTH;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.amdocs.zusammen.datatypes.Id;
import com.amdocs.zusammen.datatypes.item.Item;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.sdc.workflow.RestPath;
import org.onap.sdc.workflow.server.resolvers.UserIdResolver;
import org.onap.sdc.workflow.services.WorkflowManager;
import org.onap.sdc.workflow.services.types.ArchivingStatus;
import org.onap.sdc.workflow.services.types.Page;
import org.onap.sdc.workflow.services.types.PagingRequest;
import org.onap.sdc.workflow.services.types.RequestSpec;
import org.onap.sdc.workflow.services.types.Sort;
import org.onap.sdc.workflow.services.types.Workflow;
import org.onap.sdc.workflow.services.utilities.JsonUtil;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@RunWith(MockitoJUnitRunner.class)
public class WorkflowControllerTest {

    private static final String USER_ID = "userId";
    private static final String MISSING_USER_HEADER_ERROR = "Missing mandatory request header 'USER_ID'";
    private static final String DEFAULT_SORT_VALUE = "name:asc";

    private MockMvc mockMvc;

    @Mock
    private WorkflowManager workflowManagerMock;
    @Captor
    private ArgumentCaptor<RequestSpec> requestSpecArg;
    @InjectMocks
    private WorkflowController workflowController;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.standaloneSetup(workflowController)
                                 .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
                                 .setCustomArgumentResolvers(new UserIdResolver())
                                 .setControllerAdvice(new ExceptionsHandler()).build();
    }

    @Test
    public void shouldReturnErrorWhenMissingUserIdInGetReqHeader() throws Exception {
        Workflow workflowMock = createWorkflow(1, true, ArchivingStatus.ACTIVE);
        mockMvc.perform(get(RestPath.getWorkflowPath(workflowMock.getId())).contentType(APPLICATION_JSON))
               .andExpect(status().isBadRequest()).andExpect(jsonPath("$.message", is(MISSING_USER_HEADER_ERROR)));
    }

    @Test
    public void shouldReturnWorkflowDataWhenRequestPathIsOk() throws Exception {
        Workflow workflowMock = createWorkflow(1, true, ArchivingStatus.ACTIVE);
        doReturn(workflowMock).when(workflowManagerMock).get(any(Workflow.class));
        mockMvc.perform(get(RestPath.getWorkflowPath(workflowMock.getId())).header(USER_ID_HEADER, USER_ID)
                                                                           .contentType(APPLICATION_JSON))
               .andExpect(status().isOk()).andExpect(jsonPath("$.id", is(workflowMock.getId())))
               .andExpect(jsonPath("$.name", is(workflowMock.getName())));
    }

    @Test
    public void shouldReturnErrorWhenMissingUserIdInListReqHeader() throws Exception {
        mockMvc.perform(get(RestPath.getWorkflowsPath()).contentType(APPLICATION_JSON))
               .andExpect(status().isBadRequest()).andExpect(jsonPath("$.message", is(MISSING_USER_HEADER_ERROR)));
    }

    @Test
    public void shouldReturnOkWhenArchivingWorkflow() throws Exception {
        Workflow workflowMock = createWorkflow(1, true, ArchivingStatus.ACTIVE);
        mockMvc.perform(post(RestPath.getArchiveWorkflowPath(workflowMock.getId())).header(USER_ID_HEADER, USER_ID)
                                                                                   .contentType(APPLICATION_JSON)
                                                                                   .content(
                                                                                           "{\"status\": \"ARCHIVED\"}"))
               .andExpect(status().isOk());
        verify(workflowManagerMock).updateStatus(workflowMock.getId(), ArchivingStatus.ARCHIVED);
    }

    @Test
    public void shouldReturnOkWhenRestoringWorkflow() throws Exception {
        Workflow workflowMock = createWorkflow(1, true, ArchivingStatus.ACTIVE);
        mockMvc.perform(post(RestPath.getArchiveWorkflowPath(workflowMock.getId())).header(USER_ID_HEADER, USER_ID)
                                                                                   .contentType(APPLICATION_JSON)
                                                                                   .content("{\"status\": \"ACTIVE\"}"))
               .andExpect(status().isOk());
        verify(workflowManagerMock).updateStatus(workflowMock.getId(), ArchivingStatus.ACTIVE);
    }

    @Test
    public void listWhenExist() throws Exception {
        mockManagerList3();
        ResultActions result = mockMvc.perform(
                get(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON))
                                      .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
        for (int i = 0; i < 3; i++) {
            result.andExpect(jsonPath(String.format("$.items[%s].id", i), is(String.valueOf(i + 1))));
        }

        verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture());
        assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, DEFAULT_LIMIT, Collections.emptyList());
    }

    @Test
    public void listWhenPagingAndSortingAreSet() throws Exception {
        mockManagerList3();
        mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "1"))
                                .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON))
               .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
        verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture());
        assertRequestSpec(requestSpecArg.getValue(), 1, 2,
                Collections.singletonList(new Sort("name", true)));
    }

    @Test
    public void shouldReturnResultsWithDefaultWhenLimitIsNegative() throws Exception {
        mockManagerList3();
        mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "-2", "1"))
                                .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON))
               .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
        verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture());
        assertRequestSpec(requestSpecArg.getValue(), 1, DEFAULT_LIMIT,
                Collections.singletonList(new Sort("name", true)));
    }

    @Test
    public void shouldFallbackOnDefaultOffsetWhenOffsetIsNegative() throws Exception {
        mockManagerList3();
        mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "-1"))
                                .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON))
               .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
        verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture());
        assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 2,
                Collections.singletonList(new Sort("name", true)));
    }

    @Test
    public void shouldFallbackOnDefaultLimitWhenLimitIsNotAnInteger() throws Exception {
        mockManagerList3();
        mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "abc", "0"))
                                .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON))
               .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
        verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture());
        assertRequestSpec(requestSpecArg.getValue(), 0, DEFAULT_LIMIT,
                Collections.singletonList(new Sort("name", true)));
    }

    @Test
    public void shouldFallbackOnDefaultOffsetWhenOffsetIsNotAnInteger() throws Exception {
        mockManagerList3();
        mockMvc.perform(get(RestPath.getWorkflowsPathAllQueryParams(DEFAULT_SORT_VALUE, "2", "abc"))
                                .header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON))
               .andExpect(jsonPath("$.items", hasSize(3)));
        verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture());
        assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 2,
                Collections.singletonList(new Sort("name", true)));
    }

    @Test
    public void shouldReturnDefaultLimitOffsetAppliedWorkflowsWhenLimitIsNotSpecified() throws Exception {
        mockManagerList3();
        mockMvc.perform(get(RestPath.getWorkflowsPathNoSortAndLimit("1")).header(USER_ID_HEADER, USER_ID)
                                                                         .contentType(APPLICATION_JSON))
               .andExpect(jsonPath("$.items", hasSize(3)));
        verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture());
        assertRequestSpec(requestSpecArg.getValue(), 1, DEFAULT_LIMIT, Collections.emptyList());
    }

    @Test
    public void shouldReturnDefaultOffsetAppliedWorkflowsWhenOffsetIsNotSpecified() throws Exception {
        mockManagerList3();
        mockMvc.perform(get(RestPath.getWorkflowsPathNoSortAndOffset("1")).header(USER_ID_HEADER, USER_ID)
                                                                          .contentType(APPLICATION_JSON))
               .andExpect(status().isOk()).andExpect(jsonPath("$.items", hasSize(3)));
        verify(workflowManagerMock).list(any(), any(), any(), requestSpecArg.capture());
        assertRequestSpec(requestSpecArg.getValue(), DEFAULT_OFFSET, 1, Collections.emptyList());
    }

    @Test
    public void shouldCreateWorkflowWhenCallingPostRestRequest() throws Exception {
        Item item = new Item();
        item.setId(new Id("abc"));
        Workflow reqWorkflow = createWorkflow(1, false, any());
        mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
                                                         .content(JsonUtil.object2Json(reqWorkflow)))
               .andExpect(status().isCreated());
        verify(workflowManagerMock).create(reqWorkflow);
    }

    @Test
    public void shouldThrowExceptionWhenWorkflowNameInvalid() throws Exception {
        Workflow reqWorkflow = new Workflow();
        reqWorkflow.setName("Invalid workflow name %");
        mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
                                                         .content(JsonUtil.object2Json(reqWorkflow)))
               .andExpect(status().isBadRequest()).andExpect(
                jsonPath("$.message", is("Workflow name must contain only letters, digits and underscores.")));
    }

    @Test
    public void shouldThrowExceptionWhenWorkflowNameBlank() throws Exception {
        Workflow reqWorkflow = new Workflow();
        reqWorkflow.setName("  ");
        mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
                                                         .content(JsonUtil.object2Json(reqWorkflow)))
               .andExpect(status().isBadRequest());
    }

    @Test
    public void shouldThrowExceptionWhenWorkflowNameNull() throws Exception {
        Workflow reqWorkflow = new Workflow();
        reqWorkflow.setName(null);
        mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
                                                         .content(JsonUtil.object2Json(reqWorkflow)))
               .andExpect(status().isBadRequest());
    }

    @Test
    public void shouldThrowExceptionWhenWorkflowNameEmptyString() throws Exception {
        Workflow reqWorkflow = new Workflow();
        reqWorkflow.setName("");
        mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
                                                         .content(JsonUtil.object2Json(reqWorkflow)))
               .andExpect(status().isBadRequest());
    }

    @Test
    public void shouldThrowExceptionWhenWorkflowNameMoreThanMax() throws Exception {
        Workflow reqWorkflow = new Workflow();
        reqWorkflow.setName("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
                                                         .content(JsonUtil.object2Json(reqWorkflow)))
               .andExpect(status().isBadRequest()).andExpect(jsonPath("$.message",
                is("Workflow name must be at least " + MIN_LENGTH + " characters, and no more than " + MAX_LENGTH
                           + " characters.")));
    }

    @Test
    public void shouldThrowExceptionWhenWorkflowNameLessThanMin() throws Exception {
        Workflow reqWorkflow = new Workflow();
        reqWorkflow.setName("AAA");
        mockMvc.perform(post(RestPath.getWorkflowsPath()).header(USER_ID_HEADER, USER_ID).contentType(APPLICATION_JSON)
                                                         .content(JsonUtil.object2Json(reqWorkflow)))
               .andExpect(status().isBadRequest()).andExpect(jsonPath("$.message",
                is("Workflow name must be at least " + MIN_LENGTH + " characters, and no more than " + MAX_LENGTH
                           + " characters.")));
    }

    private void mockManagerList3() {
        doReturn(new Page<>(Arrays.asList(createWorkflow(1, true, ArchivingStatus.ACTIVE),
                createWorkflow(2, true, ArchivingStatus.ACTIVE), createWorkflow(3, true, ArchivingStatus.ACTIVE)),
                new PagingRequest(DEFAULT_OFFSET, DEFAULT_LIMIT), 3)).when(workflowManagerMock)
                                                                     .list(any(), any(), any(), any());
    }

    private static void assertRequestSpec(RequestSpec actual, int expectedOffset, int expectedLimit,
            List<Sort> expectedSorts) {
        assertEquals(Integer.valueOf(expectedOffset), actual.getPaging().getOffset());
        assertEquals(Integer.valueOf(expectedLimit), actual.getPaging().getLimit());
        if (expectedSorts.isEmpty()) {
            assertEquals(expectedSorts, actual.getSorting().getSorts());
        } else {
            for (int i = 0; i < expectedSorts.size(); i++) {
                assertEquals(expectedSorts.get(i), actual.getSorting().getSorts().get(i));
            }
        }
    }
}