summaryrefslogtreecommitdiffstats
path: root/vid-app-common
diff options
context:
space:
mode:
authorIttay Stern <ittay.stern@att.com>2020-02-20 09:45:11 +0200
committerIttay Stern <ittay.stern@att.com>2020-02-20 09:45:11 +0200
commita5ba6b59e4fc84b6aeae456c1860fcf7517e99cf (patch)
treedbdbb4ecc348dea544ea8b2a041015de64d3c838 /vid-app-common
parent22f7a0c20eb189380411df2e99928f701cc8a53f (diff)
Learn to get all model versions sorted from high to low
Issue-ID: VID-771 Change-Id: I9c7cc3f319aa13a2dab33e8c584f7f7ed90cf847 Signed-off-by: Ittay Stern <ittay.stern@att.com>
Diffstat (limited to 'vid-app-common')
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/aai/AaiClient.java25
-rw-r--r--vid-app-common/src/main/java/org/onap/vid/aai/AaiClientInterface.java2
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/aai/AaiClientTest.java37
3 files changed, 49 insertions, 15 deletions
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 78164462c..e25fa8574 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
@@ -44,6 +44,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
+import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
@@ -490,6 +491,15 @@ public class AaiClient implements AaiClientInterface {
@Override
public ModelVer getLatestVersionByInvariantId(String modelInvariantId) {
+ return maxModelVer(getAllVersionsByInvariantId(modelInvariantId));
+ }
+
+ @Override
+ public List<ModelVer> getSortedVersionsByInvariantId(String modelInvariantId) {
+ return sortedModelVer(getAllVersionsByInvariantId(modelInvariantId));
+ }
+
+ private Stream<ModelVer> getAllVersionsByInvariantId(String modelInvariantId) {
if (modelInvariantId.isEmpty()) {
throw new GenericUncheckedException("no invariant-id provided to getLatestVersionByInvariantId; request is rejected");
}
@@ -497,17 +507,13 @@ public class AaiClient implements AaiClientInterface {
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);
+ return toModelVerStream(aaiResponse.getT());
}
protected Stream<ModelVer> toModelVerStream(ModelVersions modelVersions) {
if (modelVersions == null)
return null;
- if (modelVersions == null)
- return null;
-
return Stream.of(modelVersions)
.map(ModelVersions::getResults)
.flatMap(java.util.Collection::stream)
@@ -527,6 +533,15 @@ public class AaiClient implements AaiClientInterface {
.orElseThrow(() -> new GenericUncheckedException("Could not find any version"));
}
+ protected List<ModelVer> sortedModelVer(Stream<ModelVer> modelVerStream) {
+ if (modelVerStream == null)
+ return emptyList();
+
+ return modelVerStream
+ .sorted(comparing(ModelVer::getModelVersion, comparing(DefaultArtifactVersion::new)).reversed())
+ .collect(Collectors.toList());
+ }
+
@Override
public AaiResponse<Services> getSubscriberData(String subscriberId, boolean omitServiceInstances) {
String depth = omitServiceInstances ? "1" : "2";
diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/AaiClientInterface.java b/vid-app-common/src/main/java/org/onap/vid/aai/AaiClientInterface.java
index b4b908cc3..c322afa22 100644
--- a/vid-app-common/src/main/java/org/onap/vid/aai/AaiClientInterface.java
+++ b/vid-app-common/src/main/java/org/onap/vid/aai/AaiClientInterface.java
@@ -77,6 +77,8 @@ public interface AaiClientInterface extends ProbeInterface {
ModelVer getLatestVersionByInvariantId(String modelInvariantId);
+ List<ModelVer> getSortedVersionsByInvariantId(String modelInvariantId);
+
AaiResponse<ProjectResponse> getServicesByProjectNames(List<String> projectNames);
AaiResponse getServiceModelsByDistributionStatus();
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 cfebe1a85..1a75c555c 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
@@ -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;
@@ -71,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;
@@ -799,19 +801,34 @@ public class AaiClientTest {
@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);
+ return mv;
+ });
+
+ final AaiClient aaiClient = new AaiClient(null, null, null);
+
+ 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);
@@ -820,7 +837,7 @@ public class AaiClientTest {
final AaiClient aaiClient = new AaiClient(null, null, null);
- assertThat(aaiClient.maxModelVer(modelVerStream), hasProperty("modelVersion", is(expected)));
+ assertThat(aaiClient.maxModelVer(modelVerStream), hasProperty("modelVersion", is(expectedMax)));
}
@Test(expectedExceptions = GenericUncheckedException.class)