69966407d5d79da6b960afa9d6b1223447c66ce4
[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 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 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 com.xebialabs.restito.server.StubServer;
24 import org.glassfish.grizzly.http.util.HttpStatus;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.onap.portalsdk.core.util.SystemProperties;
29 import org.onap.vid.client.SyncRestClient;
30 import org.onap.vid.controllers.MsoController;
31 import org.onap.vid.mso.MsoInterface;
32 import org.onap.vid.mso.MsoProperties;
33 import org.onap.vid.mso.MsoResponseWrapper;
34 import org.onap.vid.mso.MsoResponseWrapperInterface;
35 import org.onap.vid.mso.RestObject;
36 import org.springframework.test.context.ContextConfiguration;
37
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.util.Properties;
44 import java.util.UUID;
45
46 import static org.onap.vid.controllers.MsoController.SVC_INSTANCE_ID;
47 import static org.onap.vid.controllers.MsoController.VNF_INSTANCE_ID;
48
49 @ContextConfiguration(classes = {SystemProperties.class})
50 public class MsoRestClientNewTest {
51
52     private static StubServer server;
53     private static StubServer securedServer;
54     private static Properties props = new Properties();
55     private static String msoCreateServiceInstanceJson;
56     private static String msoScaleOutVfModule;
57     private final static String CREATE_INSTANCE_RESPONSE_STR =
58             "{\"requestReferences\":{\"instanceId\":\"baa13544-0e95-4644-9565-9a198a29a294\","
59                     + "\"requestId\":\"a42a1a35-3d63-4629-bbe0-4989fa7414cb\"}}";
60     private final static String SERVICE_INSTANCE_ID = "12345";
61     private static final String SAMPLE_VNF_INSTANCE_ID = "111";
62     private static final String SAMPLE_VNF_MODULE_ID = "987";
63     private static final String SAMPLE_NETWORK_INSTANCE_ID = "666";
64     private static final String SAMPLE_CONFIGURATION_ID = "997";
65     private static final String SAMPLE_REQUEST_ID = "7777";
66
67
68     @BeforeClass
69     public static void start() throws IOException {
70         server = new StubServer().run();
71         securedServer = new StubServer().secured().run();
72
73         Path resourceDirectory =
74                 Paths.get("src", "test", "resources", "WEB-INF", "conf", "system.properties");
75         try (InputStream is = Files.newInputStream(resourceDirectory)) {
76             props.load(is);
77         }
78
79         Path msoServiceInstantiationJsonFilePath =
80                 Paths.get("src", "test", "resources", "payload_jsons", "mso_service_instantiation.json");
81
82         Path scaleOutJsonFilePath = Paths.get("src", "test", "resources", "payload_jsons", "scaleOutVfModulePayloadToMso.json");
83         msoCreateServiceInstanceJson =
84                 String.join("\n", Files.readAllLines(msoServiceInstantiationJsonFilePath));
85         msoScaleOutVfModule = String.join("\n", Files.readAllLines(scaleOutJsonFilePath));
86
87     }
88
89     @AfterClass
90     public static void stop() {
91         server.stop();
92         securedServer.stop();
93     }
94
95
96     private String baseUrl() {
97         return String.format("http://localhost:%d", server.getPort());
98     }
99
100     @Test
101     public void testCreateSvcInstance() throws Exception {
102         String endpoint = props.getProperty(MsoProperties.MSO_REST_API_CONFIGURATIONS);
103         endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
104         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
105                 server,
106                 endpoint,
107                 HttpStatus.ACCEPTED_202,
108                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
109             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createSvcInstance);
110         }
111     }
112
113     @Test
114     public void testCreateVnf() throws Exception {
115         String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VNF_INSTANCE);
116         endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
117         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
118                 server,
119                 endpoint,
120                 HttpStatus.ACCEPTED_202,
121                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
122
123             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVnf);
124         }
125     }
126
127     @Test
128     public void testCreateNwInstance() throws Exception {
129         String endpoint = props.getProperty(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
130         String nw_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
131         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
132                 server,
133                 nw_endpoint,
134                 HttpStatus.ACCEPTED_202,
135                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
136             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createNwInstance);
137         }
138     }
139
140     @Test
141     public void testCreateVolumeGroupInstance() throws Exception {
142         String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
143         String vnf_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
144         vnf_endpoint = vnf_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
145         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
146                 server,
147                 vnf_endpoint,
148                 HttpStatus.ACCEPTED_202,
149                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
150             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVolumeGroupInstance);
151         }
152     }
153
154     @Test
155     public void testCreateVfModuleInstance() throws Exception {
156         String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
157         String partial_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
158         String vf_module_endpoint =
159                 partial_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
160
161
162         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
163                 server,
164                 vf_module_endpoint,
165                 HttpStatus.ACCEPTED_202,
166                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
167             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::createVfModuleInstance);
168         }
169     }
170
171     @Test
172     public void testCreateConfigurationInstance() throws Exception {
173         MsoRestClientNew testSubject;
174         RequestDetailsWrapper requestDetails = null;
175         String endpoint = "";
176         MsoResponseWrapper result;
177
178         // default test
179         try {
180             testSubject = createTestSubject();
181             result = testSubject.createConfigurationInstance(requestDetails, endpoint);
182         } catch (Exception e) {
183         }
184     }
185
186     @Test
187     public void testDeleteSvcInstance() throws Exception {
188         String endpoint = props.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
189         endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
190
191
192         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
193                 server,
194                 endpoint,
195                 HttpStatus.NO_CONTENT_204,
196                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
197             closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteSvcInstance);
198         }
199     }
200
201     @Test
202     public void testDeleteVnf() throws Exception {
203         String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VNF_INSTANCE);
204         endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
205
206         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
207                 server,
208                 endpoint,
209                 HttpStatus.NO_CONTENT_204,
210                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
211             closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVnf);
212         }
213     }
214
215     @Test
216     public void testDeleteVfModule() throws Exception {
217         String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
218         String part_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
219         String vf_modules_endpoint = part_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
220         String delete_vf_endpoint = vf_modules_endpoint + '/' + SAMPLE_VNF_MODULE_ID;
221
222         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
223                 server,
224                 delete_vf_endpoint,
225                 HttpStatus.NO_CONTENT_204,
226                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
227             closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVfModule);
228         }
229     }
230
231     @Test
232     public void testDeleteVolumeGroupInstance() throws Exception {
233         String endpoint = props.getProperty(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
234         String svc_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
235         String vnf_endpoint = svc_endpoint.replaceFirst(MsoController.VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
236         String delete_volume_group_endpoint = vnf_endpoint + "/" + SAMPLE_VNF_MODULE_ID;
237
238         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
239                 server,
240                 delete_volume_group_endpoint,
241                 HttpStatus.NO_CONTENT_204,
242                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
243             closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteVolumeGroupInstance);
244         }
245     }
246
247     @Test
248     public void testDeleteNwInstance() throws Exception {
249         String endpoint = props.getProperty(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
250         String svc_endpoint = endpoint.replaceFirst(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
251         String delete_nw_endpoint = svc_endpoint + "/" + SAMPLE_NETWORK_INSTANCE_ID;
252
253         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
254             server,
255             delete_nw_endpoint,
256             HttpStatus.NO_CONTENT_204,
257             CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
258             closure.executeDelete(msoCreateServiceInstanceJson, msoRestClient()::deleteNwInstance);
259         }
260     }
261
262     @Test
263     public void testGetOrchestrationRequest() {
264         String p = props.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQ);
265         String path = p + "/" + SAMPLE_REQUEST_ID;
266
267         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
268             server,
269             path,
270             HttpStatus.OK_200,
271             CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
272             closure.executeGet(msoRestClient()::getOrchestrationRequest);
273         }
274     }
275
276     @Test
277     public void testGetManualTasks() {
278         String p = props.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQ);
279         String path = p + "/" + UUID.randomUUID();
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()::getManualTasks);
287         }
288     }
289
290     @Test
291     public void testGetOrchestrationRequestsForDashboard() throws Exception {
292         MsoRestClientNew testSubject;
293         String t = "";
294         String sourceId = "";
295         String endpoint = "";
296         RestObject restObject = null;
297         MsoResponseWrapper result;
298
299         // default test
300         try {
301             testSubject = createTestSubject();
302             result = testSubject.getOrchestrationRequestsForDashboard(t, sourceId, endpoint, restObject);
303         } catch (Exception e) {
304         }
305     }
306
307     @Test
308     public void testGetManualTasksByRequestId() throws Exception {
309         MsoRestClientNew testSubject;
310         String t = "";
311         String sourceId = "";
312         String endpoint = "";
313         RestObject restObject = null;
314         MsoResponseWrapper result;
315
316         // default test
317         try {
318             testSubject = createTestSubject();
319             result = testSubject.getManualTasksByRequestId(t, sourceId, endpoint, restObject);
320         } catch (Exception e) {
321         }
322     }
323
324     @Test
325     public void testCompleteManualTask() throws Exception {
326         MsoRestClientNew testSubject;
327         RequestDetails requestDetails = null;
328         String t = "";
329         String sourceId = "";
330         String endpoint = "";
331         RestObject restObject = null;
332         MsoResponseWrapper result;
333
334         // default test
335         try {
336             testSubject = createTestSubject();
337             result = testSubject.completeManualTask(requestDetails, t, sourceId, endpoint, restObject);
338         } catch (Exception e) {
339         }
340     }
341
342     @Test
343     public void testDeleteConfiguration() throws Exception {
344         MsoRestClientNew testSubject;
345         RequestDetailsWrapper requestDetails = null;
346         String pmc_endpoint = "";
347         MsoResponseWrapper result;
348
349         // default test
350         try {
351             testSubject = createTestSubject();
352             result = testSubject.deleteConfiguration(requestDetails, pmc_endpoint);
353         } catch (Exception e) {
354         }
355     }
356
357     @Test
358     public void testSetConfigurationActiveStatus() throws Exception {
359         String endpoint = "/serviceInstances/v5/<service_instance_id>/configurations/<configuration_id>";
360         endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
361         endpoint = endpoint.replace(MsoController.CONFIGURATION_ID, SAMPLE_CONFIGURATION_ID);
362         endpoint = endpoint + "/activate";
363
364         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
365             server,
366             endpoint,
367             HttpStatus.ACCEPTED_202,
368             CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
369             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::setConfigurationActiveStatus);
370         }
371     }
372
373     @Test
374     public void testSetPortOnConfigurationStatus() throws Exception {
375         MsoRestClientNew testSubject;
376         RequestDetails request = null;
377         String path = "";
378         MsoResponseWrapper result;
379
380         // default test
381         try {
382             testSubject = createTestSubject();
383             result = testSubject.setPortOnConfigurationStatus(request, path);
384         } catch (Exception e) {
385         }
386     }
387
388     @Test
389     public void testChangeManagementUpdate() throws Exception {
390         MsoRestClientNew testSubject;
391         org.onap.vid.changeManagement.RequestDetailsWrapper requestDetails = null;
392         String endpoint = "";
393         MsoResponseWrapperInterface result;
394
395         // default test
396         try {
397             testSubject = createTestSubject();
398             result = testSubject.changeManagementUpdate(requestDetails, endpoint);
399         } catch (Exception e) {
400         }
401     }
402
403     @Test
404     public void testSetServiceInstanceStatus() throws Exception {
405         MsoRestClientNew testSubject;
406         RequestDetails requestDetails = null;
407         String t = "";
408         String sourceId = "";
409         String endpoint = "";
410         RestObject<String> restObject = null;
411
412         // default test
413         try {
414             testSubject = createTestSubject();
415             testSubject.setServiceInstanceStatus(requestDetails, t, sourceId, endpoint, restObject);
416         } catch (Exception e) {
417         }
418     }
419
420     @Test
421     public void testRemoveRelationshipFromServiceInstance() throws Exception {
422         String serviceEndpoint = props.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
423         String removeRelationshipsPath = serviceEndpoint + "/" + SERVICE_INSTANCE_ID + "/removeRelationships";
424
425         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
426                 server,
427                 removeRelationshipsPath,
428                 HttpStatus.ACCEPTED_202,
429                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
430             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::removeRelationshipFromServiceInstance);
431         }
432     }
433
434     @Test
435     public void testAddRelationshipToServiceInstance() throws Exception {
436         MsoRestClientNew testSubject;
437         RequestDetails requestDetails = null;
438         String addRelationshipsPath = "";
439         MsoResponseWrapper result;
440
441         // default test
442         try {
443             testSubject = createTestSubject();
444             result = testSubject.addRelationshipToServiceInstance(requestDetails, addRelationshipsPath);
445         } catch (Exception e) {
446         }
447     }
448     @Test
449     public void testScaleOutVfModule() throws IOException {
450         String serviceEndpoint = props.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_SCALE_OUT);
451         String partial_endpoint = serviceEndpoint.replaceFirst(SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
452         String vf_module_endpoint = partial_endpoint.replaceFirst(VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
453         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
454                 server,
455                 vf_module_endpoint,
456                 HttpStatus.ACCEPTED_202,
457                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
458             closure.executePostCall(msoScaleOutVfModule, msoRestClient()::scaleOutVFModuleInstance);
459         }
460
461     }
462
463     private MsoRestClientNew msoRestClient() {
464         return new MsoRestClientNew(new SyncRestClient(MsoInterface.objectMapper()), baseUrl());
465     }
466
467     private MsoRestClientNew createTestSubject() {
468         return new MsoRestClientNew(null, "");
469     }
470 }