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                 + "?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_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
 
 214         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
 
 216         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
 
 218         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
 
 220         invokeHttpPut(url, getServiceInstance());
 
 222         final ResponseEntity<ServiceInstance> actual =
 
 223                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
 
 225         assertEquals(HttpStatus.OK, actual.getStatusCode());
 
 226         assertTrue(actual.hasBody());
 
 228         final ServiceInstance actualServiceInstance = actual.getBody();
 
 230         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
 
 231         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
 
 236     public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
 
 238         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
 
 240         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
 
 242         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
 
 244         invokeHttpPut(url, getServiceInstance());
 
 246         final String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL
 
 247                 + SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID();
 
 249         final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
 
 250                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
 
 252         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
 
 254         final RequestError actualError = actual.getBody();
 
 255         final ServiceException serviceException = actualError.getServiceException();
 
 257         assertNotNull(serviceException);
 
 258         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
 
 259         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
 
 260         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
 
 265     public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
 
 267         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
 
 269         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
 
 271         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
 
 273         final ResponseEntity<Void> putRequestReponse = invokeHttpPut(url, getServiceInstance());
 
 274         assertEquals(HttpStatus.ACCEPTED, putRequestReponse.getStatusCode());
 
 277         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
 
 278                 + "?service-instance-name=Dummy&depth=2";
 
 280         final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
 
 281                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
 
 283         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
 
 285         final RequestError actualError = actual.getBody();
 
 286         final ServiceException serviceException = actualError.getServiceException();
 
 288         assertNotNull(serviceException);
 
 289         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
 
 290         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
 
 291         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
 
 296     public void test_PathSericeInstance_usingServiceInstanceId_OrchStatusChangedInCache() throws Exception {
 
 298         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
 
 300         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
 
 302         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
 
 304         final ResponseEntity<Void> serviceInstancePutResponse = invokeHttpPut(url, getServiceInstance());
 
 305         assertEquals(HttpStatus.ACCEPTED, serviceInstancePutResponse.getStatusCode());
 
 307         final HttpHeaders httpHeaders = getHttpHeaders();
 
 308         httpHeaders.add(X_HTTP_METHOD_OVERRIDE, HttpMethod.PATCH.toString());
 
 310         final HttpEntity<?> orchStatuUpdateServiceInstance =
 
 311                 getHttpEntity(getOrchStatuUpdateServiceInstance(), httpHeaders);
 
 313         final ResponseEntity<Void> orchStatuUpdateServiceInstanceResponse =
 
 314                 invokeHttpPost(orchStatuUpdateServiceInstance, url, getOrchStatuUpdateServiceInstance());
 
 316         assertEquals(HttpStatus.ACCEPTED, orchStatuUpdateServiceInstanceResponse.getStatusCode());
 
 319         final ResponseEntity<ServiceInstance> actual =
 
 320                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
 
 322         assertEquals(HttpStatus.OK, actual.getStatusCode());
 
 323         assertTrue(actual.hasBody());
 
 325         final ServiceInstance actualServiceInstance = actual.getBody();
 
 327         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
 
 328         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
 
 329         assertEquals(ORCHESTRATION_STATUS, actualServiceInstance.getOrchestrationStatus());
 
 334     public void test_putServiceSubscription_successfullyAddedToCache() throws Exception {
 
 335         final String serviceSubscriptionurl =
 
 336                 getCustomerEndPointUrl() + "/service-subscriptions/service-subscription/" + FIREWALL_SERVICE_TTYPE;
 
 338         final ResponseEntity<Void> customerPutResponse = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
 
 339         assertEquals(HttpStatus.ACCEPTED, customerPutResponse.getStatusCode());
 
 341         final ResponseEntity<Void> serviceSubscriptionPutResponse =
 
 342                 invokeHttpPut(serviceSubscriptionurl, getServiceSubscription());
 
 343         assertEquals(HttpStatus.ACCEPTED, serviceSubscriptionPutResponse.getStatusCode());
 
 345         final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(serviceSubscriptionurl, HttpMethod.GET,
 
 346                 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
 
 348         assertEquals(HttpStatus.OK, actual.getStatusCode());
 
 349         assertTrue(actual.hasBody());
 
 351         final ServiceSubscription actualServiceSubscription = actual.getBody();
 
 352         assertEquals(FIREWALL_SERVICE_TTYPE, actualServiceSubscription.getServiceType());
 
 356     private String getCustomer() throws Exception, IOException {
 
 357         return getJsonString("test-data/business-customer.json");
 
 360     private String getServiceSubscription() throws Exception, IOException {
 
 361         return getJsonString("test-data/service-subscription.json");
 
 365     private String getCustomerEndPointUrl() {
 
 366         return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
 
 369     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
 
 370         final HttpEntity<?> httpEntity = getHttpEntity(obj);
 
 371         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
 
 374     private ResponseEntity<Void> invokeHttpPost(final HttpEntity<?> httpEntity, final String url, final Object obj) {
 
 375         return restTemplate.exchange(url, HttpMethod.POST, httpEntity, Void.class);
 
 378     private HttpEntity<?> getHttpEntity(final Object obj) {
 
 379         return new HttpEntity<>(obj, getHttpHeaders());
 
 382     private HttpEntity<?> getHttpEntity(final Object obj, final HttpHeaders headers) {
 
 383         return new HttpEntity<>(obj, headers);
 
 386     private HttpHeaders getHttpHeaders() {
 
 387         return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
 
 390     private String getServiceInstance() throws Exception, IOException {
 
 391         return getJsonString("test-data/service-instance.json");
 
 394     private String getOrchStatuUpdateServiceInstance() throws Exception, IOException {
 
 395         return getJsonString("test-data/service-instance-orch-status-update.json");