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