Fixing project and owning entity relationship endpoints
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / OwningEntityControllerTest.java
index a443a45..f4fcade 100644 (file)
@@ -23,77 +23,63 @@ 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.TestConstants.RELATIONSHIP_URL;
-import static org.onap.so.aaisimulator.utils.TestUtils.getFile;
-import static org.onap.so.aaisimulator.utils.TestUtils.getHttpHeaders;
-import static org.onap.so.aaisimulator.utils.TestUtils.getJsonString;
+import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
+import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
+import static org.onap.so.aaisimulator.utils.TestConstants.GLOBAL_CUSTOMER_ID;
+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_SUBSCRIPTIONS_URL;
+import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_TYPE;
 import java.io.IOException;
-import java.nio.file.Files;
+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.OwningEntity;
+import org.onap.aai.domain.yang.Relationship;
+import org.onap.aai.domain.yang.RelationshipData;
+import org.onap.aai.domain.yang.ServiceInstance;
 import org.onap.so.aaisimulator.models.Format;
-import org.onap.so.aaisimulator.models.Result;
+import org.onap.so.aaisimulator.models.Results;
+import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
 import org.onap.so.aaisimulator.service.providers.OwnEntityCacheServiceProvider;
 import org.onap.so.aaisimulator.utils.Constants;
 import org.onap.so.aaisimulator.utils.TestUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-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.HttpMethod;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
-import org.springframework.test.context.ActiveProfiles;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
 /**
  * @author waqas.ikram@ericsson.com
  *
  */
-@RunWith(SpringJUnit4ClassRunner.class)
-@ActiveProfiles("test")
-@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
-@Configuration
-public class OwningEntityControllerTest {
-    private static final String OWNING_ENTITY_JSON_FILE = "test-data/owning-entity.json";
+public class OwningEntityControllerTest extends AbstractSpringBootTest {
 
     private static final String OWN_ENTITY_ID_VALUE = "oe_1";
     private static final String OWN_ENTITY_NAME_VALUE = "oe_2";
 
-    private static final String OWNING_ENTITY_RELATION_SHIP_JSON_FILE = "test-data/owning-entity-relation-ship.json";
-
-    @LocalServerPort
-    private int port;
-
     @Autowired
-    private TestRestTemplate restTemplate;
-
-    @Value("${spring.security.username}")
-    private String username;
+    private OwnEntityCacheServiceProvider cacheServiceProvider;
 
     @Autowired
-    private OwnEntityCacheServiceProvider cacheServiceProvider;
+    private CustomerCacheServiceProvider customerCacheServiceProvider;
 
     @After
     public void after() {
         cacheServiceProvider.clearAll();
+        customerCacheServiceProvider.clearAll();
     }
 
     @Test
     public void test_putOwningEntity_successfullyAddedToCache() throws Exception {
-        final String url = getOwningEntityEndPointUrl() + "/" + OWN_ENTITY_ID_VALUE;
-        final String body = new String(Files.readAllBytes(getFile(OWNING_ENTITY_JSON_FILE).toPath()));
-        final ResponseEntity<Void> actual = invokeHttpPut(url, body);
+        final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE);
+        final ResponseEntity<Void> actual =
+                testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class);
 
         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
 
-        final ResponseEntity<OwningEntity> actualResponse = invokeHttpGet(url, OwningEntity.class);
+        final ResponseEntity<OwningEntity> actualResponse =
+                testRestTemplateService.invokeHttpGet(url, OwningEntity.class);
 
         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
         assertTrue(actualResponse.hasBody());
@@ -106,18 +92,18 @@ public class OwningEntityControllerTest {
 
     @Test
     public void test_getOwningEntityCount_correctResult() throws Exception {
-        final String url = getOwningEntityEndPointUrl() + "/" + OWN_ENTITY_ID_VALUE;
-        final String body = new String(Files.readAllBytes(getFile(OWNING_ENTITY_JSON_FILE).toPath()));
-        final ResponseEntity<Void> actual = invokeHttpPut(url, body);
+        final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE);
+        final ResponseEntity<Void> actual =
+                testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class);
 
         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
 
-        final ResponseEntity<Result> actualResponse =
-                invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=" + Format.COUNT.getValue(), Result.class);
+        final ResponseEntity<Results> actualResponse = testRestTemplateService
+                .invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=" + Format.COUNT.getValue(), Results.class);
 
         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
         assertTrue(actualResponse.hasBody());
-        final Result result = actualResponse.getBody();
+        final Results result = actualResponse.getBody();
         assertNotNull(result.getValues());
         assertFalse(result.getValues().isEmpty());
         assertEquals(1, result.getValues().get(0).get(Constants.OWNING_ENTITY));
@@ -125,17 +111,22 @@ public class OwningEntityControllerTest {
 
     @Test
     public void test_putOwningEntityRelationShip_successfullyAddedToCache() throws Exception {
-        final String url = getOwningEntityEndPointUrl() + "/" + OWN_ENTITY_ID_VALUE;
-        final ResponseEntity<Void> actual = invokeHttpPut(url, getJsonString(OWNING_ENTITY_JSON_FILE));
+        addCustomerAndServiceInstance();
+
+        final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE);
+        final ResponseEntity<Void> actual =
+                testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class);
         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
 
-        final String owningEntityRelationshipUrl = url + RELATIONSHIP_URL;
+        final String owningEntityRelationshipUrl = url + RELATIONSHIP_LIST_RELATIONSHIP_URL;
 
-        final ResponseEntity<Void> putResponse = invokeHttpPut(owningEntityRelationshipUrl, getRelationship());
+        final ResponseEntity<Void> putResponse = testRestTemplateService.invokeHttpPut(owningEntityRelationshipUrl,
+                TestUtils.getOwningEntityRelationship(), Void.class);
 
         assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode());
 
-        final ResponseEntity<OwningEntity> actualResponse = invokeHttpGet(url, OwningEntity.class);
+        final ResponseEntity<OwningEntity> actualResponse =
+                testRestTemplateService.invokeHttpGet(url, OwningEntity.class);
 
         assertEquals(HttpStatus.OK, actualResponse.getStatusCode());
         assertTrue(actualResponse.hasBody());
@@ -146,27 +137,62 @@ public class OwningEntityControllerTest {
         assertFalse(actualOwningEntity.getRelationshipList().getRelationship().isEmpty());
         assertNotNull(actualOwningEntity.getRelationshipList().getRelationship().get(0));
 
-    }
+        final Relationship actualRelationship = actualOwningEntity.getRelationshipList().getRelationship().get(0);
+        final List<RelationshipData> relationshipDataList = actualRelationship.getRelationshipData();
+        assertEquals(Constants.BELONGS_TO, actualRelationship.getRelationshipLabel());
+        assertFalse(relationshipDataList.isEmpty());
+        assertEquals(3, relationshipDataList.size());
+
+        final RelationshipData globalRelationshipData =
+                getRelationshipData(relationshipDataList, Constants.CUSTOMER_GLOBAL_CUSTOMER_ID);
+        assertNotNull(globalRelationshipData);
+        assertEquals(GLOBAL_CUSTOMER_ID, globalRelationshipData.getRelationshipValue());
+
+        final RelationshipData serviceSubscriptionRelationshipData =
+                getRelationshipData(relationshipDataList, Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE);
+        assertNotNull(serviceSubscriptionRelationshipData);
+        assertEquals(SERVICE_TYPE, serviceSubscriptionRelationshipData.getRelationshipValue());
+
+        final RelationshipData serviceInstanceRelationshipData =
+                getRelationshipData(relationshipDataList, Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID);
+        assertNotNull(serviceInstanceRelationshipData);
+        assertEquals(SERVICE_INSTANCE_ID, serviceInstanceRelationshipData.getRelationshipValue());
+
+        final Optional<ServiceInstance> optional =
+                customerCacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
+        assertTrue(optional.isPresent());
+
+        final ServiceInstance serviceInstance = optional.get();
+
+        assertNotNull(serviceInstance.getRelationshipList());
+        final List<Relationship> serviceRelationshipList = serviceInstance.getRelationshipList().getRelationship();
+        assertFalse(serviceRelationshipList.isEmpty());
+        assertEquals(1, serviceRelationshipList.size());
+        final Relationship relationship = serviceRelationshipList.get(0);
+        assertEquals(Constants.BELONGS_TO, relationship.getRelationshipLabel());
+        assertEquals(Constants.OWNING_ENTITY_URL + OWN_ENTITY_ID_VALUE, relationship.getRelatedLink());
+
+        final List<RelationshipData> serviceRelationshipDataList = serviceRelationshipList.get(0).getRelationshipData();
+        assertFalse(serviceRelationshipDataList.isEmpty());
+        assertEquals(1, serviceRelationshipDataList.size());
+
+        final RelationshipData owningEntityRelationshipData =
+                getRelationshipData(serviceRelationshipDataList, Constants.OWNING_ENTITY_OWNING_ENTITY_ID);
+        assertNotNull(owningEntityRelationshipData);
+        assertEquals(OWN_ENTITY_ID_VALUE, owningEntityRelationshipData.getRelationshipValue());
 
-    private String getRelationship() throws IOException {
-        return TestUtils.getJsonString(OWNING_ENTITY_RELATION_SHIP_JSON_FILE);
     }
 
-    private <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
-        return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders(username)), clazz);
-    }
+    private void addCustomerAndServiceInstance() throws Exception, IOException {
+        final ResponseEntity<Void> customerResponse =
+                testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
+        assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
 
-    private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
-        final HttpEntity<?> httpEntity = getHttpEntity(obj);
-        return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
-    }
-
-    private HttpEntity<?> getHttpEntity(final Object obj) {
-        return new HttpEntity<>(obj, getHttpHeaders(username));
-    }
+        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());
 
-    private String getOwningEntityEndPointUrl() {
-        return TestUtils.getBaseUrl(port) + Constants.OWNING_ENTITY_URL;
     }
 
 }