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;
26 import static org.onap.so.aai.simulator.controller.TestUtils.getFile;
27 import static org.onap.so.aai.simulator.controller.TestUtils.getJsonString;
28 import static org.onap.so.aai.simulator.controller.TestUtils.getObjectFromFile;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.util.Optional;
33 import java.util.UUID;
34 import org.junit.After;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.onap.aai.domain.yang.Customer;
38 import org.onap.aai.domain.yang.ServiceInstance;
39 import org.onap.aai.domain.yang.ServiceInstances;
40 import org.onap.aai.domain.yang.ServiceSubscription;
41 import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider;
42 import org.onap.so.aai.simulator.utils.Constants;
43 import org.onap.so.aai.simulator.utils.RequestError;
44 import org.onap.so.aai.simulator.utils.ServiceException;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.beans.factory.annotation.Value;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
49 import org.springframework.boot.test.web.client.TestRestTemplate;
50 import org.springframework.boot.web.server.LocalServerPort;
51 import org.springframework.context.annotation.Configuration;
52 import org.springframework.http.HttpEntity;
53 import org.springframework.http.HttpHeaders;
54 import org.springframework.http.HttpMethod;
55 import org.springframework.http.HttpStatus;
56 import org.springframework.http.ResponseEntity;
57 import org.springframework.test.context.ActiveProfiles;
58 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
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 = Constants.CUSTOMER_URL + GLOBAL_CUSTOMER_ID;
92 private TestRestTemplate restTemplate;
94 @Value("${spring.security.username}")
95 private String username;
98 private CustomerCacheServiceProvider cacheServiceProvider;
101 public void after() {
102 cacheServiceProvider.clearAll();
106 public void test_putCustomer_successfullyAddedToCache() throws Exception {
107 final ResponseEntity<Void> actual = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
109 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
110 assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
114 public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
115 final String url = getCustomerEndPointUrl();
117 invokeHttpPut(url, getCustomer());
119 final ResponseEntity<Customer> actual =
120 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class);
122 assertEquals(HttpStatus.OK, actual.getStatusCode());
123 assertTrue(actual.hasBody());
125 final Customer actualCustomer = actual.getBody();
126 assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
127 assertNotNull(actualCustomer.getResourceVersion());
128 assertFalse(actualCustomer.getResourceVersion().isEmpty());
132 public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
133 final String url = getCustomerEndPointUrl();
135 final ResponseEntity<RequestError> actual =
136 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class);
138 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
140 final RequestError actualError = actual.getBody();
141 final ServiceException serviceException = actualError.getServiceException();
143 assertNotNull(serviceException);
144 assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
145 assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
146 assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
151 public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
152 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL;
154 invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
156 final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET,
157 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
159 assertEquals(HttpStatus.OK, actual.getStatusCode());
160 assertTrue(actual.hasBody());
162 final ServiceSubscription actualServiceSubscription = actual.getBody();
163 assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
164 assertNotNull(actualServiceSubscription.getRelationshipList());
165 assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
169 public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
171 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
173 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
175 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
177 invokeHttpPut(url, getServiceInstance());
179 final Optional<ServiceInstance> actual =
180 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
182 assertTrue(actual.isPresent());
183 final ServiceInstance actualServiceInstance = actual.get();
185 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
186 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
191 public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
194 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
196 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
198 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
200 invokeHttpPut(url, getServiceInstance());
202 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
203 + "?service-instance-name=" + SERVICE_NAME;
205 final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
206 new HttpEntity<>(getHttpHeaders()), ServiceInstances.class);
208 assertEquals(HttpStatus.OK, actual.getStatusCode());
209 assertTrue(actual.hasBody());
211 final ServiceInstances actualServiceInstances = actual.getBody();
212 assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
214 assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
219 public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
222 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
224 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
226 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
228 invokeHttpPut(url, getServiceInstance());
230 final ResponseEntity<ServiceInstance> actual =
231 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
233 assertEquals(HttpStatus.OK, actual.getStatusCode());
234 assertTrue(actual.hasBody());
236 final ServiceInstance actualServiceInstance = actual.getBody();
238 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
239 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
244 public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
246 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
248 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
250 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
252 invokeHttpPut(url, getServiceInstance());
254 final String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL
255 + SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID();
257 final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
258 new HttpEntity<>(getHttpHeaders()), RequestError.class);
260 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
262 final RequestError actualError = actual.getBody();
263 final ServiceException serviceException = actualError.getServiceException();
265 assertNotNull(serviceException);
266 assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
267 assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
268 assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
273 public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
275 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
277 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
279 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
281 final ResponseEntity<Void> putRequestReponse = invokeHttpPut(url, getServiceInstance());
282 assertEquals(HttpStatus.ACCEPTED, putRequestReponse.getStatusCode());
285 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
286 + "?service-instance-name=Dummy&depth=2";
288 final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
289 new HttpEntity<>(getHttpHeaders()), RequestError.class);
291 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
293 final RequestError actualError = actual.getBody();
294 final ServiceException serviceException = actualError.getServiceException();
296 assertNotNull(serviceException);
297 assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
298 assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
299 assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
303 private String getCustomer() throws Exception, IOException {
304 return getJsonString("test-data/business-customer.json");
307 private String getCustomerEndPointUrl() {
308 return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
311 private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
312 final HttpEntity<?> httpEntity = getHttpEntity(obj);
313 return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
316 private HttpEntity<?> getHttpEntity(final Object obj) {
317 return new HttpEntity<>(obj, getHttpHeaders());
320 private HttpHeaders getHttpHeaders() {
321 return TestUtils.getHttpHeaders(username);
324 private String getServiceInstance() throws Exception, IOException {
325 return getJsonString("test-data/service-instance.json");