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