fix url and payload for getting latest model

Issue-ID: VID-378
Signed-off-by: Eylon Malin <eylon.malin@intl.att.com>
Change-Id: I5b2dc7df0202838898ab060a71c144f69a54900f
diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/AaiClient.java b/vid-app-common/src/main/java/org/onap/vid/aai/AaiClient.java
index a0296d9..8e47bba 100644
--- a/vid-app-common/src/main/java/org/onap/vid/aai/AaiClient.java
+++ b/vid-app-common/src/main/java/org/onap/vid/aai/AaiClient.java
@@ -432,19 +432,18 @@
             throw new GenericUncheckedException("no invariant-id provided to getLatestVersionByInvariantId; request is rejected");
         }
 
-        // add the modelInvariantId to the payload
-        StringBuilder payload = new StringBuilder(GET_SERVICE_MODELS_REQUEST_BODY);
-        payload.insert(50, modelInvariantId);
-
-        Response response = doAaiPut("service-design-and-creation/models/model/", payload.toString(),false);
+        Response response = doAaiPut("query?format=resource&depth=0",  "{\"start\": [\"service-design-and-creation/models/model/" + modelInvariantId + "\"],\"query\": \"query/serviceModels-byDistributionStatus?distributionStatus=DISTRIBUTION_COMPLETE_OK\"}",false);
         AaiResponse<ModelVersions> aaiResponse = processAaiResponse(response, ModelVersions.class, null, VidObjectMapperType.FASTERXML);
+
         Stream<ModelVer> modelVerStream = toModelVerStream(aaiResponse.getT());
         return maxModelVer(modelVerStream);
-
     }
 
     protected Stream<ModelVer> toModelVerStream(ModelVersions modelVersions) {
 
+        if (modelVersions == null)
+            return null;
+
         return Stream.of(modelVersions)
                 .map(ModelVersions::getResults)
                 .flatMap(java.util.Collection::stream)
@@ -455,6 +454,10 @@
     }
 
     protected ModelVer maxModelVer(Stream<ModelVer> modelVerStream) {
+
+        if (modelVerStream == null)
+            return null;
+
         return modelVerStream
                 .filter(modelVer -> StringUtils.isNotEmpty(modelVer.getModelVersion()))
                 .max(comparing(ModelVer::getModelVersion, comparing(DefaultArtifactVersion::new)))
diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/AaiClientTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/AaiClientTest.java
index 9793862..5c37bb1 100644
--- a/vid-app-common/src/test/java/org/onap/vid/aai/AaiClientTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/aai/AaiClientTest.java
@@ -913,4 +913,76 @@
         void acceptThrows(T t, U u) throws Exception;
     }
 
+    @Test
+    public void getLatestVersionByInvariantId_verifyCallingExpectedApi(){
+
+        when(aaiClientMock.getLatestVersionByInvariantId(anyString())).thenCallRealMethod();
+
+        aaiClientMock.getLatestVersionByInvariantId("model-invariant-id");
+
+        Mockito.verify(aaiClientMock).doAaiPut(argThat(url -> url.endsWith("query?format=resource&depth=0")),argThat(payload -> payload.contains("service-design-and-creation/models/model/model-invariant-id")),anyBoolean());
+
+    }
+
+    @DataProvider
+    public static Object[][]  getSubscriberDataDataProvider() {
+        return new Object[][] {
+            { "Some-ID", true },
+            { "another id 123", false },
+        };
+    }
+
+    @Test(dataProvider = "getSubscriberDataDataProvider")
+    public void getSubscriberDataParams(String subscriberId, boolean omitServiceInstances) {
+        String depth = omitServiceInstances ? "1" : "2";
+        when(aaiClientMock.getSubscriberData(anyString(),anyBoolean())).thenCallRealMethod();
+        aaiClientMock.getSubscriberData(subscriberId, omitServiceInstances);
+        Mockito.verify(aaiClientMock).doAaiGet(argThat(s -> s.contains("customer/" + subscriberId + "?") && s.contains("depth=" + depth)),any(Boolean.class));
+    }
+
+    @Test
+    public void testToModelVerStream() 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"))
+            ));
+
+    }
+
 }