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.client.SyncRestClient;
50 import org.onap.vid.controller.MsoController;
51 import org.onap.vid.controller.WebConfig;
52 import org.onap.vid.mso.MsoProperties;
53 import org.onap.vid.mso.MsoResponseWrapper;
54 import org.onap.vid.mso.MsoResponseWrapperInterface;
55 import org.onap.vid.mso.RestObject;
56 import org.onap.vid.utils.SystemPropertiesWrapper;
57 import org.springframework.test.context.ContextConfiguration;
59 @ContextConfiguration(classes = {SystemProperties.class})
60 public class MsoRestClientNewTest {
62 private static StubServer server;
63 private static StubServer securedServer;
64 private static PropertiesConfiguration props = new PropertiesConfiguration();
65 private static String msoCreateServiceInstanceJson;
66 private static String msoScaleOutVfModule;
67 private final static String CREATE_INSTANCE_RESPONSE_STR =
68 "{\"requestReferences\":{\"instanceId\":\"baa13544-0e95-4644-9565-9a198a29a294\","
69 + "\"requestId\":\"a42a1a35-3d63-4629-bbe0-4989fa7414cb\"}}";
70 private final static String SERVICE_INSTANCE_ID = "12345";
71 private static final String SAMPLE_VNF_INSTANCE_ID = "111";
72 private static final String SAMPLE_VNF_MODULE_ID = "987";
73 private static final String SAMPLE_NETWORK_INSTANCE_ID = "666";
74 private static final String SAMPLE_CONFIGURATION_ID = "997";
75 private static final String SAMPLE_REQUEST_ID = "7777";
79 public static void start() throws Exception {
80 server = new StubServer().run();
81 securedServer = new StubServer().secured().run();
83 Path resourceDirectory =
84 Paths.get("src", "test", "resources", "WEB-INF", "conf", "system.properties");
85 try (InputStream is = Files.newInputStream(resourceDirectory)) {
89 Path msoServiceInstantiationJsonFilePath =
90 Paths.get("src", "test", "resources", "payload_jsons", "mso_service_instantiation.json");
92 Path scaleOutJsonFilePath = Paths.get("src", "test", "resources", "payload_jsons", "scaleOutVfModulePayloadToMso.json");
93 msoCreateServiceInstanceJson =
94 String.join("\n", Files.readAllLines(msoServiceInstantiationJsonFilePath));
95 msoScaleOutVfModule = String.join("\n", Files.readAllLines(scaleOutJsonFilePath));
100 public static void stop() {
102 securedServer.stop();
106 private String baseUrl() {
107 return String.format("http://localhost:%d", server.getPort());
111 public void testCreateSvcInstance() throws Exception {
112 String endpoint = props.getString(MsoProperties.MSO_REST_API_CONFIGURATIONS);
113 endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
114 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
117 HttpStatus.ACCEPTED_202,
118 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
119 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createSvcInstance);
124 public void testCreateVnf() throws Exception {
125 String endpoint = props.getString(MsoProperties.MSO_REST_API_VNF_INSTANCE);
126 endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
127 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
130 HttpStatus.ACCEPTED_202,
131 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
133 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVnf);
138 public void testCreateNwInstance() throws Exception {
139 String endpoint = props.getString(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
140 String nw_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
141 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
144 HttpStatus.ACCEPTED_202,
145 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
146 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createNwInstance);
151 public void testCreateVolumeGroupInstance() throws Exception {
152 String endpoint = props.getString(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
153 String vnf_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
154 vnf_endpoint = vnf_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
155 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
158 HttpStatus.ACCEPTED_202,
159 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
160 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVolumeGroupInstance);
165 public void testCreateVfModuleInstance() throws Exception {
166 String endpoint = props.getString(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
167 String partial_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
168 String vf_module_endpoint =
169 partial_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
172 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
175 HttpStatus.ACCEPTED_202,
176 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
177 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVfModuleInstance);
182 public void testCreateConfigurationInstance() throws Exception {
183 MsoRestClientNew testSubject;
184 RequestDetailsWrapper requestDetails = null;
185 String endpoint = "";
186 MsoResponseWrapper result;
190 testSubject = createTestSubject();
191 result = testSubject.createConfigurationInstance(requestDetails, endpoint);
192 } catch (Exception e) {
197 public void testDeleteSvcInstance() throws Exception {
198 String endpoint = props.getString(MsoProperties.MSO_REST_API_SVC_INSTANCE);
199 endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
202 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
205 HttpStatus.NO_CONTENT_204,
206 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
207 closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteSvcInstance);
213 public void testDeleteVnf() throws Exception {
214 String endpoint = props.getString(MsoProperties.MSO_REST_API_VNF_INSTANCE);
215 endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
217 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
220 HttpStatus.NO_CONTENT_204,
221 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
222 closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVnf);
228 public void testDeleteVfModule() throws Exception {
229 String endpoint = props.getString(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
230 String part_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
231 String vf_modules_endpoint = part_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
232 String delete_vf_endpoint = vf_modules_endpoint + '/' + SAMPLE_VNF_MODULE_ID;
234 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
237 HttpStatus.NO_CONTENT_204,
238 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
239 closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVfModule);
245 public void testDeleteVolumeGroupInstance() throws Exception {
246 String endpoint = props.getString(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
247 String svc_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
248 String vnf_endpoint = svc_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
249 String delete_volume_group_endpoint = vnf_endpoint + "/" + SAMPLE_VNF_MODULE_ID;
251 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
253 delete_volume_group_endpoint,
254 HttpStatus.NO_CONTENT_204,
255 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
256 closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVolumeGroupInstance);
262 public void testDeleteNwInstance() throws Exception {
263 String endpoint = props.getString(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
264 String svc_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
265 String delete_nw_endpoint = svc_endpoint + "/" + SAMPLE_NETWORK_INSTANCE_ID;
267 try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
270 HttpStatus.NO_CONTENT_204,
271 CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
272 closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteNwInstance);
277 public void testGetOrchestrationRequest() {
278 String p = props.getString(MsoProperties.MSO_REST_API_GET_ORC_REQ);
279 String path = p + "/" + SAMPLE_REQUEST_ID;
281 try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
285 CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
286 closure.executeGet(msoRestClient()::getOrchestrationRequest);
291 public void testGetManualTasksByRequestId() {
292 String p = props.getString(MsoProperties.MSO_REST_API_GET_ORC_REQ);
293 String path = p + "/" + UUID.randomUUID();
294 String validResponse = ""
296 + " \"taskList\": [ "
298 + " \"taskId\": \"daf4dd84-b77a-42da-a051-3239b7a9392c\", "
299 + " \"type\": \"fallout\", "
300 + " \"nfRole\": \"vEsmeralda\", "
301 + " \"subscriptionServiceType\": \"Emanuel\", "
302 + " \"originalRequestId\": \"d352c70d-5ef8-4977-9ea8-4c8cbe860422\", "
303 + " \"originalRequestorId\": \"ss835w\", "
304 + " \"errorSource\": \"A&AI\", "
305 + " \"errorCode\": \"404\", "
306 + " \"errorMessage\": \"Failed in A&AI 404\", "
307 + " \"validResponses\": [ "
318 try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
322 validResponse,validResponse)) {
323 closure.executeGet(endpoint -> msoRestClient().getManualTasksByRequestId(null, null, endpoint, null));
328 public void testGetOrchestrationRequestsForDashboard() throws Exception {
329 MsoRestClientNew testSubject;
331 String sourceId = "";
332 String endpoint = "";
333 RestObject restObject = null;
334 MsoResponseWrapper result;
338 testSubject = createTestSubject();
339 result = testSubject.getOrchestrationRequest(t, sourceId, endpoint, restObject, true);
340 } catch (Exception e) {
345 public void testCompleteManualTask() throws Exception {
346 MsoRestClientNew testSubject;
347 RequestDetails requestDetails = null;
349 String sourceId = "";
350 String endpoint = "";
351 RestObject restObject = null;
352 MsoResponseWrapper result;
356 testSubject = createTestSubject();
357 result = testSubject.completeManualTask(requestDetails, t, sourceId, endpoint, restObject);
358 } catch (Exception e) {
363 public void testDeleteConfiguration() throws Exception {
364 MsoRestClientNew testSubject;
365 RequestDetailsWrapper requestDetails = null;
366 String pmc_endpoint = "";
367 MsoResponseWrapper result;
371 testSubject = createTestSubject();
372 result = testSubject.deleteConfiguration(requestDetails, pmc_endpoint);
373 } catch (Exception e) {
378 public void testSetConfigurationActiveStatus() throws Exception {
379 String endpoint = "/serviceInstances/v7/<service_instance_id>/configurations/<configuration_id>";
380 endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
381 endpoint = endpoint.replace(MsoController.CONFIGURATION_ID, SAMPLE_CONFIGURATION_ID);
382 endpoint = endpoint + "/activate";
384 try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
387 HttpStatus.ACCEPTED_202,
388 CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
389 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::setConfigurationActiveStatus);
394 public void testSetPortOnConfigurationStatus() throws Exception {
395 MsoRestClientNew testSubject;
396 RequestDetails request = null;
398 MsoResponseWrapper result;
402 testSubject = createTestSubject();
403 result = testSubject.setPortOnConfigurationStatus(request, path);
404 } catch (Exception e) {
409 public void testChangeManagementUpdate() throws Exception {
410 MsoRestClientNew testSubject;
411 org.onap.vid.changeManagement.RequestDetailsWrapper requestDetails = null;
412 String endpoint = "";
413 MsoResponseWrapperInterface result;
417 testSubject = createTestSubject();
418 result = testSubject.changeManagementUpdate(requestDetails, endpoint);
419 } catch (Exception e) {
424 public void testSetServiceInstanceStatus_givenValidResponse_responseIsPopulatedAccordingly() {
425 RequestDetails requestDetails = new RequestDetails();
427 String sourceId = "";
428 String endpoint = "";
429 final SyncRestClient client = mock(SyncRestClient.class);
430 MsoRestClientNew testSubject = new MsoRestClientNew(client, "", null, new SystemPropertiesWrapper());
433 final HttpResponse<String> response = mock(HttpResponse.class);
434 final int expectedStatus = 202;
435 final String expectedResponse = "expected response";
437 when(client.post(eq(endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(response);
438 when(response.getStatus()).thenReturn(expectedStatus);
439 when(response.getBody()).thenReturn(expectedResponse);
440 when(response.getRawBody()).thenReturn(toInputStream(expectedResponse));
443 MsoResponseWrapper responseWrapper = testSubject.setServiceInstanceStatus(requestDetails, endpoint);
445 assertThat(responseWrapper.getStatus(), is(expectedStatus));
446 assertThat(responseWrapper.getEntity(), is(expectedResponse));
450 public void testRemoveRelationshipFromServiceInstance() throws Exception {
451 String serviceEndpoint = props.getString(MsoProperties.MSO_REST_API_SVC_INSTANCE);
452 String removeRelationshipsPath = serviceEndpoint + "/" + SERVICE_INSTANCE_ID + "/removeRelationships";
454 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
456 removeRelationshipsPath,
457 HttpStatus.ACCEPTED_202,
458 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
459 closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::removeRelationshipFromServiceInstance);
464 public void testAddRelationshipToServiceInstance() throws Exception {
465 MsoRestClientNew testSubject;
466 RequestDetails requestDetails = null;
467 String addRelationshipsPath = "";
468 MsoResponseWrapper result;
472 testSubject = createTestSubject();
473 result = testSubject.addRelationshipToServiceInstance(requestDetails, addRelationshipsPath);
474 } catch (Exception e) {
478 public void testScaleOutVfModule() throws IOException {
479 String serviceEndpoint = props.getString(MsoProperties.MSO_REST_API_VF_MODULE_SCALE_OUT);
480 String partial_endpoint = serviceEndpoint.replaceFirst(SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
481 String vf_module_endpoint = partial_endpoint.replaceFirst(VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
482 try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
485 HttpStatus.ACCEPTED_202,
486 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
487 closure.executePostCall(msoScaleOutVfModule, msoRestClient()::scaleOutVFModuleInstance);
492 private MsoRestClientNew msoRestClient() {
493 final WebConfig webConfig = new WebConfig();
494 return new MsoRestClientNew(new SyncRestClient(webConfig.unirestFasterxmlObjectMapper(new ObjectMapper())), baseUrl(), null, new SystemPropertiesWrapper());
497 private MsoRestClientNew createTestSubject() {
498 return new MsoRestClientNew(null, "", null, new SystemPropertiesWrapper());