18d478eb9814e395cbfecba16aebde16ba4512ec
[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 java.util.Optional;
29 import org.junit.After;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.onap.sdnc.northbound.client.model.GenericResourceApiInstanceReference;
33 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicedataServicedataVnfsVnf;
34 import org.onap.sdnc.northbound.client.model.GenericResourceApiServicemodelinfrastructureService;
35 import org.onap.so.sdncsimulator.models.InputRequest;
36 import org.onap.so.sdncsimulator.models.Output;
37 import org.onap.so.sdncsimulator.models.OutputRequest;
38 import org.onap.so.sdncsimulator.providers.ServiceOperationsCacheServiceProvider;
39 import org.onap.so.sdncsimulator.utils.Constants;
40 import org.onap.so.simulator.model.UserCredentials;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
44 import org.springframework.boot.test.web.client.TestRestTemplate;
45 import org.springframework.boot.web.server.LocalServerPort;
46 import org.springframework.context.annotation.Configuration;
47 import org.springframework.http.HttpEntity;
48 import org.springframework.http.HttpHeaders;
49 import org.springframework.http.HttpMethod;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.http.ResponseEntity;
52 import org.springframework.test.context.ActiveProfiles;
53 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
54
55 /**
56  * @author Waqas Ikram (waqas.ikram@est.tech)
57  *
58  */
59 @RunWith(SpringJUnit4ClassRunner.class)
60 @ActiveProfiles("test")
61 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
62 @Configuration
63 public class OperationsControllerTest {
64
65     private static final String SVC_REQUEST_ID = "04fc9f50-87b8-430d-a232-ef24bd6c4150";
66
67     private static final String VNF_SVC_REQUEST_ID = "8fd2622b-01fc-424d-bfc8-f48bcd64e546";
68
69     private static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b";
70
71     private static final String SERVICE_TOPOLOGY_OPERATION_URL = "/GENERIC-RESOURCE-API:service-topology-operation/";
72
73     private static final String VNF_TOPOLOGY_OPERATION_URL = "/GENERIC-RESOURCE-API:vnf-topology-operation/";
74
75     private static final String VNF_INSTANCE_ID = "dfd02fb5-d7fb-4aac-b3c4-cd6b60058701";
76
77     @LocalServerPort
78     private int port;
79
80     @Autowired
81     private TestRestTemplate restTemplate;
82
83     @Autowired
84     private ServiceOperationsCacheServiceProvider cacheServiceProvider;
85
86     @Autowired
87     private UserCredentials userCredentials;
88
89
90     @Test
91     public void test_postServiceOperationInformation_successfullyAddedToCache() throws Exception {
92
93         final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
94         final ResponseEntity<OutputRequest> responseEntity =
95                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
96
97         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
98         assertTrue(responseEntity.hasBody());
99
100         final OutputRequest actualOutputRequest = responseEntity.getBody();
101         assertNotNull(actualOutputRequest);
102
103         final Output actualObject = actualOutputRequest.getOutput();
104
105         assertNotNull(actualObject);
106         assertEquals(HttpStatus.OK.toString(), actualObject.getResponseCode());
107         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
108         assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
109         assertNotNull(actualObject.getServiceResponseInformation());
110
111         final GenericResourceApiInstanceReference acutalReference = actualObject.getServiceResponseInformation();
112         assertEquals(Constants.RESTCONF_CONFIG_END_POINT + SERVICE_INSTANCE_ID, acutalReference.getObjectPath());
113         assertEquals(SERVICE_INSTANCE_ID, acutalReference.getInstanceId());
114         final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
115                 cacheServiceProvider.getGenericResourceApiServicemodelinfrastructureService(SERVICE_INSTANCE_ID);
116         assertTrue(optional.isPresent());
117
118         final GenericResourceApiServicemodelinfrastructureService service = optional.get();
119         assertNotNull(service.getServiceInstanceId());
120         assertEquals(SERVICE_INSTANCE_ID, service.getServiceInstanceId());
121         assertNotNull(service.getServiceData());
122         assertNotNull(service.getServiceStatus());
123
124     }
125
126     @Test
127     public void test_postServiceOperationInformation_NullInputRequest_badRequest() throws Exception {
128
129         final HttpEntity<?> httpEntity = new HttpEntity<>(new InputRequest<>(), getHttpHeaders());
130         final ResponseEntity<OutputRequest> responseEntity =
131                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
132
133         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
134     }
135
136     @Test
137     public void test_postServiceOperationInformation_NullServiceInstanceId_badRequest() throws Exception {
138
139         final HttpEntity<?> httpEntity = new HttpEntity<>(getInvalidRequestInput(), getHttpHeaders());
140         final ResponseEntity<OutputRequest> responseEntity =
141                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
142
143         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
144         assertTrue(responseEntity.hasBody());
145
146         final OutputRequest actualOutputRequest = responseEntity.getBody();
147         assertNotNull(actualOutputRequest);
148
149         final Output actualObject = actualOutputRequest.getOutput();
150         assertNotNull(actualObject);
151         assertEquals(HttpStatus.BAD_REQUEST.toString(), actualObject.getResponseCode());
152         assertEquals(SVC_REQUEST_ID, actualObject.getSvcRequestId());
153         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
154
155     }
156
157     @Test
158     public void test_postVnfOperationInformation_successfullyAddToExistingServiceInCache() throws Exception {
159         final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
160         final ResponseEntity<OutputRequest> responseEntity =
161                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
162
163         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
164
165         final HttpEntity<?> httpVnfEntity = new HttpEntity<>(getVnfRequestInput(), getHttpHeaders());
166         final ResponseEntity<OutputRequest> responseVnfEntity =
167                 restTemplate.exchange(getVnfUrl(), HttpMethod.POST, httpVnfEntity, OutputRequest.class);
168         assertEquals(HttpStatus.OK, responseVnfEntity.getStatusCode());
169         assertTrue(responseVnfEntity.hasBody());
170
171         final OutputRequest actualOutputRequest = responseVnfEntity.getBody();
172         assertNotNull(actualOutputRequest);
173         assertNotNull(actualOutputRequest.getOutput());
174
175         final Output actualObject = actualOutputRequest.getOutput();
176
177         assertEquals(HttpStatus.OK.toString(), actualObject.getResponseCode());
178         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
179         assertEquals(VNF_SVC_REQUEST_ID, actualObject.getSvcRequestId());
180         assertNotNull(actualObject.getServiceResponseInformation());
181
182         final GenericResourceApiInstanceReference acutalReference = actualObject.getServiceResponseInformation();
183         assertEquals(Constants.RESTCONF_CONFIG_END_POINT + SERVICE_INSTANCE_ID, acutalReference.getObjectPath());
184         assertEquals(SERVICE_INSTANCE_ID, acutalReference.getInstanceId());
185         final Optional<GenericResourceApiServicemodelinfrastructureService> optional =
186                 cacheServiceProvider.getGenericResourceApiServicemodelinfrastructureService(SERVICE_INSTANCE_ID);
187         assertTrue(optional.isPresent());
188
189         final GenericResourceApiInstanceReference actualvnfInformation = actualObject.getVnfResponseInformation();
190         assertEquals(VNF_INSTANCE_ID, actualvnfInformation.getInstanceId());
191
192         final Optional<GenericResourceApiServicemodelinfrastructureService> serviceOptional =
193                 cacheServiceProvider.getGenericResourceApiServicemodelinfrastructureService(SERVICE_INSTANCE_ID);
194         assertTrue(serviceOptional.isPresent());
195
196         final GenericResourceApiServicemodelinfrastructureService service = serviceOptional.get();
197         assertNotNull(service.getServiceInstanceId());
198         assertNotNull(service.getServiceData().getVnfs().getVnf());
199         assertNotNull(service.getServiceData());
200         assertNotNull(service.getServiceData().getVnfs());
201         assertNotNull(service.getServiceData().getVnfs().getVnf());
202         assertEquals(1, service.getServiceData().getVnfs().getVnf().size());
203         final GenericResourceApiServicedataServicedataVnfsVnf vnf = service.getServiceData().getVnfs().getVnf().get(0);
204         assertNotNull(vnf.getVnfId());
205         assertEquals(VNF_INSTANCE_ID, vnf.getVnfId());
206         assertNotNull(vnf.getVnfData());
207     }
208
209     @Test
210     public void test_postSameVnfOperationInformationTwice_ShouldReturnbadRequest() throws Exception {
211
212         final HttpEntity<?> httpEntity = new HttpEntity<>(getRequestInput(), getHttpHeaders());
213         final ResponseEntity<OutputRequest> responseEntity =
214                 restTemplate.exchange(getUrl(), HttpMethod.POST, httpEntity, OutputRequest.class);
215
216         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
217
218         final HttpEntity<?> httpVnfEntity = new HttpEntity<>(getVnfRequestInput(), getHttpHeaders());
219         final ResponseEntity<OutputRequest> responseVnfEntity =
220                 restTemplate.exchange(getVnfUrl(), HttpMethod.POST, httpVnfEntity, OutputRequest.class);
221         assertEquals(HttpStatus.OK, responseVnfEntity.getStatusCode());
222         assertTrue(responseVnfEntity.hasBody());
223
224         final OutputRequest actualOutputRequest = responseVnfEntity.getBody();
225         assertNotNull(actualOutputRequest);
226         assertNotNull(actualOutputRequest.getOutput());
227
228         final ResponseEntity<OutputRequest> badResponse =
229                 restTemplate.exchange(getVnfUrl(), HttpMethod.POST, httpVnfEntity, OutputRequest.class);
230
231         final OutputRequest badOutputRequest = badResponse.getBody();
232         assertNotNull(badOutputRequest);
233
234         final Output actualObject = badOutputRequest.getOutput();
235         assertNotNull(actualObject);
236         assertEquals(HttpStatus.BAD_REQUEST.toString(), actualObject.getResponseCode());
237         assertEquals(VNF_SVC_REQUEST_ID, actualObject.getSvcRequestId());
238         assertEquals(Constants.YES, actualObject.getAckFinalIndicator());
239
240     }
241
242     private HttpHeaders getHttpHeaders() {
243         return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
244     }
245
246     private String getUrl() {
247         return "http://localhost:" + port + Constants.OPERATIONS_URL + SERVICE_TOPOLOGY_OPERATION_URL;
248     }
249
250     private String getVnfUrl() {
251         return "http://localhost:" + port + Constants.OPERATIONS_URL + VNF_TOPOLOGY_OPERATION_URL;
252     }
253
254     @After
255     public void after() {
256         cacheServiceProvider.clearAll();
257     }
258
259 }