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