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.utils.TestConstants.CUSTOMERS_URL;
27 import static org.onap.so.aai.simulator.utils.TestConstants.GLOBAL_CUSTOMER_ID;
28 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCES_URL;
29 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCE_ID;
30 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCE_URL;
31 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_NAME;
32 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
33 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_TYPE;
34 import static org.onap.so.aai.simulator.utils.TestUtils.getJsonString;
35 import java.io.IOException;
36 import java.util.Optional;
37 import java.util.UUID;
38 import org.junit.After;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.onap.aai.domain.yang.Customer;
42 import org.onap.aai.domain.yang.ServiceInstance;
43 import org.onap.aai.domain.yang.ServiceInstances;
44 import org.onap.aai.domain.yang.ServiceSubscription;
45 import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider;
46 import org.onap.so.aai.simulator.utils.Constants;
47 import org.onap.so.aai.simulator.utils.RequestError;
48 import org.onap.so.aai.simulator.utils.ServiceException;
49 import org.onap.so.aai.simulator.utils.TestUtils;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.beans.factory.annotation.Value;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
54 import org.springframework.boot.test.web.client.TestRestTemplate;
55 import org.springframework.boot.web.server.LocalServerPort;
56 import org.springframework.context.annotation.Configuration;
57 import org.springframework.http.HttpEntity;
58 import org.springframework.http.HttpHeaders;
59 import org.springframework.http.HttpMethod;
60 import org.springframework.http.HttpStatus;
61 import org.springframework.http.ResponseEntity;
62 import org.springframework.test.context.ActiveProfiles;
63 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
66 * @author waqas.ikram@ericsson.com
69 @RunWith(SpringJUnit4ClassRunner.class)
70 @ActiveProfiles("test")
71 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
73 public class BusinessControllerTest {
79 private TestRestTemplate restTemplate;
81 @Value("${spring.security.username}")
82 private String username;
85 private CustomerCacheServiceProvider cacheServiceProvider;
89 cacheServiceProvider.clearAll();
93 public void test_putCustomer_successfullyAddedToCache() throws Exception {
94 final ResponseEntity<Void> actual = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
96 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
97 assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
101 public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
102 final String url = getCustomerEndPointUrl();
104 invokeHttpPut(url, getCustomer());
106 final ResponseEntity<Customer> actual =
107 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class);
109 assertEquals(HttpStatus.OK, actual.getStatusCode());
110 assertTrue(actual.hasBody());
112 final Customer actualCustomer = actual.getBody();
113 assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
114 assertNotNull(actualCustomer.getResourceVersion());
115 assertFalse(actualCustomer.getResourceVersion().isEmpty());
119 public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
120 final String url = getCustomerEndPointUrl();
122 final ResponseEntity<RequestError> actual =
123 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class);
125 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
127 final RequestError actualError = actual.getBody();
128 final ServiceException serviceException = actualError.getServiceException();
130 assertNotNull(serviceException);
131 assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
132 assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
133 assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
138 public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
139 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL;
141 invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
143 final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET,
144 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
146 assertEquals(HttpStatus.OK, actual.getStatusCode());
147 assertTrue(actual.hasBody());
149 final ServiceSubscription actualServiceSubscription = actual.getBody();
150 assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
151 assertNotNull(actualServiceSubscription.getRelationshipList());
152 assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
156 public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
158 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
160 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
162 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
164 invokeHttpPut(url, getServiceInstance());
166 final Optional<ServiceInstance> actual =
167 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
169 assertTrue(actual.isPresent());
170 final ServiceInstance actualServiceInstance = actual.get();
172 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
173 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
178 public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
181 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
183 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
185 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
187 invokeHttpPut(url, getServiceInstance());
189 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
190 + "?service-instance-name=" + SERVICE_NAME;
192 final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
193 new HttpEntity<>(getHttpHeaders()), ServiceInstances.class);
195 assertEquals(HttpStatus.OK, actual.getStatusCode());
196 assertTrue(actual.hasBody());
198 final ServiceInstances actualServiceInstances = actual.getBody();
199 assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
201 assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
206 public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
209 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
211 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
213 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
215 invokeHttpPut(url, getServiceInstance());
217 final ResponseEntity<ServiceInstance> actual =
218 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
220 assertEquals(HttpStatus.OK, actual.getStatusCode());
221 assertTrue(actual.hasBody());
223 final ServiceInstance actualServiceInstance = actual.getBody();
225 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
226 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
231 public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
233 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
235 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
237 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
239 invokeHttpPut(url, getServiceInstance());
241 final String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL
242 + SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID();
244 final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
245 new HttpEntity<>(getHttpHeaders()), RequestError.class);
247 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
249 final RequestError actualError = actual.getBody();
250 final ServiceException serviceException = actualError.getServiceException();
252 assertNotNull(serviceException);
253 assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
254 assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
255 assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
260 public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
262 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
264 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
266 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
268 final ResponseEntity<Void> putRequestReponse = invokeHttpPut(url, getServiceInstance());
269 assertEquals(HttpStatus.ACCEPTED, putRequestReponse.getStatusCode());
272 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
273 + "?service-instance-name=Dummy&depth=2";
275 final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
276 new HttpEntity<>(getHttpHeaders()), RequestError.class);
278 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
280 final RequestError actualError = actual.getBody();
281 final ServiceException serviceException = actualError.getServiceException();
283 assertNotNull(serviceException);
284 assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
285 assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
286 assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
290 private String getCustomer() throws Exception, IOException {
291 return getJsonString("test-data/business-customer.json");
294 private String getCustomerEndPointUrl() {
295 return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
298 private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
299 final HttpEntity<?> httpEntity = getHttpEntity(obj);
300 return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
303 private HttpEntity<?> getHttpEntity(final Object obj) {
304 return new HttpEntity<>(obj, getHttpHeaders());
307 private HttpHeaders getHttpHeaders() {
308 return TestUtils.getHttpHeaders(username);
311 private String getServiceInstance() throws Exception, IOException {
312 return getJsonString("test-data/service-instance.json");