d317e6b6a9c5463cf805971fc0a76d17e61c4bd8
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aai / simulator / 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.aai.simulator.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 java.io.File;
27 import java.io.IOException;
28 import java.util.Base64;
29 import java.util.Optional;
30 import java.util.UUID;
31 import org.junit.After;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.onap.aai.domain.yang.Customer;
35 import org.onap.aai.domain.yang.ServiceInstance;
36 import org.onap.aai.domain.yang.ServiceInstances;
37 import org.onap.aai.domain.yang.ServiceSubscription;
38 import org.onap.so.aai.simulator.service.providers.CacheServiceProvider;
39 import org.onap.so.aai.simulator.utils.Constant;
40 import org.onap.so.aai.simulator.utils.RequestError;
41 import org.onap.so.aai.simulator.utils.ServiceException;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Value;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
46 import org.springframework.boot.test.web.client.TestRestTemplate;
47 import org.springframework.boot.web.server.LocalServerPort;
48 import org.springframework.context.annotation.Configuration;
49 import org.springframework.core.io.ClassPathResource;
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 import com.fasterxml.jackson.databind.ObjectMapper;
58 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
59
60 /**
61  * @author waqas.ikram@ericsson.com
62  *
63  */
64 @RunWith(SpringJUnit4ClassRunner.class)
65 @ActiveProfiles("test")
66 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
67 @Configuration
68 public class BusinessControllerTest {
69
70     private static final String SERVICE_INSTANCES_URL = "/service-instances";
71
72     private static final String SERVICE_NAME = "ServiceTest";
73
74     private static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b";
75
76     private static final String SERVICE_INSTANCE_URL =
77             SERVICE_INSTANCES_URL + "/service-instance/" + SERVICE_INSTANCE_ID;
78
79     private static final String SERVICE_TYPE = "vCPE";
80
81     private static final String SERVICE_SUBSCRIPTIONS_URL =
82             "/service-subscriptions/service-subscription/" + SERVICE_TYPE;
83
84     private static final String GLOBAL_CUSTOMER_ID = "DemoCustomer";
85
86     private static final String CUSTOMERS_URL = Constant.BUSINESS_URL + "customers/customer/" + GLOBAL_CUSTOMER_ID;
87
88     private static final String PASSWORD = "aai.onap.org:demo123456!";
89
90     @LocalServerPort
91     private int port;
92
93     @Autowired
94     private TestRestTemplate restTemplate;
95
96     @Value("${spring.security.username}")
97     private String username;
98
99     @Autowired
100     private CacheServiceProvider cacheServiceProvider;
101
102     @After
103     public void after() {
104         cacheServiceProvider.clearAll();
105     }
106
107     @Test
108     public void test_putCustomer_successfullyAddedToCache() throws Exception {
109         final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
110         final ResponseEntity<Void> actual = invokeHttpPut(getCustomerEndPointUrl(), customer);
111
112         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
113         assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
114     }
115
116     @Test
117     public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
118         final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
119         final String url = getCustomerEndPointUrl();
120
121         invokeHttpPut(url, customer);
122
123         final ResponseEntity<Customer> actual =
124                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class);
125
126         assertEquals(HttpStatus.OK, actual.getStatusCode());
127         assertTrue(actual.hasBody());
128
129         final Customer actualCustomer = actual.getBody();
130         assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
131         assertNotNull(actualCustomer.getResourceVersion());
132         assertFalse(actualCustomer.getResourceVersion().isEmpty());
133     }
134
135     @Test
136     public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
137         final String url = getCustomerEndPointUrl();
138
139         final ResponseEntity<RequestError> actual =
140                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class);
141
142         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
143
144         final RequestError actualError = actual.getBody();
145         final ServiceException serviceException = actualError.getServiceException();
146
147         assertNotNull(serviceException);
148         assertEquals(Constant.ERROR_MESSAGE_ID, serviceException.getMessageId());
149         assertEquals(Constant.ERROR_MESSAGE, serviceException.getText());
150         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
151
152     }
153
154     @Test
155     public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
156         final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
157         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL;
158
159         invokeHttpPut(getCustomerEndPointUrl(), customer);
160
161         final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET,
162                 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
163
164         assertEquals(HttpStatus.OK, actual.getStatusCode());
165         assertTrue(actual.hasBody());
166
167         final ServiceSubscription actualServiceSubscription = actual.getBody();
168         assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
169         assertNotNull(actualServiceSubscription.getRelationshipList());
170         assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
171     }
172
173     @Test
174     public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
175         final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
176
177         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
178
179         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), customer);
180
181         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
182
183         final ServiceInstance serviceInstance =
184                 getObjectFromFile(getFile("test-data/service-instance.json"), ServiceInstance.class);
185
186         invokeHttpPut(url, serviceInstance);
187
188         final Optional<ServiceInstance> actual =
189                 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
190
191         assertTrue(actual.isPresent());
192         final ServiceInstance actualServiceInstance = actual.get();
193
194         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
195         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
196
197     }
198
199     @Test
200     public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
201             throws Exception {
202         final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
203
204         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
205
206         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), customer);
207
208         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
209
210         final ServiceInstance serviceInstance =
211                 getObjectFromFile(getFile("test-data/service-instance.json"), ServiceInstance.class);
212
213         invokeHttpPut(url, serviceInstance);
214
215         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
216                 + "?service-instance-name=" + SERVICE_NAME;
217
218         final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
219                 new HttpEntity<>(getHttpHeaders()), ServiceInstances.class);
220
221         assertEquals(HttpStatus.OK, actual.getStatusCode());
222         assertTrue(actual.hasBody());
223
224         final ServiceInstances actualServiceInstances = actual.getBody();
225         assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
226
227         assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
228
229     }
230
231     @Test
232     public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
233             throws Exception {
234         final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
235
236         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
237
238         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), customer);
239
240         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
241
242         final ServiceInstance serviceInstance =
243                 getObjectFromFile(getFile("test-data/service-instance.json"), ServiceInstance.class);
244
245         invokeHttpPut(url, serviceInstance);
246
247         final ResponseEntity<ServiceInstance> actual =
248                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
249
250         assertEquals(HttpStatus.OK, actual.getStatusCode());
251         assertTrue(actual.hasBody());
252
253         final ServiceInstance actualServiceInstance = actual.getBody();
254
255         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
256         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
257
258     }
259
260     @Test
261     public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
262         final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
263
264         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
265
266         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), customer);
267
268         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
269
270         final ServiceInstance serviceInstance =
271                 getObjectFromFile(getFile("test-data/service-instance.json"), ServiceInstance.class);
272
273         invokeHttpPut(url, serviceInstance);
274
275         String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
276                 + "/service-instance/" + UUID.randomUUID();
277
278         final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
279                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
280
281         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
282
283         final RequestError actualError = actual.getBody();
284         final ServiceException serviceException = actualError.getServiceException();
285
286         assertNotNull(serviceException);
287         assertEquals(Constant.ERROR_MESSAGE_ID, serviceException.getMessageId());
288         assertEquals(Constant.ERROR_MESSAGE, serviceException.getText());
289         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
290
291     }
292
293     @Test
294     public void test_getSericeInstance_usinginvalidServiceInstanceName_shouldReturnError() throws Exception {
295         final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
296
297         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
298
299         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), customer);
300
301         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
302
303         final ServiceInstance serviceInstance =
304                 getObjectFromFile(getFile("test-data/service-instance.json"), ServiceInstance.class);
305
306         invokeHttpPut(url, serviceInstance);
307
308         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
309                 + "?service-instance-name=Dummy&depth=2";
310
311         final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
312                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
313
314         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
315
316         final RequestError actualError = actual.getBody();
317         final ServiceException serviceException = actualError.getServiceException();
318
319         assertNotNull(serviceException);
320         assertEquals(Constant.ERROR_MESSAGE_ID, serviceException.getMessageId());
321         assertEquals(Constant.ERROR_MESSAGE, serviceException.getText());
322         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
323
324     }
325
326     private String getCustomerEndPointUrl() {
327         return getBaseUrl() + CUSTOMERS_URL;
328     }
329
330     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
331         final HttpEntity<?> httpEntity = getHttpEntity(obj);
332         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
333     }
334
335     private HttpEntity<?> getHttpEntity(final Object obj) {
336         return new HttpEntity<>(obj, getHttpHeaders());
337     }
338
339     private HttpHeaders getHttpHeaders() {
340         final HttpHeaders requestHeaders = new HttpHeaders();
341         requestHeaders.add("Authorization", getBasicAuth());
342         return requestHeaders;
343     }
344
345     private File getFile(final String file) throws IOException {
346         return new ClassPathResource(file).getFile();
347     }
348
349     private Customer getCustomer(final File file) throws Exception {
350         return getObjectFromFile(file, Customer.class);
351     }
352
353     private <T> T getObjectFromFile(final File file, final Class<T> clazz) throws Exception {
354         final ObjectMapper mapper = new ObjectMapper();
355         mapper.registerModule(new JaxbAnnotationModule());
356
357         return mapper.readValue(file, clazz);
358     }
359
360     private String getBasicAuth() {
361         return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
362     }
363
364     private String getBaseUrl() {
365         return "http://localhost:" + port;
366     }
367
368 }