fixing bugs connected with undefined parameters
[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.onap.vid.controller.MsoController.SVC_INSTANCE_ID;
24 import static org.onap.vid.controller.MsoController.VNF_INSTANCE_ID;
25
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.xebialabs.restito.server.StubServer;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.Properties;
34 import java.util.UUID;
35 import org.glassfish.grizzly.http.util.HttpStatus;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.onap.portalsdk.core.util.SystemProperties;
41 import org.onap.vid.client.SyncRestClient;
42 import org.onap.vid.controller.MsoController;
43 import org.onap.vid.controller.WebConfig;
44 import org.onap.vid.mso.MsoProperties;
45 import org.onap.vid.mso.MsoResponseWrapper;
46 import org.onap.vid.mso.MsoResponseWrapperInterface;
47 import org.onap.vid.mso.RestObject;
48 import org.onap.vid.utils.SystemPropertiesWrapper;
49 import org.springframework.test.context.ContextConfiguration;
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 testGetManualTasksByRequestId() {
284         String p = props.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQ);
285         String path = p + "/" + UUID.randomUUID();
286         String validResponse = "" 
287             + "{ "
288             + "  \"taskList\": [ "
289             + "    { "
290             + "      \"taskId\": \"daf4dd84-b77a-42da-a051-3239b7a9392c\", "
291             + "      \"type\": \"fallout\", "
292             + "      \"nfRole\": \"vEsmeralda\", "
293             + "      \"subscriptionServiceType\": \"Emanuel\", "
294             + "      \"originalRequestId\": \"d352c70d-5ef8-4977-9ea8-4c8cbe860422\", "
295             + "      \"originalRequestorId\": \"ss835w\", "
296             + "      \"errorSource\": \"A&AI\", "
297             + "      \"errorCode\": \"404\", "
298             + "      \"errorMessage\": \"Failed in A&AI 404\", "
299             + "      \"validResponses\": [ "
300             + "        \"rollback\", "
301             + "        \"abort\", "
302             + "        \"skip\", "
303             + "        \"resume\", "
304             + "        \"retry\" "
305             + "      ] "
306             + "    } "
307             + "  ] "
308             + "}";
309
310         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
311             server,
312             path,
313             HttpStatus.OK_200,
314             validResponse,validResponse)) {
315             closure.executeGet(endpoint -> msoRestClient().getManualTasksByRequestId(null, null, endpoint, null));
316         }
317     }
318
319     @Test
320     public void testGetOrchestrationRequestsForDashboard() throws Exception {
321         MsoRestClientNew testSubject;
322         String t = "";
323         String sourceId = "";
324         String endpoint = "";
325         RestObject restObject = null;
326         MsoResponseWrapper result;
327
328         // default test
329         try {
330             testSubject = createTestSubject();
331             result = testSubject.getOrchestrationRequest(t, sourceId, endpoint, restObject, true);
332         } catch (Exception e) {
333         }
334     }
335
336     @Test
337     public void testCompleteManualTask() throws Exception {
338         MsoRestClientNew testSubject;
339         RequestDetails requestDetails = null;
340         String t = "";
341         String sourceId = "";
342         String endpoint = "";
343         RestObject restObject = null;
344         MsoResponseWrapper result;
345
346         // default test
347         try {
348             testSubject = createTestSubject();
349             result = testSubject.completeManualTask(requestDetails, t, sourceId, endpoint, restObject);
350         } catch (Exception e) {
351         }
352     }
353
354     @Test
355     public void testDeleteConfiguration() throws Exception {
356         MsoRestClientNew testSubject;
357         RequestDetailsWrapper requestDetails = null;
358         String pmc_endpoint = "";
359         MsoResponseWrapper result;
360
361         // default test
362         try {
363             testSubject = createTestSubject();
364             result = testSubject.deleteConfiguration(requestDetails, pmc_endpoint);
365         } catch (Exception e) {
366         }
367     }
368
369     @Test
370     public void testSetConfigurationActiveStatus() throws Exception {
371         String endpoint = "/serviceInstances/v5/<service_instance_id>/configurations/<configuration_id>";
372         endpoint = endpoint.replace(MsoController.SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
373         endpoint = endpoint.replace(MsoController.CONFIGURATION_ID, SAMPLE_CONFIGURATION_ID);
374         endpoint = endpoint + "/activate";
375
376         try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
377             server,
378             endpoint,
379             HttpStatus.ACCEPTED_202,
380             CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) {
381             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::setConfigurationActiveStatus);
382         }
383     }
384
385     @Test
386     public void testSetPortOnConfigurationStatus() throws Exception {
387         MsoRestClientNew testSubject;
388         RequestDetails request = null;
389         String path = "";
390         MsoResponseWrapper result;
391
392         // default test
393         try {
394             testSubject = createTestSubject();
395             result = testSubject.setPortOnConfigurationStatus(request, path);
396         } catch (Exception e) {
397         }
398     }
399
400     @Test
401     public void testChangeManagementUpdate() throws Exception {
402         MsoRestClientNew testSubject;
403         org.onap.vid.changeManagement.RequestDetailsWrapper requestDetails = null;
404         String endpoint = "";
405         MsoResponseWrapperInterface result;
406
407         // default test
408         try {
409             testSubject = createTestSubject();
410             result = testSubject.changeManagementUpdate(requestDetails, endpoint);
411         } catch (Exception e) {
412         }
413     }
414
415     @Test
416     public void testSetServiceInstanceStatus() throws Exception {
417         MsoRestClientNew testSubject;
418         RequestDetails requestDetails = null;
419         String t = "";
420         String sourceId = "";
421         String endpoint = "";
422         RestObject<String> restObject = null;
423
424         // default test
425         try {
426             testSubject = createTestSubject();
427             testSubject.setServiceInstanceStatus(requestDetails, t, sourceId, endpoint, restObject);
428         } catch (Exception e) {
429         }
430     }
431
432     @Test
433     public void testRemoveRelationshipFromServiceInstance() throws Exception {
434         String serviceEndpoint = props.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
435         String removeRelationshipsPath = serviceEndpoint + "/" + SERVICE_INSTANCE_ID + "/removeRelationships";
436
437         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
438                 server,
439                 removeRelationshipsPath,
440                 HttpStatus.ACCEPTED_202,
441                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
442             closure.executePost(msoCreateServiceInstanceJson, msoRestClient()::removeRelationshipFromServiceInstance);
443         }
444     }
445
446     @Test
447     public void testAddRelationshipToServiceInstance() throws Exception {
448         MsoRestClientNew testSubject;
449         RequestDetails requestDetails = null;
450         String addRelationshipsPath = "";
451         MsoResponseWrapper result;
452
453         // default test
454         try {
455             testSubject = createTestSubject();
456             result = testSubject.addRelationshipToServiceInstance(requestDetails, addRelationshipsPath);
457         } catch (Exception e) {
458         }
459     }
460     @Test
461     public void testScaleOutVfModule() throws IOException {
462         String serviceEndpoint = props.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_SCALE_OUT);
463         String partial_endpoint = serviceEndpoint.replaceFirst(SVC_INSTANCE_ID, SERVICE_INSTANCE_ID);
464         String vf_module_endpoint = partial_endpoint.replaceFirst(VNF_INSTANCE_ID, SAMPLE_VNF_INSTANCE_ID);
465         try (MsoRestClientTestUtil closure = new MsoRestClientTestUtil(
466                 server,
467                 vf_module_endpoint,
468                 HttpStatus.ACCEPTED_202,
469                 CREATE_INSTANCE_RESPONSE_STR, CREATE_INSTANCE_RESPONSE_STR)) {
470             closure.executePostCall(msoScaleOutVfModule, msoRestClient()::scaleOutVFModuleInstance);
471         }
472
473     }
474
475     private MsoRestClientNew msoRestClient() {
476         final WebConfig webConfig = new WebConfig();
477         return new MsoRestClientNew(new SyncRestClient(webConfig.unirestFasterxmlObjectMapper(new ObjectMapper())), baseUrl(), null, new SystemPropertiesWrapper());
478     }
479
480     private MsoRestClientNew createTestSubject() {
481         return new MsoRestClientNew(null, "", null, new SystemPropertiesWrapper());
482     }
483 }