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.aaisimulator.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.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;
67 * @author waqas.ikram@ericsson.com
70 @RunWith(SpringJUnit4ClassRunner.class)
71 @ActiveProfiles("test")
72 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
74 public class BusinessControllerTest {
76 private static final String FIREWALL_SERVICE_TTYPE = "Firewall";
78 private static final String ORCHESTRATION_STATUS = "Active";
84 private TestRestTemplate restTemplate;
87 private UserCredentials userCredentials;
90 private CustomerCacheServiceProvider cacheServiceProvider;
94 cacheServiceProvider.clearAll();
98 public void test_putCustomer_successfullyAddedToCache() throws Exception {
99 final ResponseEntity<Void> actual = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
101 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
102 assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
106 public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
107 final String url = getCustomerEndPointUrl();
109 invokeHttpPut(url, getCustomer());
111 final ResponseEntity<Customer> actual =
112 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class);
114 assertEquals(HttpStatus.OK, actual.getStatusCode());
115 assertTrue(actual.hasBody());
117 final Customer actualCustomer = actual.getBody();
118 assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
119 assertNotNull(actualCustomer.getResourceVersion());
120 assertFalse(actualCustomer.getResourceVersion().isEmpty());
124 public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
125 final String url = getCustomerEndPointUrl();
127 final ResponseEntity<RequestError> actual =
128 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class);
130 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
132 final RequestError actualError = actual.getBody();
133 final ServiceException serviceException = actualError.getServiceException();
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()));
143 public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
144 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL;
146 invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
148 final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET,
149 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
151 assertEquals(HttpStatus.OK, actual.getStatusCode());
152 assertTrue(actual.hasBody());
154 final ServiceSubscription actualServiceSubscription = actual.getBody();
155 assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
156 assertNotNull(actualServiceSubscription.getRelationshipList());
157 assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
161 public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
163 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
165 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
167 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
169 invokeHttpPut(url, getServiceInstance());
171 final Optional<ServiceInstance> actual =
172 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
174 assertTrue(actual.isPresent());
175 final ServiceInstance actualServiceInstance = actual.get();
177 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
178 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
183 public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
186 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
188 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
190 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
192 invokeHttpPut(url, getServiceInstance());
194 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
195 + "?depth=2&service-instance-name=" + SERVICE_NAME;
197 final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
198 new HttpEntity<>(getHttpHeaders()), ServiceInstances.class);
200 assertEquals(HttpStatus.OK, actual.getStatusCode());
201 assertTrue(actual.hasBody());
203 final ServiceInstances actualServiceInstances = actual.getBody();
204 assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
206 assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
211 public void test_getSericeInstance_usingServiceInstanceName_returnRequestErrorIfnoServiceInstanceFound()
215 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
217 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
219 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
220 + "?depth=2&service-instance-name=" + SERVICE_NAME;
222 final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
223 new HttpEntity<>(getHttpHeaders()), RequestError.class);
225 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
226 assertTrue(actual.hasBody());
228 assertNotNull(actual.getBody().getServiceException());
233 public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
236 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
238 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
240 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
242 invokeHttpPut(url, getServiceInstance());
244 final ResponseEntity<ServiceInstance> actual =
245 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
247 assertEquals(HttpStatus.OK, actual.getStatusCode());
248 assertTrue(actual.hasBody());
250 final ServiceInstance actualServiceInstance = actual.getBody();
252 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
253 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
258 public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
260 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
262 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
264 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
266 invokeHttpPut(url, getServiceInstance());
268 final String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL
269 + SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID();
271 final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
272 new HttpEntity<>(getHttpHeaders()), RequestError.class);
274 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
276 final RequestError actualError = actual.getBody();
277 final ServiceException serviceException = actualError.getServiceException();
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()));
287 public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
289 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
291 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
293 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
295 final ResponseEntity<Void> putRequestReponse = invokeHttpPut(url, getServiceInstance());
296 assertEquals(HttpStatus.ACCEPTED, putRequestReponse.getStatusCode());
299 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
300 + "?service-instance-name=Dummy&depth=2";
302 final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
303 new HttpEntity<>(getHttpHeaders()), RequestError.class);
305 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
307 final RequestError actualError = actual.getBody();
308 final ServiceException serviceException = actualError.getServiceException();
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()));
318 public void test_PathSericeInstance_usingServiceInstanceId_OrchStatusChangedInCache() throws Exception {
320 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
322 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
324 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
326 final ResponseEntity<Void> serviceInstancePutResponse = invokeHttpPut(url, getServiceInstance());
327 assertEquals(HttpStatus.ACCEPTED, serviceInstancePutResponse.getStatusCode());
329 final HttpHeaders httpHeaders = getHttpHeaders();
330 httpHeaders.add(X_HTTP_METHOD_OVERRIDE, HttpMethod.PATCH.toString());
332 final HttpEntity<?> orchStatuUpdateServiceInstance =
333 getHttpEntity(getOrchStatuUpdateServiceInstance(), httpHeaders);
335 final ResponseEntity<Void> orchStatuUpdateServiceInstanceResponse =
336 invokeHttpPost(orchStatuUpdateServiceInstance, url, getOrchStatuUpdateServiceInstance());
338 assertEquals(HttpStatus.ACCEPTED, orchStatuUpdateServiceInstanceResponse.getStatusCode());
341 final ResponseEntity<ServiceInstance> actual =
342 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
344 assertEquals(HttpStatus.OK, actual.getStatusCode());
345 assertTrue(actual.hasBody());
347 final ServiceInstance actualServiceInstance = actual.getBody();
349 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
350 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
351 assertEquals(ORCHESTRATION_STATUS, actualServiceInstance.getOrchestrationStatus());
356 public void test_putServiceSubscription_successfullyAddedToCache() throws Exception {
357 final String serviceSubscriptionurl =
358 getCustomerEndPointUrl() + "/service-subscriptions/service-subscription/" + FIREWALL_SERVICE_TTYPE;
360 final ResponseEntity<Void> customerPutResponse = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
361 assertEquals(HttpStatus.ACCEPTED, customerPutResponse.getStatusCode());
363 final ResponseEntity<Void> serviceSubscriptionPutResponse =
364 invokeHttpPut(serviceSubscriptionurl, getServiceSubscription());
365 assertEquals(HttpStatus.ACCEPTED, serviceSubscriptionPutResponse.getStatusCode());
367 final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(serviceSubscriptionurl, HttpMethod.GET,
368 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
370 assertEquals(HttpStatus.OK, actual.getStatusCode());
371 assertTrue(actual.hasBody());
373 final ServiceSubscription actualServiceSubscription = actual.getBody();
374 assertEquals(FIREWALL_SERVICE_TTYPE, actualServiceSubscription.getServiceType());
378 private String getCustomer() throws Exception, IOException {
379 return getJsonString("test-data/business-customer.json");
382 private String getServiceSubscription() throws Exception, IOException {
383 return getJsonString("test-data/service-subscription.json");
387 private String getCustomerEndPointUrl() {
388 return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
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);
396 private ResponseEntity<Void> invokeHttpPost(final HttpEntity<?> httpEntity, final String url, final Object obj) {
397 return restTemplate.exchange(url, HttpMethod.POST, httpEntity, Void.class);
400 private HttpEntity<?> getHttpEntity(final Object obj) {
401 return new HttpEntity<>(obj, getHttpHeaders());
404 private HttpEntity<?> getHttpEntity(final Object obj, final HttpHeaders headers) {
405 return new HttpEntity<>(obj, headers);
408 private HttpHeaders getHttpHeaders() {
409 return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
412 private String getServiceInstance() throws Exception, IOException {
413 return getJsonString("test-data/service-instance.json");
416 private String getOrchStatuUpdateServiceInstance() throws Exception, IOException {
417 return getJsonString("test-data/service-instance-orch-status-update.json");