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