Adding tenant endpoints
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / GenericVnfsControllerTest.java
index 5327a1f..4411e3e 100644 (file)
@@ -23,24 +23,26 @@ 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.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.GLOBAL_CUSTOMER_ID;
-import static org.onap.so.aaisimulator.utils.TestConstants.RELATIONSHIP_URL;
+import static org.onap.so.aaisimulator.utils.TestConstants.LINE_OF_BUSINESS_NAME;
+import static org.onap.so.aaisimulator.utils.TestConstants.PLATFORM_NAME;
 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_NAME;
 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_TYPE;
 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
-import static org.onap.so.aaisimulator.utils.TestUtils.getJsonString;
 import java.io.IOException;
 import java.util.List;
 import java.util.Optional;
 import org.junit.After;
 import org.junit.Test;
-import org.junit.runner.RunWith;
 import org.onap.aai.domain.yang.GenericVnf;
 import org.onap.aai.domain.yang.RelatedToProperty;
 import org.onap.aai.domain.yang.Relationship;
@@ -49,63 +51,50 @@ import org.onap.aai.domain.yang.RelationshipList;
 import org.onap.aai.domain.yang.ServiceInstance;
 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
+import org.onap.so.aaisimulator.service.providers.LinesOfBusinessCacheServiceProvider;
+import org.onap.so.aaisimulator.service.providers.PlatformCacheServiceProvider;
 import org.onap.so.aaisimulator.utils.Constants;
 import org.onap.so.aaisimulator.utils.TestUtils;
-import org.onap.so.simulator.model.UserCredentials;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
-import org.springframework.boot.test.web.client.TestRestTemplate;
-import org.springframework.boot.web.server.LocalServerPort;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
-import org.springframework.test.context.ActiveProfiles;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import org.springframework.web.util.UriComponentsBuilder;
 
 /**
  * @author Waqas Ikram (waqas.ikram@est.tech)
  *
  */
-@RunWith(SpringJUnit4ClassRunner.class)
-@ActiveProfiles("test")
-@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
-@Configuration
-public class GenericVnfsControllerTest {
-
-    @LocalServerPort
-    private int port;
+public class GenericVnfsControllerTest extends AbstractSpringBootTest {
 
     @Autowired
-    private TestRestTemplate restTemplate;
+    private CustomerCacheServiceProvider customerCacheServiceProvider;
 
     @Autowired
-    private UserCredentials userCredentials;
+    private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
 
     @Autowired
-    private CustomerCacheServiceProvider customerCacheServiceProvider;
+    private LinesOfBusinessCacheServiceProvider linesOfBusinessCacheServiceProvider;
 
     @Autowired
-    private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
+    private PlatformCacheServiceProvider platformVnfCacheServiceProvider;
 
     @After
     public void after() {
         customerCacheServiceProvider.clearAll();
         genericVnfCacheServiceProvider.clearAll();
+        platformVnfCacheServiceProvider.clearAll();
+        linesOfBusinessCacheServiceProvider.clearAll();
     }
 
     @Test
     public void test_putGenericVnf_successfullyAddedToCache() throws Exception {
 
         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
-        final ResponseEntity<Void> genericVnfResponse = invokeHttpPut(genericVnfUrl, getGenericVnf());
+        final ResponseEntity<Void> genericVnfResponse =
+                testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
 
-        final ResponseEntity<GenericVnf> response = invokeHttpGet(genericVnfUrl, GenericVnf.class);
+        final ResponseEntity<GenericVnf> response =
+                testRestTemplateService.invokeHttpGet(genericVnfUrl, GenericVnf.class);
         assertEquals(HttpStatus.OK, response.getStatusCode());
 
         assertTrue(response.hasBody());
@@ -119,20 +108,11 @@ public class GenericVnfsControllerTest {
     @Test
     public void test_putGenericVnfRelation_successfullyAddedToCache() throws Exception {
 
-        final ResponseEntity<Void> customerResponse = invokeHttpPut(getUrl(CUSTOMERS_URL), getCustomer());
-        assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
+        addCustomerServiceAndGenericVnf();
 
-        final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
-        final ResponseEntity<Void> serviceInstanceResponse = invokeHttpPut(serviceInstanceUrl, getServiceInstance());
-        assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
-
-        final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
-        final ResponseEntity<Void> genericVnfResponse = invokeHttpPut(genericVnfUrl, getGenericVnf());
-        assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
-
-        final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_URL);
-        final ResponseEntity<Void> genericVnfRelationShipResponse =
-                invokeHttpPut(genericVnfRelationShipUrl, getRelationShip());
+        final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
+        final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
+                .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getRelationShip(), Void.class);
 
         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
 
@@ -148,6 +128,8 @@ public class GenericVnfsControllerTest {
         assertFalse(actualRelationshipList.getRelationship().isEmpty());
         final Relationship actualRelationShip = actualRelationshipList.getRelationship().get(0);
 
+        assertEquals(Constants.COMPOSED_OF, actualRelationShip.getRelationshipLabel());
+
         assertFalse(actualRelationShip.getRelatedToProperty().isEmpty());
         assertFalse(actualRelationShip.getRelationshipData().isEmpty());
         final RelatedToProperty actualRelatedToProperty = actualRelationShip.getRelatedToProperty().get(0);
@@ -193,50 +175,155 @@ public class GenericVnfsControllerTest {
 
     }
 
-    private RelationshipData getRelationshipData(final List<RelationshipData> relationshipData, final String key) {
-        return relationshipData.stream().filter(data -> data.getRelationshipKey().equals(key)).findFirst().orElse(null);
-    }
+    @Test
+    public void test_putGenericVnfRelationToPlatform_successfullyAddedToCache() throws Exception {
+        addCustomerServiceAndGenericVnf();
 
-    private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
-        final HttpEntity<?> httpEntity = getHttpEntity(obj);
-        return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
-    }
+        final String platformUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME);
+        final ResponseEntity<Void> platformResponse =
+                testRestTemplateService.invokeHttpPut(platformUrl, TestUtils.getPlatform(), Void.class);
+        assertEquals(HttpStatus.ACCEPTED, platformResponse.getStatusCode());
 
-    private <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
-        return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), clazz);
-    }
+        final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
+        final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
+                .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getPlatformRelatedLink(), Void.class);
 
-    private HttpEntity<?> getHttpEntity(final Object obj) {
-        return new HttpEntity<>(obj, getHttpHeaders());
-    }
+        assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
+
+        final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
+        assertTrue(genericVnfOptional.isPresent());
+        final GenericVnf actualGenericVnf = genericVnfOptional.get();
+        final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
+        assertNotNull(relationshipList);
+        assertFalse(relationshipList.getRelationship().isEmpty());
+
+        final Relationship relationship = relationshipList.getRelationship().get(0);
+
+        assertEquals(Constants.USES, relationship.getRelationshipLabel());
+        assertFalse(relationship.getRelationshipData().isEmpty());
+        assertEquals(1, relationship.getRelationshipData().size());
+
+        final List<RelationshipData> relationshipData = relationship.getRelationshipData();
+
+        final RelationshipData platformRelationshipData =
+                getRelationshipData(relationshipData, Constants.PLATFORM_PLATFORM_NAME);
+        assertNotNull(platformRelationshipData);
+        assertEquals(PLATFORM_NAME, platformRelationshipData.getRelationshipValue());
 
-    private HttpHeaders getHttpHeaders() {
-        return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
     }
 
-    private String getUrl(final String... urls) {
-        final UriComponentsBuilder baseUri = UriComponentsBuilder.fromUriString("https://localhost:" + port);
-        for (final String url : urls) {
-            baseUri.path(url);
+    @Test
+    public void test_putGenericVnfRelationToLineOfBusiness_successfullyAddedToCache() throws Exception {
+        addCustomerServiceAndGenericVnf();
+
+        final String url = getUrl(Constants.LINES_OF_BUSINESS_URL, LINE_OF_BUSINESS_NAME);
+        final ResponseEntity<Void> responseEntity =
+                testRestTemplateService.invokeHttpPut(url, TestUtils.getLineOfBusiness(), Void.class);
+        assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
+
+        final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
+        final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
+                .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getLineOfBusinessRelatedLink(), Void.class);
+
+        assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
+
+        final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
+        assertTrue(genericVnfOptional.isPresent());
+        final GenericVnf actualGenericVnf = genericVnfOptional.get();
+        final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
+        assertNotNull(relationshipList);
+        assertFalse(relationshipList.getRelationship().isEmpty());
+
+        final Relationship relationship = relationshipList.getRelationship().get(0);
+
+        assertEquals(Constants.USES, relationship.getRelationshipLabel());
+        assertFalse(relationship.getRelationshipData().isEmpty());
+        assertEquals(1, relationship.getRelationshipData().size());
+
+        final List<RelationshipData> relationshipData = relationship.getRelationshipData();
+
+        final RelationshipData lineOfBusinessRelationshipData =
+                getRelationshipData(relationshipData, Constants.LINE_OF_BUSINESS_LINE_OF_BUSINESS_NAME);
+        assertNotNull(lineOfBusinessRelationshipData);
+        assertEquals(LINE_OF_BUSINESS_NAME, lineOfBusinessRelationshipData.getRelationshipValue());
 
-        }
-        return baseUri.toUriString();
     }
 
-    private String getCustomer() throws IOException {
-        return getJsonString("test-data/business-customer.json");
+    @Test
+    public void test_putGenericVnfRelationToCloudRegion_successfullyAddedToCache() throws Exception {
+        addCustomerServiceAndGenericVnf();
+
+        final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
+
+        final ResponseEntity<Void> responseEntity =
+                testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
+        assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
+
+        final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
+        final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
+                .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getCloudRegionRelatedLink(), Void.class);
+
+        assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
+
+        final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
+        assertTrue(genericVnfOptional.isPresent());
+        final GenericVnf actualGenericVnf = genericVnfOptional.get();
+        final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
+        assertNotNull(relationshipList);
+        assertFalse(relationshipList.getRelationship().isEmpty());
+
+        final Relationship relationship = relationshipList.getRelationship().get(0);
+
+        assertEquals(Constants.LOCATED_IN, relationship.getRelationshipLabel());
+        assertFalse(relationship.getRelationshipData().isEmpty());
+        assertEquals(2, relationship.getRelationshipData().size());
+
+        final List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
+
+        final RelationshipData cloudOwnerRelationshipData =
+                getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_OWNER);
+        assertNotNull(cloudOwnerRelationshipData);
+        assertEquals(CLOUD_OWNER_NAME, cloudOwnerRelationshipData.getRelationshipValue());
+
+        final RelationshipData cloudRegionIdRelationshipData =
+                getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_REGION_ID);
+        assertNotNull(cloudRegionIdRelationshipData);
+        assertEquals(CLOUD_REGION_NAME, cloudRegionIdRelationshipData.getRelationshipValue());
+
+        final List<RelatedToProperty> relatedToPropertyList = relationship.getRelatedToProperty();
+
+        final RelatedToProperty cloudRegionOwnerDefinedTypeProperty =
+                getRelatedToProperty(relatedToPropertyList, Constants.CLOUD_REGION_OWNER_DEFINED_TYPE);
+        assertNotNull(cloudRegionOwnerDefinedTypeProperty);
+        assertEquals("OwnerType", cloudRegionOwnerDefinedTypeProperty.getPropertyValue());
+
     }
 
-    private String getServiceInstance() throws IOException {
-        return getJsonString("test-data/service-instance.json");
+    private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
+        final ResponseEntity<Void> 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<Void> serviceInstanceResponse =
+                testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
+        assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
+
+        final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
+        final ResponseEntity<Void> genericVnfResponse =
+                testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
+        assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
+
     }
 
-    private String getGenericVnf() throws IOException {
-        return getJsonString("test-data/generic-vnf.json");
+    private RelationshipData getRelationshipData(final List<RelationshipData> relationshipData, final String key) {
+        return relationshipData.stream().filter(data -> data.getRelationshipKey().equals(key)).findFirst().orElse(null);
     }
 
-    private String getRelationShip() throws IOException {
-        return getJsonString("test-data/relation-ship.json");
+    private RelatedToProperty getRelatedToProperty(final List<RelatedToProperty> relatedToPropertyList,
+            final String key) {
+        return relatedToPropertyList.stream().filter(data -> data.getPropertyKey().equals(key)).findFirst()
+                .orElse(null);
     }
 
 }