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