Implant vid-app-common org.onap.vid.job (main and test)
[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_REST_API_SVC_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 testGetOrchestrationRequestsForDashboard() throws Exception {
329         MsoRestClientNew testSubject;
330         String t = "";
331         String sourceId = "";
332         String endpoint = "";
333         RestObject restObject = null;
334         MsoResponseWrapper result;
335
336         // default test
337         try {
338             testSubject = createTestSubject();
339             result = testSubject.getOrchestrationRequest(t, sourceId, endpoint, restObject, true);
340         } catch (Exception e) {
341         }
342     }
343
344     @Test
345     public void testCompleteManualTask() throws Exception {
346         MsoRestClientNew testSubject;
347         RequestDetails requestDetails = null;
348         String t = "";
349         String sourceId = "";
350         String endpoint = "";
351         RestObject restObject = null;
352         MsoResponseWrapper result;
353
354         // default test
355         try {
356             testSubject = createTestSubject();
357             result = testSubject.completeManualTask(requestDetails, t, sourceId, endpoint, restObject);
358         } catch (Exception e) {
359         }
360     }
361
362     @Test
363     public void testDeleteConfiguration() throws Exception {
364         MsoRestClientNew testSubject;
365         RequestDetailsWrapper requestDetails = null;
366         String pmc_endpoint = "";
367         MsoResponseWrapper result;
368
369         // default test
370         try {
371             testSubject = createTestSubject();
372             result = testSubject.deleteConfiguration(requestDetails, pmc_endpoint);
373         } catch (Exception e) {
374         }
375     }
376
377     @Test
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";
383
384         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
385             server,
386             endpoint,
387             HttpStatus.ACCEPTED_202,
388             CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
389             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::setConfigurationActiveStatus);
390         }
391     }
392
393     @Test
394     public void testSetPortOnConfigurationStatus() throws Exception {
395         MsoRestClientNew testSubject;
396         RequestDetails request = null;
397         String path = "";
398         MsoResponseWrapper result;
399
400         // default test
401         try {
402             testSubject = createTestSubject();
403             result = testSubject.setPortOnConfigurationStatus(request, path);
404         } catch (Exception e) {
405         }
406     }
407
408     @Test
409     public void testChangeManagementUpdate() throws Exception {
410         MsoRestClientNew testSubject;
411         org.onap.vid.changeManagement.RequestDetailsWrapper requestDetails = null;
412         String endpoint = "";
413         MsoResponseWrapperInterface result;
414
415         // default test
416         try {
417             testSubject = createTestSubject();
418             result = testSubject.changeManagementUpdate(requestDetails, endpoint);
419         } catch (Exception e) {
420         }
421     }
422
423     @Test
424     public void testSetServiceInstanceStatus_givenValidResponse_responseIsPopulatedAccordingly() {
425         RequestDetails requestDetails = new RequestDetails();
426         String t = "";
427         String sourceId = "";
428         String endpoint = "";
429         final SyncRestClient client = mock(SyncRestClient.class);
430         MsoRestClientNew testSubject = new MsoRestClientNew(client, "", null, new SystemPropertiesWrapper());
431
432         // setup
433         final HttpResponse<String> response = mock(HttpResponse.class);
434         final int expectedStatus = 202;
435         final String expectedResponse = "expected response";
436
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));
441
442         // test
443         MsoResponseWrapper responseWrapper = testSubject.setServiceInstanceStatus(requestDetails, endpoint);
444
445         assertThat(responseWrapper.getStatus(), is(expectedStatus));
446         assertThat(responseWrapper.getEntity(), is(expectedResponse));
447     }
448
449     @Test
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";
453
454         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
455                 server,
456                 removeRelationshipsPath,
457                 HttpStatus.ACCEPTED_202,
458                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
459             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::removeRelationshipFromServiceInstance);
460         }
461     }
462
463     @Test
464     public void testAddRelationshipToServiceInstance() throws Exception {
465         MsoRestClientNew testSubject;
466         RequestDetails requestDetails = null;
467         String addRelationshipsPath = "";
468         MsoResponseWrapper result;
469
470         // default test
471         try {
472             testSubject = createTestSubject();
473             result = testSubject.addRelationshipToServiceInstance(requestDetails, addRelationshipsPath);
474         } catch (Exception e) {
475         }
476     }
477     @Test
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(
483                 server,
484                 vf_module_endpoint,
485                 HttpStatus.ACCEPTED_202,
486                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
487             closure.executePostCall(msoScaleOutVfModule, msoRestClient()::scaleOutVFModuleInstance);
488         }
489
490     }
491
492     private MsoRestClientNew msoRestClient() {
493         final WebConfig webConfig = new WebConfig();
494         return new MsoRestClientNew(new SyncRestClient(webConfig.unirestFasterxmlObjectMapper(new ObjectMapper())), baseUrl(), null, new SystemPropertiesWrapper());
495     }
496
497     private MsoRestClientNew createTestSubject() {
498         return new MsoRestClientNew(null, "", null, new SystemPropertiesWrapper());
499     }
500 }