Merge "INT:1183 fix csit for sdnc_netconf_tls_post_deploy"
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / BusinessControllerTest.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.aaisimulator.controller;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
27 import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
28 import static org.onap.so.aaisimulator.utils.TestConstants.GLOBAL_CUSTOMER_ID;
29 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCES_URL;
30 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
31 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
32 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_NAME;
33 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
34 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_TYPE;
35 import static org.onap.so.aaisimulator.utils.TestUtils.getJsonString;
36 import java.io.IOException;
37 import java.util.Optional;
38 import java.util.UUID;
39 import org.junit.After;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.onap.aai.domain.yang.Customer;
43 import org.onap.aai.domain.yang.ServiceInstance;
44 import org.onap.aai.domain.yang.ServiceInstances;
45 import org.onap.aai.domain.yang.ServiceSubscription;
46 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
47 import org.onap.so.aaisimulator.utils.Constants;
48 import org.onap.so.aaisimulator.utils.RequestError;
49 import org.onap.so.aaisimulator.utils.ServiceException;
50 import org.onap.so.aaisimulator.utils.TestUtils;
51 import org.onap.so.simulator.model.UserCredentials;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.boot.test.context.SpringBootTest;
54 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
55 import org.springframework.boot.test.web.client.TestRestTemplate;
56 import org.springframework.boot.web.server.LocalServerPort;
57 import org.springframework.context.annotation.Configuration;
58 import org.springframework.http.HttpEntity;
59 import org.springframework.http.HttpHeaders;
60 import org.springframework.http.HttpMethod;
61 import org.springframework.http.HttpStatus;
62 import org.springframework.http.ResponseEntity;
63 import org.springframework.test.context.ActiveProfiles;
64 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
65
66 /**
67  * @author waqas.ikram@ericsson.com
68  *
69  */
70 @RunWith(SpringJUnit4ClassRunner.class)
71 @ActiveProfiles("test")
72 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
73 @Configuration
74 public class BusinessControllerTest {
75
76     private static final String FIREWALL_SERVICE_TTYPE = "Firewall";
77
78     private static final String ORCHESTRATION_STATUS = "Active";
79
80     @LocalServerPort
81     private int port;
82
83     @Autowired
84     private TestRestTemplate restTemplate;
85
86     @Autowired
87     private UserCredentials userCredentials;
88
89     @Autowired
90     private CustomerCacheServiceProvider cacheServiceProvider;
91
92     @After
93     public void after() {
94         cacheServiceProvider.clearAll();
95     }
96
97     @Test
98     public void test_putCustomer_successfullyAddedToCache() throws Exception {
99         final ResponseEntity<Void> actual = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
100
101         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
102         assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
103     }
104
105     @Test
106     public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
107         final String url = getCustomerEndPointUrl();
108
109         invokeHttpPut(url, getCustomer());
110
111         final ResponseEntity<Customer> actual =
112                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class);
113
114         assertEquals(HttpStatus.OK, actual.getStatusCode());
115         assertTrue(actual.hasBody());
116
117         final Customer actualCustomer = actual.getBody();
118         assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
119         assertNotNull(actualCustomer.getResourceVersion());
120         assertFalse(actualCustomer.getResourceVersion().isEmpty());
121     }
122
123     @Test
124     public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
125         final String url = getCustomerEndPointUrl();
126
127         final ResponseEntity<RequestError> actual =
128                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class);
129
130         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
131
132         final RequestError actualError = actual.getBody();
133         final ServiceException serviceException = actualError.getServiceException();
134
135         assertNotNull(serviceException);
136         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
137         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
138         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
139
140     }
141
142     @Test
143     public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
144         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL;
145
146         invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
147
148         final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET,
149                 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
150
151         assertEquals(HttpStatus.OK, actual.getStatusCode());
152         assertTrue(actual.hasBody());
153
154         final ServiceSubscription actualServiceSubscription = actual.getBody();
155         assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
156         assertNotNull(actualServiceSubscription.getRelationshipList());
157         assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
158     }
159
160     @Test
161     public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
162
163         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
164
165         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
166
167         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
168
169         invokeHttpPut(url, getServiceInstance());
170
171         final Optional<ServiceInstance> actual =
172                 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
173
174         assertTrue(actual.isPresent());
175         final ServiceInstance actualServiceInstance = actual.get();
176
177         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
178         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
179
180     }
181
182     @Test
183     public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
184             throws Exception {
185
186         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
187
188         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
189
190         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
191
192         invokeHttpPut(url, getServiceInstance());
193
194         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
195                 + "?depth=2&service-instance-name=" + SERVICE_NAME;
196
197         final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
198                 new HttpEntity<>(getHttpHeaders()), ServiceInstances.class);
199
200         assertEquals(HttpStatus.OK, actual.getStatusCode());
201         assertTrue(actual.hasBody());
202
203         final ServiceInstances actualServiceInstances = actual.getBody();
204         assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
205
206         assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
207
208     }
209
210     @Test
211     public void test_getSericeInstance_usingServiceInstanceName_returnRequestErrorIfnoServiceInstanceFound()
212             throws Exception {
213
214
215         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
216
217         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
218
219         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
220                 + "?depth=2&service-instance-name=" + SERVICE_NAME;
221
222         final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
223                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
224
225         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
226         assertTrue(actual.hasBody());
227
228         assertNotNull(actual.getBody().getServiceException());
229
230     }
231
232     @Test
233     public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
234             throws Exception {
235
236         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
237
238         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
239
240         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
241
242         invokeHttpPut(url, getServiceInstance());
243
244         final ResponseEntity<ServiceInstance> actual =
245                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
246
247         assertEquals(HttpStatus.OK, actual.getStatusCode());
248         assertTrue(actual.hasBody());
249
250         final ServiceInstance actualServiceInstance = actual.getBody();
251
252         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
253         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
254
255     }
256
257     @Test
258     public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
259
260         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
261
262         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
263
264         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
265
266         invokeHttpPut(url, getServiceInstance());
267
268         final String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL
269                 + SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID();
270
271         final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
272                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
273
274         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
275
276         final RequestError actualError = actual.getBody();
277         final ServiceException serviceException = actualError.getServiceException();
278
279         assertNotNull(serviceException);
280         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
281         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
282         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
283
284     }
285
286     @Test
287     public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
288
289         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
290
291         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
292
293         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
294
295         final ResponseEntity<Void> putRequestReponse = invokeHttpPut(url, getServiceInstance());
296         assertEquals(HttpStatus.ACCEPTED, putRequestReponse.getStatusCode());
297
298
299         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
300                 + "?service-instance-name=Dummy&depth=2";
301
302         final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
303                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
304
305         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
306
307         final RequestError actualError = actual.getBody();
308         final ServiceException serviceException = actualError.getServiceException();
309
310         assertNotNull(serviceException);
311         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
312         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
313         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
314
315     }
316
317     @Test
318     public void test_PathSericeInstance_usingServiceInstanceId_OrchStatusChangedInCache() throws Exception {
319
320         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
321
322         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
323
324         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
325
326         final ResponseEntity<Void> serviceInstancePutResponse = invokeHttpPut(url, getServiceInstance());
327         assertEquals(HttpStatus.ACCEPTED, serviceInstancePutResponse.getStatusCode());
328
329         final HttpHeaders httpHeaders = getHttpHeaders();
330         httpHeaders.add(X_HTTP_METHOD_OVERRIDE, HttpMethod.PATCH.toString());
331
332         final HttpEntity<?> orchStatuUpdateServiceInstance =
333                 getHttpEntity(getOrchStatuUpdateServiceInstance(), httpHeaders);
334
335         final ResponseEntity<Void> orchStatuUpdateServiceInstanceResponse =
336                 invokeHttpPost(orchStatuUpdateServiceInstance, url, getOrchStatuUpdateServiceInstance());
337
338         assertEquals(HttpStatus.ACCEPTED, orchStatuUpdateServiceInstanceResponse.getStatusCode());
339
340
341         final ResponseEntity<ServiceInstance> actual =
342                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
343
344         assertEquals(HttpStatus.OK, actual.getStatusCode());
345         assertTrue(actual.hasBody());
346
347         final ServiceInstance actualServiceInstance = actual.getBody();
348
349         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
350         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
351         assertEquals(ORCHESTRATION_STATUS, actualServiceInstance.getOrchestrationStatus());
352
353     }
354
355     @Test
356     public void test_putServiceSubscription_successfullyAddedToCache() throws Exception {
357         final String serviceSubscriptionurl =
358                 getCustomerEndPointUrl() + "/service-subscriptions/service-subscription/" + FIREWALL_SERVICE_TTYPE;
359
360         final ResponseEntity<Void> customerPutResponse = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
361         assertEquals(HttpStatus.ACCEPTED, customerPutResponse.getStatusCode());
362
363         final ResponseEntity<Void> serviceSubscriptionPutResponse =
364                 invokeHttpPut(serviceSubscriptionurl, getServiceSubscription());
365         assertEquals(HttpStatus.ACCEPTED, serviceSubscriptionPutResponse.getStatusCode());
366
367         final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(serviceSubscriptionurl, HttpMethod.GET,
368                 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
369
370         assertEquals(HttpStatus.OK, actual.getStatusCode());
371         assertTrue(actual.hasBody());
372
373         final ServiceSubscription actualServiceSubscription = actual.getBody();
374         assertEquals(FIREWALL_SERVICE_TTYPE, actualServiceSubscription.getServiceType());
375
376     }
377
378     private String getCustomer() throws Exception, IOException {
379         return getJsonString("test-data/business-customer.json");
380     }
381
382     private String getServiceSubscription() throws Exception, IOException {
383         return getJsonString("test-data/service-subscription.json");
384     }
385
386
387     private String getCustomerEndPointUrl() {
388         return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
389     }
390
391     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
392         final HttpEntity<?> httpEntity = getHttpEntity(obj);
393         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
394     }
395
396     private ResponseEntity<Void> invokeHttpPost(final HttpEntity<?> httpEntity, final String url, final Object obj) {
397         return restTemplate.exchange(url, HttpMethod.POST, httpEntity, Void.class);
398     }
399
400     private HttpEntity<?> getHttpEntity(final Object obj) {
401         return new HttpEntity<>(obj, getHttpHeaders());
402     }
403
404     private HttpEntity<?> getHttpEntity(final Object obj, final HttpHeaders headers) {
405         return new HttpEntity<>(obj, headers);
406     }
407
408     private HttpHeaders getHttpHeaders() {
409         return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
410     }
411
412     private String getServiceInstance() throws Exception, IOException {
413         return getJsonString("test-data/service-instance.json");
414     }
415
416     private String getOrchStatuUpdateServiceInstance() throws Exception, IOException {
417         return getJsonString("test-data/service-instance-orch-status-update.json");
418     }
419
420 }