Merge "AaiController tests"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / MsoControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.controller;
22
23 import static java.lang.String.format;
24 import static org.mockito.ArgumentMatchers.argThat;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.BDDMockito.given;
27 import static org.mockito.BDDMockito.then;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.only;
30 import static org.mockito.Mockito.when;
31 import static org.springframework.http.MediaType.APPLICATION_JSON;
32 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
33 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
34 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
35 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
36
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import java.util.LinkedHashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.stream.Collectors;
43 import org.jeasy.random.EasyRandom;
44 import org.jeasy.random.EasyRandomParameters;
45 import org.jeasy.random.randomizers.misc.BooleanRandomizer;
46 import org.jeasy.random.randomizers.text.StringRandomizer;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.onap.vid.model.RequestReferencesContainer;
50 import org.onap.vid.mso.MsoBusinessLogic;
51 import org.onap.vid.mso.MsoResponseWrapper;
52 import org.onap.vid.mso.MsoResponseWrapper2;
53 import org.onap.vid.mso.RestObject;
54 import org.onap.vid.mso.rest.MsoRestClientNew;
55 import org.onap.vid.mso.rest.Request;
56 import org.onap.vid.mso.rest.RequestDetails;
57 import org.onap.vid.mso.rest.RequestDetailsWrapper;
58 import org.onap.vid.mso.rest.Task;
59 import org.onap.vid.services.CloudOwnerService;
60 import org.springframework.test.web.servlet.MockMvc;
61 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
62
63 public class MsoControllerTest {
64
65     private static final long STATIC_SEED = 5336L;
66     private EasyRandomParameters parameters = new EasyRandomParameters()
67         .randomize(Boolean.class, new BooleanRandomizer(STATIC_SEED))
68         .randomize(String.class, new StringRandomizer(4, 4, STATIC_SEED))
69         .collectionSizeRange(2, 3);
70     private EasyRandom modelGenerator = new EasyRandom(parameters);
71     private ObjectMapper objectMapper = new ObjectMapper();
72
73     private MockMvc mockMvc;
74     private MsoBusinessLogic msoBusinessLogic;
75     private CloudOwnerService cloudService;
76     private MsoRestClientNew msoRestClient;
77
78     @Before
79     public void setUp() {
80         msoBusinessLogic = mock(MsoBusinessLogic.class);
81         cloudService = mock(CloudOwnerService.class);
82         msoRestClient = mock(MsoRestClientNew.class);
83         MsoController msoController = new MsoController(msoBusinessLogic, msoRestClient, cloudService);
84
85         mockMvc = MockMvcBuilders.standaloneSetup(msoController).build();
86     }
87
88     @Test
89     public void shouldGetOrchestrationRequest() throws Exception {
90         // given
91         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
92         String requestId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
93
94         MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
95         given(msoBusinessLogic
96             .getOrchestrationRequest(requestId))
97             .willReturn(expectedResponse);
98
99         // when & then
100         mockMvc.perform(get(format("/mso/mso_get_orch_req/%s", requestId))
101             .content(asJson(requestDetails))
102             .contentType(APPLICATION_JSON))
103             .andExpect(status().isOk())
104             .andExpect(content().json(asJson(expectedResponse)));
105
106         then(cloudService).shouldHaveZeroInteractions();
107     }
108
109     @Test
110     public void shouldDelegateNewServiceInstantiation() throws Exception {
111         // given
112         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
113
114         MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
115         given(msoBusinessLogic
116             .createSvcInstance(objectEqualTo(requestDetails)))
117             .willReturn(expectedResponse);
118
119         // when & then
120         mockMvc.perform(post("/mso/mso_create_svc_instance")
121             .content(asJson(requestDetails))
122             .contentType(APPLICATION_JSON))
123             .andExpect(status().isOk())
124             .andExpect(content().json(asJson(expectedResponse)));
125
126         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
127     }
128
129     @Test
130     public void shouldCreateVolumeInstance() throws Exception {
131         // given
132         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
133         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
134         String vnfInstanceId = "fe9000-0009-9999";
135
136         MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
137         given(msoBusinessLogic
138             .createVolumeGroupInstance(objectEqualTo(requestDetails), eq(serviceInstanceId), eq(vnfInstanceId)))
139             .willReturn(expectedResponse);
140
141         // when & then
142         mockMvc.perform(post(format("/mso/mso_create_volumegroup_instance/%s/vnfs/%s", serviceInstanceId, vnfInstanceId))
143             .content(asJson(requestDetails))
144             .contentType(APPLICATION_JSON))
145             .andExpect(status().isOk())
146             .andExpect(content().json(asJson(expectedResponse)));
147
148         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
149     }
150
151     @Test
152     public void shouldReturnOrchestrationRequestsForDashboard() throws Exception {
153         // given
154         List<Request> orchestrationRequests = modelGenerator
155             .objects(Request.class, 2)
156             .collect(Collectors.toList());
157
158         given(msoBusinessLogic.getOrchestrationRequestsForDashboard()).willReturn(orchestrationRequests);
159
160         // when & then
161         mockMvc.perform(get("/mso/mso_get_orch_reqs/dashboard"))
162             .andExpect(status().isOk())
163             .andExpect(content().json(objectMapper.writeValueAsString(orchestrationRequests)));
164
165         then(cloudService).shouldHaveZeroInteractions();
166     }
167
168     @Test
169     public void shouldReturnManualTasksById() throws Exception {
170         // given
171         List<Task> manualTasks = modelGenerator
172             .objects(Task.class, 2)
173             .collect(Collectors.toList());
174
175         String originalRequestId = "za1234d1-5a33-55df-13ab-12abad84e335";
176         given(msoBusinessLogic.getManualTasksByRequestId(originalRequestId)).willReturn(manualTasks);
177
178         // when & then
179         mockMvc.perform(get("/mso/mso_get_man_task/{id}", originalRequestId))
180             .andExpect(status().isOk())
181             .andExpect(content().json(objectMapper.writeValueAsString(manualTasks)));
182
183         then(cloudService).shouldHaveZeroInteractions();
184     }
185
186     @Test
187     public void shouldDelegateE2EServiceInstantiation() throws Exception {
188         // given
189         String requestDetails = "some request details";
190         Map<String, Object> payload = new LinkedHashMap<>();
191         payload.put("requestDetails", requestDetails);
192
193         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
194         given(wrapper.getResponse()).willReturn("some response");
195         given(msoBusinessLogic.createE2eSvcInstance(requestDetails)).willReturn(wrapper);
196
197         // when & then
198         mockMvc.perform(post("/mso/mso_create_e2e_svc_instance")
199             .content(asJson(payload))
200             .contentType(APPLICATION_JSON))
201             .andExpect(status().isOk())
202             .andExpect(content().string("some response"));
203
204         then(cloudService).shouldHaveZeroInteractions();
205     }
206
207     @Test
208     public void shouldDelegateServiceInstantiation() throws Exception {
209         // given
210         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
211
212         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
213         given(wrapper.getResponse()).willReturn("some response");
214         given(msoBusinessLogic.createSvcInstance(objectEqualTo(requestDetails))).willReturn(wrapper);
215
216         // when & then
217         mockMvc.perform(post("/mso/mso_create_svc_instance")
218             .content(asJson(requestDetails))
219             .contentType(APPLICATION_JSON))
220             .andExpect(status().isOk())
221             .andExpect(content().string("some response"));
222
223         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
224     }
225
226     @Test
227     public void shouldDelegateVnfInstantiation() throws Exception {
228         // given
229         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
230         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
231
232         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
233         given(wrapper.getResponse()).willReturn("some response");
234         given(msoBusinessLogic.createVnf(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
235
236         // when & then
237         mockMvc.perform(post("/mso/mso_create_vnf_instance/" + serviceInstanceId)
238             .content(asJson(requestDetails))
239             .contentType(APPLICATION_JSON))
240             .andExpect(status().isOk())
241             .andExpect(content().string("some response"));
242
243         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
244     }
245
246     @Test
247     public void shouldDeleteVfModuleInstance() throws Exception {
248         // given
249         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
250         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
251         String vnfInstanceId = "fe9000-0009-9999";
252         String vfModuleId = "abeeee-abeeee-abeeee";
253
254         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
255         given(wrapper.getResponse()).willReturn("some response");
256         given(msoBusinessLogic.deleteVfModule(objectEqualTo(requestDetails), eq(serviceInstanceId), eq(vnfInstanceId), eq(vfModuleId))).willReturn(wrapper);
257
258         // when & then
259         mockMvc.perform(post(format("/mso/mso_delete_vfmodule_instance/%s/vnfs/%s/vfModules/%s", serviceInstanceId, vnfInstanceId, vfModuleId))
260             .content(asJson(requestDetails))
261             .contentType(APPLICATION_JSON))
262             .andExpect(status().isOk())
263             .andExpect(content().string("some response"));
264
265         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
266     }
267
268     @Test
269     public void shouldDeleteVolumeGroup() throws Exception {
270         // given
271         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
272         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
273         String vnfInstanceId = "fe9000-0009-9999";
274         String volumeGroupId = "abeeee-abeeee-abeeee";
275
276         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
277         given(wrapper.getResponse()).willReturn("some response");
278         given(msoBusinessLogic.deleteVolumeGroupInstance(objectEqualTo(requestDetails), eq(serviceInstanceId), eq(vnfInstanceId), eq(volumeGroupId))).willReturn(wrapper);
279
280         // when & then
281         mockMvc.perform(post(format("/mso/mso_delete_volumegroup_instance/%s/vnfs/%s/volumeGroups/%s", serviceInstanceId, vnfInstanceId, volumeGroupId))
282             .content(asJson(requestDetails))
283             .contentType(APPLICATION_JSON))
284             .andExpect(status().isOk())
285             .andExpect(content().string("some response"));
286
287         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
288     }
289
290     @Test
291     public void shouldDeleteInstance() throws Exception {
292         // given
293         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
294         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
295         String networkInstanceId = "fe9000-0009-9999";
296
297         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
298         given(wrapper.getResponse()).willReturn("some response");
299         given(msoBusinessLogic.deleteNwInstance(objectEqualTo(requestDetails), eq(serviceInstanceId), eq(networkInstanceId))).willReturn(wrapper);
300
301         // when & then
302         mockMvc.perform(post(format("/mso/mso_delete_nw_instance/%s/networks/%s", serviceInstanceId, networkInstanceId))
303             .content(asJson(requestDetails))
304             .contentType(APPLICATION_JSON))
305             .andExpect(status().isOk())
306             .andExpect(content().string("some response"));
307
308         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
309     }
310
311     @Test
312     public void shouldDeleteServiceInstance() throws Exception {
313         // given
314         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
315         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
316         String serviceStatus = "ACTIVE";
317
318         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
319         given(wrapper.getResponse()).willReturn("some response");
320         given(msoBusinessLogic.deleteSvcInstance(objectEqualTo(requestDetails), eq(serviceInstanceId), eq(serviceStatus))).willReturn(wrapper);
321
322         // when & then
323         mockMvc.perform(post(format("/mso/mso_delete_svc_instance/%s", serviceInstanceId))
324             .param("serviceStatus", serviceStatus)
325             .content(asJson(requestDetails))
326             .contentType(APPLICATION_JSON))
327             .andExpect(status().isOk())
328             .andExpect(content().string("some response"));
329
330         then(cloudService).shouldHaveZeroInteractions();
331     }
332
333     @Test
334     public void shouldDeleteVnf() throws Exception {
335         // given
336         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
337         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
338         String vnfInstanceId = "fe9000-0009-9999";
339
340         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
341         given(wrapper.getResponse()).willReturn("some response");
342         given(msoBusinessLogic.deleteVnf(objectEqualTo(requestDetails), eq(serviceInstanceId), eq(vnfInstanceId))).willReturn(wrapper);
343
344         // when & then
345         mockMvc.perform(post(format("/mso/mso_delete_vnf_instance/%s/vnfs/%s", serviceInstanceId, vnfInstanceId))
346             .content(asJson(requestDetails))
347             .contentType(APPLICATION_JSON))
348             .andExpect(status().isOk())
349             .andExpect(content().string("some response"));
350
351         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
352     }
353
354     @Test
355     public void shouldDeleteConfiguration() throws Exception {
356         // given
357         RequestDetailsWrapper requestDetails = modelGenerator.nextObject(RequestDetailsWrapper.class);
358         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
359         String configurationId = "fe9000-0009-9999";
360
361         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
362         given(wrapper.getResponse()).willReturn("some response");
363         given(msoBusinessLogic.deleteConfiguration(objectEqualTo(requestDetails), eq(serviceInstanceId), eq(configurationId))).willReturn(wrapper);
364
365         // when & then
366         mockMvc.perform(post(format("/mso/mso_delete_configuration/%s/configurations/%s", serviceInstanceId, configurationId))
367             .content(asJson(requestDetails))
368             .contentType(APPLICATION_JSON))
369             .andExpect(status().isOk())
370             .andExpect(content().string("some response"));
371
372         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails.getRequestDetails()));
373     }
374
375     @Test
376     public void shouldDelegateNewInstanceCreation() throws Exception {
377         // given
378         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
379         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
380
381         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
382         given(wrapper.getResponse()).willReturn("some response");
383         given(msoBusinessLogic.createNwInstance(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
384
385         // when & then
386         mockMvc.perform(post("/mso/mso_create_nw_instance/" + serviceInstanceId)
387             .content(asJson(requestDetails))
388             .contentType(APPLICATION_JSON))
389             .andExpect(status().isOk())
390             .andExpect(content().string("some response"));
391
392         then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
393     }
394
395     @Test
396     public void shouldCompleteManualTask() throws Exception {
397         // given
398         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
399         String taskId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
400
401         MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
402         given(wrapper.getResponse()).willReturn("some response");
403         given(msoBusinessLogic.completeManualTask(objectEqualTo(requestDetails), eq(taskId))).willReturn(wrapper);
404
405         // when & then
406         mockMvc.perform(post("/mso/mso_post_man_task/" + taskId)
407             .content(asJson(requestDetails))
408             .contentType(APPLICATION_JSON))
409             .andExpect(status().isOk())
410             .andExpect(content().string("some response"));
411
412         then(cloudService).shouldHaveZeroInteractions();
413     }
414
415     @Test
416     public void shouldActivateFabricConfiguration() throws Exception {
417         // given
418         RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
419         String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
420
421         String path = "/mso/path";
422         given(msoBusinessLogic.getActivateFabricConfigurationPath(eq(serviceInstanceId))).willReturn(path);
423
424         RestObject<RequestReferencesContainer> response = new RestObject<>();
425         response.set(mock(RequestReferencesContainer.class));
426         response.setRaw("some response");
427         response.setStatusCode(200);
428
429         given(msoRestClient.PostForObject(objectEqualTo(requestDetails), eq(path), eq(RequestReferencesContainer.class))).willReturn(response);
430
431         // when & then
432         mockMvc.perform(post(format("/mso/mso_activate_fabric_configuration/%s", serviceInstanceId))
433             .content(asJson(requestDetails))
434             .contentType(APPLICATION_JSON))
435             .andExpect(status().isOk())
436             .andExpect(content().string("{\"status\":200,\"entity\":{}}"));
437
438         then(cloudService).shouldHaveZeroInteractions();
439     }
440
441     private <T> String asJson(T value) {
442         try {
443             return objectMapper.writeValueAsString(value);
444         } catch (JsonProcessingException e) {
445             throw new RuntimeException(e);
446         }
447     }
448
449     private <T> T objectEqualTo(T expected) {
450         return argThat(given -> asJson(given).equals(asJson(expected)));
451     }
452
453     @Test
454     public void testActivateFabricConfiguration() throws Exception {
455
456         String serviceInstanceId = "tempId";
457
458         //define mock response object
459         String responseString = "{" +
460             "      \"requestReferences\": {" +
461             "        \"instanceId\": \"tempId\"," +
462             "        \"requestId\": \"dbe54591-c8ed-46d3-abc7-d3a24873dfbd\"" +
463             "      }" +
464             "    }";
465         final RestObject<RequestReferencesContainer> restObject = new RestObject<>();
466         restObject.set(objectMapper.readValue(responseString, RequestReferencesContainer.class));
467         restObject.setStatusCode(200);
468
469         //register mock
470         String msoPath = "justAFakePath";
471
472         when(msoBusinessLogic.getActivateFabricConfigurationPath(serviceInstanceId)).thenReturn(msoPath);
473         when(msoRestClient.PostForObject(new RequestDetails(), msoPath, RequestReferencesContainer.class)).thenReturn(restObject);
474
475         //expected response
476         MsoResponseWrapper2<RequestReferencesContainer> expectedResponse = new MsoResponseWrapper2<>(restObject);
477
478         //get response from controller
479         // when & then
480         mockMvc.perform(post(format("/mso/mso_activate_fabric_configuration/%s", serviceInstanceId))
481             .content(asJson(new RequestDetails()))
482             .contentType(APPLICATION_JSON))
483             .andExpect(status().isOk())
484             .andExpect(content().json(asJson(expectedResponse)));
485     }
486 }