Merge "VNF's LCP regions found by Line-of-business (and owning-entity)"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / AaiClientTest.java
index 5c37bb1..1a75c55 100644 (file)
@@ -23,6 +23,7 @@ package org.onap.vid.aai;
 import static java.util.stream.Collectors.toList;
 import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.Matchers.containsString;
 import static org.hamcrest.Matchers.either;
@@ -56,7 +57,6 @@ import java.net.URI;
 import java.security.cert.CertificateException;
 import java.util.ArrayList;
 import java.util.Map;
-import java.util.Optional;
 import java.util.function.BiConsumer;
 import java.util.function.Function;
 import java.util.stream.Stream;
@@ -72,6 +72,7 @@ import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.commons.lang3.reflect.FieldUtils;
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.http.HttpStatus;
+import org.hamcrest.Matcher;
 import org.mockito.Mockito;
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 import org.onap.portalsdk.core.util.SystemProperties;
@@ -97,6 +98,7 @@ import org.onap.vid.model.probes.ExternalComponentStatus;
 import org.onap.vid.model.probes.HttpRequestMetadata;
 import org.onap.vid.model.probes.StatusMetadata;
 import org.onap.vid.testUtils.TestUtils;
+import org.onap.vid.utils.Logging;
 import org.onap.vid.utils.Unchecked;
 import org.springframework.http.HttpMethod;
 import org.springframework.test.context.ContextConfiguration;
@@ -137,6 +139,43 @@ public class AaiClientTest {
         };
     }
 
+      private static String nfRoleOnly = "{\"start\":[\"/business/customers/customer/globalCustomerId1-360-as988q/service-subscriptions/service-subscription/TEST1-360/service-instances\"],\"query\":\"query/vnfs-fromServiceInstance-filter?nfRole=test360\"}";
+    private static String nfRoleAndCloudRegion = "{\"start\":[\"/business/customers/customer/globalCustomerId1-360-as988q/service-subscriptions/service-subscription/TEST1-360/service-instances\"],\"query\":\"query/vnfs-fromServiceInstance-filterByCloudRegion?nfRole=test360&cloudRegionID=cloudRegion-1\"}";
+    private static String cloudRegionOnly = "{\"start\":[\"/business/customers/customer/globalCustomerId1-360-as988q/service-subscriptions/service-subscription/TEST1-360/service-instances\"],\"query\":\"query/vnfs-fromServiceInstance-filterByCloudRegion?cloudRegionID=cloudRegion-1\"}";
+    private static String withoutNfroleAndCloudRegion = "{\"start\":[\"/business/customers/customer/globalCustomerId1-360-as988q/service-subscriptions/service-subscription/TEST1-360/service-instances\"],\"query\":\"query/vnfs-fromServiceInstance-filter\"}";
+    private static String withoutNfroleAndCloudRegionWithSpace = "{\"start\":[\"/business/customers/customer/globalCustomerId1with%20space%20360-as988q/service-subscriptions/service-subscription/TEST1%20360/service-instances\"],\"query\":\"query/vnfs-fromServiceInstance-filter\"}";
+
+    private static String responseJsonNfRole = "/payload_jsons/changeManagement/vnfs-fromServiceInstance-filterNfRole.json";
+    private static String responseJsonCloudRegion ="/payload_jsons/changeManagement/vnfs-fromServiceInstance-filterByCloudRegion.json";
+
+
+    @DataProvider
+    public static Object[][] aaiPutCustomQueryData() {
+        return new Object[][] {
+            {"globalCustomerId1-360-as988q", "TEST1-360", "test360", null, nfRoleOnly, responseJsonNfRole, "908419144", 200},
+            {"globalCustomerId1-360-as988q", "TEST1-360", null, "cloudRegion-1", cloudRegionOnly, responseJsonCloudRegion, "1165906024", 200},
+            {"globalCustomerId1-360-as988q", "TEST1-360", "test360", "cloudRegion-1", nfRoleAndCloudRegion,
+                responseJsonCloudRegion, "1165906024", 200},
+            {"globalCustomerId1with space 360-as988q", "TEST1 360", null, null, withoutNfroleAndCloudRegionWithSpace, responseJsonNfRole, "908419144", 200},
+                {"globalCustomerId1-360-as988q", "TEST1-360", null, null, withoutNfroleAndCloudRegion, responseJsonNfRole, "908419144", 200},
+        };
+    }
+
+    @Test(dataProvider = "aaiPutCustomQueryData")
+    public void testAaiPutCustomQueryByParams(String globalCustomerId, String serviceType, String nfRole, String cloudRegion, String expectedPayload, String responseBody, String expectedId, int responseHttpCode) {
+        String queryFormat = "query?format=simple";
+        final ResponseWithRequestInfo mockedResponseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK,
+            TestUtils.readFileAsString(responseBody),
+            "query?format=simple&Mock=True",
+            HttpMethod.PUT);
+        when(aaiClientMock.doAaiPut(eq(queryFormat), anyString(), anyBoolean(), anyBoolean())).thenReturn(mockedResponseWithRequestInfo);
+        when(aaiClientMock.getVnfsByParamsForChangeManagement(anyString(), anyString(), nullable(String.class), nullable(String.class))).thenCallRealMethod();
+        AaiResponse<AaiGetVnfResponse> response = aaiClientMock.getVnfsByParamsForChangeManagement(globalCustomerId, serviceType, nfRole, cloudRegion);
+        verify(aaiClientMock).doAaiPut(eq(queryFormat), eq(expectedPayload), eq(false), eq(false));
+        assertEquals(response.getHttpCode(), responseHttpCode);
+        assertEquals(response.getT().getResults().get(0).id, expectedId);
+    }
+
     @Test(dataProvider = "logicalLinkData")
     public void getLogicalLink_Link_Is_Empty(String link, String expectedUrl) {
 
@@ -518,7 +557,7 @@ public class AaiClientTest {
 
     }
     @Test(expectedExceptions = GenericUncheckedException.class, expectedExceptionsMessageRegExp = "A&AI has no homing data associated to vfModule 'vfModuleId' of vnf 'vnfInstanceId'")
-    public void getVfMoudule_Homing_Arguments_Are_Valid_But_Not_Exists() {
+    public void getVfModule_Homing_Arguments_Are_Valid_But_Not_Exists() {
         when(aaiClientMock.getHomingDataByVfModule(any(String.class), any(String.class))).thenCallRealMethod();
 
         Response generalEmptyResponse = mock(Response.class);
@@ -536,7 +575,7 @@ public class AaiClientTest {
     }
 
     @Test(dataProvider = "invalidDataId", expectedExceptions = GenericUncheckedException.class, expectedExceptionsMessageRegExp = "Failed to retrieve homing data associated to vfModule from A&AI, VNF InstanceId or VF Module Id is missing.")
-    public void getVfMoudule_Homing_Arguments_Are_Empty_Or_Null(String data) {
+    public void getVfModule_Homing_Arguments_Are_Empty_Or_Null(String data) {
         when(aaiClientMock.getHomingDataByVfModule(any(), any())).thenCallRealMethod();
              aaiClientMock.getHomingDataByVfModule(data, data);
     }
@@ -649,7 +688,10 @@ public class AaiClientTest {
         Response responseMock = mocks.getFakeResponse();
 
         // prepare real AAIRestInterface and AaiClient, and wire mocks
-        AAIRestInterface aaiRestInterface = new AAIRestInterface(httpsAuthClientMock, mock(ServletRequestHelper.class), mock(SystemPropertyHelper.class));
+        AAIRestInterface aaiRestInterface = new AAIRestInterface(httpsAuthClientMock,
+            mock(ServletRequestHelper.class),
+            mock(SystemPropertyHelper.class),
+            mock(Logging.class));
         final AaiClient aaiClient = new AaiClient(aaiRestInterface, null, null);
         when(httpsAuthClientMock.getClient(any())).thenReturn(javaxClientMock);
 
@@ -756,67 +798,21 @@ public class AaiClientTest {
         };
     }
 
-    @Test
-    public void testGetLatestVersionByInvariantId() throws IOException {
-
-        ModelVersions modelVersions = JACKSON_OBJECT_MAPPER.readValue("" +
-                "{\n" +
-                "    \"results\": [\n" +
-                "        {\n" +
-                "            \"model\": {\n" +
-                "                \"model-invariant-id\": \"f6342be5-d66b-4d03-a1aa-c82c3094c4ea\",\n" +
-                "                \"model-type\": \"service\",\n" +
-                "                \"resource-version\": \"1534274421300\"\n" +
-                "            }\n" +
-                "        },\n" +
-                "        {\n" +
-                "            \"model-ver\": {\n" +
-                "                \"model-version-id\": \"a92f899d-a3ec-465b-baed-1663b0a5aee1\",\n" +
-                "                \"model-name\": \"NCM_VLAN_SVC_ym161f\",\n" +
-                "                \"model-version\": \"bbb\",\n" +
-                "                \"distribution-status\": \"DISTRIBUTION_COMPLETE_OK\",\n" +
-                "                \"model-description\": \"Network Collection service for vLAN tagging\",\n" +
-                "                \"resource-version\": \"1534788756086\"\n" +
-                "            }\n" +
-                "        },\n" +
-                "        {\n" +
-                "            \"model-ver\": {\n" +
-                "                \"model-version-id\": \"d2fda667-e92e-4cfa-9620-5da5de01a319\",\n" +
-                "                \"model-name\": \"NCM_VLAN_SVC_ym161f\",\n" +
-                "                \"model-version\": \"aaa\",\n" +
-                "                \"distribution-status\": \"DISTRIBUTION_COMPLETE_OK\",\n" +
-                "                \"model-description\": \"Network Collection service for vLAN tagging\",\n" +
-                "                \"resource-version\": \"1534444087221\"\n" +
-                "            }\n" +
-                "        }]}", ModelVersions.class);
-
-
-        final AaiClient aaiClient = new AaiClient(null, null, null);
-
-        assertThat(aaiClient.toModelVerStream(modelVersions).collect(toList()),
-                containsInAnyOrder(
-                        hasProperty("modelVersionId", is("a92f899d-a3ec-465b-baed-1663b0a5aee1")),
-                        hasProperty("modelVersionId", is("d2fda667-e92e-4cfa-9620-5da5de01a319"))
-                ));
-
-    }
-
     @DataProvider
     public static Object[][]  versionsDataProvider() {
         return new Object[][] {
-                { Stream.of("10","20","30"), "30" },
-                { Stream.of("10","20","20"), "20" },
-                { Stream.of("c","b","a"), "c" },
-                { Stream.of("1.0","2.0","1.8"), "2.0" },
-                { Stream.of("1.0.7","2.0.2","2.0.9"), "2.0.9" },
-                { Stream.of("0","0","0"), "0" },
-                { Stream.of("","10"), "10" },
-
+                { Stream.of("20","10","30"), Stream.of("30","20","10"), "30" },
+                { Stream.of("10","20","20"), Stream.of("20","20","10"), "20" },
+                { Stream.of("c","b","a"), Stream.of("c","b","a"), "c" },
+                { Stream.of("1.0","2.0","1.8"), Stream.of("2.0","1.8","1.0"), "2.0" },
+                { Stream.of("1.0.7","2.0.9","2.0.2"), Stream.of("2.0.9","2.0.2","1.0.7"), "2.0.9" },
+                { Stream.of("0","0","0"), Stream.of("0","0","0"), "0" },
+                { Stream.of("","10"), Stream.of("10",""), "10" },
         };
     }
 
     @Test(dataProvider = "versionsDataProvider")
-    public void maxModelVer(Stream<String> input, String expected) {
+    public void sortedModelVer(Stream<String> input, Stream<String> expectedSorted, String expectedMax) {
         Stream<ModelVer> modelVerStream = input.map(version -> {
             ModelVer mv = new ModelVer();
             mv.setModelVersion(version);
@@ -825,7 +821,23 @@ public class AaiClientTest {
 
         final AaiClient aaiClient = new AaiClient(null, null, null);
 
-        assertThat(aaiClient.maxModelVer(modelVerStream), hasProperty("modelVersion", is(expected)));
+        assertThat(aaiClient.sortedModelVer(modelVerStream),
+            contains(
+                expectedSorted.map(it -> hasProperty("modelVersion", is(it))).toArray(Matcher[]::new)
+            ));
+    }
+
+    @Test(dataProvider = "versionsDataProvider")
+    public void maxModelVer(Stream<String> input, Stream<String> expectedSorted, String expectedMax) {
+        Stream<ModelVer> modelVerStream = input.map(version -> {
+            ModelVer mv = new ModelVer();
+            mv.setModelVersion(version);
+            return mv;
+        });
+
+        final AaiClient aaiClient = new AaiClient(null, null, null);
+
+        assertThat(aaiClient.maxModelVer(modelVerStream), hasProperty("modelVersion", is(expectedMax)));
     }
 
     @Test(expectedExceptions = GenericUncheckedException.class)
@@ -984,5 +996,4 @@ public class AaiClientTest {
             ));
 
     }
-
 }