Fixing SDNC vnf endpoint bug
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / sdnc-simulator / src / test / java / org / onap / so / sdncsimulator / controller / OperationsControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.sdncsimulator.controller;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.onap.so.sdncsimulator.controller.TestUtils.getInvalidRequestInput;
26 import static org.onap.so.sdncsimulator.controller.TestUtils.getRequestInput;
27 import static org.onap.so.sdncsimulator.controller.TestUtils.getVnfRequestInput;
28 import static org.onap.so.sdncsimulator.controller.TestUtils.getVnfRequestWithSvcActionActivateInput;
29 import java.util.Optional;
30 import org.junit.After;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.onap.sdnc.northbound.client.model.GenericResourceApiInstanceReference;
34 import org.onap.sdnc.northbound.client.model.GenericResourceApiLastRpcActionEnumeration;
35 import org.onap.sdnc.northbound.client.model.GenericResourceApiOperStatusData;
36 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServicedataVnfsVnf;
37 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicemodelinfrastructureService;
38 import org.onap.so.sdncsimulator.models.InputRequest;
39 import org.onap.so.sdncsimulator.models.Output;
40 import org.onap.so.sdncsimulator.models.OutputRequest;
41 import org.onap.so.sdncsimulator.providers.ServiceOperationsCacheServiceProvider;
42 import org.onap.so.sdncsimulator.utils.Constants;
43 import org.onap.so.simulator.model.UserCredentials;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
47 import org.springframework.boot.test.web.client.TestRestTemplate;
48 import org.springframework.boot.web.server.LocalServerPort;
49 import org.springframework.context.annotation.Configuration;
50 import org.springframework.http.HttpEntity;
51 import org.springframework.http.HttpHeaders;
52 import org.springframework.http.HttpMethod;
53 import org.springframework.http.HttpStatus;
54 import org.springframework.http.ResponseEntity;
55 import org.springframework.test.context.ActiveProfiles;
56 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
57
58 /**
59  * @author Waqas Ikram (waqas.ikram@est.tech)
60  *
61  */
62 @RunWith(SpringJUnit4ClassRunner.class)
63 @ActiveProfiles("test")
64 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
65 @Configuration
66 public class OperationsControllerTest {
67
68     private static final String SVC_REQUEST_ID = "04fc9f50-87b8-430d-a232-ef24bd6c4150";
69
70     private static final String VNF_SVC_REQUEST_ID = "8fd2622b-01fc-424d-bfc8-f48bcd64e546";
71
72     private static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b";
73
74     private static final String SERVICE_TOPOLOGY_OPERATION_URL = "/GENERIC-RESOURCE-API:service-topology-operation/";
75
76     private static final String VNF_TOPOLOGY_OPERATION_URL = "/GENERIC-RESOURCE-API:vnf-topology-operation/";
77
78     private static final String VNF_INSTANCE_ID = "dfd02fb5-d7fb-4aac-b3c4-cd6b60058701";
79
80     @LocalServerPort
81     private int port;
82
83     @Autowired
84     private TestRestTemplate restTemplate;
85
86     @Autowired
87     private ServiceOperationsCacheServiceProvider cacheServiceProvider;
88
89     @Autowired
90     private UserCredentials userCredentials;
91
92
93     @Test
94     public void test_postServiceOperationInformation_successfullyAddedToCache() throws Exception {
95
96         final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
97         final ResponseEntity<OutputRequest> responseEntity =
98                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
99
100         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
101         assertTrue(responseEntity.hasBody());
102
103         final OutputRequest actualOutputRequest = responseEntity.getBody();
104         assertNotNull(actualOutputRequest);
105
106         final Output actualObject = actualOutputRequest.getOutput();
107
108         assertNotNull(actualObject);
109         assertEquals(HttpStatus.OK.toString(), actualObject.getResponseCode());
110         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
111         assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
112         assertNotNull(actualObject.getServiceResponseInformation());
113
114         final GenericResourceApiInstanceReference acutalReference = actualObject.getServiceResponseInformation();
115         assertEquals(Constants.RESTCONF_CONFIG_END_POINT + SERVICE_INSTANCE_ID, acutalReference.getObjectPath());
116         assertEquals(SERVICE_INSTANCE_ID, acutalReference.getInstanceId());
117         final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
118                 cacheServiceProvider.getGenericResourceApiServicemodelinfrastructureService(SERVICE_INSTANCE_ID);
119         assertTrue(optional.isPresent());
120
121         final GenericResourceApiServicemodelinfrastructureService service = optional.get();
122         assertNotNull(service.getServiceInstanceId());
123         assertEquals(SERVICE_INSTANCE_ID, service.getServiceInstanceId());
124         assertNotNull(service.getServiceData());
125         assertNotNull(service.getServiceStatus());
126
127     }
128
129     @Test
130     public void test_postServiceOperationInformation_NullInputRequest_badRequest() throws Exception {
131
132         final HttpEntity<?> httpEntity = new HttpEntity<>(new InputRequest<>(), getHttpHeaders());
133         final ResponseEntity<OutputRequest> responseEntity =
134                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
135
136         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
137     }
138
139     @Test
140     public void test_postServiceOperationInformation_NullServiceInstanceId_badRequest() throws Exception {
141
142         final HttpEntity<?> httpEntity = new HttpEntity<>(getInvalidRequestInput(), getHttpHeaders());
143         final ResponseEntity<OutputRequest> responseEntity =
144                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
145
146         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
147         assertTrue(responseEntity.hasBody());
148
149         final OutputRequest actualOutputRequest = responseEntity.getBody();
150         assertNotNull(actualOutputRequest);
151
152         final Output actualObject = actualOutputRequest.getOutput();
153         assertNotNull(actualObject);
154         assertEquals(HttpStatus.BAD_REQUEST.toString(), actualObject.getResponseCode());
155         assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
156         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
157
158     }
159
160     @Test
161     public void test_postVnfOperationInformation_successfullyAddToExistingServiceInCache() throws Exception {
162         final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
163         final ResponseEntity<OutputRequest> responseEntity =
164                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
165
166         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
167
168         final HttpEntity<?> httpVnfEntity = new HttpEntity<>(getVnfRequestInput(), getHttpHeaders());
169         final ResponseEntity<OutputRequest> responseVnfEntity =
170                 restTemplate.exchange(getVnfUrl(), HttpMethod.POST, httpVnfEntity, OutputRequest.class);
171         assertEquals(HttpStatus.OK, responseVnfEntity.getStatusCode());
172         assertTrue(responseVnfEntity.hasBody());
173
174         final OutputRequest actualOutputRequest = responseVnfEntity.getBody();
175         assertNotNull(actualOutputRequest);
176         assertNotNull(actualOutputRequest.getOutput());
177
178         final Output actualObject = actualOutputRequest.getOutput();
179
180         assertEquals(HttpStatus.OK.toString(), actualObject.getResponseCode());
181         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
182         assertEquals(VNF_SVC_REQUEST_ID, actualObject.getSvcRequestId());
183         assertNotNull(actualObject.getServiceResponseInformation());
184
185         final GenericResourceApiInstanceReference acutalReference = actualObject.getServiceResponseInformation();
186         assertEquals(Constants.RESTCONF_CONFIG_END_POINT + SERVICE_INSTANCE_ID, acutalReference.getObjectPath());
187         assertEquals(SERVICE_INSTANCE_ID, acutalReference.getInstanceId());
188         final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
189                 cacheServiceProvider.getGenericResourceApiServicemodelinfrastructureService(SERVICE_INSTANCE_ID);
190         assertTrue(optional.isPresent());
191
192         final GenericResourceApiInstanceReference actualvnfInformation = actualObject.getVnfResponseInformation();
193         assertEquals(VNF_INSTANCE_ID, actualvnfInformation.getInstanceId());
194
195         final Optional<GenericResourceApiServicemodelinfrastructureService> serviceOptional =
196                 cacheServiceProvider.getGenericResourceApiServicemodelinfrastructureService(SERVICE_INSTANCE_ID);
197         assertTrue(serviceOptional.isPresent());
198
199         final GenericResourceApiServicemodelinfrastructureService service = serviceOptional.get();
200         assertNotNull(service.getServiceInstanceId());
201         assertNotNull(service.getServiceData().getVnfs().getVnf());
202         assertNotNull(service.getServiceData());
203         assertNotNull(service.getServiceData().getVnfs());
204         assertNotNull(service.getServiceData().getVnfs().getVnf());
205         assertEquals(1, service.getServiceData().getVnfs().getVnf().size());
206         final GenericResourceApiServicedataServicedataVnfsVnf vnf = service.getServiceData().getVnfs().getVnf().get(0);
207         assertNotNull(vnf.getVnfId());
208         assertEquals(VNF_INSTANCE_ID, vnf.getVnfId());
209         assertNotNull(vnf.getVnfData());
210     }
211
212     @Test
213     public void test_postSameVnfOperationInformationTwice_ShouldReturnbadRequest() throws Exception {
214
215         final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
216         final ResponseEntity<OutputRequest> responseEntity =
217                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
218
219         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
220
221         final HttpEntity<?> httpVnfEntity = new HttpEntity<>(getVnfRequestInput(), getHttpHeaders());
222         final ResponseEntity<OutputRequest> responseVnfEntity =
223                 restTemplate.exchange(getVnfUrl(), HttpMethod.POST, httpVnfEntity, OutputRequest.class);
224         assertEquals(HttpStatus.OK, responseVnfEntity.getStatusCode());
225         assertTrue(responseVnfEntity.hasBody());
226
227         final OutputRequest actualOutputRequest = responseVnfEntity.getBody();
228         assertNotNull(actualOutputRequest);
229         assertNotNull(actualOutputRequest.getOutput());
230
231         final ResponseEntity<OutputRequest> badResponse =
232                 restTemplate.exchange(getVnfUrl(), HttpMethod.POST, httpVnfEntity, OutputRequest.class);
233
234         final OutputRequest badOutputRequest = badResponse.getBody();
235         assertNotNull(badOutputRequest);
236
237         final Output actualObject = badOutputRequest.getOutput();
238         assertNotNull(actualObject);
239         assertEquals(HttpStatus.BAD_REQUEST.toString(), actualObject.getResponseCode());
240         assertEquals(VNF_SVC_REQUEST_ID, actualObject.getSvcRequestId());
241         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
242
243     }
244
245     @Test
246     public void test_postVnfOperationInformationWithSvcActionChanged_successfullyAddToExistingServiceInCache()
247             throws Exception {
248         final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
249         final ResponseEntity<OutputRequest> responseEntity =
250                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
251
252         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
253
254         final HttpEntity<?> httpVnfWithSvcActionAssignEntity = new HttpEntity<>(getVnfRequestInput(), getHttpHeaders());
255         final ResponseEntity<OutputRequest> response = restTemplate.exchange(getVnfUrl(), HttpMethod.POST,
256                 httpVnfWithSvcActionAssignEntity, OutputRequest.class);
257         assertEquals(HttpStatus.OK, response.getStatusCode());
258         assertTrue(response.hasBody());
259
260         final HttpEntity<?> httpVnfEntity =
261                 new HttpEntity<>(getVnfRequestWithSvcActionActivateInput(), getHttpHeaders());
262         final ResponseEntity<OutputRequest> responseVnfEntity =
263                 restTemplate.exchange(getVnfUrl(), HttpMethod.POST, httpVnfEntity, OutputRequest.class);
264         assertEquals(HttpStatus.OK, responseVnfEntity.getStatusCode());
265         assertTrue(responseVnfEntity.hasBody());
266
267         final OutputRequest actualOutputRequest = responseVnfEntity.getBody();
268         assertNotNull(actualOutputRequest);
269         assertNotNull(actualOutputRequest.getOutput());
270
271         final Output actualObject = actualOutputRequest.getOutput();
272
273         assertEquals(HttpStatus.OK.toString(), actualObject.getResponseCode());
274         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
275         assertEquals(VNF_SVC_REQUEST_ID, actualObject.getSvcRequestId());
276         assertNotNull(actualObject.getServiceResponseInformation());
277
278         final GenericResourceApiInstanceReference acutalReference = actualObject.getServiceResponseInformation();
279         assertEquals(Constants.RESTCONF_CONFIG_END_POINT + SERVICE_INSTANCE_ID, acutalReference.getObjectPath());
280         assertEquals(SERVICE_INSTANCE_ID, acutalReference.getInstanceId());
281         final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
282                 cacheServiceProvider.getGenericResourceApiServicemodelinfrastructureService(SERVICE_INSTANCE_ID);
283         assertTrue(optional.isPresent());
284
285         final GenericResourceApiInstanceReference actualvnfInformation = actualObject.getVnfResponseInformation();
286         assertEquals(VNF_INSTANCE_ID, actualvnfInformation.getInstanceId());
287
288         final Optional<GenericResourceApiServicemodelinfrastructureService> serviceOptional =
289                 cacheServiceProvider.getGenericResourceApiServicemodelinfrastructureService(SERVICE_INSTANCE_ID);
290         assertTrue(serviceOptional.isPresent());
291
292         final GenericResourceApiServicemodelinfrastructureService service = serviceOptional.get();
293         assertNotNull(service.getServiceInstanceId());
294         assertNotNull(service.getServiceData().getVnfs().getVnf());
295         assertNotNull(service.getServiceData());
296         assertNotNull(service.getServiceData().getVnfs());
297         assertNotNull(service.getServiceData().getVnfs().getVnf());
298         assertEquals(1, service.getServiceData().getVnfs().getVnf().size());
299         final GenericResourceApiServicedataServicedataVnfsVnf vnf = service.getServiceData().getVnfs().getVnf().get(0);
300         assertNotNull(vnf.getVnfId());
301         assertEquals(VNF_INSTANCE_ID, vnf.getVnfId());
302         assertNotNull(vnf.getVnfData());
303         GenericResourceApiOperStatusData vnfLevelOperStatus = vnf.getVnfData().getVnfLevelOperStatus();
304         assertNotNull(vnfLevelOperStatus);
305         assertEquals(GenericResourceApiLastRpcActionEnumeration.ACTIVATE, vnfLevelOperStatus.getLastRpcAction());
306
307     }
308
309
310     private HttpHeaders getHttpHeaders() {
311         return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
312     }
313
314     private String getUrl() {
315         return "http://localhost:" + port + Constants.OPERATIONS_URL + SERVICE_TOPOLOGY_OPERATION_URL;
316     }
317
318     private String getVnfUrl() {
319         return "http://localhost:" + port + Constants.OPERATIONS_URL + VNF_TOPOLOGY_OPERATION_URL;
320     }
321
322     @After
323     public void after() {
324         cacheServiceProvider.clearAll();
325     }
326
327 }