/*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ package org.onap.so.aaisimulator.controller; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL; import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL; import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_OWNER_NAME; import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_REGION_NAME; import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL; import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME; import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL; import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL; import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL; import static org.onap.so.aaisimulator.utils.TestConstants.TENANTS_TENANT; import static org.onap.so.aaisimulator.utils.TestConstants.TENANT_ID; import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID; import java.io.IOException; import java.util.List; import java.util.Optional; import org.junit.After; import org.junit.Test; import org.onap.aai.domain.yang.CloudRegion; import org.onap.aai.domain.yang.GenericVnf; import org.onap.aai.domain.yang.RelatedToProperty; import org.onap.aai.domain.yang.Relationship; import org.onap.aai.domain.yang.RelationshipData; import org.onap.aai.domain.yang.RelationshipList; import org.onap.aai.domain.yang.Tenant; import org.onap.so.aaisimulator.models.CloudRegionKey; import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider; import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider; import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider; import org.onap.so.aaisimulator.utils.Constants; import org.onap.so.aaisimulator.utils.TestConstants; import org.onap.so.aaisimulator.utils.TestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; /** * @author Waqas Ikram (waqas.ikram@est.tech) * */ public class CloudRegionsControllerTest extends AbstractSpringBootTest { private static final CloudRegionKey CLOUD_REGION_KEY = new CloudRegionKey(CLOUD_OWNER_NAME, CLOUD_REGION_NAME); @Autowired private CloudRegionCacheServiceProvider cloudRegionCacheServiceProvider; @Autowired private CustomerCacheServiceProvider customerCacheServiceProvider; @Autowired private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider; @After public void after() { cloudRegionCacheServiceProvider.clearAll(); customerCacheServiceProvider.clearAll(); genericVnfCacheServiceProvider.clearAll(); } @Test public void test_putCloudRegion_successfullyAddedToCache() throws Exception { final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME); invokeCloudRegionHttpPutEndPointAndAssertResponse(url); final ResponseEntity response = testRestTemplateService.invokeHttpGet(url, CloudRegion.class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertTrue(response.hasBody()); final CloudRegion cloudRegion = response.getBody(); assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner()); assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId()); assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion()); } @Test public void test_getCloudRegionWithDepthValue_shouldReturnMatchedCloudRegion() throws Exception { final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME); invokeCloudRegionHttpPutEndPointAndAssertResponse(url); final ResponseEntity response = testRestTemplateService.invokeHttpGet(url + "?depth=2", CloudRegion.class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertTrue(response.hasBody()); final CloudRegion cloudRegion = response.getBody(); assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner()); assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId()); assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion()); } @Test public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception { final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME); invokeCloudRegionHttpPutEndPointAndAssertResponse(url); final String relationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, BI_DIRECTIONAL_RELATIONSHIP_LIST_URL); final ResponseEntity responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl, TestUtils.getGenericVnfRelationShip(), Relationship.class); assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode()); final Optional optional = cloudRegionCacheServiceProvider.getCloudRegion(CLOUD_REGION_KEY); assertTrue(optional.isPresent()); final CloudRegion actual = optional.get(); assertNotNull(actual.getRelationshipList()); final List relationshipList = actual.getRelationshipList().getRelationship(); assertFalse("Relationship list should not be empty", relationshipList.isEmpty()); final Relationship relationship = relationshipList.get(0); assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty()); assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty()); final RelationshipData relationshipData = relationship.getRelationshipData().get(0); assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey()); assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue()); final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0); assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey()); assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue()); } @Test public void test_putTenant_successfullyAddedToCache() throws Exception { final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME); invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl); final String tenantUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME + TENANTS_TENANT + TENANT_ID); addTenantAndAssertResponse(tenantUrl); final ResponseEntity response = testRestTemplateService.invokeHttpGet(tenantUrl, Tenant.class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertTrue(response.hasBody()); final Tenant tenant = response.getBody(); assertEquals(TENANT_ID, tenant.getTenantId()); assertEquals("admin", tenant.getTenantName()); assertNotNull("ResourceVersion should not be null", tenant.getResourceVersion()); } @Test public void test_putTenantRelationToGenericVnf_successfullyAddedToCache() throws Exception { addCustomerServiceAndGenericVnf(); final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME); invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl); final String tenantUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, TENANTS_TENANT + TENANT_ID); addTenantAndAssertResponse(tenantUrl); final String tenantRelationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, TENANTS_TENANT + TENANT_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL); final ResponseEntity tenantRelationShipResponse = testRestTemplateService .invokeHttpPut(tenantRelationShipUrl, TestUtils.getGenericVnfRelatedLink(), Void.class); assertEquals(HttpStatus.ACCEPTED, tenantRelationShipResponse.getStatusCode()); final Optional optional = cloudRegionCacheServiceProvider.getTenant(CLOUD_REGION_KEY, TENANT_ID); assertTrue(optional.isPresent()); final Tenant actualTenant = optional.get(); final RelationshipList relationshipList = actualTenant.getRelationshipList(); assertNotNull(relationshipList); assertFalse(relationshipList.getRelationship().isEmpty()); final Relationship relationship = relationshipList.getRelationship().get(0); assertEquals(Constants.COMPOSED_OF, relationship.getRelationshipLabel()); assertFalse(relationship.getRelationshipData().isEmpty()); assertEquals(1, relationship.getRelationshipData().size()); final List relationshipDataList = relationship.getRelationshipData(); final RelationshipData relationshipData = getRelationshipData(relationshipDataList, Constants.GENERIC_VNF_VNF_ID); assertNotNull(relationshipData); assertEquals(VNF_ID, relationshipData.getRelationshipValue()); final List relatedToPropertyList = relationship.getRelatedToProperty(); final RelatedToProperty property = getRelatedToProperty(relatedToPropertyList, Constants.GENERIC_VNF_VNF_NAME); assertNotNull(property); assertEquals(GENERIC_VNF_NAME, property.getPropertyValue()); final Optional genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID); assertTrue(genericVnfOptional.isPresent()); final GenericVnf actualGenericVnf = genericVnfOptional.get(); final RelationshipList relationshipListGenericVnf = actualGenericVnf.getRelationshipList(); assertNotNull(relationshipListGenericVnf); assertFalse(relationshipListGenericVnf.getRelationship().isEmpty()); final Relationship relationshipGenericVnf = relationshipListGenericVnf.getRelationship().get(0); assertEquals(Constants.BELONGS_TO, relationshipGenericVnf.getRelationshipLabel()); assertFalse(relationshipGenericVnf.getRelationshipData().isEmpty()); assertEquals(3, relationshipGenericVnf.getRelationshipData().size()); } private void addTenantAndAssertResponse(final String tenantUrl) throws IOException { final ResponseEntity responseEntity = testRestTemplateService.invokeHttpPut(tenantUrl, TestUtils.getTenant(), Void.class); assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode()); } private void addCustomerServiceAndGenericVnf() throws Exception, IOException { final ResponseEntity customerResponse = testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class); assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode()); final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL); final ResponseEntity serviceInstanceResponse = testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class); assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode()); final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID); final ResponseEntity genericVnfResponse = testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class); assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode()); } private RelationshipData getRelationshipData(final List relationshipData, final String key) { return relationshipData.stream().filter(data -> data.getRelationshipKey().equals(key)).findFirst().orElse(null); } private RelatedToProperty getRelatedToProperty(final List relatedToPropertyList, final String key) { return relatedToPropertyList.stream().filter(data -> data.getPropertyKey().equals(key)).findFirst() .orElse(null); } private void invokeCloudRegionHttpPutEndPointAndAssertResponse(final String url) throws IOException { final ResponseEntity responseEntity = testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class); assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode()); } }