From a1cdcda28701f603cf95f591ba447bd723273622 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Fri, 27 Aug 2021 15:07:43 +0100 Subject: Include custom data types from VSP in csar imports When a model with custom data types is created, the data types are now being added as part of the default model imports files. When a CSAR is generated from a Service Template that is associated to a derived model, the default imports from the parent model are also included in the package. Change-Id: I027d25d7237989a40085edec7fdd399ac09c4db1 Issue-ID: SDC-3692 Signed-off-by: andre.schmid --- .../be/model/operations/impl/ModelOperation.java | 19 ++++++++ .../model/operations/impl/ModelOperationTest.java | 56 ++++++++++++++++++++-- 2 files changed, 70 insertions(+), 5 deletions(-) (limited to 'catalog-model') diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java index d522e10d7c..87ab3fc79d 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java @@ -176,6 +176,25 @@ public class ModelOperation { toscaModelImportCassandraDao.importAll(modelId, toscaImportByModelList); } + /** + * Find all the model default imports, with the option to include the default imports from the parent model. + * + * @param modelId the model id + * @param includeParent a flag to include the parent model imports. + * @return the list of model default imports, or an empty list if no imports were found. + */ + public List findAllModelImports(final String modelId, final boolean includeParent) { + final List toscaImportByModelList = toscaModelImportCassandraDao.findAllByModel(modelId); + if (includeParent) { + findModelByName(modelId).ifPresent(model -> { + if (model.getDerivedFrom() != null) { + toscaImportByModelList.addAll(toscaModelImportCassandraDao.findAllByModel(model.getDerivedFrom())); + } + }); + } + return toscaImportByModelList; + } + /** * Finds all the models. * diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/ModelOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/ModelOperationTest.java index 6071b4d59d..e7c5ddba16 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/ModelOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/ModelOperationTest.java @@ -35,11 +35,14 @@ import static org.mockito.Mockito.when; import fj.data.Either; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.TreeMap; +import java.util.stream.Stream; +import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -49,11 +52,12 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.cassandra.ToscaModelImportCassandraDao; +import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge; import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels; import org.openecomp.sdc.be.data.model.ToscaImportByModel; @@ -228,8 +232,8 @@ class ModelOperationTest extends ModelTestBase { when(expectedVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(modelName); when(expectedVertex.getMetadataProperty(GraphPropertyEnum.MODEL_TYPE)).thenReturn(ModelTypeEnum.NORMATIVE.getValue()); when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), mapArgumentCaptor.capture())).thenReturn(Either.left(List.of(expectedVertex))); - when(janusGraphGenericDao.getChild("uid", UniqueIdBuilder.buildModelUid(modelName), GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model, - ModelData.class)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); + when(janusGraphGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Model), UniqueIdBuilder.buildModelUid(modelName), + GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model, ModelData.class)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); final Optional modelByNameOpt = modelOperation.findModelByName(modelName); final Map value = mapArgumentCaptor.getValue(); @@ -263,8 +267,8 @@ class ModelOperationTest extends ModelTestBase { when(expectedVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(modelName); when(expectedVertex.getMetadataProperty(GraphPropertyEnum.MODEL_TYPE)).thenReturn(ModelTypeEnum.NORMATIVE.getValue()); when(janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, Collections.emptyMap())).thenReturn(Either.left(List.of(expectedVertex))); - when(janusGraphGenericDao.getChild("uid", UniqueIdBuilder.buildModelUid(modelName), GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model, - ModelData.class)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); + when(janusGraphGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Model), UniqueIdBuilder.buildModelUid(modelName), + GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model, ModelData.class)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); final List actualModelList = modelOperation.findAllModels(); assertFalse(actualModelList.isEmpty()); @@ -295,4 +299,46 @@ class ModelOperationTest extends ModelTestBase { final var expectedException = ModelOperationExceptionSupplier.failedToRetrieveModels(JanusGraphOperationStatus.GENERAL_ERROR).get(); assertEquals(expectedException.getMessage(), actualException.getMessage()); } + + @Test + void findAllModelImportsTest() { + //given + final var modelName = "modelName"; + final var parentModelName = "parentModelName"; + final GraphVertex expectedVertex = mock(GraphVertex.class); + when(expectedVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(modelName); + when(expectedVertex.getMetadataProperty(GraphPropertyEnum.MODEL_TYPE)).thenReturn(ModelTypeEnum.NORMATIVE_EXTENSION.getValue()); + when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.MODEL), anyMap())).thenReturn(Either.left(List.of(expectedVertex))); + + final var modelData = new ModelData(parentModelName, parentModelName, ModelTypeEnum.NORMATIVE); + final ImmutablePair modelDataGraphEdgePair = new ImmutablePair<>(modelData, null); + + when(janusGraphGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Model), UniqueIdBuilder.buildModelUid(modelName), + GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model, ModelData.class)).thenReturn(Either.left(modelDataGraphEdgePair)); + + final ArrayList childModelImportList = new ArrayList<>(); + childModelImportList.add(createModelImport(modelName, "anyPath1")); + childModelImportList.add(createModelImport(modelName, "anyPath2")); + when(toscaModelImportCassandraDao.findAllByModel(modelName)).thenReturn(new ArrayList<>(childModelImportList)); + final ArrayList parentModelImportList = new ArrayList<>(); + parentModelImportList.add(createModelImport(parentModelName, "anyPath1")); + parentModelImportList.add(createModelImport(parentModelName, "anyPath2")); + when(toscaModelImportCassandraDao.findAllByModel(parentModelName)).thenReturn(parentModelImportList); + + //when + final List actualModelImportList = modelOperation.findAllModelImports(modelName, true); + + //then + assertFalse(actualModelImportList.isEmpty()); + assertEquals(childModelImportList.size() + parentModelImportList.size(), actualModelImportList.size()); + Stream.concat(childModelImportList.stream(), parentModelImportList.stream()) + .forEach(toscaImportByModel -> assertTrue(actualModelImportList.contains(toscaImportByModel))); + } + + private ToscaImportByModel createModelImport(final String parentModelName, final String importPath) { + var toscaImportByModel = new ToscaImportByModel(); + toscaImportByModel.setModelId(parentModelName); + toscaImportByModel.setFullPath(importPath); + return toscaImportByModel; + } } -- cgit 1.2.3-korg