39d777be9e8ba47ad768212356ebfe20a8a97e97
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / MsoBusinessLogicImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.mso;
23
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import io.joshworks.restclient.http.HttpResponse;
27 import org.apache.commons.io.IOUtils;
28 import org.jetbrains.annotations.NotNull;
29 import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
30 import org.testng.annotations.BeforeClass;
31 import org.testng.annotations.Test;
32 import org.mockito.ArgumentMatcher;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.portalsdk.core.util.SystemProperties;
36 import org.onap.vid.changeManagement.ChangeManagementRequest;
37 import org.onap.vid.controller.OperationalEnvironmentController;
38 import org.onap.vid.exceptions.GenericUncheckedException;
39 import org.onap.vid.model.RequestReferencesContainer;
40 import org.onap.vid.model.SoftDeleteRequest;
41 import org.onap.vid.mso.model.CloudConfiguration;
42 import org.onap.vid.mso.model.ModelInfo;
43 import org.onap.vid.mso.model.OperationalEnvironmentActivateInfo;
44 import org.onap.vid.mso.model.OperationalEnvironmentDeactivateInfo;
45 import org.onap.vid.mso.model.RequestInfo;
46 import org.onap.vid.mso.model.RequestParameters;
47 import org.onap.vid.mso.model.RequestReferences;
48 import org.onap.vid.mso.rest.OperationalEnvironment.OperationEnvironmentRequestDetails;
49 import org.onap.vid.mso.rest.Request;
50 import org.onap.vid.mso.rest.RequestDetails;
51 import org.onap.vid.mso.rest.RequestDetailsWrapper;
52 import org.onap.vid.mso.rest.Task;
53 import org.onap.vid.properties.Features;
54 import org.springframework.http.HttpStatus;
55 import org.springframework.test.context.ContextConfiguration;
56 import org.togglz.core.manager.FeatureManager;
57
58 import javax.ws.rs.BadRequestException;
59 import java.io.IOException;
60 import java.net.URL;
61 import java.nio.file.Path;
62 import java.nio.file.Paths;
63 import java.util.List;
64 import java.util.stream.Collectors;
65
66 import static org.assertj.core.api.Assertions.assertThat;
67 import static org.assertj.core.api.Assertions.tuple;
68 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
69 import static org.junit.Assert.assertEquals;
70 import static org.mockito.ArgumentMatchers.any;
71 import static org.mockito.ArgumentMatchers.anyBoolean;
72 import static org.mockito.ArgumentMatchers.argThat;
73 import static org.mockito.ArgumentMatchers.eq;
74 import static org.mockito.ArgumentMatchers.isA;
75 import static org.mockito.ArgumentMatchers.endsWith;
76 import static org.mockito.BDDMockito.given;
77 import static org.mockito.Mockito.doThrow;
78 import static org.mockito.Mockito.mock;
79 import static org.onap.vid.controller.MsoController.CONFIGURATION_ID;
80 import static org.onap.vid.controller.MsoController.REQUEST_TYPE;
81 import static org.onap.vid.controller.MsoController.SVC_INSTANCE_ID;
82 import static org.onap.vid.controller.MsoController.VNF_INSTANCE_ID;
83 import static org.onap.vid.mso.MsoBusinessLogicImpl.validateEndpointPath;
84
85 @ContextConfiguration(classes = {SystemProperties.class})
86 public class MsoBusinessLogicImplTest extends AbstractTestNGSpringContextTests {
87
88     private static final ObjectMapper objectMapper = new ObjectMapper();
89
90     @Mock
91     private FeatureManager featureManager;
92
93     @Mock
94     private MsoInterface msoInterface;
95
96     @Mock
97     private RequestDetails msoRequest;
98
99
100     private MsoBusinessLogicImpl msoBusinessLogic;
101     private String userId = "testUserId";
102     private String operationalEnvironmentId = "testOperationalEnvironmentId";
103
104     @BeforeClass
105     public void setUp() {
106         MockitoAnnotations.initMocks(this);
107         msoBusinessLogic = new MsoBusinessLogicImpl(msoInterface, featureManager);
108     }
109
110     @Test
111     public void shouldProperlyCreateConfigurationInstanceWithCorrectServiceInstanceId() throws Exception {
112         // given
113         String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
114         String endpointTemplate = String.format("/serviceInstances/v6/%s/configurations", serviceInstanceId);
115         RequestDetailsWrapper requestDetailsWrapper = createRequestDetails();
116         MsoResponseWrapper expectedResponse = createOkResponse();
117         given(msoInterface.createConfigurationInstance(requestDetailsWrapper, endpointTemplate))
118                 .willReturn(expectedResponse);
119
120         // when
121         MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
122                 .createConfigurationInstance(requestDetailsWrapper, serviceInstanceId);
123
124         // then
125         assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse);
126     }
127
128     private RequestDetailsWrapper createRequestDetails() throws Exception {
129         final URL resource = this.getClass().getResource("/payload_jsons/mso_request_create_configuration.json");
130         RequestDetails requestDetails = objectMapper.readValue(resource, RequestDetails.class);
131         return new RequestDetailsWrapper(requestDetails);
132     }
133
134     @Test
135     public void shouldProperlyValidateEndpointPathWheEendPointIsNotEmptyAndValid() {
136         System.setProperty("TestEnv", "123");
137         String foundEndPoint = validateEndpointPath("TestEnv");
138         assertEquals("123", foundEndPoint);
139     }
140
141     @Test
142     public void shouldThrowRuntimeExceptionWhenValidateEndpointPathEndPointIsNotEmptyAndValid() {
143         assertThatExceptionOfType(RuntimeException.class)
144                 .isThrownBy(() -> validateEndpointPath("NotExists"));
145     }
146
147     @Test
148     public void shouldThrowRuntimeExceptionWhenValidateEndpointPathWhenEndPointIsNotEmptyButDoesntExists() {
149         String endPoint = "EmptyEndPoint";
150         System.setProperty(endPoint, "");
151         assertThatExceptionOfType(GenericUncheckedException.class)
152                 .isThrownBy(() -> validateEndpointPath(endPoint))
153                 .withMessage(endPoint + " env variable is not defined");
154     }
155
156     @Test
157     public void shouldProperlyCreateSvcInstanceWithProperParameters() {
158
159         MsoResponseWrapper expectedResponse = createOkResponse();
160         String svcEndpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
161         given(msoInterface.createSvcInstance(msoRequest, svcEndpoint)).willReturn(expectedResponse);
162
163         MsoResponseWrapper response = msoBusinessLogic.createSvcInstance(msoRequest);
164
165         // then
166         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
167     }
168
169     @Test
170     public void shouldProperlyCreateE2eSvcInstanceWithProperParameters() {
171         //  given
172         MsoResponseWrapper expectedResponse = createOkResponse();
173         String svcEndpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_E2E_SVC_INSTANCE);
174         given(msoInterface.createE2eSvcInstance(msoRequest, svcEndpoint)).willReturn(expectedResponse);
175
176         //  when
177         MsoResponseWrapper response = msoBusinessLogic.createE2eSvcInstance(msoRequest);
178
179         // then
180         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
181     }
182
183     @Test
184     public void shouldProperlyCreateVnfWithProperParameters() {
185
186         MsoResponseWrapper expectedResponse = createOkResponse();
187         String endpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_VNF_INSTANCE);
188         String vnfInstanceId = "testVnfInstanceTempId";
189         String vnfEndpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, vnfInstanceId);
190
191         given(msoInterface.createVnf(msoRequest, vnfEndpoint)).willReturn(expectedResponse);
192
193         MsoResponseWrapper response = msoBusinessLogic.createVnf(msoRequest, vnfInstanceId);
194
195         // then
196         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
197     }
198
199     @Test
200     public void shouldProperlyCreateNwInstanceWithProperParameters() {
201
202         MsoResponseWrapper expectedResponse = createOkResponse();
203         String vnfInstanceId = "testNwInstanceTempId";
204         String nwEndpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
205         String nw_endpoint = nwEndpoint.replaceFirst(SVC_INSTANCE_ID, vnfInstanceId);
206
207         given(msoInterface.createNwInstance(msoRequest, nw_endpoint)).willReturn(expectedResponse);
208
209         MsoResponseWrapper response = msoBusinessLogic.createNwInstance(msoRequest, vnfInstanceId);
210
211         // then
212         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
213     }
214
215     @Test
216     public void shouldProperlyCreateVolumeGroupInstanceWithProperParameters() {
217         MsoResponseWrapper expectedResponse = createOkResponse();
218         String serviceInstanceId = "testServiceInstanceTempId";
219         String vnfInstanceId = "testVnfInstanceTempId";
220         String nwEndpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
221         String vnfEndpoint = nwEndpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
222         vnfEndpoint = vnfEndpoint.replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);
223
224         given(msoInterface.createVolumeGroupInstance(msoRequest, vnfEndpoint)).willReturn(expectedResponse);
225
226         MsoResponseWrapper response = msoBusinessLogic.createVolumeGroupInstance(msoRequest, serviceInstanceId, vnfInstanceId);
227
228         // then
229         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
230     }
231
232     @Test
233     public void shouldProperlyCreateVfModuleInstanceWithProperParameters() {
234         MsoResponseWrapper expectedResponse = createOkResponse();
235         String serviceInstanceId = "testServiceInstanceTempId";
236         String vnfInstanceId = "testVnfInstanceTempId";
237         String partial_endpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
238         String vf_module_endpoint = partial_endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
239         vf_module_endpoint = vf_module_endpoint.replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);
240
241         given(msoInterface.createVfModuleInstance(msoRequest, vf_module_endpoint)).willReturn(expectedResponse);
242
243         MsoResponseWrapper response = msoBusinessLogic.createVfModuleInstance(msoRequest, serviceInstanceId, vnfInstanceId);
244
245         // then
246         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
247     }
248
249     @Test
250     public void shouldProperlyDeleteE2eSvcInstanceWithProperParameters() {
251         MsoResponseWrapper expectedResponse = createOkResponse();
252         String serviceInstanceId = "testDeleteE2eSvcInstanceTempId";
253         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_E2E_SVC_INSTANCE) + "/" + serviceInstanceId;
254
255         given(msoInterface.deleteE2eSvcInstance(msoRequest, endpoint)).willReturn(expectedResponse);
256
257         MsoResponseWrapper response = msoBusinessLogic.deleteE2eSvcInstance(msoRequest, serviceInstanceId);
258
259         // then
260         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
261     }
262
263     @Test
264     public void shouldProperlyDeleteSvcInstanceWithProperParametersAndFalseFeatureFlag() {
265         // given
266         String endpointTemplate = "/serviceInstances/v5/%s";
267         String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
268         String svcEndpoint = String.format(endpointTemplate, serviceInstanceId);
269         RequestDetails requestDetails = new RequestDetails();
270         MsoResponseWrapper expectedResponse = createOkResponse();
271         given(msoInterface.deleteSvcInstance(requestDetails, svcEndpoint)).willReturn(expectedResponse);
272         given(featureManager.isActive(Features.FLAG_UNASSIGN_SERVICE)).willReturn(false);
273
274         // when
275         MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
276                 .deleteSvcInstance(requestDetails, serviceInstanceId, "unAssignOrDeleteParams");
277
278         // then
279         assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse);
280     }
281
282     @Test
283     public void shouldProperlyDeleteSvcInstanceWithProperParametersAndTrueFeatureFlag() {
284         // given
285         String endpointTemplate = "/serviceInstantiation/v5/serviceInstances/%s/unassign";
286         String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
287         String svcEndpoint = String.format(endpointTemplate, serviceInstanceId);
288         RequestDetails requestDetails = new RequestDetails();
289         MsoResponseWrapper expectedResponse = createOkResponse();
290         given(msoInterface.unassignSvcInstance(requestDetails, svcEndpoint)).willReturn(expectedResponse);
291         given(featureManager.isActive(Features.FLAG_UNASSIGN_SERVICE)).willReturn(true);
292
293         // when
294         MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
295                 .deleteSvcInstance(requestDetails, serviceInstanceId, "assigned");
296
297         // then
298         assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse);
299     }
300
301     @Test
302     public void shouldProperlyDeleteVnfWithProperParameters() {
303         // when
304         String endpointTemplate = "/serviceInstances/v5/%s/vnfs/%s";
305         String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
306         String vnfInstanceId = "testVnfInstanceTempId";
307         String vnfEndpoint = String.format(endpointTemplate, serviceInstanceId, vnfInstanceId);
308         RequestDetails requestDetails = new RequestDetails();
309         MsoResponseWrapper expectedResponse = createOkResponse();
310         given(msoInterface.deleteVnf(requestDetails, vnfEndpoint)).willReturn(expectedResponse);
311
312         // when
313         MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
314                 .deleteVnf(requestDetails, serviceInstanceId, vnfInstanceId);
315
316         // then
317         assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse);
318     }
319
320     @Test
321     public void shouldProperlyDeleteVfModuleWithProperParameters() {
322         // when
323         String endpointTemplate = "/serviceInstances/v7/%s/vnfs/%s/vfModules/%s";
324         String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
325         String vnfInstanceId = "testVnfInstanceTempId";
326         String vfModuleId = "testVfModuleId";
327         String vnfEndpoint = String.format(endpointTemplate, serviceInstanceId, vnfInstanceId, vfModuleId);
328         RequestDetails requestDetails = new RequestDetails();
329         MsoResponseWrapper expectedResponse = createOkResponse();
330         given(msoInterface.deleteVfModule(requestDetails, vnfEndpoint)).willReturn(expectedResponse);
331
332         // when
333         MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
334                 .deleteVfModule(requestDetails, serviceInstanceId, vnfInstanceId, vfModuleId);
335         // then
336         assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedResponse);
337     }
338
339     @Test
340     public void shouldProperlyDeleteVolumeGroupInstanceWithProperParameters() {
341         MsoResponseWrapper expectedResponse = createOkResponse();
342         String serviceInstanceId = "testServiceInstanceTempId";
343         String vnfInstanceId = "testVnfInstanceTempId";
344         String volumeGroupId = "testvolumeGroupIdTempId";
345
346         String deleteVolumeGroupEndpoint = getDeleteVolumeGroupEndpoint(serviceInstanceId, vnfInstanceId, volumeGroupId);
347
348         given(msoInterface.deleteVolumeGroupInstance(msoRequest, deleteVolumeGroupEndpoint)).willReturn(expectedResponse);
349
350         MsoResponseWrapper response = msoBusinessLogic.deleteVolumeGroupInstance(msoRequest, serviceInstanceId, vnfInstanceId, volumeGroupId);
351
352         // then
353         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
354     }
355
356     @NotNull
357     private String getDeleteVolumeGroupEndpoint(String serviceInstanceId, String vnfInstanceId, String volumeGroupId) {
358         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
359         String svc_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
360         String vnfEndpoint = svc_endpoint.replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);
361         return vnfEndpoint + "/" + volumeGroupId;
362     }
363
364     @Test
365     public void shouldProperlyDeleteNwInstanceWithProperParameters() {
366         MsoResponseWrapper expectedResponse = createOkResponse();
367         String serviceInstanceId = "testServiceInstanceTempId";
368         String networkInstanceId = "testNetworkInstanceTempId";
369
370         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
371         String svc_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
372         String delete_nw_endpoint = svc_endpoint + "/" + networkInstanceId;
373
374         given(msoInterface.deleteNwInstance(msoRequest, delete_nw_endpoint)).willReturn(expectedResponse);
375
376         MsoResponseWrapper response = msoBusinessLogic.deleteNwInstance(msoRequest, serviceInstanceId, networkInstanceId);
377
378         // then
379         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
380     }
381
382     @Test
383     public void shouldProperlyGetOrchestrationRequestWithProperParameters() {
384         MsoResponseWrapper expectedResponse = createOkResponse();
385         String requestId = "testRequestTempId";
386         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQ);
387         String path = p + "/" + requestId;
388
389         given(msoInterface.getOrchestrationRequest(path)).willReturn(expectedResponse);
390
391         MsoResponseWrapper response = msoBusinessLogic.getOrchestrationRequest(requestId);
392         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
393     }
394
395     @Test(expectedExceptions = MsoTestException.class)
396     public void shouldProperlyGetOrchestrationRequestWithWrongParameters() {
397         String requestId = "testWrongRequestTempId";
398         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQ);
399         String path = p + "/" + requestId;
400
401         given(msoInterface.getOrchestrationRequest(path)).willThrow(new MsoTestException("testException"));
402
403         msoBusinessLogic.getOrchestrationRequest(requestId);
404     }
405
406     @Test
407     public void shouldProperlyGetOrchestrationRequestsWithProperParameters() {
408         MsoResponseWrapper expectedResponse = createOkResponse();
409         String filterString = "testRequestsTempId";
410         String url = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQS);
411         String path = url + filterString;
412
413         given(msoInterface.getOrchestrationRequest(path)).willReturn(expectedResponse);
414
415         MsoResponseWrapper response = msoBusinessLogic.getOrchestrationRequests(filterString);
416         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
417     }
418
419     @Test(expectedExceptions = MsoTestException.class)
420     public void shouldThrowExceptionWhenGetOrchestrationRequestsWithWrongParameters() {
421         String filterString = "testRequestsTempId";
422         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQS);
423         String path = p + filterString;
424
425         given(msoInterface.getOrchestrationRequest(path)).willThrow(new MsoTestException("testException"));
426
427         msoBusinessLogic.getOrchestrationRequests(filterString);
428     }
429
430     @Test
431     public void shouldSendProperScaleOutRequest() throws IOException {
432         // given
433         String serviceInstanceId = "3f93c7cb-2fd0-4557-9514-e189b7b04f9d";
434         String vnfInstanceId = "testVnfInstanceTempId";
435         String endpointTemplate = "/serviceInstantiation/v7/serviceInstances/%s/vnfs/%s/vfModules/scaleOut";
436         String vnfEndpoint = String.format(endpointTemplate, serviceInstanceId, vnfInstanceId);
437         org.onap.vid.changeManagement.RequestDetails requestDetails = readRequest();
438         org.onap.vid.changeManagement.RequestDetailsWrapper<org.onap.vid.changeManagement.RequestDetails> expectedRequest = readExpectedRequest();
439         MsoResponseWrapper expectedMsoResponseWrapper = createOkResponse();
440         given(
441                 msoInterface
442                         .scaleOutVFModuleInstance(argThat(new MsoRequestWrapperMatcher(expectedRequest)),
443                                 eq(vnfEndpoint)))
444                 .willReturn(expectedMsoResponseWrapper);
445
446         // when
447         MsoResponseWrapper msoResponseWrapper = msoBusinessLogic
448                 .scaleOutVfModuleInstance(requestDetails, serviceInstanceId, vnfInstanceId);
449
450         // then
451         assertThat(msoResponseWrapper).isEqualToComparingFieldByField(expectedMsoResponseWrapper);
452     }
453
454     private org.onap.vid.changeManagement.RequestDetails readRequest() throws IOException {
455         Path path = Paths.get("payload_jsons", "scaleOutVfModulePayload.json");
456         URL url = this.getClass().getClassLoader().getResource(path.toString());
457         return objectMapper.readValue(url, org.onap.vid.changeManagement.RequestDetails.class);
458     }
459
460     private org.onap.vid.changeManagement.RequestDetailsWrapper<org.onap.vid.changeManagement.RequestDetails> readExpectedRequest()
461             throws IOException {
462         Path path = Paths.get("payload_jsons", "scaleOutVfModulePayloadToMso.json");
463         URL url = this.getClass().getClassLoader().getResource(path.toString());
464         return objectMapper.readValue(url,
465                 new TypeReference<org.onap.vid.changeManagement.RequestDetailsWrapper<org.onap.vid.changeManagement.RequestDetails>>() {
466                 });
467     }
468
469
470     @Test
471     public void shouldFilterOutOrchestrationRequestsNotAllowedInDashboard() throws Exception {
472         //given
473         String vnfModelTypeOrchestrationRequests = getFileContentAsString("mso_model_info_sample_response.json");
474         String scaleOutActionOrchestrationRequests = getFileContentAsString("mso_action_scaleout_sample_response.json");
475
476         MsoResponseWrapper msoResponseWrapperMock = mock(MsoResponseWrapper.class);
477         given(msoInterface
478                 .getOrchestrationRequest(any(String.class), any(String.class), any(String.class),
479                         any(RestObject.class), anyBoolean()))
480                 .willReturn(msoResponseWrapperMock);
481         given(msoResponseWrapperMock.getEntity())
482                 .willReturn(vnfModelTypeOrchestrationRequests, scaleOutActionOrchestrationRequests);
483
484         //when
485         List<Request> filteredOrchestrationReqs = msoBusinessLogic.getOrchestrationRequestsForDashboard();
486         //then
487         assertThat(filteredOrchestrationReqs).hasSize(3);
488         assertThat(MsoBusinessLogicImpl.DASHBOARD_ALLOWED_TYPES)
489                 .containsAll(filteredOrchestrationReqs
490                         .stream()
491                         .map(el -> el.getRequestType().toUpperCase())
492                         .collect(Collectors.toList()));
493         assertThat(filteredOrchestrationReqs)
494                 .extracting(Request::getRequestScope)
495                 .containsOnly("vnf", "vfModule");
496     }
497
498     @Test(expectedExceptions = GenericUncheckedException.class)
499     public void shouldThrowGenericUncheckedExceptionWhenGetOrchestrationRequestsForDashboardWithWrongJsonFile_() throws Exception {
500         //given
501         String vnfModelTypeOrchestrationRequests = getFileContentAsString("mso_model_info_sample_wrong_response.json");
502
503         MsoResponseWrapper msoResponseWrapperMock = mock(MsoResponseWrapper.class);
504         given(msoInterface
505                 .getOrchestrationRequest(any(String.class), any(String.class), any(String.class),
506                         any(RestObject.class), anyBoolean()))
507                 .willReturn(msoResponseWrapperMock);
508         given(msoResponseWrapperMock.getEntity())
509                 .willReturn(vnfModelTypeOrchestrationRequests);
510
511         //when
512         msoBusinessLogic.getOrchestrationRequestsForDashboard();
513     }
514
515     @Test
516     public void shouldProperlyGetManualTasksByRequestIdWithProperParameters() throws Exception {
517         //given
518         String manualTasksList = getFileContentAsString("manual_tasks_by_requestId_test.json");
519
520         MsoResponseWrapper msoResponseWrapperMock = mock(MsoResponseWrapper.class);
521         given(msoInterface
522                 .getManualTasksByRequestId(any(String.class), any(String.class), any(String.class),
523                         any(RestObject.class)))
524                 .willReturn(msoResponseWrapperMock);
525         given(msoResponseWrapperMock.getEntity())
526                 .willReturn(manualTasksList);
527
528         //when
529         List<Task> filteredOrchestrationReqs = msoBusinessLogic.getManualTasksByRequestId("TestId");
530
531         //then
532         assertThat(filteredOrchestrationReqs).hasSize(2);
533         assertThat(filteredOrchestrationReqs).extracting("taskId", "type").
534                 contains(
535                         tuple("123123abc", "testTask"),
536                         tuple("321321abc", "testTask")
537                 );
538     }
539
540     @Test(expectedExceptions = GenericUncheckedException.class)
541     public void shouldThrowGenericUncheckedExceptionWhenGetManualTasksByRequestIdWithWrongJsonFile() throws Exception {
542         //given
543         String manualTasksList = getFileContentAsString("manual_tasks_by_requestId_wrongJson_test.json");
544
545         MsoResponseWrapper msoResponseWrapperMock = mock(MsoResponseWrapper.class);
546         given(msoInterface
547                 .getManualTasksByRequestId(any(String.class), any(String.class), any(String.class),
548                         any(RestObject.class)))
549                 .willReturn(msoResponseWrapperMock);
550         given(msoResponseWrapperMock.getEntity())
551                 .willReturn(manualTasksList);
552
553         //when
554         msoBusinessLogic.getManualTasksByRequestId("TestId");
555     }
556
557     @Test(expectedExceptions = MsoTestException.class)
558     public void getManualTasksByRequestIdWithArgument_shouldThrowException() {
559         //given
560         given(msoInterface
561                 .getManualTasksByRequestId(any(String.class), any(String.class), any(String.class),
562                         any(RestObject.class)))
563                 .willThrow(MsoTestException.class);
564
565         //when
566         msoBusinessLogic.getManualTasksByRequestId("TestId");
567     }
568
569     @Test
570     public void shouldProperlyCompleteManualTaskWithProperParameters() {
571         //given
572         MsoResponseWrapper expectedResponse = createOkResponse();
573         RequestDetails requestDetails = new RequestDetails();
574         String taskId = "testTaskId";
575
576         String url = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_MAN_TASKS);
577         String path = url + "/" + taskId + "/complete";
578
579         given(msoInterface.completeManualTask(eq(requestDetails), any(String.class), any(String.class), eq(path), any(RestObject.class))).willReturn(expectedResponse);
580
581         //when
582         MsoResponseWrapper response = msoBusinessLogic.completeManualTask(requestDetails, taskId);
583
584         //then
585         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
586
587     }
588
589     @Test
590     public void shouldProperlyActivateServiceInstanceWithProperParameters() {
591         //given
592         RequestDetails detail = new RequestDetails();
593         String taskId = "testTaskId";
594
595         RestObject<String> restObjStr = new RestObject<>();
596         restObjStr.set("");
597         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(restObjStr);
598
599         //when
600         MsoResponseWrapper response = msoBusinessLogic.activateServiceInstance(detail, taskId);
601
602         //then
603         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
604
605     }
606
607     @Test(expectedExceptions = MsoTestException.class)
608     public void shouldThrowExceptionWhenActivateServiceInstanceWithWrongParameters() {
609         //given
610         RequestDetails requestDetails = new RequestDetails();
611         String taskId = "testTaskId";
612
613         RestObject<String> restObjStr = new RestObject<>();
614         restObjStr.set("");
615         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(restObjStr);
616
617         doThrow(new MsoTestException("testException")).
618                 when(msoInterface).setServiceInstanceStatus(eq(requestDetails), any(String.class), any(String.class), any(String.class), any(RestObject.class));
619
620         //when
621         MsoResponseWrapper response = msoBusinessLogic.activateServiceInstance(requestDetails, taskId);
622
623         //then
624         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
625     }
626
627     @Test(expectedExceptions = MsoTestException.class)
628     public void shouldThrowExceptionWhenManualTaskWithWrongParameters() {
629         //given
630         RequestDetails requestDetails = new RequestDetails();
631         String taskId = "";
632
633         String url = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_MAN_TASKS);
634         String path = url + "/" + taskId + "/complete";
635
636         given(msoInterface.completeManualTask(eq(requestDetails), any(String.class), any(String.class), eq(path), any(RestObject.class))).willThrow(new MsoTestException("empty path"));
637
638         //when
639         msoBusinessLogic.completeManualTask(requestDetails, taskId);
640     }
641
642     @Test
643     public void shouldProperlyUpdateVnfWithProperParameters() {
644         //given
645         MsoResponseWrapper expectedResponse = createOkResponse();
646         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
647
648         String serviceInstanceId = "testServiceId";
649         String vnfInstanceId = "testVnfInstanceId";
650
651         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VNF_INSTANCE);
652         String vnfEndpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
653         vnfEndpoint = vnfEndpoint + '/' + vnfInstanceId;
654
655         given(msoInterface.updateVnf(requestDetails, vnfEndpoint)).willReturn(expectedResponse);
656
657         //when
658         MsoResponseWrapper response = (MsoResponseWrapper) msoBusinessLogic.updateVnf(requestDetails, serviceInstanceId, vnfInstanceId);
659
660         //then
661         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
662     }
663
664     @Test
665     public void shouldProperlyReplaceVnfWithProperParameters() {
666         //given
667         MsoResponseWrapper expectedResponse = createOkResponse();
668         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
669
670         String serviceInstanceId = "testServiceId";
671         String vnfInstanceId = "testVnfInstanceId";
672
673         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VNF_CHANGE_MANAGEMENT_INSTANCE);
674         String vnfEndpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
675         vnfEndpoint = vnfEndpoint.replace(VNF_INSTANCE_ID, vnfInstanceId);
676         vnfEndpoint = vnfEndpoint.replace(REQUEST_TYPE, ChangeManagementRequest.MsoChangeManagementRequest.REPLACE);
677
678         given(msoInterface.replaceVnf(requestDetails, vnfEndpoint)).willReturn(expectedResponse);
679
680         //when
681         MsoResponseWrapper response = (MsoResponseWrapper) msoBusinessLogic.replaceVnf(requestDetails, serviceInstanceId, vnfInstanceId);
682
683         //then
684         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
685     }
686
687     @Test
688     public void shouldProperlyGenerateInPlaceMsoRequestWithProperParameters() {
689         //given
690         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
691
692         requestDetails.setVnfInstanceId("testVnfInstanceId");
693         requestDetails.setVnfName("testVnfName");
694         requestDetails.setRequestParameters(new RequestParameters());
695
696         requestDetails.getRequestParameters().setAdditionalProperty("payload", "{" +
697                 "\"existing_software_version\": \"testExistingSoftwareParam\"," +
698                 "\"new_software_version\": \"testNewSoftwareParam\"," +
699                 "\"operations_timeout\": \"100\"" +
700                 "}");
701
702         RequestDetails inPlaceSoftwareUpdateRequest = new RequestDetails();
703         inPlaceSoftwareUpdateRequest.setCloudConfiguration(requestDetails.getCloudConfiguration());
704         inPlaceSoftwareUpdateRequest.setRequestParameters(requestDetails.getRequestParameters());
705         inPlaceSoftwareUpdateRequest.setRequestInfo(requestDetails.getRequestInfo());
706         org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new org.onap.vid.changeManagement.RequestDetailsWrapper();
707         requestDetailsWrapper.requestDetails = inPlaceSoftwareUpdateRequest;
708
709         //when
710         org.onap.vid.changeManagement.RequestDetailsWrapper response = msoBusinessLogic.generateInPlaceMsoRequest(requestDetails);
711
712         //then
713         assertThat(response).isEqualToComparingFieldByField(requestDetailsWrapper);
714     }
715
716     @Test(expectedExceptions = BadRequestException.class)
717     public void shouldThrowExceptionWhenGenerateInPlaceMsoRequestWithParametersWithWrongCharacters() {
718         //given
719         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
720
721         requestDetails.setVnfInstanceId("testVnfInstanceId");
722         requestDetails.setVnfName("testVnfName");
723         requestDetails.setRequestParameters(new RequestParameters());
724
725         requestDetails.getRequestParameters().setAdditionalProperty("payload", "{" +
726                 "\"existing_software_version\": \"#####\"," +
727                 "\"new_software_version\": \"testNewSoftwareParam\"" +
728                 "}");
729
730         //when
731         msoBusinessLogic.generateInPlaceMsoRequest(requestDetails);
732     }
733
734     @Test(expectedExceptions = BadRequestException.class)
735     public void shouldThrowExceptionWhenGenerateInPlaceMsoRequestWithWrongParameters() {
736         //given
737         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
738
739         requestDetails.setVnfInstanceId("testVnfInstanceId");
740         requestDetails.setVnfName("testVnfName");
741         requestDetails.setRequestParameters(new RequestParameters());
742
743         requestDetails.getRequestParameters().setAdditionalProperty("payload", "{" +
744                 "\"test-wrong-parameter\": \"testParam\"," +
745                 "\"new_software_version\": \"testNewSoftwareParam\"" +
746                 "}");
747
748         //when
749         msoBusinessLogic.generateInPlaceMsoRequest(requestDetails);
750     }
751
752     @Test
753     public void shouldProprleyGenerateConfigMsoRequestWithProperParameters() {
754         //given
755         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
756
757         requestDetails.setVnfInstanceId("testVnfInstanceId");
758         requestDetails.setVnfName("testVnfName");
759         requestDetails.setRequestParameters(new RequestParameters());
760
761         requestDetails.getRequestParameters().setAdditionalProperty("payload", "{" +
762                 "\"request-parameters\": \"testRequestParam\"," +
763                 "\"configuration-parameters\": \"testConfigParams\"" +
764                 "}");
765
766         RequestDetails configUpdateRequest = new RequestDetails();
767         configUpdateRequest.setRequestParameters(requestDetails.getRequestParameters());
768         configUpdateRequest.setRequestInfo(requestDetails.getRequestInfo());
769
770         org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new org.onap.vid.changeManagement.RequestDetailsWrapper();
771         requestDetailsWrapper.requestDetails = configUpdateRequest;
772
773         //when
774         org.onap.vid.changeManagement.RequestDetailsWrapper response = msoBusinessLogic.generateConfigMsoRequest(requestDetails);
775
776         //then
777         assertThat(response).isEqualToComparingFieldByField(requestDetailsWrapper);
778     }
779
780     @Test(expectedExceptions = BadRequestException.class)
781     public void shouldThrowExceptionGenerateConfigMsoRequestWithoutAdditionalParameters() {
782         //given
783         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
784
785         requestDetails.setVnfInstanceId("testVnfInstanceId");
786         requestDetails.setVnfName("testVnfName");
787         requestDetails.setRequestParameters(null);
788
789         //when
790         msoBusinessLogic.generateConfigMsoRequest(requestDetails);
791     }
792
793     @Test(expectedExceptions = BadRequestException.class)
794     public void shouldThrowExceptionWhenGenerateConfigMsoRequestWithWrongPayload() {
795         //given
796         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
797
798         requestDetails.setVnfInstanceId("testVnfInstanceId");
799         requestDetails.setVnfName("testVnfName");
800         requestDetails.setRequestParameters(new RequestParameters());
801
802         requestDetails.getRequestParameters().setAdditionalProperty("payload", null);
803
804         //when
805         msoBusinessLogic.generateConfigMsoRequest(requestDetails);
806     }
807
808     @Test(expectedExceptions = BadRequestException.class)
809     public void shouldThrowExceptionGenerateConfigMsoRequestWithoutAnyParameter() {
810         //given
811         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
812
813         requestDetails.setVnfInstanceId("testVnfInstanceId");
814         requestDetails.setVnfName("testVnfName");
815         requestDetails.setRequestParameters(new RequestParameters());
816
817         requestDetails.getRequestParameters().setAdditionalProperty("payload", "{" +
818                 "\"test-wrong-parameter\": \"testParam\"," +
819                 "\"configuration-parameters\": \"testConfigParam\"" +
820                 "}");
821
822         //when
823         msoBusinessLogic.generateConfigMsoRequest(requestDetails);
824     }
825
826     @Test
827     public void shouldProperlyGetActivateFabricConfigurationPathWithProperParameters() {
828         // given
829         String serviceInstanceId = "testServiceId";
830         String path = validateEndpointPath(MsoProperties.MSO_REST_API_SERVICE_INSTANCE_CREATE);
831         path += "/" + serviceInstanceId + "/activateFabricConfiguration";
832
833         // when
834         String response = msoBusinessLogic.getActivateFabricConfigurationPath(serviceInstanceId);
835
836         // then
837         assertThat(response).isEqualTo(path);
838     }
839
840     @Test
841     public void shouldProperlyGetDeactivateAndCloudDeletePathWithProperParameters() {
842         // given
843         String serviceInstanceId = "testServiceId";
844         String vnfInstanceId = "testVnfInstanceId";
845         String vfModuleInstanceId = "testVfModuleInstanceId";
846         String path = validateEndpointPath(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
847         path = path.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
848         path = path.replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);
849         path += "/" + vfModuleInstanceId + "/deactivateAndCloudDelete";
850
851         // when
852         String response = msoBusinessLogic.getDeactivateAndCloudDeletePath(serviceInstanceId, vnfInstanceId, vfModuleInstanceId);
853
854         // then
855         assertThat(response).isEqualTo(path);
856     }
857
858     @Test
859     public void shouldProperlyBuildRequestDetailsForSoftDeleteWithProperParameters() {
860         //  given
861         SoftDeleteRequest softDeleteRequest = new SoftDeleteRequest();
862         RequestDetails requestDetails = new RequestDetails();
863
864         String userId = "testUserID";
865         String tenantId = "testTenantId ";
866         String cloudRegionId = "testCloudId";
867
868
869         RequestInfo requestInfo = new RequestInfo();
870         requestInfo.setSource("VID");
871         requestInfo.setRequestorId(userId);
872         requestDetails.setRequestInfo(requestInfo);
873
874         CloudConfiguration cloudConfiguration = new CloudConfiguration();
875         cloudConfiguration.setTenantId(tenantId);
876         cloudConfiguration.setLcpCloudRegionId(cloudRegionId);
877         requestDetails.setCloudConfiguration(cloudConfiguration);
878
879         setModelInfoForRequestDetails(requestDetails);
880
881         setRequestParametersForRequestDetails(requestDetails);
882
883         softDeleteRequest.setLcpCloudRegionId(cloudRegionId);
884         softDeleteRequest.setTenantId(tenantId);
885         softDeleteRequest.setUserId(userId);
886
887         //  when
888         RequestDetails response = msoBusinessLogic.buildRequestDetailsForSoftDelete(softDeleteRequest);
889
890         //  then
891         assertThat(response).isEqualTo(requestDetails);
892     }
893
894     private void setRequestParametersForRequestDetails(RequestDetails requestDetails) {
895         RequestParameters requestParameters = new RequestParameters();
896         requestParameters.setTestApi("GR_API");
897         requestDetails.setRequestParameters(requestParameters);
898     }
899
900     private void setModelInfoForRequestDetails(RequestDetails requestDetails) {
901         ModelInfo modelInfo = new ModelInfo();
902         modelInfo.setModelType("vfModule");
903         requestDetails.setModelInfo(modelInfo);
904     }
905
906     @Test
907     public void shouldProperlyDeactivateAndCloudDeleteWithProperParameters() {
908         //  given
909         String serviceInstanceId = "testServiceId";
910         String vnfInstanceId = "testVnfInstanceId";
911         String vfModuleInstanceId = "testVfModuleInstanceId";
912         RequestDetails requestDetails = new RequestDetails();
913
914         String path = msoBusinessLogic.getDeactivateAndCloudDeletePath(serviceInstanceId, vnfInstanceId, vfModuleInstanceId);
915
916         RequestReferences requestReferences = new RequestReferences();
917         requestReferences.setInstanceId("testInstance");
918         requestReferences.setRequestId("testRequest");
919
920         HttpResponse<RequestReferencesContainer> expectedResponse = HttpResponse.fallback(new RequestReferencesContainer(requestReferences));
921
922         MsoResponseWrapper2 responseWrapped = new MsoResponseWrapper2<>(expectedResponse);
923
924         given(msoInterface.post(eq(path), any(org.onap.vid.changeManagement.RequestDetailsWrapper.class), eq(RequestReferencesContainer.class))).willReturn(expectedResponse);
925
926         //  when
927         MsoResponseWrapper2 response = msoBusinessLogic.deactivateAndCloudDelete(serviceInstanceId, vnfInstanceId, vfModuleInstanceId, requestDetails);
928
929         //  then
930         assertThat(response).isEqualToComparingFieldByField(responseWrapped);
931     }
932
933     @Test
934     public void shouldProperlyActivateFabricConfigurationWithProperParameters() {
935         //  given
936         String serviceInstanceId = "testServiceId";
937         RequestDetails requestDetails = new RequestDetails();
938
939         String path = msoBusinessLogic.getActivateFabricConfigurationPath(serviceInstanceId);
940
941         RequestReferences requestReferences = new RequestReferences();
942         requestReferences.setInstanceId("testInstance");
943         requestReferences.setRequestId("testRequest");
944
945         HttpResponse<RequestReferencesContainer> expectedResponse = HttpResponse.fallback(new RequestReferencesContainer(requestReferences));
946
947         MsoResponseWrapper2 responseWrapped = new MsoResponseWrapper2<>(expectedResponse);
948
949         given(msoInterface.post(eq(path), any(org.onap.vid.changeManagement.RequestDetailsWrapper.class), eq(RequestReferencesContainer.class))).willReturn(expectedResponse);
950
951         //  when
952         MsoResponseWrapper2 response = msoBusinessLogic.activateFabricConfiguration(serviceInstanceId, requestDetails);
953
954         //  then
955         assertThat(response).isEqualToComparingFieldByField(responseWrapped);
956     }
957
958     @Test
959     public void shouldProperlyUpdateVnfSoftwareWithProperParameters() {
960         //  given
961         String serviceInstanceId = "testServiceId";
962         String vnfInstanceId = "testVnfInstanceId";
963
964         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
965
966         requestDetails.setVnfInstanceId("testVnfInstanceId");
967         requestDetails.setVnfName("testVnfName");
968         requestDetails.setRequestParameters(new RequestParameters());
969
970         requestDetails.getRequestParameters().setAdditionalProperty("payload", "{" +
971                 "\"existing_software_version\": \"testExistingSoftwareParam\"," +
972                 "\"new_software_version\": \"testNewSoftwareParam\"," +
973                 "\"operations_timeout\": \"100\"" +
974                 "}");
975
976         MsoResponseWrapper okResponse = createOkResponse();
977
978         given(msoInterface.changeManagementUpdate(isA(org.onap.vid.changeManagement.RequestDetailsWrapper.class), any(String.class))).willReturn(okResponse);
979
980         //  when
981         MsoResponseWrapper response = (MsoResponseWrapper) msoBusinessLogic.updateVnfSoftware(requestDetails, serviceInstanceId, vnfInstanceId);
982
983         //  then
984         assertThat(response).isEqualToComparingFieldByField(okResponse);
985     }
986
987     @Test
988     public void shouldProperlyUpdateVnfConfigWithProperParameters() {
989         //  given
990         String serviceInstanceId = "testServiceId";
991         String vnfInstanceId = "testVnfInstanceId";
992
993         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
994
995         requestDetails.setVnfInstanceId("testVnfInstanceId");
996         requestDetails.setVnfName("testVnfName");
997         requestDetails.setRequestParameters(new RequestParameters());
998
999         requestDetails.getRequestParameters().setAdditionalProperty("payload", "{" +
1000                 "\"request-parameters\": \"testRequestParam\"," +
1001                 "\"configuration-parameters\": \"testConfigParams\"" +
1002                 "}");
1003
1004         MsoResponseWrapper okResponse = createOkResponse();
1005
1006         given(msoInterface.changeManagementUpdate(isA(org.onap.vid.changeManagement.RequestDetailsWrapper.class), any(String.class))).willReturn(okResponse);
1007
1008         //  when
1009         MsoResponseWrapper response = (MsoResponseWrapper) msoBusinessLogic.updateVnfConfig(requestDetails, serviceInstanceId, vnfInstanceId);
1010
1011         //  then
1012         assertThat(response).isEqualToComparingFieldByField(okResponse);
1013     }
1014
1015     @Test
1016     public void shouldProperlyDeleteConfigurationWithProperParameters() {
1017         //  given
1018         String serviceInstanceId = "testServiceId";
1019         String configurationId = "testConfigurationId";
1020
1021         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1022
1023         requestDetails.setVnfInstanceId("testVnfInstanceId");
1024         requestDetails.setVnfName("testVnfName");
1025
1026         MsoResponseWrapper okResponse = createOkResponse();
1027         RequestDetailsWrapper wrappedRequestDetail = new RequestDetailsWrapper(requestDetails);
1028
1029         given(msoInterface.deleteConfiguration(eq(wrappedRequestDetail), any(String.class))).willReturn(okResponse);
1030
1031         //  when
1032         MsoResponseWrapper response = msoBusinessLogic.deleteConfiguration(wrappedRequestDetail, serviceInstanceId, configurationId);
1033
1034         //  then
1035         assertThat(response).isEqualToComparingFieldByField(okResponse);
1036     }
1037
1038     @Test
1039     public void shouldProperlySetConfigurationActiveStatusActiveWithProperParameters() {
1040         //  given
1041         String serviceInstanceId = "testServiceId";
1042         String configurationId = "testConfigurationId";
1043
1044         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1045
1046         requestDetails.setVnfInstanceId("testVnfInstanceId");
1047         requestDetails.setVnfName("testVnfName");
1048
1049         MsoResponseWrapper okResponse = createOkResponse();
1050
1051         String endpoint =
1052                 validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATION_INSTANCE)
1053                         .replace(SVC_INSTANCE_ID, serviceInstanceId)
1054                         .replace(CONFIGURATION_ID, configurationId)
1055                         + "/activate";
1056
1057         given(msoInterface.setConfigurationActiveStatus(eq(requestDetails), eq(endpoint))).willReturn(okResponse);
1058
1059         //  when
1060         MsoResponseWrapper response = msoBusinessLogic.setConfigurationActiveStatus(requestDetails, serviceInstanceId, configurationId, true);
1061
1062         //  then
1063         assertThat(response).isEqualToComparingFieldByField(okResponse);
1064     }
1065
1066     @Test
1067     public void shouldProperlySetConfigurationActiveStatusDeactivateWithProperParameters() {
1068         //  given
1069         String serviceInstanceId = "testServiceId";
1070         String configurationId = "testConfigurationId";
1071
1072         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1073
1074         requestDetails.setVnfInstanceId("testVnfInstanceId");
1075         requestDetails.setVnfName("testVnfName");
1076
1077         MsoResponseWrapper okResponse = createOkResponse();
1078
1079         String endpoint =
1080                 validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATION_INSTANCE)
1081                         .replace(SVC_INSTANCE_ID, serviceInstanceId)
1082                         .replace(CONFIGURATION_ID, configurationId)
1083                         + "/deactivate";
1084
1085         given(msoInterface.setConfigurationActiveStatus(eq(requestDetails), eq(endpoint))).willReturn(okResponse);
1086
1087         //  when
1088         MsoResponseWrapper response = msoBusinessLogic.setConfigurationActiveStatus(requestDetails, serviceInstanceId, configurationId, false);
1089
1090         //  then
1091         assertThat(response).isEqualToComparingFieldByField(okResponse);
1092     }
1093
1094     @Test
1095     public void shouldProperlySetServiceInstanceStatusActiveWithProperParameters() {
1096         //  given
1097         String serviceInstanceId = "testServiceId";
1098
1099         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1100
1101         RestObject<String> restObjStr = new RestObject<>();
1102         restObjStr.set("");
1103         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(restObjStr);
1104
1105         //  when
1106         MsoResponseWrapper response = msoBusinessLogic.setServiceInstanceStatus(requestDetails, serviceInstanceId, true);
1107
1108         //  then
1109         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
1110
1111     }
1112
1113     @Test
1114     public void shouldProperlySetServiceInstanceStatusDeactivateWithProperParameters() {
1115         //  given
1116         String serviceInstanceId = "testServiceId";
1117
1118         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1119
1120         RestObject<String> restObjStr = new RestObject<>();
1121         restObjStr.set("");
1122         MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(restObjStr);
1123
1124         //  when
1125         MsoResponseWrapper response = msoBusinessLogic.setServiceInstanceStatus(requestDetails, serviceInstanceId, false);
1126
1127         //  then
1128         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
1129
1130     }
1131
1132     @Test(expectedExceptions = MsoTestException.class)
1133     public void shouldThrowExceptionWhenSetServiceInstanceStatusWithWrongParameters() {
1134         //  given
1135         String serviceInstanceId = "testServiceId";
1136
1137         doThrow(new MsoTestException("testException")).
1138                 when(msoInterface).setServiceInstanceStatus(eq(null), any(String.class), any(String.class), any(String.class), any(RestObject.class));
1139
1140         //  when
1141         msoBusinessLogic.setServiceInstanceStatus(null, serviceInstanceId, true);
1142     }
1143
1144     @Test
1145     public void shouldProperlySetPortOnConfigurationStatusEnableWithProperParameters() {
1146         //  given
1147         String serviceInstanceId = "testServiceId";
1148         String configurationId = "testConfigurationId";
1149         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1150         requestDetails.setVnfInstanceId("testVnfInstanceId");
1151         requestDetails.setVnfName("testVnfName");
1152         MsoResponseWrapper okResponse = createOkResponse();
1153
1154         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATION_INSTANCE)
1155                 .replace(SVC_INSTANCE_ID, serviceInstanceId)
1156                 .replace(CONFIGURATION_ID, configurationId)
1157                 + "/enablePort";
1158
1159         given(msoInterface.setPortOnConfigurationStatus(eq(requestDetails), eq(endpoint))).willReturn(okResponse);
1160
1161         //  when
1162         MsoResponseWrapper response = msoBusinessLogic.setPortOnConfigurationStatus(requestDetails, serviceInstanceId, configurationId, true);
1163
1164         //  then
1165         assertThat(response).isEqualToComparingFieldByField(okResponse);
1166     }
1167
1168     @Test
1169     public void shouldProperlySetPortOnConfigurationStatusDisableWithProperParameters() {
1170         //  given
1171         String serviceInstanceId = "testServiceId";
1172         String configurationId = "testConfigurationId";
1173         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1174         requestDetails.setVnfInstanceId("testVnfInstanceId");
1175         requestDetails.setVnfName("testVnfName");
1176         MsoResponseWrapper okResponse = createOkResponse();
1177
1178         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATION_INSTANCE)
1179                 .replace(SVC_INSTANCE_ID, serviceInstanceId)
1180                 .replace(CONFIGURATION_ID, configurationId)
1181                 + "/disablePort";
1182
1183         given(msoInterface.setPortOnConfigurationStatus(eq(requestDetails), eq(endpoint))).willReturn(okResponse);
1184
1185         //  when
1186         MsoResponseWrapper response = msoBusinessLogic.setPortOnConfigurationStatus(requestDetails, serviceInstanceId, configurationId, false);
1187
1188         //  then
1189         assertThat(response).isEqualToComparingFieldByField(okResponse);
1190     }
1191
1192     @Test
1193     public void shouldProperlyCreateOperationalEnvironmentActivationRequestDetailsWithProperParameters() {
1194         //  given
1195         OperationalEnvironmentActivateInfo details = createTestOperationalEnvironmentActivateInfo();
1196         //  when
1197         org.onap.vid.changeManagement.RequestDetailsWrapper<RequestDetails> requestDetails = msoBusinessLogic.createOperationalEnvironmentActivationRequestDetails(details);
1198
1199         //  then
1200         assertThat(requestDetails.requestDetails.getRequestParameters().getAdditionalProperties().values()).contains(details.getWorkloadContext(), details.getManifest());
1201         assertThat(requestDetails.requestDetails.getRequestInfo().getRequestorId()).isEqualTo(userId);
1202     }
1203
1204     @Test
1205     public void shouldProperlyGetOperationalEnvironmentActivationPathWithProperParameters() {
1206         // given
1207         OperationalEnvironmentActivateInfo details = createTestOperationalEnvironmentActivateInfo();
1208
1209         // when
1210         String response = msoBusinessLogic.getOperationalEnvironmentActivationPath(details);
1211
1212         // then
1213         assertThat(response).contains(operationalEnvironmentId);
1214     }
1215
1216     @Test
1217     public void shouldProperlyCreateOperationalEnvironmentDeactivationRequestDetailsWithProperParameters() {
1218         // given
1219         OperationalEnvironmentDeactivateInfo details = createTestOperationalEnvironmentDeactivateInfo();
1220
1221         // when
1222         org.onap.vid.changeManagement.RequestDetailsWrapper<RequestDetails> response;
1223         response = msoBusinessLogic.createOperationalEnvironmentDeactivationRequestDetails(details);
1224
1225         // then
1226         assertThat(response.requestDetails.getRequestInfo().getRequestorId()).isEqualTo(userId);
1227     }
1228
1229     @Test
1230     public void shouldProperlyGetCloudResourcesRequestsStatusPathWithProperParameters() {
1231         // given
1232         String requestId = "testRequestId";
1233
1234         // when
1235         String response = msoBusinessLogic.getCloudResourcesRequestsStatusPath(requestId);
1236
1237         // then
1238         assertThat(response).contains(requestId);
1239     }
1240
1241     @Test
1242     public void shouldProperlyGetOperationalEnvironmentDeactivationPathWithProperParameters() {
1243         // given
1244         OperationalEnvironmentDeactivateInfo details = createTestOperationalEnvironmentDeactivateInfo();
1245
1246         // when
1247         String response = msoBusinessLogic.getOperationalEnvironmentDeactivationPath(details);
1248
1249         // then
1250         assertThat(response).contains(operationalEnvironmentId);
1251     }
1252
1253     @Test
1254     public void shouldProperlyGetOperationalEnvironmentCreationPathWithProperParameters() {
1255         // when
1256         String response = msoBusinessLogic.getOperationalEnvironmentCreationPath();
1257
1258         // then
1259         assertThat(response).isNotBlank();
1260     }
1261
1262     @Test
1263     public void shouldProperlyConvertParametersToRequestDetailsWithProperParameters() {
1264         // given
1265         OperationalEnvironmentController.OperationalEnvironmentCreateBody input = createTestOperationalEnvironmentCreateBody();
1266
1267         // when
1268         org.onap.vid.changeManagement.RequestDetailsWrapper<OperationEnvironmentRequestDetails> response
1269                 = msoBusinessLogic.convertParametersToRequestDetails(input, userId);
1270
1271         // then
1272         assertThat(response.requestDetails.getRequestInfo().getInstanceName()).isEqualTo(input.getInstanceName());
1273         assertThat(response.requestDetails.getRequestInfo().getRequestorId()).isEqualTo(userId);
1274         assertThat(response.requestDetails.getRequestParameters().getOperationalEnvironmentType()).isEqualTo(input.getOperationalEnvironmentType());
1275         assertThat(response.requestDetails.getRequestParameters().getTenantContext()).isEqualTo(input.getTenantContext());
1276         assertThat(response.requestDetails.getRequestParameters().getWorkloadContext()).isEqualTo(input.getWorkloadContext());
1277     }
1278
1279     @Test
1280     public void shouldProperlyRemoveRelationshipFromServiceInstanceWithProperParameters() {
1281         // given
1282         MsoResponseWrapper expectedResponse = createOkResponse();
1283         String serviceInstanceId = "testServiceId";
1284         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1285
1286         given(msoInterface.removeRelationshipFromServiceInstance(eq(requestDetails), endsWith("/" + serviceInstanceId + "/removeRelationships")))
1287                 .willReturn(expectedResponse);
1288
1289         // when
1290         MsoResponseWrapper response = msoBusinessLogic.removeRelationshipFromServiceInstance(requestDetails, serviceInstanceId);
1291
1292         // then
1293         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
1294     }
1295
1296     @Test
1297     public void shouldProperlyAddRelationshipToServiceInstanceWithProperParameters() {
1298         // given
1299         MsoResponseWrapper expectedResponse = createOkResponse();
1300         String serviceInstanceId = "testServiceId";
1301         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1302
1303         given(msoInterface.addRelationshipToServiceInstance(eq(requestDetails), endsWith("/" + serviceInstanceId + "/addRelationships")))
1304                 .willReturn(expectedResponse);
1305
1306         // when
1307         MsoResponseWrapper response = msoBusinessLogic.addRelationshipToServiceInstance(requestDetails, serviceInstanceId);
1308
1309         // then
1310         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
1311     }
1312
1313     @Test
1314     public void shouldProperlyRequestTypeFromValueWithValidParameters() {
1315         // given
1316         String testValue = "createInstance";
1317         // when
1318         MsoBusinessLogicImpl.RequestType response = MsoBusinessLogicImpl.RequestType.fromValue(testValue);
1319
1320         // then
1321         assertThat(response.toString()).isEqualTo(testValue);
1322     }
1323
1324     @Test(expectedExceptions = IllegalArgumentException.class)
1325     public void shouldThrowExceptionWhenRequestTypeFromValueWithWrongParameter() {
1326         // given
1327         String testValue = "notExistingParameter";
1328         // when
1329         MsoBusinessLogicImpl.RequestType.fromValue(testValue);
1330     }
1331
1332
1333     private OperationalEnvironmentActivateInfo createTestOperationalEnvironmentActivateInfo() {
1334         OperationalEnvironmentController.OperationalEnvironmentActivateBody operationalEnvironmentActivateBody = new OperationalEnvironmentController.OperationalEnvironmentActivateBody(
1335                 "testRelatedInstanceId",
1336                 "testRelatedInstanceName",
1337                 "testWorkloadContext",
1338                 new OperationalEnvironmentController.OperationalEnvironmentManifest()
1339         );
1340         return new OperationalEnvironmentActivateInfo(operationalEnvironmentActivateBody, userId, operationalEnvironmentId);
1341     }
1342
1343     private OperationalEnvironmentDeactivateInfo createTestOperationalEnvironmentDeactivateInfo() {
1344         return new OperationalEnvironmentDeactivateInfo(userId, operationalEnvironmentId);
1345     }
1346
1347     private OperationalEnvironmentController.OperationalEnvironmentCreateBody createTestOperationalEnvironmentCreateBody() {
1348         return new OperationalEnvironmentController.OperationalEnvironmentCreateBody(
1349                 "testInstanceName",
1350                 "testEcompInstanceId",
1351                 "testEcompInstanceName",
1352                 "testOperationalEnvironmentType",
1353                 "testTenantContext",
1354                 "testWorkloadContext"
1355         );
1356     }
1357
1358     private MsoResponseWrapper createOkResponse() {
1359         HttpStatus expectedStatus = HttpStatus.ACCEPTED;
1360         String expectedBody = " \"body\": {\n" +
1361                 "      \"requestReferences\": {\n" +
1362                 "        \"instanceId\": \" 123456 \",\n" +
1363                 "        \"requestId\": \"b6dc9806-b094-42f7-9386-a48de8218ce8\"\n" +
1364                 "      }";
1365         MsoResponseWrapper responseWrapper = new MsoResponseWrapper();
1366         responseWrapper.setEntity(expectedBody);
1367         responseWrapper.setStatus(expectedStatus.value());
1368         return responseWrapper;
1369     }
1370
1371     private String getFileContentAsString(String resourceName) throws Exception {
1372         Path path = Paths.get("payload_jsons", resourceName);
1373         URL url = this.getClass().getClassLoader().getResource(path.toString());
1374         String result = "";
1375         if (url != null) {
1376             result = IOUtils.toString(url.toURI(), "UTF-8");
1377         }
1378         return result;
1379     }
1380
1381     private static class MsoRequestWrapperMatcher implements
1382             ArgumentMatcher<org.onap.vid.changeManagement.RequestDetailsWrapper> {
1383
1384         private final org.onap.vid.changeManagement.RequestDetailsWrapper expectedRequest;
1385
1386         MsoRequestWrapperMatcher(org.onap.vid.changeManagement.RequestDetailsWrapper expectedRequest) {
1387             this.expectedRequest = expectedRequest;
1388         }
1389
1390         @Override
1391         public boolean matches(org.onap.vid.changeManagement.RequestDetailsWrapper argument) {
1392             return expectedRequest.requestDetails.equals(argument.requestDetails);
1393         }
1394     }
1395
1396     private class MsoTestException extends RuntimeException {
1397         MsoTestException(String testException) {
1398             super(testException);
1399         }
1400     }
1401 }
1402