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.GENERIC_VNF_NAME;
 
  29 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
 
  30 import static org.onap.so.aaisimulator.utils.TestConstants.GLOBAL_CUSTOMER_ID;
 
  31 import static org.onap.so.aaisimulator.utils.TestConstants.RELATED_TO_URL;
 
  32 import static org.onap.so.aaisimulator.utils.TestConstants.RELATIONSHIP_URL;
 
  33 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCES_URL;
 
  34 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
 
  35 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
 
  36 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_NAME;
 
  37 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
 
  38 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_TYPE;
 
  39 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
 
  40 import static org.onap.so.aaisimulator.utils.TestUtils.getCustomer;
 
  41 import static org.onap.so.aaisimulator.utils.TestUtils.getServiceInstance;
 
  42 import java.io.IOException;
 
  43 import java.util.Optional;
 
  44 import java.util.UUID;
 
  45 import org.junit.After;
 
  46 import org.junit.Test;
 
  47 import org.onap.aai.domain.yang.Customer;
 
  48 import org.onap.aai.domain.yang.GenericVnf;
 
  49 import org.onap.aai.domain.yang.GenericVnfs;
 
  50 import org.onap.aai.domain.yang.Relationship;
 
  51 import org.onap.aai.domain.yang.ServiceInstance;
 
  52 import org.onap.aai.domain.yang.ServiceInstances;
 
  53 import org.onap.aai.domain.yang.ServiceSubscription;
 
  54 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
 
  55 import org.onap.so.aaisimulator.utils.RequestError;
 
  56 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
 
  57 import org.onap.so.aaisimulator.utils.ServiceException;
 
  58 import org.onap.so.aaisimulator.utils.TestUtils;
 
  59 import org.springframework.beans.factory.annotation.Autowired;
 
  60 import org.springframework.http.HttpHeaders;
 
  61 import org.springframework.http.HttpMethod;
 
  62 import org.springframework.http.HttpStatus;
 
  63 import org.springframework.http.ResponseEntity;
 
  66  * @author waqas.ikram@ericsson.com
 
  69 public class BusinessControllerTest extends AbstractSpringBootTest {
 
  71     private static final String FIREWALL_SERVICE_TYPE = "Firewall";
 
  73     private static final String ORCHESTRATION_STATUS = "Active";
 
  76     private CustomerCacheServiceProvider cacheServiceProvider;
 
  80         cacheServiceProvider.clearAll();
 
  84     public void test_putCustomer_successfullyAddedToCache() throws Exception {
 
  85         invokeCustomerEndPointAndAssertResponse();
 
  86         assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
 
  90     public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
 
  91         final String url = getUrl(CUSTOMERS_URL);
 
  93         final ResponseEntity<Void> response = testRestTemplateService.invokeHttpPut(url, getCustomer(), Void.class);
 
  94         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
 
  96         final ResponseEntity<Customer> actual = testRestTemplateService.invokeHttpGet(url, Customer.class);
 
  98         assertEquals(HttpStatus.OK, actual.getStatusCode());
 
  99         assertTrue(actual.hasBody());
 
 101         final Customer actualCustomer = actual.getBody();
 
 102         assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
 
 103         assertNotNull(actualCustomer.getResourceVersion());
 
 104         assertFalse(actualCustomer.getResourceVersion().isEmpty());
 
 108     public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
 
 109         final String url = getUrl(CUSTOMERS_URL);
 
 111         final ResponseEntity<RequestError> actual = testRestTemplateService.invokeHttpGet(url, RequestError.class);
 
 113         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
 
 115         final RequestError actualError = actual.getBody();
 
 116         final ServiceException serviceException = actualError.getServiceException();
 
 118         assertNotNull(serviceException);
 
 119         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
 
 120         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
 
 121         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
 
 126     public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
 
 127         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL);
 
 129         invokeCustomerEndPointAndAssertResponse();
 
 131         final ResponseEntity<ServiceSubscription> actual =
 
 132                 testRestTemplateService.invokeHttpGet(url, ServiceSubscription.class);
 
 134         assertEquals(HttpStatus.OK, actual.getStatusCode());
 
 135         assertTrue(actual.hasBody());
 
 137         final ServiceSubscription actualServiceSubscription = actual.getBody();
 
 138         assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
 
 139         assertNotNull(actualServiceSubscription.getRelationshipList());
 
 140         assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
 
 144     public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
 
 146         invokeCustomerEndPointAndAssertResponse();
 
 147         invokeServiceInstanceEndPointAndAssertResponse();
 
 150         final Optional<ServiceInstance> actual =
 
 151                 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
 
 153         assertTrue(actual.isPresent());
 
 154         final ServiceInstance actualServiceInstance = actual.get();
 
 156         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
 
 157         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
 
 162     public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
 
 165         invokeCustomerEndPointAndAssertResponse();
 
 166         invokeServiceInstanceEndPointAndAssertResponse();
 
 169         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCES_URL)
 
 170                 + "?depth=2&service-instance-name=" + SERVICE_NAME;
 
 172         final ResponseEntity<ServiceInstances> actual =
 
 173                 testRestTemplateService.invokeHttpGet(serviceInstanceUrl, ServiceInstances.class);
 
 175         assertEquals(HttpStatus.OK, actual.getStatusCode());
 
 176         assertTrue(actual.hasBody());
 
 178         final ServiceInstances actualServiceInstances = actual.getBody();
 
 179         assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
 
 181         assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
 
 186     public void test_getSericeInstance_usingServiceInstanceName_returnRequestErrorIfnoServiceInstanceFound()
 
 189         invokeCustomerEndPointAndAssertResponse();
 
 191         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCES_URL)
 
 192                 + "?depth=2&service-instance-name=" + SERVICE_NAME;
 
 194         final ResponseEntity<RequestError> actual =
 
 195                 testRestTemplateService.invokeHttpGet(serviceInstanceUrl, RequestError.class);
 
 197         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
 
 198         assertTrue(actual.hasBody());
 
 200         assertNotNull(actual.getBody().getServiceException());
 
 205     public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
 
 208         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
 
 210         invokeCustomerEndPointAndAssertResponse();
 
 211         invokeServiceInstanceEndPointAndAssertResponse();
 
 213         final ResponseEntity<ServiceInstance> actual =
 
 214                 testRestTemplateService.invokeHttpGet(url, ServiceInstance.class);
 
 216         assertEquals(HttpStatus.OK, actual.getStatusCode());
 
 217         assertTrue(actual.hasBody());
 
 219         final ServiceInstance actualServiceInstance = actual.getBody();
 
 221         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
 
 222         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
 
 227     public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
 
 229         invokeCustomerEndPointAndAssertResponse();
 
 231         invokeServiceInstanceEndPointAndAssertResponse();
 
 234         final String invalidServiceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL,
 
 235                 SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID());
 
 237         final ResponseEntity<RequestError> actual =
 
 238                 testRestTemplateService.invokeHttpGet(invalidServiceInstanceUrl, RequestError.class);
 
 240         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
 
 242         final RequestError actualError = actual.getBody();
 
 243         final ServiceException serviceException = actualError.getServiceException();
 
 245         assertNotNull(serviceException);
 
 246         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
 
 247         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
 
 248         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
 
 253     public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
 
 255         invokeCustomerEndPointAndAssertResponse();
 
 256         invokeServiceInstanceEndPointAndAssertResponse();
 
 259         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCES_URL)
 
 260                 + "?service-instance-name=Dummy&depth=2";
 
 262         final ResponseEntity<RequestError> actual =
 
 263                 testRestTemplateService.invokeHttpGet(serviceInstanceUrl, RequestError.class);
 
 265         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
 
 267         final RequestError actualError = actual.getBody();
 
 268         final ServiceException serviceException = actualError.getServiceException();
 
 270         assertNotNull(serviceException);
 
 271         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
 
 272         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
 
 273         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
 
 278     public void test_PathSericeInstance_usingServiceInstanceId_OrchStatusChangedInCache() throws Exception {
 
 280         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
 
 282         invokeCustomerEndPointAndAssertResponse();
 
 283         invokeServiceInstanceEndPointAndAssertResponse();
 
 285         final HttpHeaders httpHeaders = testRestTemplateService.getHttpHeaders();
 
 286         httpHeaders.add(X_HTTP_METHOD_OVERRIDE, HttpMethod.PATCH.toString());
 
 288         final ResponseEntity<Void> orchStatuUpdateServiceInstanceResponse = testRestTemplateService
 
 289                 .invokeHttpPost(httpHeaders, url, TestUtils.getOrchStatuUpdateServiceInstance(), Void.class);
 
 291         assertEquals(HttpStatus.ACCEPTED, orchStatuUpdateServiceInstanceResponse.getStatusCode());
 
 293         final ResponseEntity<ServiceInstance> actual =
 
 294                 testRestTemplateService.invokeHttpGet(url, ServiceInstance.class);
 
 296         assertEquals(HttpStatus.OK, actual.getStatusCode());
 
 297         assertTrue(actual.hasBody());
 
 299         final ServiceInstance actualServiceInstance = actual.getBody();
 
 301         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
 
 302         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
 
 303         assertEquals(ORCHESTRATION_STATUS, actualServiceInstance.getOrchestrationStatus());
 
 308     public void test_putServiceSubscription_successfullyAddedToCache() throws Exception {
 
 309         final String serviceSubscriptionurl =
 
 310                 getUrl(CUSTOMERS_URL, "/service-subscriptions/service-subscription/", FIREWALL_SERVICE_TYPE);
 
 312         invokeCustomerEndPointAndAssertResponse();
 
 314         final ResponseEntity<Void> responseEntity = testRestTemplateService.invokeHttpPut(serviceSubscriptionurl,
 
 315                 TestUtils.getServiceSubscription(), Void.class);
 
 316         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
 
 318         final ResponseEntity<ServiceSubscription> actual =
 
 319                 testRestTemplateService.invokeHttpGet(serviceSubscriptionurl, ServiceSubscription.class);
 
 321         assertEquals(HttpStatus.OK, actual.getStatusCode());
 
 322         assertTrue(actual.hasBody());
 
 324         final ServiceSubscription actualServiceSubscription = actual.getBody();
 
 325         assertEquals(FIREWALL_SERVICE_TYPE, actualServiceSubscription.getServiceType());
 
 330     public void test_putSericeInstanceRelatedTo_ableToRetrieveServiceInstanceFromCache() throws Exception {
 
 332         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
 
 334         invokeCustomerEndPointAndAssertResponse();
 
 336         invokeServiceInstanceEndPointAndAssertResponse();
 
 338         final String relationShipUrl =
 
 339                 getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL, RELATIONSHIP_URL);
 
 341         final ResponseEntity<Relationship> responseEntity2 = testRestTemplateService.invokeHttpPut(relationShipUrl,
 
 342                 TestUtils.getRelationShipJsonObject(), Relationship.class);
 
 344         assertEquals(HttpStatus.ACCEPTED, responseEntity2.getStatusCode());
 
 346         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
 
 347         final ResponseEntity<Void> genericVnfResponse =
 
 348                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
 
 349         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
 
 351         final ResponseEntity<GenericVnfs> actual = testRestTemplateService
 
 352                 .invokeHttpGet(url + RELATED_TO_URL + "?vnf-name=" + GENERIC_VNF_NAME, GenericVnfs.class);
 
 354         assertEquals(HttpStatus.OK, actual.getStatusCode());
 
 356         assertTrue(actual.hasBody());
 
 357         final GenericVnfs genericVnfs = actual.getBody();
 
 358         assertFalse(genericVnfs.getGenericVnf().isEmpty());
 
 359         final GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
 
 360         assertEquals(GENERIC_VNF_NAME, genericVnf.getVnfName());
 
 363     private void invokeServiceInstanceEndPointAndAssertResponse() throws IOException {
 
 364         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
 
 365         final ResponseEntity<Void> responseEntity =
 
 366                 testRestTemplateService.invokeHttpPut(url, getServiceInstance(), Void.class);
 
 367         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
 
 370     private void invokeCustomerEndPointAndAssertResponse() throws Exception, IOException {
 
 371         final ResponseEntity<Void> response =
 
 372                 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), getCustomer(), Void.class);
 
 374         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());