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