From d6eedb1f342ac32c8339b553848267e443410d57 Mon Sep 17 00:00:00 2001 From: Ittay Stern Date: Wed, 12 Feb 2020 12:28:59 +0200 Subject: Topology tree: extract AAITreeNodesEnricher out of AAIServiceTree Issue-ID: VID-771 Change-Id: I138a89b7b2f00e6e603ff26addefc494aeb019b0 Signed-off-by: Ittay Stern --- .../java/org/onap/vid/services/AAIServiceTree.java | 96 ++--------- .../onap/vid/services/AAITreeNodesEnricher.java | 117 +++++++++++++ .../vid/services/AAIServiceIntegrativeTest.java | 10 +- .../services/AAIServiceTreeIntegrativeTest.java | 14 +- .../org/onap/vid/services/AAIServiceTreeTest.java | 143 +--------------- .../vid/services/AAITreeNodesEnricherTest.java | 183 +++++++++++++++++++++ 6 files changed, 331 insertions(+), 232 deletions(-) create mode 100644 vid-app-common/src/main/java/org/onap/vid/services/AAITreeNodesEnricher.java create mode 100644 vid-app-common/src/test/java/org/onap/vid/services/AAITreeNodesEnricherTest.java (limited to 'vid-app-common/src') diff --git a/vid-app-common/src/main/java/org/onap/vid/services/AAIServiceTree.java b/vid-app-common/src/main/java/org/onap/vid/services/AAIServiceTree.java index 7b0e5f56f..579fd09cb 100644 --- a/vid-app-common/src/main/java/org/onap/vid/services/AAIServiceTree.java +++ b/vid-app-common/src/main/java/org/onap/vid/services/AAIServiceTree.java @@ -21,29 +21,18 @@ package org.onap.vid.services; import static java.util.Comparator.comparing; -import static java.util.stream.Collectors.toSet; import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; -import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER; -import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.collect.ImmutableList; -import java.util.Collection; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Objects; -import java.util.Set; import java.util.concurrent.ConcurrentSkipListSet; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.inject.Inject; -import javax.ws.rs.core.Response; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.onap.vid.aai.AaiClientInterface; import org.onap.vid.aai.util.AAITreeConverter; import org.onap.vid.asdc.AsdcCatalogException; -import org.onap.vid.asdc.parser.ServiceModelInflator; import org.onap.vid.exceptions.GenericUncheckedException; import org.onap.vid.model.ServiceModel; import org.onap.vid.model.aaiTree.AAITreeNode; @@ -57,15 +46,9 @@ import org.springframework.stereotype.Component; public class AAIServiceTree { private final AAITreeNodeBuilder aaiTreeNodeBuilder; - + private final AAITreeNodesEnricher aaiTreeNodesEnricher; private final AAITreeConverter aaiTreeConverter; - - private final AaiClientInterface aaiClient; - private final VidService sdcService; - - private final ServiceModelInflator serviceModelInflator; - private final ExecutorService executorService; private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AAIServiceTree.class); @@ -89,14 +72,17 @@ public class AAIServiceTree { } @Inject - public AAIServiceTree(AaiClientInterface aaiClient, AAITreeNodeBuilder aaiTreeNodeBuilder, - AAITreeConverter aaiTreeConverter, VidService sdcService, - ServiceModelInflator serviceModelInflator, ExecutorService executorService) { - this.aaiClient = aaiClient; + public AAIServiceTree( + AAITreeNodeBuilder aaiTreeNodeBuilder, + AAITreeNodesEnricher aaiTreeNodesEnricher, + AAITreeConverter aaiTreeConverter, + VidService sdcService, + ExecutorService executorService + ) { this.aaiTreeNodeBuilder = aaiTreeNodeBuilder; + this.aaiTreeNodesEnricher = aaiTreeNodesEnricher; this.aaiTreeConverter = aaiTreeConverter; this.sdcService = sdcService; - this.serviceModelInflator = serviceModelInflator; this.executorService = executorService; } @@ -116,7 +102,7 @@ public class AAIServiceTree { List aaiTreeNodes = fetchAAITree(url, payload, method, pathsToSearch, nodesAccumulator); if (enrichWithModelVersion) { - enrichNodesWithModelVersionAndModelName(nodesAccumulator); + aaiTreeNodesEnricher.enrichNodesWithModelVersionAndModelName(nodesAccumulator); } return aaiTreeNodes; @@ -134,12 +120,12 @@ public class AAIServiceTree { AAITreeNode aaiTree = fetchAAITree(getURL, null, HttpMethod.GET, AAI_TREE_PATHS, nodesAccumulator).get(0); //Populate nodes with model-name & model-version (from aai) - enrichNodesWithModelVersionAndModelName(nodesAccumulator); + aaiTreeNodesEnricher.enrichNodesWithModelVersionAndModelName(nodesAccumulator); final ServiceModel serviceModel = getServiceModel(aaiTree.getModelVersionId()); //Populate nodes with model-customization-name (from sdc model) - enrichNodesWithModelCustomizationName(nodesAccumulator, serviceModel); + aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesAccumulator, serviceModel); return aaiTreeConverter.convertTreeToUIModel(aaiTree, globalCustomerId, serviceType, getInstantiationType(serviceModel), getInstanceRole(serviceModel), getInstanceType(serviceModel)); } @@ -187,64 +173,6 @@ public class AAIServiceTree { } catch (AsdcCatalogException e) { throw new GenericUncheckedException("Exception while loading model version '" + modelVersionId + "'", e); } - - } - - void enrichNodesWithModelCustomizationName(Collection nodes, ServiceModel serviceModel) { - final Map customizationNameByVersionId = serviceModelInflator.toNamesByVersionId(serviceModel); - - nodes.forEach(node -> { - final ServiceModelInflator.Names names = customizationNameByVersionId.get(node.getModelVersionId()); - if (names != null) { - node.setKeyInModel(names.getModelKey()); - node.setModelCustomizationName(names.getModelCustomizationName()); - } - }); - } - - - private void enrichNodesWithModelVersionAndModelName(Collection nodes) { - - Collection invariantIDs = getModelInvariantIds(nodes); - - Map modelVersionByModelVersionId = new HashMap<>(); - Map modelNameByModelVersionId = new HashMap<>(); - - JsonNode models = getModels(aaiClient, invariantIDs); - if (models!=null) { - for (JsonNode model : models) { - JsonNode modelVersions = model.get("model-vers").get("model-ver"); - for (JsonNode modelVersion : modelVersions) { - final String modelVersionId = modelVersion.get("model-version-id").asText(); - modelVersionByModelVersionId.put(modelVersionId, modelVersion.get("model-version").asText()); - modelNameByModelVersionId.put(modelVersionId, modelVersion.get("model-name").asText()); - } - } - } - - nodes.forEach(node -> { - node.setModelVersion(modelVersionByModelVersionId.get(node.getModelVersionId())); - node.setModelName(modelNameByModelVersionId.get(node.getModelVersionId())); - }); - - } - - private JsonNode getModels(AaiClientInterface aaiClient, Collection invariantIDs) { - Response response = aaiClient.getVersionByInvariantId(ImmutableList.copyOf(invariantIDs)); - try { - JsonNode responseJson = JACKSON_OBJECT_MAPPER.readTree(response.readEntity(String.class)); - return responseJson.get("model"); - } catch (Exception e) { - LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to getVersionByInvariantId from A&AI", e); - } - return JACKSON_OBJECT_MAPPER.createObjectNode(); - } - - private Set getModelInvariantIds(Collection nodes) { - return nodes.stream() - .map(AAITreeNode::getModelInvariantId) - .filter(Objects::nonNull) - .collect(toSet()); } public static class AaiRelationship { diff --git a/vid-app-common/src/main/java/org/onap/vid/services/AAITreeNodesEnricher.java b/vid-app-common/src/main/java/org/onap/vid/services/AAITreeNodesEnricher.java new file mode 100644 index 000000000..e1e35cbec --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/services/AAITreeNodesEnricher.java @@ -0,0 +1,117 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 - 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.services; + +import static java.util.stream.Collectors.toSet; +import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER; + +import com.fasterxml.jackson.databind.JsonNode; +import com.google.common.collect.ImmutableList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import javax.inject.Inject; +import javax.ws.rs.core.Response; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.vid.aai.AaiClientInterface; +import org.onap.vid.asdc.parser.ServiceModelInflator; +import org.onap.vid.asdc.parser.ServiceModelInflator.Names; +import org.onap.vid.model.ServiceModel; +import org.onap.vid.model.aaiTree.AAITreeNode; +import org.springframework.stereotype.Component; + +@Component +public class AAITreeNodesEnricher { + + private final AaiClientInterface aaiClient; + + private final ServiceModelInflator serviceModelInflator; + + private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AAITreeNodesEnricher.class); + + @Inject + public AAITreeNodesEnricher( + AaiClientInterface aaiClient, + ServiceModelInflator serviceModelInflator + ) { + this.aaiClient = aaiClient; + this.serviceModelInflator = serviceModelInflator; + } + + void enrichNodesWithModelCustomizationName(Collection nodes, ServiceModel serviceModel) { + final Map customizationNameByVersionId = serviceModelInflator.toNamesByVersionId(serviceModel); + + nodes.forEach(node -> { + final Names names = customizationNameByVersionId.get(node.getModelVersionId()); + if (names != null) { + node.setKeyInModel(names.getModelKey()); + node.setModelCustomizationName(names.getModelCustomizationName()); + } + }); + } + + public void enrichNodesWithModelVersionAndModelName(Collection nodes) { + + Collection invariantIDs = getModelInvariantIds(nodes); + + Map modelVersionByModelVersionId = new HashMap<>(); + Map modelNameByModelVersionId = new HashMap<>(); + + JsonNode models = getModels(aaiClient, invariantIDs); + if (models!=null) { + for (JsonNode model : models) { + JsonNode modelVersions = model.get("model-vers").get("model-ver"); + for (JsonNode modelVersion : modelVersions) { + final String modelVersionId = modelVersion.get("model-version-id").asText(); + modelVersionByModelVersionId.put(modelVersionId, modelVersion.get("model-version").asText()); + modelNameByModelVersionId.put(modelVersionId, modelVersion.get("model-name").asText()); + } + } + } + + nodes.forEach(node -> { + node.setModelVersion(modelVersionByModelVersionId.get(node.getModelVersionId())); + node.setModelName(modelNameByModelVersionId.get(node.getModelVersionId())); + }); + + } + + private JsonNode getModels(AaiClientInterface aaiClient, Collection invariantIDs) { + Response response = aaiClient.getVersionByInvariantId(ImmutableList.copyOf(invariantIDs)); + try { + JsonNode responseJson = JACKSON_OBJECT_MAPPER.readTree(response.readEntity(String.class)); + return responseJson.get("model"); + } catch (Exception e) { + LOGGER.error(EELFLoggerDelegate.errorLogger, "Failed to getVersionByInvariantId from A&AI", e); + } + return JACKSON_OBJECT_MAPPER.createObjectNode(); + } + + private Set getModelInvariantIds(Collection nodes) { + return nodes.stream() + .map(AAITreeNode::getModelInvariantId) + .filter(Objects::nonNull) + .collect(toSet()); + } + +} diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceIntegrativeTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceIntegrativeTest.java index a73a5a7bd..e447ac71c 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceIntegrativeTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceIntegrativeTest.java @@ -64,12 +64,10 @@ public class AAIServiceIntegrativeTest extends TestWithAaiClient { AaiClient aaiClient = new AaiClient(aaiRestInterface, null, cacheProvider); ExecutorService executorService = MoreExecutors.newDirectExecutorService(); AAIServiceTree aaiServiceTree = new AAIServiceTree( - aaiClient, - new AAITreeNodeBuilder(aaiClient, logging), - new AAITreeConverter(new ModelUtil()), - null, - null, - executorService + new AAITreeNodeBuilder(aaiClient, logging), + new AAITreeNodesEnricher(aaiClient, null), + new AAITreeConverter(new ModelUtil()), null, + executorService ); return new AaiServiceImpl(aaiClient, null, aaiServiceTree, executorService, logging); } diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java index 0d2d51cee..02f127d71 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeIntegrativeTest.java @@ -61,6 +61,7 @@ import org.onap.vid.utils.Logging; import org.springframework.http.HttpMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import org.togglz.core.manager.FeatureManager; public class AAIServiceTreeIntegrativeTest { @@ -79,9 +80,13 @@ public class AAIServiceTreeIntegrativeTest { @Mock ServiceModelInflator serviceModelInflator; + @Mock + FeatureManager featureManager; + @Mock Logging logging; + private AAITreeNodesEnricher aaiTreeNodesEnricher; private AAITreeNodeBuilder aaiTreeNodeBuilder; private AAITreeConverter aaiTreeConverter = new AAITreeConverter(new ModelUtil()); @@ -133,7 +138,6 @@ public class AAIServiceTreeIntegrativeTest { "\"relationship-key\": \"owning-entity.owning-entity-id\"," + "\"relationship-value\": \"43b8a85a-0421-4265-9069-117dd6526b8a\"}]}]}}"; - //TODO Amichai: if in the future it is neede, add here the SUFFIX to the URL: "?format=simple" private static String genericVnfRequestUri = "/aai/v12/network/generic-vnfs/generic-vnf/59bde732-9b84-46bd-a59a-3c45fee0538b"; private String genericVnfResponseString(boolean isDuplicatedKeysInTenantRelation) { @@ -300,6 +304,7 @@ public class AAIServiceTreeIntegrativeTest { TestUtils.initMockitoMocks(this); reboundLoggingWithMdcMock(); aaiTreeNodeBuilder = new AAITreeNodeBuilder(aaiClient, logging); + aaiTreeNodesEnricher = new AAITreeNodesEnricher(aaiClient, serviceModelInflator); } private void reboundLoggingWithMdcMock() { @@ -320,7 +325,7 @@ public class AAIServiceTreeIntegrativeTest { "11c6dc3e-cd6a-41b3-a50e-b5a10f7157d0", new ServiceModelInflator.Names("vnf-model-customization-name", "vnf-key-in-model") )); - ServiceInstance root = new AAIServiceTree(aaiClient, aaiTreeNodeBuilder, aaiTreeConverter, sdcService, serviceModelInflator, executorService) + ServiceInstance root = new AAIServiceTree(aaiTreeNodeBuilder, aaiTreeNodesEnricher, aaiTreeConverter, sdcService, executorService) .getServiceInstanceTopology(globalCustomerID, serviceType, serviceInstanceId); assertServiceNode(root, 1); @@ -370,7 +375,8 @@ public class AAIServiceTreeIntegrativeTest { when(sdcService.getService(any())).thenReturn( TestUtils.readJsonResourceFileAsObject("/getTopology/serviceWithCR/serviceWithCRModel.json", ServiceModel.class)); - ServiceInstance serviceInstance = new AAIServiceTree(aaiClient, aaiTreeNodeBuilder, aaiTreeConverter, sdcService, new ServiceModelInflator(), executorService) + ServiceInstance serviceInstance = new AAIServiceTree(aaiTreeNodeBuilder, + new AAITreeNodesEnricher(aaiClient, new ServiceModelInflator()), aaiTreeConverter, sdcService, executorService) .getServiceInstanceTopology("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb", "Emanuel", "a565e6ad-75d1-4493-98f1-33234b5c17e2"); String expected = TestUtils.readFileAsString("/getTopology/serviceWithCR/getTopologyWithCR.json"); @@ -437,7 +443,7 @@ public class AAIServiceTreeIntegrativeTest { when(sdcService.getService(any())).thenReturn(mock(ServiceModel.class)); when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of()); - new AAIServiceTree(aaiClient, aaiTreeNodeBuilder, aaiTreeConverter, sdcService, serviceModelInflator, executorService) + new AAIServiceTree(aaiTreeNodeBuilder, aaiTreeNodesEnricher, aaiTreeConverter, sdcService, executorService) .getServiceInstanceTopology(globalCustomerID, serviceType, serviceInstanceId); } diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeTest.java index be195c89b..ca3e98e2d 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeTest.java @@ -20,37 +20,24 @@ package org.onap.vid.services; -import static java.util.Collections.emptyList; -import static java.util.Collections.emptyMap; -import static java.util.stream.Collectors.toList; import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.onap.vid.services.AAITreeNodeBuilderTest.createExpectedVnfTreeNode; import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Streams; import com.google.common.util.concurrent.MoreExecutors; -import java.io.IOException; import java.util.List; import java.util.concurrent.ExecutorService; import net.javacrumbs.jsonunit.core.Option; -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.jetbrains.annotations.NotNull; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.onap.vid.aai.AaiClient; import org.onap.vid.aai.util.AAITreeConverter; import org.onap.vid.asdc.parser.ServiceModelInflator; -import org.onap.vid.asdc.parser.ServiceModelInflator.Names; import org.onap.vid.model.ModelUtil; import org.onap.vid.model.aaiTree.AAITreeNode; import org.onap.vid.model.aaiTree.NodeType; @@ -76,136 +63,16 @@ public class AAIServiceTreeTest { MockitoAnnotations.initMocks(this); } - private final static String nullString = "null placeholder"; - - - - @Test - public void enrichNodesWithModelCustomizationName_simple3NodesCase_nodesEnriched() { - - when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of( - "version id a", new Names("name a", "key a"), - "version id b", new Names("name b", "key b"), - "version id c", new Names("name c", "key c") - )); - - final ImmutableList versionIds = ImmutableList.of("version id a", "version id b", "version id c"); - final ImmutableList expectedNames = ImmutableList.of( - new Names("name a", "key a"), - new Names("name b", "key b"), - new Names("name c", "key c")); - - - final List nodesUnderTest = nodesWithVersionIds(versionIds); - aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null); - - assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames)))); - } - - @Test - public void enrichNodesWithModelCustomizationName_noNodes_noError() { - - when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of( - "11c6dc3e-cd6a-41b3-a50e-b5a10f7157d0", new Names("my model cust name", "my key") - )); - - aaiServiceTree.enrichNodesWithModelCustomizationName(emptyList(), null); - } - - @Test - public void enrichNodesWithModelCustomizationName_nothingInModel_nodesUnchanged() { - - when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(emptyMap()); - - final ImmutableList versionIds = ImmutableList.of("version id a", "version id b", "version id c"); - final Names nullNames = new Names(nullString, nullString); - final ImmutableList expectedNames = ImmutableList.of(nullNames, nullNames, nullNames); - - - final List nodesUnderTest = nodesWithVersionIds(versionIds); - - aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null); - - assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames)))); - } - - @Test - public void enrichNodesWithModelCustomizationName_staggered4NodesAndNull_3nodesEnriched2isNull() { - - when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of( - "version id Z", new Names("name Z", "key Z"), - "version id d", new Names(null, "key d"), - "version id c", new Names("name c", null), - "version id a", new Names("name a", "key a") - )); - - final ImmutableList versionIds = ImmutableList.of("version id a", "version id b", "version id c", "version id d", nullString); - final ImmutableList expectedNames = ImmutableList.of( - new Names("name a", "key a"), - new Names(nullString, nullString), - new Names("name c", nullString), - new Names(nullString, "key d"), - new Names(nullString, nullString) - ); - - - final List nodesUnderTest = nodesWithVersionIds(versionIds); - - aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null); - - assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames)))); - } - - - - @NotNull - private String[] toStringsArray(List nodes) { - return toStrings(nodes).toArray(new String[] {}); - } - - @NotNull - private List toStrings(List nodes) { - return nodes.stream().map(n -> { - final ReflectionToStringBuilder reflectionToStringBuilder = new ReflectionToStringBuilder(n, ToStringStyle.SHORT_PREFIX_STYLE); - reflectionToStringBuilder.setExcludeNullValues(true); - return reflectionToStringBuilder.toString(); - }).collect(toList()); - } - - @NotNull - private List nodesWithVersionIdsAndCustomizationNames(List versionIds, List customizationNames) { - return Streams - .zip(versionIds.stream(), customizationNames.stream(), this::nodeWithVersionIdAndCustomizationName) - .collect(toList()); - } - - @NotNull - private List nodesWithVersionIds(List versionIds) { - return versionIds.stream() - .map(versionId -> nodeWithVersionIdAndCustomizationName(versionId, new Names(nullString, nullString))) - .collect(toList()); - } - - private AAITreeNode nodeWithVersionIdAndCustomizationName(String versionId, Names names) { - AAITreeNode newNode = new AAITreeNode(); - newNode.setModelVersionId(versionId.equals(nullString) ? null : versionId); - newNode.setModelCustomizationName(names.getModelCustomizationName().equals(nullString) ? null : names.getModelCustomizationName()); - newNode.setKeyInModel(names.getModelKey().equals(nullString) ? null : names.getModelKey()); - return newNode; - } - @Test - public void whenBuildTreeForOneResource_resultAsExpected() throws IOException { + public void whenBuildTreeForOneResource_resultAsExpected() { AaiClient aaiClientMock = mock(AaiClient.class); ExecutorService executorService = MoreExecutors.newDirectExecutorService(); AAIServiceTree aaiServiceTree = new AAIServiceTree( - aaiClientMock, - new AAITreeNodeBuilder(aaiClientMock, new Logging()), - new AAITreeConverter(new ModelUtil()), - null, - null, - executorService + new AAITreeNodeBuilder(aaiClientMock, new Logging()), + null, + new AAITreeConverter(new ModelUtil()), null, + executorService ); String url = "anyUrl/vnf"; diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AAITreeNodesEnricherTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AAITreeNodesEnricherTest.java new file mode 100644 index 000000000..8aba27932 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/services/AAITreeNodesEnricherTest.java @@ -0,0 +1,183 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 - 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.services; + +import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; +import static java.util.stream.Collectors.toList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Streams; +import java.util.List; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.jetbrains.annotations.NotNull; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.vid.aai.AaiClientInterface; +import org.onap.vid.asdc.parser.ServiceModelInflator; +import org.onap.vid.asdc.parser.ServiceModelInflator.Names; +import org.onap.vid.model.aaiTree.AAITreeNode; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; +import org.togglz.core.manager.FeatureManager; + +public class AAITreeNodesEnricherTest { + + @Mock + private AaiClientInterface aaiClient; + @Mock + private VidService sdcService; + @Mock + private ServiceModelInflator serviceModelInflator; + @Mock + private FeatureManager featureManager; + @InjectMocks + private AAITreeNodesEnricher aaiTreeNodesEnricher; + + @BeforeTest + public void initMocks() { + MockitoAnnotations.initMocks(this); + } + + private final static String nullString = "null placeholder"; + + @Test + public void enrichNodesWithModelCustomizationName_simple3NodesCase_nodesEnriched() { + + when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of( + "version id a", new Names("name a", "key a"), + "version id b", new Names("name b", "key b"), + "version id c", new Names("name c", "key c") + )); + + final ImmutableList versionIds = ImmutableList.of("version id a", "version id b", "version id c"); + final ImmutableList expectedNames = ImmutableList.of( + new Names("name a", "key a"), + new Names("name b", "key b"), + new Names("name c", "key c")); + + + final List nodesUnderTest = nodesWithVersionIds(versionIds); + aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesUnderTest, null); + + assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames)))); + } + + @Test + public void enrichNodesWithModelCustomizationName_noNodes_noError() { + + when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of( + "11c6dc3e-cd6a-41b3-a50e-b5a10f7157d0", new Names("my model cust name", "my key") + )); + + aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(emptyList(), null); + } + + @Test + public void enrichNodesWithModelCustomizationName_nothingInModel_nodesUnchanged() { + + when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(emptyMap()); + + final ImmutableList versionIds = ImmutableList.of("version id a", "version id b", "version id c"); + final Names nullNames = new Names(nullString, nullString); + final ImmutableList expectedNames = ImmutableList.of(nullNames, nullNames, nullNames); + + + final List nodesUnderTest = nodesWithVersionIds(versionIds); + + aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesUnderTest, null); + + assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames)))); + } + + @Test + public void enrichNodesWithModelCustomizationName_staggered4NodesAndNull_3nodesEnriched2isNull() { + + when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of( + "version id Z", new Names("name Z", "key Z"), + "version id d", new Names(null, "key d"), + "version id c", new Names("name c", null), + "version id a", new Names("name a", "key a") + )); + + final ImmutableList versionIds = ImmutableList.of("version id a", "version id b", "version id c", "version id d", nullString); + final ImmutableList expectedNames = ImmutableList.of( + new Names("name a", "key a"), + new Names(nullString, nullString), + new Names("name c", nullString), + new Names(nullString, "key d"), + new Names(nullString, nullString) + ); + + + final List nodesUnderTest = nodesWithVersionIds(versionIds); + + aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesUnderTest, null); + + assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames)))); + } + + + + @NotNull + private String[] toStringsArray(List nodes) { + return toStrings(nodes).toArray(new String[] {}); + } + + @NotNull + private List toStrings(List nodes) { + return nodes.stream().map(n -> { + final ReflectionToStringBuilder reflectionToStringBuilder = new ReflectionToStringBuilder(n, ToStringStyle.SHORT_PREFIX_STYLE); + reflectionToStringBuilder.setExcludeNullValues(true); + return reflectionToStringBuilder.toString(); + }).collect(toList()); + } + + @NotNull + private List nodesWithVersionIdsAndCustomizationNames(List versionIds, List customizationNames) { + return Streams + .zip(versionIds.stream(), customizationNames.stream(), this::nodeWithVersionIdAndCustomizationName) + .collect(toList()); + } + + @NotNull + private List nodesWithVersionIds(List versionIds) { + return versionIds.stream() + .map(versionId -> nodeWithVersionIdAndCustomizationName(versionId, new Names(nullString, nullString))) + .collect(toList()); + } + + private AAITreeNode nodeWithVersionIdAndCustomizationName(String versionId, Names names) { + AAITreeNode newNode = new AAITreeNode(); + newNode.setModelVersionId(versionId.equals(nullString) ? null : versionId); + newNode.setModelCustomizationName(names.getModelCustomizationName().equals(nullString) ? null : names.getModelCustomizationName()); + newNode.setKeyInModel(names.getModelKey().equals(nullString) ? null : names.getModelKey()); + return newNode; + } + +} \ No newline at end of file -- cgit 1.2.3-korg