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