2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.controller;
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;
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import java.util.LinkedHashMap;
40 import java.util.List;
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;
63 public class MsoControllerTest {
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();
73 private MockMvc mockMvc;
74 private MsoBusinessLogic msoBusinessLogic;
75 private CloudOwnerService cloudService;
76 private MsoRestClientNew msoRestClient;
80 msoBusinessLogic = mock(MsoBusinessLogic.class);
81 cloudService = mock(CloudOwnerService.class);
82 msoRestClient = mock(MsoRestClientNew.class);
83 MsoController msoController = new MsoController(msoBusinessLogic, msoRestClient, cloudService);
85 mockMvc = MockMvcBuilders.standaloneSetup(msoController).build();
89 public void shouldGetOrchestrationRequest() throws Exception {
91 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
92 String requestId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
94 MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
95 given(msoBusinessLogic
96 .getOrchestrationRequest(requestId))
97 .willReturn(expectedResponse);
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)));
106 then(cloudService).shouldHaveZeroInteractions();
110 public void shouldDelegateNewServiceInstantiation() throws Exception {
112 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
114 MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
115 given(msoBusinessLogic
116 .createSvcInstance(objectEqualTo(requestDetails)))
117 .willReturn(expectedResponse);
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)));
126 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
130 public void shouldCreateVolumeInstance() throws Exception {
132 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
133 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
134 String vnfInstanceId = "fe9000-0009-9999";
136 MsoResponseWrapper expectedResponse = new MsoResponseWrapper(200, "test");
137 given(msoBusinessLogic
138 .createVolumeGroupInstance(objectEqualTo(requestDetails), eq(serviceInstanceId), eq(vnfInstanceId)))
139 .willReturn(expectedResponse);
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)));
148 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
152 public void shouldReturnOrchestrationRequestsForDashboard() throws Exception {
154 List<Request> orchestrationRequests = modelGenerator
155 .objects(Request.class, 2)
156 .collect(Collectors.toList());
158 given(msoBusinessLogic.getOrchestrationRequestsForDashboard()).willReturn(orchestrationRequests);
161 mockMvc.perform(get("/mso/mso_get_orch_reqs/dashboard"))
162 .andExpect(status().isOk())
163 .andExpect(content().json(objectMapper.writeValueAsString(orchestrationRequests)));
165 then(cloudService).shouldHaveZeroInteractions();
169 public void shouldReturnManualTasksById() throws Exception {
171 List<Task> manualTasks = modelGenerator
172 .objects(Task.class, 2)
173 .collect(Collectors.toList());
175 String originalRequestId = "za1234d1-5a33-55df-13ab-12abad84e335";
176 given(msoBusinessLogic.getManualTasksByRequestId(originalRequestId)).willReturn(manualTasks);
179 mockMvc.perform(get("/mso/mso_get_man_task/{id}", originalRequestId))
180 .andExpect(status().isOk())
181 .andExpect(content().json(objectMapper.writeValueAsString(manualTasks)));
183 then(cloudService).shouldHaveZeroInteractions();
187 public void shouldDelegateE2EServiceInstantiation() throws Exception {
189 String requestDetails = "some request details";
190 Map<String, Object> payload = new LinkedHashMap<>();
191 payload.put("requestDetails", requestDetails);
193 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
194 given(wrapper.getResponse()).willReturn("some response");
195 given(msoBusinessLogic.createE2eSvcInstance(requestDetails)).willReturn(wrapper);
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"));
204 then(cloudService).shouldHaveZeroInteractions();
208 public void shouldDelegateServiceInstantiation() throws Exception {
210 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
212 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
213 given(wrapper.getResponse()).willReturn("some response");
214 given(msoBusinessLogic.createSvcInstance(objectEqualTo(requestDetails))).willReturn(wrapper);
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"));
223 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
227 public void shouldDelegateVnfInstantiation() throws Exception {
229 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
230 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
232 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
233 given(wrapper.getResponse()).willReturn("some response");
234 given(msoBusinessLogic.createVnf(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
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"));
243 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
247 public void shouldDeleteVfModuleInstance() throws Exception {
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";
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);
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"));
265 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
269 public void shouldDeleteVolumeGroup() throws Exception {
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";
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);
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"));
287 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
291 public void shouldDeleteInstance() throws Exception {
293 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
294 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
295 String networkInstanceId = "fe9000-0009-9999";
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);
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"));
308 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
312 public void shouldDeleteServiceInstance() throws Exception {
314 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
315 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
316 String serviceStatus = "ACTIVE";
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);
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"));
330 then(cloudService).shouldHaveZeroInteractions();
334 public void shouldDeleteVnf() throws Exception {
336 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
337 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
338 String vnfInstanceId = "fe9000-0009-9999";
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);
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"));
351 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
355 public void shouldDeleteConfiguration() throws Exception {
357 RequestDetailsWrapper requestDetails = modelGenerator.nextObject(RequestDetailsWrapper.class);
358 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
359 String configurationId = "fe9000-0009-9999";
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);
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"));
372 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails.getRequestDetails()));
376 public void shouldDelegateNewInstanceCreation() throws Exception {
378 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
379 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
381 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
382 given(wrapper.getResponse()).willReturn("some response");
383 given(msoBusinessLogic.createNwInstance(objectEqualTo(requestDetails), eq(serviceInstanceId))).willReturn(wrapper);
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"));
392 then(cloudService).should(only()).enrichRequestWithCloudOwner(objectEqualTo(requestDetails));
396 public void shouldCompleteManualTask() throws Exception {
398 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
399 String taskId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
401 MsoResponseWrapper wrapper = mock(MsoResponseWrapper.class);
402 given(wrapper.getResponse()).willReturn("some response");
403 given(msoBusinessLogic.completeManualTask(objectEqualTo(requestDetails), eq(taskId))).willReturn(wrapper);
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"));
412 then(cloudService).shouldHaveZeroInteractions();
416 public void shouldActivateFabricConfiguration() throws Exception {
418 RequestDetails requestDetails = modelGenerator.nextObject(RequestDetails.class);
419 String serviceInstanceId = "bc305d54-75b4-431b-adb2-eb6b9e546014";
421 String path = "/mso/path";
422 given(msoBusinessLogic.getActivateFabricConfigurationPath(eq(serviceInstanceId))).willReturn(path);
424 RestObject<RequestReferencesContainer> response = new RestObject<>();
425 response.set(mock(RequestReferencesContainer.class));
426 response.setRaw("some response");
427 response.setStatusCode(200);
429 given(msoRestClient.PostForObject(objectEqualTo(requestDetails), eq(path), eq(RequestReferencesContainer.class))).willReturn(response);
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\":{}}"));
438 then(cloudService).shouldHaveZeroInteractions();
441 private <T> String asJson(T value) {
443 return objectMapper.writeValueAsString(value);
444 } catch (JsonProcessingException e) {
445 throw new RuntimeException(e);
449 private <T> T objectEqualTo(T expected) {
450 return argThat(given -> asJson(given).equals(asJson(expected)));
454 public void testActivateFabricConfiguration() throws Exception {
456 String serviceInstanceId = "tempId";
458 //define mock response object
459 String responseString = "{" +
460 " \"requestReferences\": {" +
461 " \"instanceId\": \"tempId\"," +
462 " \"requestId\": \"dbe54591-c8ed-46d3-abc7-d3a24873dfbd\"" +
465 final RestObject<RequestReferencesContainer> restObject = new RestObject<>();
466 restObject.set(objectMapper.readValue(responseString, RequestReferencesContainer.class));
467 restObject.setStatusCode(200);
470 String msoPath = "justAFakePath";
472 when(msoBusinessLogic.getActivateFabricConfigurationPath(serviceInstanceId)).thenReturn(msoPath);
473 when(msoRestClient.PostForObject(new RequestDetails(), msoPath, RequestReferencesContainer.class)).thenReturn(restObject);
476 MsoResponseWrapper2<RequestReferencesContainer> expectedResponse = new MsoResponseWrapper2<>(restObject);
478 //get response from controller
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)));