2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 - 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.mso.rest;
23 import static org.apache.commons.io.IOUtils.toInputStream;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.mockito.ArgumentMatchers.anyMap;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30 import static org.onap.vid.controller.MsoController.SVC_INSTANCE_ID;
31 import static org.onap.vid.controller.MsoController.VNF_INSTANCE_ID;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.xebialabs.restito.server.StubServer;
35 import io.joshworks.restclient.http.HttpResponse;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.UUID;
42 import org.apache.commons.configuration.PropertiesConfiguration;
43 import org.glassfish.grizzly.http.util.HttpStatus;
44 import org.junit.AfterClass;
45 import org.junit.BeforeClass;
46 import org.junit.Ignore;
47 import org.junit.Test;
48 import org.onap.portalsdk.core.util.SystemProperties;
49 import org.onap.vid.aai.HttpResponseWithRequestInfo;
50 import org.onap.vid.client.SyncRestClient;
51 import org.onap.vid.controller.MsoController;
52 import org.onap.vid.controller.WebConfig;
53 import org.onap.vid.mso.MsoProperties;
54 import org.onap.vid.mso.MsoResponseWrapper;
55 import org.onap.vid.mso.MsoResponseWrapperInterface;
56 import org.onap.vid.mso.RestObject;
57 import org.onap.vid.utils.SystemPropertiesWrapper;
58 import org.springframework.test.context.ContextConfiguration;
60 @ContextConfiguration(classes = {SystemProperties.class})
61 public class MsoRestClientNewTest {
63 private static StubServer server;
64 private static StubServer securedServer;
65 private static PropertiesConfiguration props = new PropertiesConfiguration();
66 private static String msoCreateServiceInstanceJson;
67 private static String msoScaleOutVfModule;
68 private final static String CREATE_INSTANCE_RESPONSE_STR =
69 "{\"requestReferences\":{\"instanceId\":\"baa13544-0e95-4644-9565-9a198a29a294\","
70 + "\"requestId\":\"a42a1a35-3d63-4629-bbe0-4989fa7414cb\"}}";
71 private final static String SERVICE_INSTANCE_ID = "12345";
72 private static final String SAMPLE_VNF_INSTANCE_ID = "111";
73 private static final String SAMPLE_VNF_MODULE_ID = "987";
74 private static final String SAMPLE_NETWORK_INSTANCE_ID = "666";
75 private static final String SAMPLE_CONFIGURATION_ID = "997";
76 private static final String SAMPLE_REQUEST_ID = "7777";
80 public static void start() throws Exception {
81 server = new StubServer().run();
82 securedServer = new StubServer().secured().run();
84 Path resourceDirectory =
85 Paths.get("src", "test", "resources", "WEB-INF", "conf", "system.properties");
86 try (InputStream is = Files.newInputStream(resourceDirectory)) {
90 Path msoServiceInstantiationJsonFilePath =
91 Paths.get("src", "test", "resources", "payload_jsons", "mso_service_instantiation.json");
93 Path scaleOutJsonFilePath = Paths.get("src", "test", "resources", "payload_jsons", "scaleOutVfModulePayloadToMso.json");
94 msoCreateServiceInstanceJson =
95 String.join("\n", Files.readAllLines(msoServiceInstantiationJsonFilePath));
96 msoScaleOutVfModule = String.join("\n", Files.readAllLines(scaleOutJsonFilePath));
101 public static void stop() {
103 securedServer.stop();
107 private String baseUrl() {
108 return String.format("http://localhost:%d", server.getPort());
112 public void testCreateSvcInstance() throws Exception {
113 String endpoint = props.getString(MsoProperties.MSO_REST_API_CONFIGURATIONS);
114 endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
115 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
118 HttpStatus.ACCEPTED_202,
119 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
120 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createSvcInstance);
125 public void testCreateVnf() throws Exception {
126 String endpoint = props.getString(MsoProperties.MSO_REST_API_VNF_INSTANCE);
127 endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
128 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
131 HttpStatus.ACCEPTED_202,
132 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
134 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVnf);
139 public void testCreateNwInstance() throws Exception {
140 String endpoint = props.getString(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
141 String nw_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
142 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
145 HttpStatus.ACCEPTED_202,
146 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
147 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createNwInstance);
152 public void testCreateVolumeGroupInstance() throws Exception {
153 String endpoint = props.getString(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
154 String vnf_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
155 vnf_endpoint = vnf_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
156 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
159 HttpStatus.ACCEPTED_202,
160 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
161 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVolumeGroupInstance);
166 public void testCreateVfModuleInstance() throws Exception {
167 String endpoint = props.getString(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
168 String partial_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
169 String vf_module_endpoint =
170 partial_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
173 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
176 HttpStatus.ACCEPTED_202,
177 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
178 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVfModuleInstance);
183 public void testCreateConfigurationInstance() throws Exception {
184 MsoRestClientNew testSubject;
185 RequestDetailsWrapper requestDetails = null;
186 String endpoint = "";
187 MsoResponseWrapper result;
191 testSubject = createTestSubject();
192 result = testSubject.createConfigurationInstance(requestDetails, endpoint);
193 } catch (Exception e) {
198 public void testDeleteSvcInstance() throws Exception {
199 String endpoint = props.getString(MsoProperties.MSO_REST_API_SVC_INSTANCE);
200 endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
203 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
206 HttpStatus.NO_CONTENT_204,
207 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
208 closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteSvcInstance);
214 public void testDeleteVnf() throws Exception {
215 String endpoint = props.getString(MsoProperties.MSO_REST_API_VNF_INSTANCE);
216 endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
218 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
221 HttpStatus.NO_CONTENT_204,
222 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
223 closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVnf);
229 public void testDeleteVfModule() throws Exception {
230 String endpoint = props.getString(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
231 String part_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
232 String vf_modules_endpoint = part_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
233 String delete_vf_endpoint = vf_modules_endpoint + '/' + SAMPLE_VNF_MODULE_ID;
235 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
238 HttpStatus.NO_CONTENT_204,
239 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
240 closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVfModule);
246 public void testDeleteVolumeGroupInstance() throws Exception {
247 String endpoint = props.getString(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
248 String svc_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
249 String vnf_endpoint = svc_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
250 String delete_volume_group_endpoint = vnf_endpoint + "/" + SAMPLE_VNF_MODULE_ID;
252 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
254 delete_volume_group_endpoint,
255 HttpStatus.NO_CONTENT_204,
256 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
257 closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVolumeGroupInstance);
263 public void testDeleteNwInstance() throws Exception {
264 String endpoint = props.getString(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
265 String svc_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
266 String delete_nw_endpoint = svc_endpoint + "/" + SAMPLE_NETWORK_INSTANCE_ID;
268 try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
271 HttpStatus.NO_CONTENT_204,
272 CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
273 closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteNwInstance);
278 public void testGetOrchestrationRequest() {
279 String p = props.getString(MsoProperties.MSO_REST_API_GET_ORC_REQ);
280 String path = p + "/" + SAMPLE_REQUEST_ID;
282 try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
286 CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
287 closure.executeGet(msoRestClient()::getOrchestrationRequest);
292 public void testGetManualTasksByRequestId() {
293 String p = props.getString(MsoProperties.MSO_REST_API_GET_ORC_REQ);
294 String path = p + "/" + UUID.randomUUID();
295 String validResponse = ""
297 + " \"taskList\": [ "
299 + " \"taskId\": \"daf4dd84-b77a-42da-a051-3239b7a9392c\", "
300 + " \"type\": \"fallout\", "
301 + " \"nfRole\": \"vEsmeralda\", "
302 + " \"subscriptionServiceType\": \"Emanuel\", "
303 + " \"originalRequestId\": \"d352c70d-5ef8-4977-9ea8-4c8cbe860422\", "
304 + " \"originalRequestorId\": \"ss835w\", "
305 + " \"errorSource\": \"A&AI\", "
306 + " \"errorCode\": \"404\", "
307 + " \"errorMessage\": \"Failed in A&AI 404\", "
308 + " \"validResponses\": [ "
319 try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
323 validResponse,validResponse)) {
324 closure.executeGet(endpoint -> msoRestClient().getManualTasksByRequestId(null, null, endpoint, null));
329 public void testCompleteManualTask() throws Exception {
330 MsoRestClientNew testSubject;
331 RequestDetails requestDetails = null;
333 String sourceId = "";
334 String endpoint = "";
335 RestObject restObject = null;
336 MsoResponseWrapper result;
340 testSubject = createTestSubject();
341 result = testSubject.completeManualTask(requestDetails, t, sourceId, endpoint, restObject);
342 } catch (Exception e) {
347 public void testDeleteConfiguration() throws Exception {
348 MsoRestClientNew testSubject;
349 RequestDetailsWrapper requestDetails = null;
350 String pmc_endpoint = "";
351 MsoResponseWrapper result;
355 testSubject = createTestSubject();
356 result = testSubject.deleteConfiguration(requestDetails, pmc_endpoint);
357 } catch (Exception e) {
362 public void testSetConfigurationActiveStatus() throws Exception {
363 String endpoint = "/serviceInstances/v7/<service_instance_id>/configurations/<configuration_id>";
364 endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
365 endpoint = endpoint.replace(MsoController.CONFIGURATION_ID, SAMPLE_CONFIGURATION_ID);
366 endpoint = endpoint + "/activate";
368 try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
371 HttpStatus.ACCEPTED_202,
372 CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
373 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::setConfigurationActiveStatus);
378 public void testSetPortOnConfigurationStatus() throws Exception {
379 MsoRestClientNew testSubject;
380 RequestDetails request = null;
382 MsoResponseWrapper result;
386 testSubject = createTestSubject();
387 result = testSubject.setPortOnConfigurationStatus(request, path);
388 } catch (Exception e) {
393 public void testChangeManagementUpdate() throws Exception {
394 MsoRestClientNew testSubject;
395 org.onap.vid.changeManagement.RequestDetailsWrapper requestDetails = null;
396 String endpoint = "";
397 MsoResponseWrapperInterface result;
401 testSubject = createTestSubject();
402 result = testSubject.changeManagementUpdate(requestDetails, endpoint);
403 } catch (Exception e) {
408 public void testSetServiceInstanceStatus_givenValidResponse_responseIsPopulatedAccordingly() {
409 RequestDetails requestDetails = new RequestDetails();
411 String sourceId = "";
412 String endpoint = "";
413 final SyncRestClient client = mock(SyncRestClient.class);
414 MsoRestClientNew testSubject = new MsoRestClientNew(client, "", null, new SystemPropertiesWrapper());
417 final HttpResponse<String> response = mock(HttpResponse.class);
418 final int expectedStatus = 202;
419 final String expectedResponse = "expected response";
421 when(client.post(eq(endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(response);
422 when(response.getStatus()).thenReturn(expectedStatus);
423 when(response.getBody()).thenReturn(expectedResponse);
424 when(response.getRawBody()).thenReturn(toInputStream(expectedResponse));
427 MsoResponseWrapper responseWrapper = testSubject.setServiceInstanceStatus(requestDetails, endpoint);
429 assertThat(responseWrapper.getStatus(), is(expectedStatus));
430 assertThat(responseWrapper.getEntity(), is(expectedResponse));
434 public void testRemoveRelationshipFromServiceInstance() throws Exception {
435 String serviceEndpoint = props.getString(MsoProperties.MSO_REST_API_SVC_INSTANCE);
436 String removeRelationshipsPath = serviceEndpoint + "/" + SERVICE_INSTANCE_ID + "/removeRelationships";
438 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
440 removeRelationshipsPath,
441 HttpStatus.ACCEPTED_202,
442 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
443 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::removeRelationshipFromServiceInstance);
448 public void testAddRelationshipToServiceInstance() throws Exception {
449 MsoRestClientNew testSubject;
450 RequestDetails requestDetails = null;
451 String addRelationshipsPath = "";
452 MsoResponseWrapper result;
456 testSubject = createTestSubject();
457 result = testSubject.addRelationshipToServiceInstance(requestDetails, addRelationshipsPath);
458 } catch (Exception e) {
462 public void testScaleOutVfModule() throws IOException {
463 String serviceEndpoint = props.getString(MsoProperties.MSO_REST_API_VF_MODULE_SCALE_OUT);
464 String partial_endpoint = serviceEndpoint.replaceFirst(SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
465 String vf_module_endpoint = partial_endpoint.replaceFirst(VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
466 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
469 HttpStatus.ACCEPTED_202,
470 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
471 closure.executePostCall(msoScaleOutVfModule, msoRestClient()::scaleOutVFModuleInstance);
476 private MsoRestClientNew msoRestClient() {
477 final WebConfig webConfig = new WebConfig();
478 return new MsoRestClientNew(new SyncRestClient(webConfig.unirestFasterxmlObjectMapper(new ObjectMapper())), baseUrl(), null, new SystemPropertiesWrapper());
481 private MsoRestClientNew createTestSubject() {
482 return new MsoRestClientNew(null, "", null, new SystemPropertiesWrapper());