From d89a5d3cf1ed1f81cdda754464eff07781824053 Mon Sep 17 00:00:00 2001 From: franciscovila Date: Thu, 12 Oct 2023 11:09:43 +0100 Subject: Increase unit test coverage for backend Provided some Junit test coverage for backed Issue-ID: SDC-4654 Signed-off-by: franciscovila Change-Id: I91b3f4326256312ab998d32fce9eb13b3e39b8d1 --- .../sdc/be/model/LifecycleStateEnumTest.java | 40 ++ .../sdc/be/model/dto/FilterConstraintDtoTest.java | 69 ++- .../operations/ArtifactsOperationsTest.java | 632 ++++++++++++++++++++- .../operations/ForwardingPathOperationTest.java | 212 +++++++ .../operations/GroupsOperationMockTest.java | 142 +++++ 5 files changed, 1078 insertions(+), 17 deletions(-) create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/LifecycleStateEnumTest.java create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ForwardingPathOperationTest.java create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/GroupsOperationMockTest.java (limited to 'catalog-model/src/test/java') diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/LifecycleStateEnumTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/LifecycleStateEnumTest.java new file mode 100644 index 0000000000..05389c9527 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/LifecycleStateEnumTest.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 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.openecomp.sdc.be.model; + +import org.junit.Assert; +import org.junit.Test; + +public class LifecycleStateEnumTest { + + @Test + public void testFindState() { + LifecycleStateEnum result = LifecycleStateEnum.findState("CERTIFIED"); + Assert.assertNotNull(result); + Assert.assertEquals(LifecycleStateEnum.CERTIFIED,result); + } + + @Test + public void testFindStateNull() { + LifecycleStateEnum result = LifecycleStateEnum.findState("mock"); + Assert.assertNull(result); + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/dto/FilterConstraintDtoTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/dto/FilterConstraintDtoTest.java index e67fdc0f33..8bffdd6613 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/dto/FilterConstraintDtoTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/dto/FilterConstraintDtoTest.java @@ -25,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; @@ -89,13 +90,75 @@ class FilterConstraintDtoTest { final ToscaGetFunctionDataDefinition toscaGetFunctionDataDefinition = readGetFunctionOpt.get(); assertEquals(toscaGetFunctionDataDefinition.getPropertyUniqueId(), propertyUniqueId); assertEquals(toscaGetFunctionDataDefinition.getPropertyName(), propertyName); - assertEquals(toscaGetFunctionDataDefinition.getPropertySource(), PropertySource.SELF); + assertEquals(PropertySource.SELF, toscaGetFunctionDataDefinition.getPropertySource()); assertEquals(toscaGetFunctionDataDefinition.getSourceUniqueId(), sourceUniqueId); assertEquals(toscaGetFunctionDataDefinition.getSourceName(), sourceName); - assertEquals(toscaGetFunctionDataDefinition.getFunctionType(), ToscaGetFunctionType.GET_INPUT); - assertEquals(toscaGetFunctionDataDefinition.getPropertyPathFromSource().size(), 2); + assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunctionDataDefinition.getFunctionType()); + assertEquals(2, toscaGetFunctionDataDefinition.getPropertyPathFromSource().size()); assertEquals(toscaGetFunctionDataDefinition.getPropertyPathFromSource().get(0), propertyPathFromSource.get(0)); assertEquals(toscaGetFunctionDataDefinition.getPropertyPathFromSource().get(1), propertyPathFromSource.get(1)); } + @Test + void readGetFunctionWithGetFunctionAsListValueAsMap() { + //given + final List propertyPathFromSource1 = List.of("input1", "path1"); + final String propertyUniqueId1 = "propertyUniqueIdValue1"; + final String propertyName1 = "propertyNameValue1"; + final String sourceUniqueId1 = "sourceUniqueIdValue1"; + final String sourceName1 = "sourceNameValue1"; + final Map toscaGetFunctionAsMap1 = Map.of( + "propertyUniqueId", propertyUniqueId1, + "propertyName", propertyName1, + "propertySource", PropertySource.SELF.getName(), + "sourceUniqueId", sourceUniqueId1, + "sourceName", sourceName1, + "functionType", ToscaGetFunctionType.GET_INPUT.getFunctionName(), + "propertyPathFromSource", propertyPathFromSource1 + ); + final List propertyPathFromSource2 = List.of("input2", "path2"); + final String propertyUniqueId2 = "propertyUniqueIdValue2"; + final String propertyName2 = "propertyNameValue2"; + final String sourceUniqueId2 = "sourceUniqueIdValue2"; + final String sourceName2 = "sourceNameValue2"; + final Map toscaGetFunctionAsMap2 = Map.of( + "propertyUniqueId", propertyUniqueId2, + "propertyName", propertyName2, + "propertySource", PropertySource.SELF.getName(), + "sourceUniqueId", sourceUniqueId2, + "sourceName", sourceName2, + "functionType", ToscaGetFunctionType.GET_INPUT.getFunctionName(), + "propertyPathFromSource", propertyPathFromSource2 + ); + + List toscaGetFunctionDataDefinitionList = new ArrayList<>(); + toscaGetFunctionDataDefinitionList.add(toscaGetFunctionAsMap1); + toscaGetFunctionDataDefinitionList.add(toscaGetFunctionAsMap2); + + final var filterConstraintDto = new FilterConstraintDto(); + filterConstraintDto.setValue(toscaGetFunctionDataDefinitionList); + //when + final Optional> readListGetFunctionOpt = filterConstraintDto.getAsListToscaGetFunction(); + //then + assertTrue(readListGetFunctionOpt.isPresent()); + final List toscaGetFunctionDataDefinition = readListGetFunctionOpt.get(); + assertEquals(toscaGetFunctionDataDefinition.get(0).getPropertyUniqueId(), propertyUniqueId1); + assertEquals(toscaGetFunctionDataDefinition.get(1).getPropertyUniqueId(), propertyUniqueId2); + assertEquals(toscaGetFunctionDataDefinition.get(0).getPropertyName(), propertyName1); + assertEquals(toscaGetFunctionDataDefinition.get(1).getPropertyName(), propertyName2); + assertEquals(PropertySource.SELF, toscaGetFunctionDataDefinition.get(0).getPropertySource()); + assertEquals(PropertySource.SELF, toscaGetFunctionDataDefinition.get(1).getPropertySource()); + assertEquals(toscaGetFunctionDataDefinition.get(0).getSourceUniqueId(), sourceUniqueId1); + assertEquals(toscaGetFunctionDataDefinition.get(1).getSourceUniqueId(), sourceUniqueId2); + assertEquals(toscaGetFunctionDataDefinition.get(0).getSourceName(), sourceName1); + assertEquals(toscaGetFunctionDataDefinition.get(1).getSourceName(), sourceName2); + assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunctionDataDefinition.get(0).getFunctionType()); + assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunctionDataDefinition.get(1).getFunctionType()); + assertEquals(2, toscaGetFunctionDataDefinition.get(0).getPropertyPathFromSource().size()); + assertEquals(2, toscaGetFunctionDataDefinition.get(1).getPropertyPathFromSource().size()); + assertEquals(toscaGetFunctionDataDefinition.get(0).getPropertyPathFromSource().get(0), propertyPathFromSource1.get(0)); + assertEquals(toscaGetFunctionDataDefinition.get(1).getPropertyPathFromSource().get(0), propertyPathFromSource2.get(0)); + assertEquals(toscaGetFunctionDataDefinition.get(0).getPropertyPathFromSource().get(1), propertyPathFromSource1.get(1)); + assertEquals(toscaGetFunctionDataDefinition.get(1).getPropertyPathFromSource().get(1), propertyPathFromSource2.get(1)); + } } \ No newline at end of file diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ArtifactsOperationsTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ArtifactsOperationsTest.java index 7bbb5a1263..8f872ca017 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ArtifactsOperationsTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ArtifactsOperationsTest.java @@ -20,22 +20,37 @@ package org.openecomp.sdc.be.model.jsonjanusgraph.operations; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + import fj.data.Either; -import org.junit.Test; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition; import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; public class ArtifactsOperationsTest { @@ -43,6 +58,507 @@ public class ArtifactsOperationsTest { private static final String INSTANCE_ID = "instanceId"; private ArtifactsOperations testInstance = mock(ArtifactsOperations.class, CALLS_REAL_METHODS); + @Test + public void addArtifactToComponent() { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + final Resource resource = new Resource(); + resource.setUniqueId(INSTANCE_ID); + Map instanceArtifacts = Collections.singletonMap(INSTANCE_ID, getArtifactsByInstance("name1")); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.left(false)).when(testInstance).isCloneNeeded(Mockito.any(), + Mockito.any()); + Map artMap = new HashMap<>(); + artMap.put(INSTANCE_ID, artifactDef); + doReturn(Either.left(artifactDef)).when(testInstance).updateArtifactOnGraph(resource, + artifactDef, NodeTypeEnum.Resource, null, INSTANCE_ID, false, + false); + + Either ret = testInstance.addArtifactToComponent( + artifactDef, resource, NodeTypeEnum.Resource, false, "instanceId"); + + assertTrue(ret.isLeft()); + } + + @Test + public void addArtifactToComponentEsId() { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + artifactDef.setUniqueId(null); + artifactDef.setEsId(INSTANCE_ID); + final Resource resource = new Resource(); + resource.setUniqueId(INSTANCE_ID); + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.left(false)).when(testInstance).isCloneNeeded(Mockito.any(), + Mockito.any()); + Map artMap = new HashMap<>(); + artMap.put(INSTANCE_ID, artifactDef); + doReturn(StorageOperationStatus.OK).when(testInstance).updateToscaDataOfToscaElement(Mockito.anyString(), + Mockito.any(), Mockito.any(), (List) Mockito.any(), Mockito.any()); + doReturn(Either.left(artifactDef)).when(testInstance).updateArtifactOnGraph(resource, + artifactDef, NodeTypeEnum.Resource, null, INSTANCE_ID, false, + false); + + Either ret = testInstance.addArtifactToComponent( + artifactDef, resource, NodeTypeEnum.Resource, false, "instanceId"); + + assertTrue(ret.isLeft()); + } + + @Test + public void addArtifactToComponentFail() { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + final Resource resource = new Resource(); + resource.setUniqueId(INSTANCE_ID); + Map instanceArtifacts = Collections.singletonMap(INSTANCE_ID, getArtifactsByInstance("name1")); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.left(false)).when(testInstance).isCloneNeeded(Mockito.any(), + Mockito.any()); + Map artMap = new HashMap<>(); + artMap.put(INSTANCE_ID, artifactDef); + doReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND)).when(testInstance).updateArtifactOnGraph(resource, + artifactDef, NodeTypeEnum.Resource, null, INSTANCE_ID, false, + false); + + Either ret = testInstance.addArtifactToComponent( + artifactDef, resource, NodeTypeEnum.Resource, false, "instanceId"); + + assertTrue(ret.isRight()); + } + + @Test + public void updateArtifactOnResource() { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + final Resource resource = new Resource(); + resource.setUniqueId(INSTANCE_ID); + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.left(false)).when(testInstance).isCloneNeeded(Mockito.any(), + Mockito.any()); + Map artMap = new HashMap<>(); + artMap.put(INSTANCE_ID, artifactDef); + doReturn(StorageOperationStatus.OK).when(testInstance).updateToscaDataOfToscaElement(Mockito.anyString(), + Mockito.any(), Mockito.any(), (List) Mockito.any(), Mockito.any()); + doReturn(Either.left(artifactDef)).when(testInstance).updateArtifactOnGraph(resource, + artifactDef, NodeTypeEnum.Resource, null, INSTANCE_ID, false, + false); + + Either ret = testInstance.updateArtifactOnResource( + artifactDef, resource, "instanceId", NodeTypeEnum.Resource, "instanceId", false); + + assertTrue(ret.isLeft()); + } + + @Test + public void updateArtifactOnResourceFail() { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + final Resource resource = new Resource(); + resource.setUniqueId(INSTANCE_ID); + Map instanceArtifacts = Collections.singletonMap(INSTANCE_ID, getArtifactsByInstance("name1")); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.left(false)).when(testInstance).isCloneNeeded(Mockito.any(), + Mockito.any()); + Map artMap = new HashMap<>(); + artMap.put(INSTANCE_ID, artifactDef); + + doReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND)).when(testInstance).updateArtifactOnGraph(resource, + artifactDef, NodeTypeEnum.Resource, INSTANCE_ID, INSTANCE_ID, false, + false); + + Either ret = testInstance.updateArtifactOnResource( + artifactDef, resource, "instanceId", NodeTypeEnum.Resource, "instanceId", false); + + assertTrue(ret.isRight()); + } + + @Test + public void isCloneNeeded() { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + doReturn(Either.left(false)).when(testInstance).isCloneNeeded(Mockito.any(), + Mockito.any()); + + Either ret = testInstance.isCloneNeeded( + INSTANCE_ID, artifactDef, NodeTypeEnum.Resource); + + assertTrue(ret.isLeft()); + } + + @Test + public void getArtifactById() { + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + Either ret = testInstance.getArtifactById( + INSTANCE_ID, "artifactDefId"); + + assertTrue(ret.isLeft()); + } + + @Test + public void getArtifactById2() { + Map instanceArtifacts = + Collections.singletonMap(INSTANCE_ID, getArtifactsByInstanceSettingUniqueId("artifactDefId")); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + Either ret = testInstance.getArtifactById( + INSTANCE_ID, "artifactDefId", ComponentTypeEnum.RESOURCE_INSTANCE, "containerId"); + + assertTrue(ret.isLeft()); + } + + @Test + public void getArtifactById3() { + Map instanceArtifacts = + Collections.singletonMap(INSTANCE_ID, getArtifactsByInstanceSettingUniqueId("artifactDefId")); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph("containerId", EdgeLabelEnum.INSTANCE_ARTIFACTS); + Either ret = testInstance.getArtifactById( + INSTANCE_ID, "artifactDefId", ComponentTypeEnum.RESOURCE_INSTANCE, "containerId"); + + assertTrue(ret.isLeft()); + } + + @Test + public void getArtifactById4() { + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INSTANCE_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, + EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.TOSCA_ARTIFACTS); + Either ret = testInstance.getArtifactById( + INSTANCE_ID, "artifactDefId", ComponentTypeEnum.RESOURCE_INSTANCE, "containerId"); + + assertTrue(ret.isLeft()); + } + + @Test + public void getArtifactById5() { + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INSTANCE_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, + EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.TOSCA_ARTIFACTS); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + Either ret = testInstance.getArtifactById( + INSTANCE_ID, "artifactDefId", ComponentTypeEnum.RESOURCE_INSTANCE, "containerId"); + + assertTrue(ret.isLeft()); + } + + @Test + public void getArtifactById6() { + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INSTANCE_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, + EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.TOSCA_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.SERVICE_API_ARTIFACTS); + Either ret = testInstance.getArtifactById( + INSTANCE_ID, "artifactDefId", ComponentTypeEnum.RESOURCE_INSTANCE, "containerId"); + + assertTrue(ret.isLeft()); + } + + @Test + public void getArtifactById7_NotFound1() { + Map interfaceDefinitions = getInterfaceDataDefinitionsByName("artifactDefId"); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INSTANCE_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, + EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.TOSCA_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, + EdgeLabelEnum.SERVICE_API_ARTIFACTS); + doReturn(Either.left(interfaceDefinitions)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.INTERFACE); + Either ret = testInstance.getArtifactById( + INSTANCE_ID, "artifactDefId", ComponentTypeEnum.RESOURCE_INSTANCE, "containerId"); + + assertTrue(ret.isRight()); + } + + @Test + public void getArtifactById7_NotFound2() { + Map interfaceDefinitions = getInterfaceDataDefinitionsByName("artifactDefId"); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INSTANCE_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, + EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.TOSCA_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, + EdgeLabelEnum.SERVICE_API_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.INTERFACE); + Either ret = testInstance.getArtifactById( + INSTANCE_ID, "artifactDefId", ComponentTypeEnum.RESOURCE_INSTANCE, "containerId"); + + assertTrue(ret.isRight()); + } + + @Test + public void getArtifactById7_NotFound3() { + Map interfaceDefinitions = + getInterfaceDataDefinitionsWithOperationsByNameNotFound("artifactDefId"); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INSTANCE_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, + EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.TOSCA_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, + EdgeLabelEnum.SERVICE_API_ARTIFACTS); + doReturn(Either.left(interfaceDefinitions)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.INTERFACE); + Either ret = testInstance.getArtifactById( + INSTANCE_ID, "artifactDefId", ComponentTypeEnum.RESOURCE_INSTANCE, "containerId"); + + assertTrue(ret.isRight()); + } + + @Test + public void getArtifactById7() { + Map interfaceDefinitions = getInterfaceDataDefinitionsWithOperationsByName("artifactDefId"); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph("containerId", + EdgeLabelEnum.INSTANCE_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, + EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.TOSCA_ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, + EdgeLabelEnum.SERVICE_API_ARTIFACTS); + doReturn(Either.left(interfaceDefinitions)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.INTERFACE); + Either ret = testInstance.getArtifactById( + INSTANCE_ID, "artifactDefId", ComponentTypeEnum.RESOURCE_INSTANCE, "containerId"); + + assertTrue(ret.isLeft()); + } + + @Test + public void removeArtifactFromResource() { + Map instanceArtifacts = getMapArtifactsByName2("artifactDefId"); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(StorageOperationStatus.OK).when(testInstance).deleteToscaDataElement(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS, VertexTypeEnum.ARTIFACTS, + null, JsonPresentationFields.ARTIFACT_LABEL); + Either ret = testInstance.removeArifactFromResource( + INSTANCE_ID, "instanceId", NodeTypeEnum.Resource, false); + + assertTrue(ret.isLeft()); + } + + @Test + public void removeArtifactFromResource_Mandatory() { + Map instanceArtifacts = getMapArtifactsByName2("artifactDefId"); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(StorageOperationStatus.OK).when(testInstance).deleteToscaDataElement(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS, VertexTypeEnum.ARTIFACTS, + null, JsonPresentationFields.ARTIFACT_LABEL); + Either ret = testInstance.removeArifactFromResource( + INSTANCE_ID, "instanceId", NodeTypeEnum.Resource, true); + + assertTrue(ret.isLeft()); + } + + @Test + public void removeArtifactFromResource_NotFound() { + doReturn(Either.right(StorageOperationStatus.NOT_FOUND)).when(testInstance).getArtifactById(INSTANCE_ID, INSTANCE_ID); + Either ret = testInstance.removeArifactFromResource( + INSTANCE_ID, "instanceId", NodeTypeEnum.Resource, false); + + assertTrue(ret.isRight()); + } + + @Test + public void removeArtifactFromResource_NotFound2() { + doReturn(Either.right(StorageOperationStatus.NOT_FOUND)).when(testInstance).removeArtifactOnGraph(INSTANCE_ID, INSTANCE_ID, + NodeTypeEnum.Resource, false); + Either ret = testInstance.removeArifactFromResource( + INSTANCE_ID, "instanceId", NodeTypeEnum.Resource, false); + + assertTrue(ret.isRight()); + } + + @Test + public void getArtifacts() { + Map instanceArtifacts = getMapArtifactsByName2("artifactDefId"); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + + Either, StorageOperationStatus> ret = testInstance.getArtifacts(INSTANCE_ID, NodeTypeEnum.Resource, + ArtifactGroupTypeEnum.INFORMATIONAL, INSTANCE_ID); + + assertTrue(ret.isLeft()); + } + + @Test + public void getArtifacts_NotFound() { + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + + Either, StorageOperationStatus> ret = testInstance.getArtifacts(INSTANCE_ID, NodeTypeEnum.Resource, + ArtifactGroupTypeEnum.INFORMATIONAL, INSTANCE_ID); + + assertTrue(ret.isRight()); + } + + @Test + public void getArtifacts2() { + Map instanceArtifacts = getMapArtifactsByName2("artifactDefId"); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.TOSCA_ARTIFACTS); + + Either, StorageOperationStatus> ret = testInstance.getArtifacts(INSTANCE_ID); + + assertTrue(ret.isLeft()); + } + + @Test + public void addHeatEnvArtifact() { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + ArtifactDefinition artifactDefHeat = new ArtifactDefinition(); + artifactDefHeat.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + final Resource resource = new Resource(); + resource.setUniqueId(INSTANCE_ID); + doReturn(Either.left(false)).when(testInstance).isCloneNeeded(Mockito.any(), + Mockito.any()); + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(StorageOperationStatus.OK).when(testInstance).updateToscaDataOfToscaElement(Mockito.anyString(), + Mockito.any(), Mockito.any(), (List) Mockito.any(), Mockito.any()); + Either ret = testInstance.addHeatEnvArtifact(artifactDef, artifactDefHeat, + resource, NodeTypeEnum.Resource, false, INSTANCE_ID); + + assertTrue(ret.isLeft()); + } + + @Test + public void getHeatArtifactByHeatEnvId() { + Map interfaceDefinitions = getInterfaceDataDefinitionsByName("artifactDefId"); + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + ArtifactDefinition artifactDefHeat = new ArtifactDefinition(); + artifactDefHeat.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.TOSCA_ARTIFACTS); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.SERVICE_API_ARTIFACTS); + doReturn(Either.left(interfaceDefinitions)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.INTERFACE); + Either ret = testInstance.getHeatArtifactByHeatEnvId( + INSTANCE_ID, artifactDefHeat, "containerId", ComponentTypeEnum.RESOURCE); + + assertTrue(ret.isRight()); + } + + @Test + public void updateHeatEnvArtifact() { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setUniqueId("artifactId"); + artifactDef.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + ArtifactDefinition artifactDefHeat = new ArtifactDefinition(); + artifactDefHeat.setUniqueId("newArtifactId"); + artifactDefHeat.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + final Resource resource = new Resource(); + resource.setUniqueId(INSTANCE_ID); + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(Either.left(false)).when(testInstance).isCloneNeeded(Mockito.any(), + Mockito.any()); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(StorageOperationStatus.OK).when(testInstance).updateToscaDataOfToscaElement(Mockito.anyString(), + Mockito.any(), Mockito.any(), (List) Mockito.any(), Mockito.any()); + + Either ret = testInstance.updateHeatEnvArtifact( + resource, artifactDef, "artifactId", "newArtifactId", NodeTypeEnum.Resource, INSTANCE_ID); + + assertTrue(ret.isLeft()); + } + + @Test + public void updateHeatEnvArtifact_NotFound() { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setUniqueId("artifactId"); + artifactDef.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + ArtifactDefinition artifactDefHeat = new ArtifactDefinition(); + artifactDefHeat.setUniqueId("newArtifactId"); + artifactDefHeat.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + final Resource resource = new Resource(); + resource.setUniqueId(INSTANCE_ID); + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + Either ret = testInstance.updateHeatEnvArtifact( + resource, artifactDef, "artifactId", "newArtifactId", NodeTypeEnum.Resource, INSTANCE_ID); + + assertTrue(ret.isRight()); + } + + @Test + public void updateHeatEnvArtifactOnInstance() { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setUniqueId("artifactId"); + artifactDef.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + ArtifactDefinition artifactDefHeat = new ArtifactDefinition(); + artifactDefHeat.setUniqueId("newArtifactId"); + artifactDefHeat.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + final Resource resource = new Resource(); + resource.setUniqueId(INSTANCE_ID); + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS); + doReturn(Either.left(false)).when(testInstance).isCloneNeeded(Mockito.any(), + Mockito.any()); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(StorageOperationStatus.OK).when(testInstance).updateToscaDataOfToscaElement(Mockito.anyString(), + Mockito.any(), Mockito.any(), (List) Mockito.any(), Mockito.any()); + + Either ret = testInstance.updateHeatEnvArtifactOnInstance( + resource, artifactDef, "artifactId", "newArtifactId", NodeTypeEnum.Resource, INSTANCE_ID); + + assertTrue(ret.isLeft()); + } + + @Test + public void updateHeatEnvPlaceholder() { + ArtifactDefinition artifactDef = new ArtifactDefinition(); + artifactDef.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + final Resource resource = new Resource(); + resource.setUniqueId(INSTANCE_ID); + Map instanceArtifacts = getMapArtifactsByName("artifactDefId"); + doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(INSTANCE_ID, EdgeLabelEnum.ARTIFACTS); + doReturn(Either.left(false)).when(testInstance).isCloneNeeded(Mockito.any(), + Mockito.any()); + Map artMap = new HashMap<>(); + artMap.put(INSTANCE_ID, artifactDef); + doReturn(StorageOperationStatus.OK).when(testInstance).updateToscaDataOfToscaElement(Mockito.anyString(), + Mockito.any(), Mockito.any(), (List) Mockito.any(), Mockito.any()); + doReturn(Either.left(artifactDef)).when(testInstance).updateArtifactOnGraph(resource, + artifactDef, NodeTypeEnum.Resource, null, INSTANCE_ID, false, + false); + + Either ret = testInstance.updateHeatEnvPlaceholder( + artifactDef, resource, NodeTypeEnum.Resource); + + assertTrue(ret.isLeft()); + } + @Test public void getInstanceArtifacts_collectAllInstanceArtifacts() throws Exception { Map instanceArtifacts = Collections.singletonMap(INSTANCE_ID, getArtifactsByInstance("name1")); @@ -53,7 +569,8 @@ public class ArtifactsOperationsTest { doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INSTANCE_ARTIFACTS); doReturn(Either.left(instanceDeploymentArtifacts)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); - Either, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID); + Either, StorageOperationStatus> allInstArtifacts = + testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID); assertTrue(allInstArtifacts.isLeft()); assertEquals(allInstArtifacts.left().value().size(), 3); @@ -69,7 +586,8 @@ public class ArtifactsOperationsTest { doReturn(Either.left(instanceArtifacts)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INSTANCE_ARTIFACTS); doReturn(Either.left(new HashMap<>())).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); - Either, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, "someOtherInstance"); + Either, StorageOperationStatus> allInstArtifacts = + testInstance.getAllInstanceArtifacts(SERVICE_ID, "someOtherInstance"); assertTrue(allInstArtifacts.isLeft()); assertTrue(allInstArtifacts.left().value().isEmpty()); @@ -77,8 +595,10 @@ public class ArtifactsOperationsTest { @Test public void getInstanceArtifacts_errorGettingInstanceArtifacts() throws Exception { - doReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INSTANCE_ARTIFACTS); - Either, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID); + doReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR)).when(testInstance) + .getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INSTANCE_ARTIFACTS); + Either, StorageOperationStatus> allInstArtifacts = + testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID); verify(testInstance, times(0)).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); assertTrue(allInstArtifacts.isRight()); } @@ -86,8 +606,10 @@ public class ArtifactsOperationsTest { @Test public void getAllInstanceArtifacts_errorGettingDeploymentArtifacts() throws Exception { doReturn(Either.left(new HashMap<>())).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INSTANCE_ARTIFACTS); - doReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); - Either, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID); + doReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR)).when(testInstance).getDataFromGraph(SERVICE_ID, + EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + Either, StorageOperationStatus> allInstArtifacts = + testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID); assertTrue(allInstArtifacts.isRight()); } @@ -100,4 +622,86 @@ public class ArtifactsOperationsTest { artifactsByInstance.setMapToscaDataDefinition(artifactsByName); return artifactsByInstance; } + + private ToscaDataDefinition getArtifactsByInstanceSettingUniqueId(String ... artifactsNames) { + MapArtifactDataDefinition artifactsByInstance = new MapArtifactDataDefinition(); + Map artifactsByName = new HashMap<>(); + for (String artifactName : artifactsNames) { + ArtifactDataDefinition artifactDataDefinition = new ArtifactDataDefinition(); + artifactDataDefinition.setUniqueId(artifactName); + artifactsByName.put(artifactName, artifactDataDefinition); + } + artifactsByInstance.setMapToscaDataDefinition(artifactsByName); + return artifactsByInstance; + } + + private Map getMapArtifactsByName(String ... artifactsNames) { + Map artifactsByName = new HashMap<>(); + for (String artifactName : artifactsNames) { + ArtifactDataDefinition artifactDataDefinition = new ArtifactDataDefinition(); + artifactDataDefinition.setUniqueId(artifactName); + artifactDataDefinition.setGeneratedFromId("artifactId"); + artifactDataDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + artifactsByName.put(artifactName, artifactDataDefinition); + } + return artifactsByName; + } + + private Map getMapArtifactsByName2(String ... artifactsNames) { + Map artifactsByName = new HashMap<>(); + for (String artifactName : artifactsNames) { + ArtifactDataDefinition artifactDataDefinition = new ArtifactDataDefinition(); + artifactDataDefinition.setUniqueId(INSTANCE_ID); + artifactDataDefinition.setMandatory(true); + artifactDataDefinition.setArtifactGroupType(ArtifactGroupTypeEnum.INFORMATIONAL); + artifactsByName.put(artifactName, artifactDataDefinition); + } + return artifactsByName; + } + + private Map getInterfaceDataDefinitionsByName(String ... artifactsNames) { + Map interfaceDataDefinitionsByName = new HashMap<>(); + for (String artifactName : artifactsNames) { + InterfaceDataDefinition interfaceDataDefinition = new InterfaceDataDefinition(); + interfaceDataDefinition.setUniqueId(artifactName); + interfaceDataDefinitionsByName.put(artifactName, interfaceDataDefinition); + } + return interfaceDataDefinitionsByName; + } + + private Map getInterfaceDataDefinitionsWithOperationsByName(String ... artifactsNames) { + Map interfaceDataDefinitionsByName = new HashMap<>(); + for (String artifactName : artifactsNames) { + Map operations = new HashMap<>(); + OperationDataDefinition operation1 = new OperationDataDefinition(); + operation1.setName("operation1"); + ArtifactDataDefinition impl1 = new ArtifactDataDefinition(); + impl1.setUniqueId(artifactName); + operation1.setImplementation(impl1); + operations.put(artifactName, operation1); + InterfaceDataDefinition interfaceDataDefinition = new InterfaceDataDefinition(); + interfaceDataDefinition.setUniqueId(artifactName); + interfaceDataDefinition.setOperations(operations); + interfaceDataDefinitionsByName.put(artifactName, interfaceDataDefinition); + } + return interfaceDataDefinitionsByName; + } + + private Map getInterfaceDataDefinitionsWithOperationsByNameNotFound(String ... artifactsNames) { + Map interfaceDataDefinitionsByName = new HashMap<>(); + for (String artifactName : artifactsNames) { + Map operations = new HashMap<>(); + OperationDataDefinition operation1 = new OperationDataDefinition(); + operation1.setName("operation1"); + ArtifactDataDefinition impl1 = new ArtifactDataDefinition(); + impl1.setUniqueId("implementation1"); + operation1.setImplementation(impl1); + operations.put(artifactName, operation1); + InterfaceDataDefinition interfaceDataDefinition = new InterfaceDataDefinition(); + interfaceDataDefinition.setUniqueId(artifactName); + interfaceDataDefinition.setOperations(operations); + interfaceDataDefinitionsByName.put(artifactName, interfaceDataDefinition); + } + return interfaceDataDefinitionsByName; + } } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ForwardingPathOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ForwardingPathOperationTest.java new file mode 100644 index 0000000000..9889e90bc0 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/ForwardingPathOperationTest.java @@ -0,0 +1,212 @@ +/* + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2023 Nordix Foundation. 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.openecomp.sdc.be.model.jsonjanusgraph.operations; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +import fj.data.Either; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.janusgraph.core.JanusGraphVertex; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; +import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; + +@ExtendWith(MockitoExtension.class) +public class ForwardingPathOperationTest { + + @Mock + private JanusGraphDao janusGraphDao; + + @Mock + private JanusGraphVertex vertex; + + private ForwardingPathOperation test = mock(ForwardingPathOperation.class, CALLS_REAL_METHODS); + + @BeforeEach + public void setUp() throws Exception { + test.setJanusGraphDao(janusGraphDao); + } + + @Test + public void deleteForwardingPath() { + Service service = new Service(); + Set paths = Set.of("path1", "path2"); + String uniqueId = "uniqueId"; + service.setUniqueId(uniqueId); + Map hasProps1 = new HashMap<>(); + hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + List list = new ArrayList<>(); + GraphVertex graphVertex = new GraphVertex(); + graphVertex.setVertex(vertex); + graphVertex.setUniqueId(uniqueId); + graphVertex.setMetadataProperties(hasProps1); + list.add(graphVertex); + doReturn(Either.left(graphVertex)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse); + doReturn(StorageOperationStatus.OK).when(test).deleteToscaDataElements(graphVertex, EdgeLabelEnum.FORWARDING_PATH, + new ArrayList<>(paths)); + Either, StorageOperationStatus> ret = test.deleteForwardingPath(service, paths); + assertTrue(ret.isLeft()); + } + + @Test + public void deleteForwardingPath_NotFound() { + Service service = new Service(); + Set paths = Set.of("path1", "path2"); + String uniqueId = "uniqueId"; + service.setUniqueId(uniqueId); + Map hasProps1 = new HashMap<>(); + hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + List list = new ArrayList<>(); + GraphVertex graphVertex = new GraphVertex(); + graphVertex.setVertex(vertex); + graphVertex.setUniqueId(uniqueId); + graphVertex.setMetadataProperties(hasProps1); + list.add(graphVertex); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(janusGraphDao).getVertexById(service.getUniqueId(), + JsonParseFlagEnum.NoParse); + Either, StorageOperationStatus> ret = test.deleteForwardingPath(service, paths); + assertTrue(ret.isRight()); + } + + @Test + public void deleteForwardingPath_ErrorDeleting() { + Service service = new Service(); + Set paths = Set.of("path1", "path2"); + String uniqueId = "uniqueId"; + service.setUniqueId(uniqueId); + Map hasProps1 = new HashMap<>(); + hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + List list = new ArrayList<>(); + GraphVertex graphVertex = new GraphVertex(); + graphVertex.setVertex(vertex); + graphVertex.setUniqueId(uniqueId); + graphVertex.setMetadataProperties(hasProps1); + list.add(graphVertex); + doReturn(Either.left(graphVertex)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse); + doReturn(StorageOperationStatus.CANNOT_UPDATE_EXISTING_ENTITY).when(test).deleteToscaDataElements(graphVertex, EdgeLabelEnum.FORWARDING_PATH, + new ArrayList<>(paths)); + Either, StorageOperationStatus> ret = test.deleteForwardingPath(service, paths); + assertTrue(ret.isRight()); + } + + @Test + public void addForwardingPath() { + Service service = new Service(); + String uniqueId = "uniqueId"; + service.setUniqueId(uniqueId); + Map hasProps1 = new HashMap<>(); + hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + GraphVertex graphVertex = new GraphVertex(); + graphVertex.setVertex(vertex); + graphVertex.setUniqueId(uniqueId); + graphVertex.setMetadataProperties(hasProps1); + ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition("forwardingPath"); + doReturn(Either.left(graphVertex)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse); + doReturn(StorageOperationStatus.OK).when(test).addToscaDataToToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH, + VertexTypeEnum.FORWARDING_PATH, List.of(forwardingPathDataDefinition), JsonPresentationFields.UNIQUE_ID); + Either ret = test.addForwardingPath(service.getUniqueId(), forwardingPathDataDefinition); + assertTrue(ret.isLeft()); + } + + @Test + public void addForwardingPath_NotFound() { + Service service = new Service(); + String uniqueId = "uniqueId"; + service.setUniqueId(uniqueId); + Map hasProps1 = new HashMap<>(); + hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + GraphVertex graphVertex = new GraphVertex(); + graphVertex.setVertex(vertex); + graphVertex.setUniqueId(uniqueId); + graphVertex.setMetadataProperties(hasProps1); + ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition("forwardingPath"); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse); + Either ret = test.addForwardingPath(service.getUniqueId(), forwardingPathDataDefinition); + assertTrue(ret.isRight()); + } + + @Test + public void updateForwardingPath() { + Service service = new Service(); + String uniqueId = "uniqueId"; + service.setUniqueId(uniqueId); + Map hasProps1 = new HashMap<>(); + hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + GraphVertex graphVertex = new GraphVertex(); + graphVertex.setVertex(vertex); + graphVertex.setUniqueId(uniqueId); + graphVertex.setMetadataProperties(hasProps1); + ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition("forwardingPath"); + doReturn(Either.left(graphVertex)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse); + doReturn(StorageOperationStatus.OK).when(test).updateToscaDataOfToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH, + VertexTypeEnum.FORWARDING_PATH, List.of(forwardingPathDataDefinition), JsonPresentationFields.UNIQUE_ID); + Either ret = test.updateForwardingPath(service.getUniqueId(), forwardingPathDataDefinition); + assertTrue(ret.isLeft()); + } + + @Test + public void updateForwardingPath_Error() { + Service service = new Service(); + String uniqueId = "uniqueId"; + service.setUniqueId(uniqueId); + Map hasProps1 = new HashMap<>(); + hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + GraphVertex graphVertex = new GraphVertex(); + graphVertex.setVertex(vertex); + graphVertex.setUniqueId(uniqueId); + graphVertex.setMetadataProperties(hasProps1); + ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition("forwardingPath"); + doReturn(Either.left(graphVertex)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse); + doReturn(StorageOperationStatus.ARTIFACT_NOT_FOUND).when(test).updateToscaDataOfToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH, + VertexTypeEnum.FORWARDING_PATH, List.of(forwardingPathDataDefinition), JsonPresentationFields.UNIQUE_ID); + Either ret = test.updateForwardingPath(service.getUniqueId(), forwardingPathDataDefinition); + assertTrue(ret.isRight()); + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/GroupsOperationMockTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/GroupsOperationMockTest.java new file mode 100644 index 0000000000..9c633b6ad8 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/GroupsOperationMockTest.java @@ -0,0 +1,142 @@ +/* + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2023 Nordix Foundation. 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.openecomp.sdc.be.model.jsonjanusgraph.operations; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +import com.google.gson.Gson; +import fj.data.Either; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.janusgraph.core.JanusGraphVertex; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; +import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.model.GroupDefinition; +import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate; +import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; + +@ExtendWith(MockitoExtension.class) +public class GroupsOperationMockTest { + + @Mock + private JanusGraphDao janusGraphDao; + + @Mock + private JanusGraphVertex vertex; + + protected static final String CONTAINER_ID = "containerId"; + protected static final String CONTAINER_NAME = "containerName"; + + private GroupsOperation test = mock(GroupsOperation.class, CALLS_REAL_METHODS); + + @BeforeEach + public void setUp() throws Exception { + test.setJanusGraphDao(janusGraphDao); + } + + @Test + public void deleteCalculatedCapabilitiesWithProperties() { + GroupDefinition g1 = createGroupDefinition("g1"); + GroupDefinition g2 = createGroupDefinition("g2"); + List listGroup = new ArrayList<>(); + listGroup.add(g1); + listGroup.add(g2); + GraphVertex createdGraphVertex = createBasicContainerGraphVertex(); + doReturn(Either.left(createdGraphVertex)).when(janusGraphDao).getChildVertex(createdGraphVertex, EdgeLabelEnum.CALCULATED_CAPABILITIES, + JsonParseFlagEnum.ParseJson); + doReturn(Either.left(createdGraphVertex)).when(janusGraphDao).getVertexById(CONTAINER_ID, JsonParseFlagEnum.NoParse); + doReturn(Either.left(createdGraphVertex)).when(test).updateOrCopyOnUpdate(createdGraphVertex, createdGraphVertex, + EdgeLabelEnum.CALCULATED_CAPABILITIES); + doReturn(Either.left(createdGraphVertex)).when(janusGraphDao).getChildVertex(createdGraphVertex, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, + JsonParseFlagEnum.ParseJson); + doReturn(Either.left(createdGraphVertex)).when(test).updateOrCopyOnUpdate(createdGraphVertex, createdGraphVertex, + EdgeLabelEnum.CALCULATED_CAP_PROPERTIES); + final StorageOperationStatus ret = test.deleteCalculatedCapabilitiesWithProperties(CONTAINER_ID, listGroup); + assertEquals(StorageOperationStatus.OK, ret); + } + + @Test + public void deleteCalculatedCapabilitiesWithProperties_NotFound() { + GroupDefinition g1 = createGroupDefinition("g1"); + GroupDefinition g2 = createGroupDefinition("g2"); + List listGroup = new ArrayList<>(); + listGroup.add(g1); + listGroup.add(g2); + GraphVertex createdGraphVertex = createBasicContainerGraphVertex(); + doReturn(Either.left(createdGraphVertex)).when(janusGraphDao).getVertexById(CONTAINER_ID, JsonParseFlagEnum.NoParse); + doReturn(Either.left(createdGraphVertex)).when(janusGraphDao).getChildVertex(createdGraphVertex, EdgeLabelEnum.CALCULATED_CAPABILITIES, + JsonParseFlagEnum.ParseJson); + doReturn(Either.left(createdGraphVertex)).when(test).updateOrCopyOnUpdate(createdGraphVertex, createdGraphVertex, + EdgeLabelEnum.CALCULATED_CAPABILITIES); + doReturn(Either.left(createdGraphVertex)).when(janusGraphDao).getChildVertex(createdGraphVertex, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, + JsonParseFlagEnum.ParseJson); + doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(test).updateOrCopyOnUpdate(createdGraphVertex, createdGraphVertex, + EdgeLabelEnum.CALCULATED_CAP_PROPERTIES); + final StorageOperationStatus ret = test.deleteCalculatedCapabilitiesWithProperties(CONTAINER_ID, listGroup); + assertEquals(StorageOperationStatus.NOT_FOUND, ret); + } + + private GroupDefinition createGroupDefinition(String id) { + GroupDefinition groupDefinition = new GroupDefinition(); + groupDefinition.setUniqueId(id); + groupDefinition.setInvariantName("name" + id); + return groupDefinition; + } + + private GraphVertex createBasicContainerGraphVertex() { + GraphVertex resource = new GraphVertex(VertexTypeEnum.TOPOLOGY_TEMPLATE); + resource.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, CONTAINER_ID); + resource.addMetadataProperty(GraphPropertyEnum.NAME, CONTAINER_NAME); + resource.setJsonMetadataField(JsonPresentationFields.NAME, CONTAINER_NAME); + resource.setJsonMetadataField(JsonPresentationFields.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + Map groups = new HashMap<>(); + GroupDefinition g1 = createGroupDefinition("g1"); + GroupDefinition g2 = createGroupDefinition("g2"); + groups.put(g1.getInvariantName(), new GroupDataDefinition(g1)); + groups.put(g2.getInvariantName(), new GroupDataDefinition(g2)); + resource.setJson(groups); + return resource; + } +} -- cgit 1.2.3-korg