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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aai.simulator.controller;
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;
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;
61 * @author waqas.ikram@ericsson.com
64 @RunWith(SpringJUnit4ClassRunner.class)
65 @ActiveProfiles("test")
66 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
68 public class BusinessControllerTest {
70 private static final String SERVICE_INSTANCES_URL = "/service-instances";
72 private static final String SERVICE_NAME = "ServiceTest";
74 private static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b";
76 private static final String SERVICE_INSTANCE_URL =
77 SERVICE_INSTANCES_URL + "/service-instance/" + SERVICE_INSTANCE_ID;
79 private static final String SERVICE_TYPE = "vCPE";
81 private static final String SERVICE_SUBSCRIPTIONS_URL =
82 "/service-subscriptions/service-subscription/" + SERVICE_TYPE;
84 private static final String GLOBAL_CUSTOMER_ID = "DemoCustomer";
86 private static final String CUSTOMERS_URL = Constant.BUSINESS_URL + "customers/customer/" + GLOBAL_CUSTOMER_ID;
88 private static final String PASSWORD = "aai.onap.org:demo123456!";
94 private TestRestTemplate restTemplate;
96 @Value("${spring.security.username}")
97 private String username;
100 private CacheServiceProvider cacheServiceProvider;
103 public void after() {
104 cacheServiceProvider.clearAll();
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);
112 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
113 assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
117 public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
118 final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
119 final String url = getCustomerEndPointUrl();
121 invokeHttpPut(url, customer);
123 final ResponseEntity<Customer> actual =
124 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class);
126 assertEquals(HttpStatus.OK, actual.getStatusCode());
127 assertTrue(actual.hasBody());
129 final Customer actualCustomer = actual.getBody();
130 assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
131 assertNotNull(actualCustomer.getResourceVersion());
132 assertFalse(actualCustomer.getResourceVersion().isEmpty());
136 public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
137 final String url = getCustomerEndPointUrl();
139 final ResponseEntity<RequestError> actual =
140 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class);
142 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
144 final RequestError actualError = actual.getBody();
145 final ServiceException serviceException = actualError.getServiceException();
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()));
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;
159 invokeHttpPut(getCustomerEndPointUrl(), customer);
161 final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET,
162 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
164 assertEquals(HttpStatus.OK, actual.getStatusCode());
165 assertTrue(actual.hasBody());
167 final ServiceSubscription actualServiceSubscription = actual.getBody();
168 assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
169 assertNotNull(actualServiceSubscription.getRelationshipList());
170 assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
174 public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
175 final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
177 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
179 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), customer);
181 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
183 final ServiceInstance serviceInstance =
184 getObjectFromFile(getFile("test-data/service-instance.json"), ServiceInstance.class);
186 invokeHttpPut(url, serviceInstance);
188 final Optional<ServiceInstance> actual =
189 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
191 assertTrue(actual.isPresent());
192 final ServiceInstance actualServiceInstance = actual.get();
194 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
195 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
200 public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
202 final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
204 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
206 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), customer);
208 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
210 final ServiceInstance serviceInstance =
211 getObjectFromFile(getFile("test-data/service-instance.json"), ServiceInstance.class);
213 invokeHttpPut(url, serviceInstance);
215 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
216 + "?service-instance-name=" + SERVICE_NAME;
218 final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
219 new HttpEntity<>(getHttpHeaders()), ServiceInstances.class);
221 assertEquals(HttpStatus.OK, actual.getStatusCode());
222 assertTrue(actual.hasBody());
224 final ServiceInstances actualServiceInstances = actual.getBody();
225 assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
227 assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
232 public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
234 final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
236 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
238 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), customer);
240 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
242 final ServiceInstance serviceInstance =
243 getObjectFromFile(getFile("test-data/service-instance.json"), ServiceInstance.class);
245 invokeHttpPut(url, serviceInstance);
247 final ResponseEntity<ServiceInstance> actual =
248 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
250 assertEquals(HttpStatus.OK, actual.getStatusCode());
251 assertTrue(actual.hasBody());
253 final ServiceInstance actualServiceInstance = actual.getBody();
255 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
256 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
261 public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
262 final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
264 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
266 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), customer);
268 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
270 final ServiceInstance serviceInstance =
271 getObjectFromFile(getFile("test-data/service-instance.json"), ServiceInstance.class);
273 invokeHttpPut(url, serviceInstance);
275 String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
276 + "/service-instance/" + UUID.randomUUID();
278 final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
279 new HttpEntity<>(getHttpHeaders()), RequestError.class);
281 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
283 final RequestError actualError = actual.getBody();
284 final ServiceException serviceException = actualError.getServiceException();
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()));
294 public void test_getSericeInstance_usinginvalidServiceInstanceName_shouldReturnError() throws Exception {
295 final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
297 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
299 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), customer);
301 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
303 final ServiceInstance serviceInstance =
304 getObjectFromFile(getFile("test-data/service-instance.json"), ServiceInstance.class);
306 invokeHttpPut(url, serviceInstance);
308 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
309 + "?service-instance-name=Dummy&depth=2";
311 final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
312 new HttpEntity<>(getHttpHeaders()), RequestError.class);
314 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
316 final RequestError actualError = actual.getBody();
317 final ServiceException serviceException = actualError.getServiceException();
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()));
326 private String getCustomerEndPointUrl() {
327 return getBaseUrl() + CUSTOMERS_URL;
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);
335 private HttpEntity<?> getHttpEntity(final Object obj) {
336 return new HttpEntity<>(obj, getHttpHeaders());
339 private HttpHeaders getHttpHeaders() {
340 final HttpHeaders requestHeaders = new HttpHeaders();
341 requestHeaders.add("Authorization", getBasicAuth());
342 return requestHeaders;
345 private File getFile(final String file) throws IOException {
346 return new ClassPathResource(file).getFile();
349 private Customer getCustomer(final File file) throws Exception {
350 return getObjectFromFile(file, Customer.class);
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());
357 return mapper.readValue(file, clazz);
360 private String getBasicAuth() {
361 return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
364 private String getBaseUrl() {
365 return "http://localhost:" + port;