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