94a7cf9f0a89c6d5ea58b2463d6c71bffcb666b4
[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 net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
27 import static org.assertj.core.api.Assertions.tuple;
28 import static org.hamcrest.Matchers.allOf;
29 import static org.hamcrest.Matchers.containsString;
30 import static org.hamcrest.Matchers.equalTo;
31 import static org.hamcrest.Matchers.hasEntry;
32 import static org.hamcrest.Matchers.instanceOf;
33 import static org.hamcrest.Matchers.is;
34 import static org.hamcrest.Matchers.startsWith;
35 import static org.junit.Assert.assertEquals;
36 import static org.mockito.ArgumentMatchers.any;
37 import static org.mockito.ArgumentMatchers.anyBoolean;
38 import static org.mockito.ArgumentMatchers.anyString;
39 import static org.mockito.ArgumentMatchers.argThat;
40 import static org.mockito.ArgumentMatchers.endsWith;
41 import static org.mockito.ArgumentMatchers.eq;
42 import static org.mockito.ArgumentMatchers.isA;
43 import static org.mockito.BDDMockito.given;
44 import static org.mockito.Mockito.doThrow;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47 import static org.onap.vid.controller.MsoController.CONFIGURATION_ID;
48 import static org.onap.vid.controller.MsoController.REQUEST_TYPE;
49 import static org.onap.vid.controller.MsoController.SVC_INSTANCE_ID;
50 import static org.onap.vid.controller.MsoController.VNF_INSTANCE_ID;
51 import static org.onap.vid.model.probes.ExternalComponentStatus.Component.MSO;
52 import static org.onap.vid.mso.MsoBusinessLogicImpl.validateEndpointPath;
53
54 import com.fasterxml.jackson.core.type.TypeReference;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56 import io.joshworks.restclient.http.HttpResponse;
57 import java.io.IOException;
58 import java.net.URL;
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.RequestParameters;
94 import org.onap.vid.mso.rest.OperationalEnvironment.OperationEnvironmentRequestDetails;
95 import org.onap.vid.mso.rest.Request;
96 import org.onap.vid.mso.rest.RequestDetails;
97 import org.onap.vid.mso.rest.RequestDetailsWrapper;
98 import org.onap.vid.mso.rest.Task;
99 import org.onap.vid.testUtils.TestUtils;
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_RESTAPI_SERVICE_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_RESTAPI_SERVICE_INSTANCE);
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
849         // when
850         String response = msoBusinessLogic.getDeactivateAndCloudDeletePath("testServiceId", "testVnfInstanceId", "testVfModuleInstanceId");
851
852         // then
853         String expectedPath = "/serviceInstantiation/v7/serviceInstances/testServiceId/vnfs/testVnfInstanceId/"+
854             "vfModules/testVfModuleInstanceId/deactivateAndCloudDelete";
855         assertThat(response).isEqualTo(expectedPath);
856     }
857
858     @Test
859     public void shouldProperlyBuildRequestDetailsForSoftDeleteWithProperParameters() throws IOException {
860         SoftDeleteRequest request = new SoftDeleteRequest("testTenantId", "testCloudId", "testUserID");
861         String expected = IOUtils.toString(this.getClass().getResource("/payload_jsons/vfModuleDeactivateAndCloudDelete.json"), "UTF-8");
862         final RequestDetails result = msoBusinessLogic.buildRequestDetailsForSoftDelete(request);
863         MatcherAssert.assertThat(result, jsonEquals(expected));
864     }
865
866     private void setRequestParametersForRequestDetails(RequestDetails requestDetails) {
867         RequestParameters requestParameters = new RequestParameters();
868         requestParameters.setTestApi("GR_API");
869         requestDetails.setRequestParameters(requestParameters);
870     }
871
872     private void setModelInfoForRequestDetails(RequestDetails requestDetails) {
873         ModelInfo modelInfo = new ModelInfo();
874         modelInfo.setModelType("vfModule");
875         requestDetails.setModelInfo(modelInfo);
876     }
877
878     @Test
879     public void shouldProperlyUpdateVnfSoftwareWithProperParameters() {
880         //  given
881         String serviceInstanceId = "testServiceId";
882         String vnfInstanceId = "testVnfInstanceId";
883
884         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
885
886         requestDetails.setVnfInstanceId("testVnfInstanceId");
887         requestDetails.setVnfName("testVnfName");
888         requestDetails.setRequestParameters(new RequestParameters());
889
890         requestDetails.getRequestParameters().setAdditionalProperty("payload", "{" +
891                 "\"existing_software_version\": \"testExistingSoftwareParam\"," +
892                 "\"new_software_version\": \"testNewSoftwareParam\"," +
893                 "\"operations_timeout\": \"100\"" +
894                 "}");
895
896         MsoResponseWrapper okResponse = createOkResponse();
897
898         given(msoInterface.changeManagementUpdate(isA(org.onap.vid.changeManagement.RequestDetailsWrapper.class), any(String.class))).willReturn(okResponse);
899
900         //  when
901         MsoResponseWrapper response = (MsoResponseWrapper) msoBusinessLogic.updateVnfSoftware(requestDetails, serviceInstanceId, vnfInstanceId);
902
903         //  then
904         assertThat(response).isEqualToComparingFieldByField(okResponse);
905     }
906
907     @Test
908     public void shouldProperlyUpdateVnfConfigWithProperParameters() {
909         //  given
910         String serviceInstanceId = "testServiceId";
911         String vnfInstanceId = "testVnfInstanceId";
912
913         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
914
915         requestDetails.setVnfInstanceId("testVnfInstanceId");
916         requestDetails.setVnfName("testVnfName");
917         requestDetails.setRequestParameters(new RequestParameters());
918
919         requestDetails.getRequestParameters().setAdditionalProperty("payload", "{" +
920                 "\"request-parameters\": \"testRequestParam\"," +
921                 "\"configuration-parameters\": \"testConfigParams\"" +
922                 "}");
923
924         MsoResponseWrapper okResponse = createOkResponse();
925
926         given(msoInterface.changeManagementUpdate(isA(org.onap.vid.changeManagement.RequestDetailsWrapper.class), any(String.class))).willReturn(okResponse);
927
928         //  when
929         MsoResponseWrapper response = (MsoResponseWrapper) msoBusinessLogic.updateVnfConfig(requestDetails, serviceInstanceId, vnfInstanceId);
930
931         //  then
932         assertThat(response).isEqualToComparingFieldByField(okResponse);
933     }
934
935     @Test
936     public void shouldProperlyDeleteConfigurationWithProperParameters() {
937         //  given
938         String serviceInstanceId = "testServiceId";
939         String configurationId = "testConfigurationId";
940
941         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
942
943         requestDetails.setVnfInstanceId("testVnfInstanceId");
944         requestDetails.setVnfName("testVnfName");
945
946         MsoResponseWrapper okResponse = createOkResponse();
947         RequestDetailsWrapper wrappedRequestDetail = new RequestDetailsWrapper(requestDetails);
948
949         given(msoInterface.deleteConfiguration(eq(wrappedRequestDetail), any(String.class))).willReturn(okResponse);
950
951         //  when
952         MsoResponseWrapper response = msoBusinessLogic.deleteConfiguration(wrappedRequestDetail, serviceInstanceId, configurationId);
953
954         //  then
955         assertThat(response).isEqualToComparingFieldByField(okResponse);
956     }
957
958     @Test
959     public void shouldProperlySetConfigurationActiveStatusActiveWithProperParameters() {
960         //  given
961         String serviceInstanceId = "testServiceId";
962         String configurationId = "testConfigurationId";
963
964         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
965
966         requestDetails.setVnfInstanceId("testVnfInstanceId");
967         requestDetails.setVnfName("testVnfName");
968
969         MsoResponseWrapper okResponse = createOkResponse();
970
971         String endpoint =
972                 validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATION_INSTANCE)
973                         .replace(SVC_INSTANCE_ID, serviceInstanceId)
974                         .replace(CONFIGURATION_ID, configurationId)
975                         + "/activate";
976
977         given(msoInterface.setConfigurationActiveStatus(eq(requestDetails), eq(endpoint))).willReturn(okResponse);
978
979         //  when
980         MsoResponseWrapper response = msoBusinessLogic.setConfigurationActiveStatus(requestDetails, serviceInstanceId, configurationId, true);
981
982         //  then
983         assertThat(response).isEqualToComparingFieldByField(okResponse);
984     }
985
986     @Test
987     public void shouldProperlySetConfigurationActiveStatusDeactivateWithProperParameters() {
988         //  given
989         String serviceInstanceId = "testServiceId";
990         String configurationId = "testConfigurationId";
991
992         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
993
994         requestDetails.setVnfInstanceId("testVnfInstanceId");
995         requestDetails.setVnfName("testVnfName");
996
997         MsoResponseWrapper okResponse = createOkResponse();
998
999         String endpoint =
1000                 validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATION_INSTANCE)
1001                         .replace(SVC_INSTANCE_ID, serviceInstanceId)
1002                         .replace(CONFIGURATION_ID, configurationId)
1003                         + "/deactivate";
1004
1005         given(msoInterface.setConfigurationActiveStatus(eq(requestDetails), eq(endpoint))).willReturn(okResponse);
1006
1007         //  when
1008         MsoResponseWrapper response = msoBusinessLogic.setConfigurationActiveStatus(requestDetails, serviceInstanceId, configurationId, false);
1009
1010         //  then
1011         assertThat(response).isEqualToComparingFieldByField(okResponse);
1012     }
1013
1014     @Test
1015     public void shouldProperlySetServiceInstanceStatusActiveWithProperParameters() {
1016         //  given
1017         String serviceInstanceId = "testServiceId";
1018         MsoResponseWrapper okResponse = createOkResponse();
1019
1020         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1021
1022         given(msoInterface.setServiceInstanceStatus(eq(requestDetails), endsWith(serviceInstanceId + "/activate"))).willReturn(okResponse);
1023
1024         //  when
1025         MsoResponseWrapper response = msoBusinessLogic.setServiceInstanceStatus(requestDetails, serviceInstanceId, true);
1026
1027         //  then
1028         assertThat(response).isEqualToComparingFieldByField(okResponse);
1029     }
1030
1031     @Test
1032     public void shouldProperlySetServiceInstanceStatusDeactivateWithProperParameters() {
1033         //  given
1034         String serviceInstanceId = "testServiceId";
1035         MsoResponseWrapper okResponse = createOkResponse();
1036
1037         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1038
1039         given(msoInterface.setServiceInstanceStatus(eq(requestDetails), endsWith(serviceInstanceId + "/deactivate"))).willReturn(okResponse);
1040
1041         //  when
1042         MsoResponseWrapper response = msoBusinessLogic.setServiceInstanceStatus(requestDetails, serviceInstanceId, false);
1043
1044         //  then
1045         assertThat(response).isEqualToComparingFieldByField(okResponse);
1046     }
1047
1048     @Test(expectedExceptions = MsoTestException.class)
1049     public void shouldThrowExceptionWhenSetServiceInstanceStatusWithWrongParameters() {
1050         //  given
1051         String serviceInstanceId = "testServiceId";
1052
1053         doThrow(new MsoTestException("testException")).
1054                 when(msoInterface).setServiceInstanceStatus(eq(null), any(String.class));
1055
1056         //  when
1057         msoBusinessLogic.setServiceInstanceStatus(null, serviceInstanceId, true);
1058     }
1059
1060     @Test
1061     public void shouldProperlySetPortOnConfigurationStatusEnableWithProperParameters() {
1062         //  given
1063         String serviceInstanceId = "testServiceId";
1064         String configurationId = "testConfigurationId";
1065         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1066         requestDetails.setVnfInstanceId("testVnfInstanceId");
1067         requestDetails.setVnfName("testVnfName");
1068         MsoResponseWrapper okResponse = createOkResponse();
1069
1070         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATION_INSTANCE)
1071                 .replace(SVC_INSTANCE_ID, serviceInstanceId)
1072                 .replace(CONFIGURATION_ID, configurationId)
1073                 + "/enablePort";
1074
1075         given(msoInterface.setPortOnConfigurationStatus(eq(requestDetails), eq(endpoint))).willReturn(okResponse);
1076
1077         //  when
1078         MsoResponseWrapper response = msoBusinessLogic.setPortOnConfigurationStatus(requestDetails, serviceInstanceId, configurationId, true);
1079
1080         //  then
1081         assertThat(response).isEqualToComparingFieldByField(okResponse);
1082     }
1083
1084     @Test
1085     public void shouldProperlySetPortOnConfigurationStatusDisableWithProperParameters() {
1086         //  given
1087         String serviceInstanceId = "testServiceId";
1088         String configurationId = "testConfigurationId";
1089         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1090         requestDetails.setVnfInstanceId("testVnfInstanceId");
1091         requestDetails.setVnfName("testVnfName");
1092         MsoResponseWrapper okResponse = createOkResponse();
1093
1094         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATION_INSTANCE)
1095                 .replace(SVC_INSTANCE_ID, serviceInstanceId)
1096                 .replace(CONFIGURATION_ID, configurationId)
1097                 + "/disablePort";
1098
1099         given(msoInterface.setPortOnConfigurationStatus(eq(requestDetails), eq(endpoint))).willReturn(okResponse);
1100
1101         //  when
1102         MsoResponseWrapper response = msoBusinessLogic.setPortOnConfigurationStatus(requestDetails, serviceInstanceId, configurationId, false);
1103
1104         //  then
1105         assertThat(response).isEqualToComparingFieldByField(okResponse);
1106     }
1107
1108     @Test
1109     public void shouldProperlyCreateOperationalEnvironmentActivationRequestDetailsWithProperParameters() {
1110         //  given
1111         OperationalEnvironmentActivateInfo details = createTestOperationalEnvironmentActivateInfo();
1112         //  when
1113         org.onap.vid.changeManagement.RequestDetailsWrapper<RequestDetails> requestDetails = msoBusinessLogic.createOperationalEnvironmentActivationRequestDetails(details);
1114
1115         //  then
1116         assertThat(requestDetails.requestDetails.getRequestParameters().getAdditionalProperties().values()).contains(details.getWorkloadContext(), details.getManifest());
1117         assertThat(requestDetails.requestDetails.getRequestInfo().getRequestorId()).isEqualTo(userId);
1118     }
1119
1120     @Test
1121     public void shouldProperlyGetOperationalEnvironmentActivationPathWithProperParameters() {
1122         // given
1123         OperationalEnvironmentActivateInfo details = createTestOperationalEnvironmentActivateInfo();
1124
1125         // when
1126         String response = msoBusinessLogic.getOperationalEnvironmentActivationPath(details);
1127
1128         // then
1129         assertThat(response).contains(operationalEnvironmentId);
1130     }
1131
1132     @Test
1133     public void shouldProperlyCreateOperationalEnvironmentDeactivationRequestDetailsWithProperParameters() {
1134         // given
1135         OperationalEnvironmentDeactivateInfo details = createTestOperationalEnvironmentDeactivateInfo();
1136
1137         // when
1138         org.onap.vid.changeManagement.RequestDetailsWrapper<RequestDetails> response;
1139         response = msoBusinessLogic.createOperationalEnvironmentDeactivationRequestDetails(details);
1140
1141         // then
1142         assertThat(response.requestDetails.getRequestInfo().getRequestorId()).isEqualTo(userId);
1143     }
1144
1145     @Test
1146     public void shouldProperlyGetCloudResourcesRequestsStatusPathWithProperParameters() {
1147         // given
1148         String requestId = "testRequestId";
1149
1150         // when
1151         String response = msoBusinessLogic.getCloudResourcesRequestsStatusPath(requestId);
1152
1153         // then
1154         assertThat(response).contains(requestId);
1155     }
1156
1157     @Test
1158     public void shouldProperlyGetOperationalEnvironmentDeactivationPathWithProperParameters() {
1159         // given
1160         OperationalEnvironmentDeactivateInfo details = createTestOperationalEnvironmentDeactivateInfo();
1161
1162         // when
1163         String response = msoBusinessLogic.getOperationalEnvironmentDeactivationPath(details);
1164
1165         // then
1166         assertThat(response).contains(operationalEnvironmentId);
1167     }
1168
1169     @Test
1170     public void shouldProperlyGetOperationalEnvironmentCreationPathWithProperParameters() {
1171         // when
1172         String response = msoBusinessLogic.getOperationalEnvironmentCreationPath();
1173
1174         // then
1175         assertThat(response).isNotBlank();
1176     }
1177
1178     @Test
1179     public void shouldProperlyConvertParametersToRequestDetailsWithProperParameters() {
1180         // given
1181         OperationalEnvironmentController.OperationalEnvironmentCreateBody input = createTestOperationalEnvironmentCreateBody();
1182
1183         // when
1184         org.onap.vid.changeManagement.RequestDetailsWrapper<OperationEnvironmentRequestDetails> response
1185                 = msoBusinessLogic.convertParametersToRequestDetails(input, userId);
1186
1187         // then
1188         assertThat(response.requestDetails.getRequestInfo().getInstanceName()).isEqualTo(input.getInstanceName());
1189         assertThat(response.requestDetails.getRequestInfo().getRequestorId()).isEqualTo(userId);
1190         assertThat(response.requestDetails.getRequestParameters().getOperationalEnvironmentType()).isEqualTo(input.getOperationalEnvironmentType());
1191         assertThat(response.requestDetails.getRequestParameters().getTenantContext()).isEqualTo(input.getTenantContext());
1192         assertThat(response.requestDetails.getRequestParameters().getWorkloadContext()).isEqualTo(input.getWorkloadContext());
1193     }
1194
1195     @Test
1196     public void shouldProperlyRemoveRelationshipFromServiceInstanceWithProperParameters() {
1197         // given
1198         MsoResponseWrapper expectedResponse = createOkResponse();
1199         String serviceInstanceId = "testServiceId";
1200         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1201
1202         given(msoInterface.removeRelationshipFromServiceInstance(eq(requestDetails), endsWith("/" + serviceInstanceId + "/removeRelationships")))
1203                 .willReturn(expectedResponse);
1204
1205         // when
1206         MsoResponseWrapper response = msoBusinessLogic.removeRelationshipFromServiceInstance(requestDetails, serviceInstanceId);
1207
1208         // then
1209         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
1210     }
1211
1212     @Test
1213     public void shouldProperlyAddRelationshipToServiceInstanceWithProperParameters() {
1214         // given
1215         MsoResponseWrapper expectedResponse = createOkResponse();
1216         String serviceInstanceId = "testServiceId";
1217         org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails();
1218
1219         given(msoInterface.addRelationshipToServiceInstance(eq(requestDetails), endsWith("/" + serviceInstanceId + "/addRelationships")))
1220                 .willReturn(expectedResponse);
1221
1222         // when
1223         MsoResponseWrapper response = msoBusinessLogic.addRelationshipToServiceInstance(requestDetails, serviceInstanceId);
1224
1225         // then
1226         assertThat(response).isEqualToComparingFieldByField(expectedResponse);
1227     }
1228
1229     @Test
1230     public void shouldProperlyRequestTypeFromValueWithValidParameters() {
1231         // given
1232         String testValue = "createInstance";
1233         // when
1234         MsoBusinessLogicImpl.RequestType response = MsoBusinessLogicImpl.RequestType.fromValue(testValue);
1235
1236         // then
1237         assertThat(response.toString()).isEqualTo(testValue);
1238     }
1239
1240     @Test(expectedExceptions = IllegalArgumentException.class)
1241     public void shouldThrowExceptionWhenRequestTypeFromValueWithWrongParameter() {
1242         // given
1243         String testValue = "notExistingParameter";
1244         // when
1245         MsoBusinessLogicImpl.RequestType.fromValue(testValue);
1246     }
1247
1248     @Test
1249     public void shouldProperlyInvokeVnfWorkflowWithValidParameters() {
1250         // given
1251         MsoResponseWrapper okResponse = createOkResponse();
1252         WorkflowRequestDetail request = createWorkflowRequestDetail();
1253         UUID serviceInstanceId = new UUID(1,10);
1254         UUID vnfInstanceId = new UUID(2,20);
1255         UUID workflow_UUID = new UUID(3,30);
1256         String path = "/instanceManagement/v1/serviceInstances/"+serviceInstanceId+"/vnfs/"+vnfInstanceId+"/workflows/"+workflow_UUID;
1257
1258         given(msoInterface.invokeWorkflow(eq(request), eq(path), MockitoHamcrest.argThat(allOf(hasEntry("X-RequestorID", "testRequester"),hasEntry("X-ONAP-PartnerName", "VID"))))).willReturn(okResponse);
1259
1260         // when
1261         MsoResponseWrapper response = msoBusinessLogic.invokeVnfWorkflow(request, "testRequester", serviceInstanceId, vnfInstanceId, workflow_UUID);
1262
1263         // then
1264         assertThat(response).isEqualToComparingFieldByField(okResponse);
1265     }
1266
1267
1268     @Test
1269     public void shouldReturnWorkflowListForGivenModelId() {
1270         given(msoInterface.getWorkflowListByModelId(anyString())).willReturn(workflowListResponse);
1271         given(workflowListResponse.getBody()).willReturn(workflowList);
1272         given(workflowListResponse.getStatus()).willReturn(HttpStatus.ACCEPTED.value());
1273
1274         SOWorkflowList workflows = msoBusinessLogic.getWorkflowListByModelId("sampleModelId");
1275
1276         assertThat(workflows).isEqualTo(workflowList);
1277     }
1278
1279     @Test(expectedExceptions = {MsoBusinessLogicImpl.WorkflowListException.class})
1280     public void shouldRaiseExceptionWhenRetrievingWorkflowsFailed() {
1281         given(msoInterface.getWorkflowListByModelId(anyString())).willReturn(workflowListResponse);
1282         given(workflowListResponse.getStatus()).willReturn(HttpStatus.INTERNAL_SERVER_ERROR.value());
1283
1284         msoBusinessLogic.getWorkflowListByModelId("sampleModelId");
1285     }
1286
1287
1288     @Test
1289     public void probeShouldReturnOrchestrationRequestsAndConnectionStatus(){
1290         String body =
1291                 "{"
1292                 + "  \"requestList\":"
1293                 + "  [{"
1294                 + "      \"request\": {}"
1295                 + "    }"
1296                 + "  ]"
1297                 + "}";
1298         mockForGetOrchestrationRequest(200, body);
1299
1300         ExternalComponentStatus externalComponentStatus = msoBusinessLogic.probeComponent();
1301
1302         assertThat(externalComponentStatus.isAvailable()).isTrue();
1303         assertThat(externalComponentStatus.getComponent()).isEqualTo(ExternalComponentStatus.Component.MSO);
1304     }
1305
1306     private WorkflowRequestDetail createWorkflowRequestDetail() {
1307         WorkflowRequestDetail workflowRequestDetail = new WorkflowRequestDetail();
1308         org.onap.vid.changeManagement.RequestParameters requestParameters = new org.onap.vid.changeManagement.RequestParameters();
1309         HashMap<String,String> paramsMap = new HashMap<>();
1310         paramsMap.put("testKey1","testValue1");
1311         paramsMap.put("testKey2","testValue2");
1312
1313         List<Map<String,String>> mapArray= new ArrayList<>();
1314         mapArray.add(paramsMap);
1315         requestParameters.setUserParams(mapArray);
1316
1317         CloudConfiguration cloudConfiguration = new CloudConfiguration();
1318         cloudConfiguration.setCloudOwner("testOwne");
1319         cloudConfiguration.setTenantId("testId");
1320         cloudConfiguration.setLcpCloudRegionId("testLcpCloudId");
1321
1322         workflowRequestDetail.setRequestParameters(requestParameters);
1323         workflowRequestDetail.setCloudConfiguration(cloudConfiguration);
1324         return workflowRequestDetail;
1325     }
1326
1327     private OperationalEnvironmentActivateInfo createTestOperationalEnvironmentActivateInfo() {
1328         OperationalEnvironmentController.OperationalEnvironmentActivateBody operationalEnvironmentActivateBody = new OperationalEnvironmentController.OperationalEnvironmentActivateBody(
1329                 "testRelatedInstanceId",
1330                 "testRelatedInstanceName",
1331                 "testWorkloadContext",
1332                 new OperationalEnvironmentController.OperationalEnvironmentManifest()
1333         );
1334         return new OperationalEnvironmentActivateInfo(operationalEnvironmentActivateBody, userId, operationalEnvironmentId);
1335     }
1336
1337     private OperationalEnvironmentDeactivateInfo createTestOperationalEnvironmentDeactivateInfo() {
1338         return new OperationalEnvironmentDeactivateInfo(userId, operationalEnvironmentId);
1339     }
1340
1341     private OperationalEnvironmentController.OperationalEnvironmentCreateBody createTestOperationalEnvironmentCreateBody() {
1342         return new OperationalEnvironmentController.OperationalEnvironmentCreateBody(
1343                 "testInstanceName",
1344                 "testEcompInstanceId",
1345                 "testEcompInstanceName",
1346                 "testOperationalEnvironmentType",
1347                 "testTenantContext",
1348                 "testWorkloadContext"
1349         );
1350     }
1351
1352     private MsoResponseWrapper createOkResponse() {
1353         HttpStatus expectedStatus = HttpStatus.ACCEPTED;
1354         String expectedBody = " \"body\": {\n" +
1355                 "      \"requestReferences\": {\n" +
1356                 "        \"instanceId\": \" 123456 \",\n" +
1357                 "        \"requestId\": \"b6dc9806-b094-42f7-9386-a48de8218ce8\"\n" +
1358                 "      }";
1359         MsoResponseWrapper responseWrapper = new MsoResponseWrapper();
1360         responseWrapper.setEntity(expectedBody);
1361         responseWrapper.setStatus(expectedStatus.value());
1362         return responseWrapper;
1363     }
1364
1365     private String getFileContentAsString(String resourceName) throws Exception {
1366         Path path = Paths.get("payload_jsons", resourceName);
1367         URL url = this.getClass().getClassLoader().getResource(path.toString());
1368         String result = "";
1369         if (url != null) {
1370             result = IOUtils.toString(url.toURI(), "UTF-8");
1371         }
1372         return result;
1373     }
1374
1375     private static class MsoRequestWrapperMatcher implements
1376             ArgumentMatcher<org.onap.vid.changeManagement.RequestDetailsWrapper> {
1377
1378         private final org.onap.vid.changeManagement.RequestDetailsWrapper expectedRequest;
1379
1380         MsoRequestWrapperMatcher(org.onap.vid.changeManagement.RequestDetailsWrapper expectedRequest) {
1381             this.expectedRequest = expectedRequest;
1382         }
1383
1384         @Override
1385         public boolean matches(org.onap.vid.changeManagement.RequestDetailsWrapper argument) {
1386             return expectedRequest.requestDetails.equals(argument.requestDetails);
1387         }
1388     }
1389
1390     private class MsoTestException extends RuntimeException {
1391         MsoTestException(String testException) {
1392             super(testException);
1393         }
1394     }
1395
1396     //you need to add mocks to httpResponse
1397     private HttpResponse<String> mockForGetOrchestrationRequest() {
1398
1399         HttpResponse<String> httpResponse = mock(HttpResponse.class);
1400         HttpResponseWithRequestInfo<String> httpResponseWithRequestInfo = new HttpResponseWithRequestInfo<>(httpResponse, MY_PRETTY_URL, HttpMethod.GET);
1401         when(msoInterface.getOrchestrationRequest(any(String.class),anyBoolean()))
1402             .thenReturn(httpResponseWithRequestInfo);
1403         return httpResponse;
1404     }
1405
1406     private HttpResponse<String> mockForGetOrchestrationRequest(int statusCode, String body) {
1407
1408         HttpResponse<String> httpResponse = mockForGetOrchestrationRequest();
1409         when(httpResponse.getStatus()).thenReturn(statusCode);
1410         when(httpResponse.getBody()).thenReturn(body);
1411         TestUtils.mockGetRawBodyWithStringBody(httpResponse, body);
1412         return httpResponse;
1413     }
1414
1415     @Test
1416     public void probeComponent_verifyGoodRequest(){
1417         String responseString = "" +
1418             "{ " +
1419             " \"requestList\": [{ " +
1420             "   \"request\": { " +
1421             "    \"requestDetails\": { " +
1422             "     \"cloudConfiguration\": { " +
1423             "      \"lcpCloudRegionId\": \"hvf6\", " +
1424             "      \"cloudOwner\": \"irma-aic\", " +
1425             "      \"tenantId\": \"ffdf52b5e5104b0e8f329b0b1637ee2e\" " +
1426             "     }, " +
1427             "     \"modelInfo\": { " +
1428             "      \"modelCustomizationName\": \"VSP1710PID298109_vWINIFRED 0\", " +
1429             "      \"modelCustomizationId\": \"24d43fdb-9aa6-4287-a68e-1e702ea89d13\", " +
1430             "      \"modelInvariantId\": \"e7961100-cde6-4b5a-bcda-b8945086950a\", " +
1431             "      \"modelVersionId\": \"959a7ba0-89ee-4984-9af6-65d5bdda4b0e\", " +
1432             "      \"modelName\": \"VSP1710PID298109_vWINIFRED\", " +
1433             "      \"modelType\": \"vnf\", " +
1434             "      \"modelVersion\": \"1.0\" " +
1435             "     }, " +
1436             "     \"relatedModelList\": [{ " +
1437             "       \"relatedInstance\": { " +
1438             "        \"instanceId\": \"6dd0f8de-93c7-48a2-914b-1a8d58e0eb48\", " +
1439             "        \"modelInfo\": { " +
1440             "         \"modelInvariantId\": \"57e00952-0af7-4f0f-b19a-408a6f73c8df\", " +
1441             "         \"modelType\": \"service\", " +
1442             "         \"modelName\": \"ServicevWINIFREDPID298109\", " +
1443             "         \"modelVersion\": \"1.0\", " +
1444             "         \"modelVersionId\": \"fe6985cd-ea33-3346-ac12-ab121484a3fe\" " +
1445             "        } " +
1446             "       } " +
1447             "      } " +
1448             "     ], " +
1449             "     \"requestInfo\": { " +
1450             "      \"source\": \"VID\", " +
1451             "      \"suppressRollback\": false, " +
1452             "      \"requestorId\": \"ds828e\" " +
1453             "     }, " +
1454             "     \"requestParameters\": { " +
1455             "      \"userParams\": [ " +
1456             "      ], " +
1457             "      \"aLaCarte\": false, " +
1458             "      \"usePreload\": true, " +
1459             "      \"rebuildVolumeGroups\": false, " +
1460             "      \"autoBuildVfModules\": false, " +
1461             "      \"cascadeDelete\": false " +
1462             "     }, " +
1463             "     \"relatedInstanceList\": [{ " +
1464             "       \"relatedInstance\": { " +
1465             "        \"instanceId\": \"6dd0f8de-93c7-48a2-914b-1a8d58e0eb48\", " +
1466             "        \"modelInfo\": { " +
1467             "         \"modelInvariantId\": \"57e00952-0af7-4f0f-b19a-408a6f73c8df\", " +
1468             "         \"modelType\": \"service\", " +
1469             "         \"modelName\": \"ServicevWINIFREDPID298109\", " +
1470             "         \"modelVersion\": \"1.0\", " +
1471             "         \"modelVersionId\": \"fe6985cd-ea33-3346-ac12-ab121484a3fe\" " +
1472             "        } " +
1473             "       } " +
1474             "      } " +
1475             "     ] " +
1476             "    }, " +
1477             "    \"requestId\": \"d352c70d-5ef8-4977-9ea8-4c8cbe860422\", " +
1478             "    \"requestScope\": \"vnf\", " +
1479             "    \"requestStatus\": { " +
1480             "     \"percentProgress\": 100.0, " +
1481             "     \"requestState\": \"Some Unknown Value\", " +
1482             "     \"statusMessage\": \"Update Is In Progress\", " +
1483             "     \"finishTime\": \"Fri, 08 Sep 2017 19:34:33 GMT\" " +
1484             "    }, " +
1485             "    \"requestType\": \"updateInstance\", " +
1486             "    \"startTime\": \"<IN_PROGRESS_DATE>\", " +
1487             "    \"instanceReferences\": { " +
1488             "     \"serviceInstanceId\": \"6dd0f8de-93c7-48a2-914b-1a8d58e0eb48\", " +
1489             "     \"vnfInstanceId\": \"7c00cc1e-6425-4fc3-afc3-0289db288d4c\", " +
1490             "     \"requestorId\": \"ds828e\" " +
1491             "    } " +
1492             "   } " +
1493             "  } " +
1494             " ] " +
1495             "} ";
1496
1497         mockForGetOrchestrationRequest(200, responseString);
1498
1499         final ExternalComponentStatus msoStatus = msoBusinessLogic.probeComponent();
1500
1501         assertMsoStatus(msoStatus, true);
1502         assertMetadata(msoStatus, 200, startsWith(responseString.substring(0, 400)), MY_PRETTY_URL, equalTo("OK"));
1503     }
1504
1505     @Test
1506     public void probeComponent_response200OkButEmptyPayload_shouldDescribeCorrectly() {
1507         String responseString = "" +
1508             "{ " +
1509             " \"requestList\": []" +
1510             "}";
1511
1512         mockForGetOrchestrationRequest(200, responseString);
1513
1514         final ExternalComponentStatus msoStatus = msoBusinessLogic.probeComponent();
1515
1516         assertMsoStatus(msoStatus, true);
1517
1518         assertMetadata(msoStatus, 200, equalTo(responseString), MY_PRETTY_URL, containsString("OK"));
1519     }
1520
1521     @Test
1522     public void probeComponent_response200OkButInvalidPayload_shouldDescribeCorrectly() {
1523         String responseString = "this payload is an invalid json";
1524
1525         mockForGetOrchestrationRequest(200, responseString);
1526
1527         final ExternalComponentStatus msoStatus = msoBusinessLogic.probeComponent();
1528
1529         assertMsoStatus(msoStatus, false);
1530
1531         assertMetadata(msoStatus, 200, equalTo(responseString), MY_PRETTY_URL, containsString("JsonParseException: Unrecognized token"));
1532     }
1533
1534     @Test
1535     public void probeComponent_verifyResponse406() {
1536         String responseString = "my raw data";
1537
1538         when(msoInterface.getOrchestrationRequest(any(), eq(true))).thenThrow(
1539             new ExceptionWithRequestInfo(HttpMethod.GET, MY_PRETTY_URL, responseString, 406,
1540                 new GenericUncheckedException(
1541                     new HttpException("Simulating as 406 was returned (200 or 202 expected)"))));
1542
1543         final ExternalComponentStatus msoStatus = msoBusinessLogic.probeComponent();
1544
1545         assertMsoStatus(msoStatus, false);
1546
1547         assertMetadata(msoStatus, 406, equalTo(responseString), MY_PRETTY_URL, containsString("HttpException: Simulating as 406 was returned"));
1548     }
1549
1550
1551     @Test
1552     public void probeComponent_throwNullPointerException_resultIsWithErrorMetadata() {
1553         when(msoInterface.getOrchestrationRequest(any(), eq(true))).thenThrow(new NullPointerException());
1554
1555         final ExternalComponentStatus msoStatus = msoBusinessLogic.probeComponent();
1556
1557         MatcherAssert.assertThat(msoStatus.isAvailable(), is(false));
1558         MatcherAssert.assertThat(msoStatus.getComponent(), is(MSO));
1559         MatcherAssert.assertThat(msoStatus.getMetadata(), instanceOf(ErrorMetadata.class));
1560
1561         final ErrorMetadata metadata = ((ErrorMetadata) msoStatus.getMetadata());
1562         org.junit.Assert.assertThat(metadata.getDescription(), containsString("NullPointerException"));
1563     }
1564
1565     private void assertMsoStatus(ExternalComponentStatus msoStatus, boolean isAvailable) {
1566         MatcherAssert.assertThat(msoStatus.isAvailable(), is(isAvailable));
1567         MatcherAssert.assertThat(msoStatus.getComponent(), is(MSO));
1568         MatcherAssert.assertThat(msoStatus.getMetadata(), instanceOf(HttpRequestMetadata.class));
1569     }
1570
1571     private void assertMetadata(ExternalComponentStatus msoStatus, int httpCode, Matcher<String> rawData, String url, Matcher<String> descriptionMatcher) {
1572         final HttpRequestMetadata metadata = ((HttpRequestMetadata) msoStatus.getMetadata());
1573         org.junit.Assert.assertThat(metadata.getHttpMethod(), equalTo(HttpMethod.GET));
1574         org.junit.Assert.assertThat(metadata.getHttpCode(), equalTo(httpCode));
1575         org.junit.Assert.assertThat(metadata.getUrl(), equalTo(url));
1576         org.junit.Assert.assertThat(metadata.getRawData(), rawData);
1577         org.junit.Assert.assertThat(metadata.getDescription(), descriptionMatcher);
1578     }
1579 }
1580