update MSO configuration URL
[vid.git] / vid-app-common / src / test / java / org / onap / vid / mso / rest / MsoRestClientNewTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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 package org.onap.vid.mso.rest;
22
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;
32
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;
58
59 @ContextConfiguration(classes = {SystemProperties.class})
60 public class MsoRestClientNewTest {
61
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";
76
77
78     @BeforeClass
79     public static void start() throws Exception {
80         server = new StubServer().run();
81         securedServer = new StubServer().secured().run();
82
83         Path resourceDirectory =
84                 Paths.get("src", "test", "resources", "WEB-INF", "conf", "system.properties");
85         try (InputStream is = Files.newInputStream(resourceDirectory)) {
86             props.load(is);
87         }
88
89         Path msoServiceInstantiationJsonFilePath =
90                 Paths.get("src", "test", "resources", "payload_jsons", "mso_service_instantiation.json");
91
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));
96
97     }
98
99     @AfterClass
100     public static void stop() {
101         server.stop();
102         securedServer.stop();
103     }
104
105
106     private String baseUrl() {
107         return String.format("http://localhost:%d", server.getPort());
108     }
109
110     @Test
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(
115                 server,
116                 endpoint,
117                 HttpStatus.ACCEPTED_202,
118                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
119             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createSvcInstance);
120         }
121     }
122
123     @Test
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(
128                 server,
129                 endpoint,
130                 HttpStatus.ACCEPTED_202,
131                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
132
133             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVnf);
134         }
135     }
136
137     @Test
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(
142                 server,
143                 nw_endpoint,
144                 HttpStatus.ACCEPTED_202,
145                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
146             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createNwInstance);
147         }
148     }
149
150     @Test
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(
156                 server,
157                 vnf_endpoint,
158                 HttpStatus.ACCEPTED_202,
159                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
160             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVolumeGroupInstance);
161         }
162     }
163
164     @Test
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);
170
171
172         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
173                 server,
174                 vf_module_endpoint,
175                 HttpStatus.ACCEPTED_202,
176                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
177             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVfModuleInstance);
178         }
179     }
180
181     @Test
182     public void testCreateConfigurationInstance() throws Exception {
183         MsoRestClientNew testSubject;
184         RequestDetailsWrapper requestDetails = null;
185         String endpoint = "";
186         MsoResponseWrapper result;
187
188         // default test
189         try {
190             testSubject = createTestSubject();
191             result = testSubject.createConfigurationInstance(requestDetails, endpoint);
192         } catch (Exception e) {
193         }
194     }
195     @Ignore
196     @Test
197     public void testDeleteSvcInstance() throws Exception {
198         String endpoint = props.getString(MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE);
199         endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
200
201
202         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
203                 server,
204                 endpoint,
205                 HttpStatus.NO_CONTENT_204,
206                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
207             closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteSvcInstance);
208         }
209     }
210
211     @Ignore
212     @Test
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);
216
217         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
218                 server,
219                 endpoint,
220                 HttpStatus.NO_CONTENT_204,
221                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
222             closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVnf);
223         }
224     }
225
226     @Ignore
227     @Test
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;
233
234         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
235                 server,
236                 delete_vf_endpoint,
237                 HttpStatus.NO_CONTENT_204,
238                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
239             closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVfModule);
240         }
241     }
242
243     @Ignore
244     @Test
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;
250
251         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
252                 server,
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);
257         }
258     }
259
260     @Ignore
261     @Test
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;
266
267         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
268             server,
269             delete_nw_endpoint,
270             HttpStatus.NO_CONTENT_204,
271             CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
272             closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteNwInstance);
273         }
274     }
275
276     @Test
277     public void testGetOrchestrationRequest() {
278         String p = props.getString(MsoProperties.MSO_REST_API_GET_ORC_REQ);
279         String path = p + "/" + SAMPLE_REQUEST_ID;
280
281         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
282             server,
283             path,
284             HttpStatus.OK_200,
285             CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
286             closure.executeGet(msoRestClient()::getOrchestrationRequest);
287         }
288     }
289
290     @Test
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 = "" 
295             + "{ "
296             + "  \"taskList\": [ "
297             + "    { "
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\": [ "
308             + "        \"rollback\", "
309             + "        \"abort\", "
310             + "        \"skip\", "
311             + "        \"resume\", "
312             + "        \"retry\" "
313             + "      ] "
314             + "    } "
315             + "  ] "
316             + "}";
317
318         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
319             server,
320             path,
321             HttpStatus.OK_200,
322             validResponse,validResponse)) {
323             closure.executeGet(endpoint -> msoRestClient().getManualTasksByRequestId(null, null, endpoint, null));
324         }
325     }
326
327     @Test
328     public void testCompleteManualTask() throws Exception {
329         MsoRestClientNew testSubject;
330         RequestDetails requestDetails = null;
331         String t = "";
332         String sourceId = "";
333         String endpoint = "";
334         RestObject restObject = null;
335         MsoResponseWrapper result;
336
337         // default test
338         try {
339             testSubject = createTestSubject();
340             result = testSubject.completeManualTask(requestDetails, t, sourceId, endpoint, restObject);
341         } catch (Exception e) {
342         }
343     }
344
345     @Test
346     public void testDeleteConfiguration() throws Exception {
347         MsoRestClientNew testSubject;
348         RequestDetailsWrapper requestDetails = null;
349         String pmc_endpoint = "";
350         MsoResponseWrapper result;
351
352         // default test
353         try {
354             testSubject = createTestSubject();
355             result = testSubject.deleteConfiguration(requestDetails, pmc_endpoint);
356         } catch (Exception e) {
357         }
358     }
359
360     @Test
361     public void testSetConfigurationActiveStatus() throws Exception {
362         String endpoint = "/serviceInstantiation/v7/serviceInstances/<service_instance_id>/configurations/<configuration_id>";
363         endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
364         endpoint = endpoint.replace(MsoController.CONFIGURATION_ID, SAMPLE_CONFIGURATION_ID);
365         endpoint = endpoint + "/activate";
366
367         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
368             server,
369             endpoint,
370             HttpStatus.ACCEPTED_202,
371             CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
372             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::setConfigurationActiveStatus);
373         }
374     }
375
376     @Test
377     public void testSetPortOnConfigurationStatus() throws Exception {
378         MsoRestClientNew testSubject;
379         RequestDetails request = null;
380         String path = "";
381         MsoResponseWrapper result;
382
383         // default test
384         try {
385             testSubject = createTestSubject();
386             result = testSubject.setPortOnConfigurationStatus(request, path);
387         } catch (Exception e) {
388         }
389     }
390
391     @Test
392     public void testChangeManagementUpdate() throws Exception {
393         MsoRestClientNew testSubject;
394         org.onap.vid.changeManagement.RequestDetailsWrapper requestDetails = null;
395         String endpoint = "";
396         MsoResponseWrapperInterface result;
397
398         // default test
399         try {
400             testSubject = createTestSubject();
401             result = testSubject.changeManagementUpdate(requestDetails, endpoint);
402         } catch (Exception e) {
403         }
404     }
405
406     @Test
407     public void testSetServiceInstanceStatus_givenValidResponse_responseIsPopulatedAccordingly() {
408         RequestDetails requestDetails = new RequestDetails();
409         String t = "";
410         String sourceId = "";
411         String endpoint = "";
412         final SyncRestClient client = mock(SyncRestClient.class);
413         MsoRestClientNew testSubject = new MsoRestClientNew(client, "", null, new SystemPropertiesWrapper());
414
415         // setup
416         final HttpResponse<String> response = mock(HttpResponse.class);
417         final int expectedStatus = 202;
418         final String expectedResponse = "expected response";
419
420         when(client.post(eq(endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(response);
421         when(response.getStatus()).thenReturn(expectedStatus);
422         when(response.getBody()).thenReturn(expectedResponse);
423         when(response.getRawBody()).thenReturn(toInputStream(expectedResponse));
424
425         // test
426         MsoResponseWrapper responseWrapper = testSubject.setServiceInstanceStatus(requestDetails, endpoint);
427
428         assertThat(responseWrapper.getStatus(), is(expectedStatus));
429         assertThat(responseWrapper.getEntity(), is(expectedResponse));
430     }
431
432     @Test
433     public void testRemoveRelationshipFromServiceInstance() throws Exception {
434         String serviceEndpoint = props.getString(MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE);
435         String removeRelationshipsPath = serviceEndpoint + "/" + SERVICE_INSTANCE_ID + "/removeRelationships";
436
437         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
438                 server,
439                 removeRelationshipsPath,
440                 HttpStatus.ACCEPTED_202,
441                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
442             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::removeRelationshipFromServiceInstance);
443         }
444     }
445
446     @Test
447     public void testAddRelationshipToServiceInstance() throws Exception {
448         MsoRestClientNew testSubject;
449         RequestDetails requestDetails = null;
450         String addRelationshipsPath = "";
451         MsoResponseWrapper result;
452
453         // default test
454         try {
455             testSubject = createTestSubject();
456             result = testSubject.addRelationshipToServiceInstance(requestDetails, addRelationshipsPath);
457         } catch (Exception e) {
458         }
459     }
460     @Test
461     public void testScaleOutVfModule() throws IOException {
462         String serviceEndpoint = props.getString(MsoProperties.MSO_REST_API_VF_MODULE_SCALE_OUT);
463         String partial_endpoint = serviceEndpoint.replaceFirst(SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
464         String vf_module_endpoint = partial_endpoint.replaceFirst(VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
465         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
466                 server,
467                 vf_module_endpoint,
468                 HttpStatus.ACCEPTED_202,
469                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
470             closure.executePostCall(msoScaleOutVfModule, msoRestClient()::scaleOutVFModuleInstance);
471         }
472
473     }
474
475     private MsoRestClientNew msoRestClient() {
476         final WebConfig webConfig = new WebConfig();
477         return new MsoRestClientNew(new SyncRestClient(webConfig.unirestFasterxmlObjectMapper(new ObjectMapper())), baseUrl(), null, new SystemPropertiesWrapper());
478     }
479
480     private MsoRestClientNew createTestSubject() {
481         return new MsoRestClientNew(null, "", null, new SystemPropertiesWrapper());
482     }
483 }