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