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