Respect owning-entity-id when searching instances by Subscriber
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AaiServiceTest.java
index 6aa6705..12774f4 100644 (file)
 
 package org.onap.vid.services;
 
+import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.arrayWithSize;
 import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.collect.ImmutableMap;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import org.mockito.InjectMocks;
@@ -38,6 +45,7 @@ import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.onap.vid.aai.AaiClientInterface;
 import org.onap.vid.aai.AaiResponse;
+import org.onap.vid.aai.ServiceInstance;
 import org.onap.vid.aai.model.AaiGetPnfResponse;
 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
@@ -47,8 +55,8 @@ import org.onap.vid.aai.model.RelationshipData;
 import org.onap.vid.aai.model.RelationshipList;
 import org.onap.vid.aai.model.ServiceRelationships;
 import org.onap.vid.model.aaiTree.AAITreeNode;
-import org.onap.vid.roles.Role;
 import org.onap.vid.roles.RoleValidator;
+import org.onap.vid.roles.RoleValidatorFactory;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
@@ -61,6 +69,9 @@ public class AaiServiceTest {
     @Mock
     private AaiClientInterface aaiClientInterface;
 
+    @Mock
+    private RoleValidatorFactory roleValidatorFactory;
+
     @BeforeMethod
     public void initMocks(){
         MockitoAnnotations.initMocks(this);
@@ -68,7 +79,7 @@ public class AaiServiceTest {
 
     @Test
     public void testGetSpecificPnf(){
-        Pnf pnf = new Pnf("11111", null, null, null, null, null, null);
+        Pnf pnf = Pnf.builder().withPnfId("11111").build();
         AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "aaaa", 200);
         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getSpecificPnf(Mockito.anyString());
         AaiResponse<Pnf> specificPnf = aaiService.getSpecificPnf("1345667");
@@ -150,8 +161,6 @@ public class AaiServiceTest {
     public static Object[][] getTenantsData() {
         return new Object[][] {
                 {"customer1", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", true},
-                {"customer1", "serviceType1", "TeNant1", "customer1", "serviceType1", "tenant1", "id-1", true},
-                {"customer1", "serviceType1", "TENANT1", "customer1", "serviceType1", "tenant1", "id-1", true},
                 {"customer1", "serviceType1", "tenant2", "customer1", "serviceType1", "tenant1", "tenant2", false},
                 {"customer1", "serviceType1", null, "customer1", "serviceType1", "tenant1", "tenant2", true},
                 {"customer2", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false},
@@ -162,14 +171,20 @@ public class AaiServiceTest {
     }
 
     @Test(dataProvider = "getTenantsData")
-    public void testGetTenants(String userGlobalCustomerId, String userServiceType, String userTenantName, String serviceGlobalCustomerId,
-                               String serviceServiceType, String serviceTenantName, String serviceTenantId, boolean expectedIsPermitted) {
+    public void testGetTenants(String userGlobalCustomerId, String userServiceType, String userTenantName,
+                                String serviceGlobalCustomerId, String serviceServiceType, String serviceTenantName,
+                                String serviceTenantId, boolean expectedIsPermitted) {
         GetTenantsResponse[] getTenantsResponses = new GetTenantsResponse[] {new GetTenantsResponse(null, null, serviceTenantName, serviceTenantId, false)};
         AaiResponse<GetTenantsResponse[]> aaiResponse = new AaiResponse<>(getTenantsResponses, null, 200);
         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getTenants(serviceGlobalCustomerId, serviceServiceType);
-        Role role = new Role(null, userGlobalCustomerId, userServiceType, userTenantName);
-        RoleValidator roleValidator = RoleValidator.by(Collections.singletonList(role), false);
-        AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidator);
+
+        RoleValidator roleValidatorMock = mock(RoleValidator.class);
+        when(roleValidatorMock.isTenantPermitted(
+            eq(userGlobalCustomerId), eq(userServiceType),
+            (userTenantName == null) ? anyString() : eq(userTenantName))
+        ).thenReturn(true);
+
+        AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidatorMock);
 
         assertThat(actualTenants.getT(), arrayWithSize(1));
         assertThat(actualTenants.getT()[0].tenantName, equalTo(serviceTenantName));
@@ -206,4 +221,59 @@ public class AaiServiceTest {
         assertThat(anyMatch, equalTo(expectedMatch));
     }
 
+    @DataProvider
+    public static Object[][] dataToDestroy() {
+        return new Object[][]{
+            {"nothing"}, {"relationship-list"}, {"relationship"}, {"relationship-data"} ,{"owning-entity-id"}
+        };
+    }
+
+
+    @Test(dataProvider = "dataToDestroy")
+    public void relatedOwningEntityId_givenInstanceAndOptionalError_extractCorrectlyOrReturnNull(String dataToDestroy) throws JsonProcessingException {
+        ServiceInstance serviceInstance = new ObjectMapper().readValue((""
+            + "{ "
+            + "  \"service-instance-id\": \"5d521981-33be-4bb5-bb20-5616a9c52a5a\", "
+            + "  \"service-instance-name\": \"dfgh\", "
+            + "  \"service-type\": \"\", "
+            + "  \"service-role\": \"\", "
+            + "  \"environment-context\": \"null\", "
+            + "  \"workload-context\": \"null\", "
+            + "  \"model-invariant-id\": \"331a194d-9248-4533-88bc-62c812ccb5c1\", "
+            + "  \"model-version-id\": \"171b3887-e73e-479d-8ef8-2690bf74f2aa\", "
+            + "  \"resource-version\": \"1508832105498\", "
+            + "  \"orchestration-status\": \"Active\", "
+            + "  \"relationship-list\": { "
+            + "    \"relationship\": [ "
+            + "      { "
+            + "        \"related-to\": \"project\", "
+            + "        \"related-link\": \"/aai/v11/business/projects/project/Kennedy\", "
+            + "        \"relationship-data\": [ "
+            + "          { "
+            + "            \"relationship-key\": \"project.project-name\", "
+            + "            \"relationship-value\": \"Kennedy\" "
+            + "          } "
+            + "        ] "
+            + "      }, "
+            + "      { "
+            + "        \"related-to\": \"owning-entity\", "
+            + "        \"related-link\": \"/aai/v11/business/owning-entities/owning-entity/4d4ecf59-41f1-40d4-818d-885234680a42\", "
+            + "        \"relationship-data\": [ "
+            + "          { "
+            + "            \"relationship-key\": \"owning-entity.owning-entity-id\", "
+            + "            \"relationship-value\": \"4d4ecf59-41f1-40d4-818d-885234680a42\" "
+            + "          } "
+            + "        ] "
+            + "      } "
+            + "    ] "
+            + "  } "
+            + "}").replace(dataToDestroy, "omitted"), ServiceInstance.class);
+
+        if (dataToDestroy.equals("nothing")) {
+            assertThat(aaiService.relatedOwningEntityId(serviceInstance), is("4d4ecf59-41f1-40d4-818d-885234680a42"));
+        } else {
+            assertThat(aaiService.relatedOwningEntityId(serviceInstance), is(nullValue()));
+        }
+    }
+
 }