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