diff options
Diffstat (limited to 'catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations')
10 files changed, 1180 insertions, 16 deletions
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ExternalReferencesOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ExternalReferencesOperationTest.java new file mode 100644 index 0000000000..c0ba256957 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ExternalReferencesOperationTest.java @@ -0,0 +1,178 @@ +package org.openecomp.sdc.be.model.jsontitan.operations; + +import fj.data.Either; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.MapComponentInstanceExternalRefs; +import org.openecomp.sdc.be.model.ModelTestBase; +import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils; +import org.openecomp.sdc.be.model.jsontitan.utils.IdMapper; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +/** + * Created by yavivi on 26/01/2018. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:application-context-test.xml") +public class ExternalReferencesOperationTest extends ModelTestBase { + + @Resource + private ExternalReferencesOperation externalReferenceOperation; + + @Resource + private TitanDao titanDao; + + private boolean isInitialized; + + private GraphVertex serviceVertex; + + private String serviceVertexUuid; + private static final String COMPONENT_ID = "ci-MyComponentName"; + + private static final String MONITORING_OBJECT_TYPE = "monitoring"; + private static final String WORKFLOW_OBJECT_TYPE = "workflow"; + private static final String REF_1 = "ref1"; + private static final String REF_2 = "ref2"; + private static final String REF_3 = "ref3"; + private static final String REF_4 = "ref4"; + private static final String REF_5 = "ref5"; + + //workflow + private static final String REF_6 = "ref6"; + + private IdMapper idMapper; + + @BeforeClass + public static void initTest(){ + ModelTestBase.init(); + } + + @Before + public void beforeTest() { + idMapper = Mockito.mock(IdMapper.class); + this.externalReferenceOperation.setIdMapper(idMapper); + when(idMapper.mapComponentNameToUniqueId(Mockito.anyString(), Mockito.any(GraphVertex.class))).thenReturn(COMPONENT_ID); + if (!isInitialized) { + GraphTestUtils.clearGraph(titanDao); + initGraphForTest(); + isInitialized = true; + } + } + + @Test + public void testAddComponentInstanceExternalRef(){ + Either<String, ActionStatus> addResult = externalReferenceOperation.addExternalReference(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_4); + assertThat(addResult.isLeft()).isEqualTo(true); + + //commit changes to titan + final TitanOperationStatus commit = this.titanDao.commit(); + assertThat(commit).isEqualTo(TitanOperationStatus.OK); + + assertThat(getServiceExternalRefs()).contains(REF_1, REF_2, REF_3, REF_4); + } + + @Test + public void testGetComponentInstanceExternalRef(){ + assertThat(externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE).left().value()).contains(REF_1, REF_2, REF_3, REF_5); + assertThat(externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE).left().value()).containsExactly(REF_6); + } + + @Test + public void testGetComponentInstanceExternalRefForNonExistingObjectId(){ + assertThat(externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE).left().value()).contains(REF_1, REF_2, REF_3, REF_5); + Either<List<String>, ActionStatus> getResult = externalReferenceOperation.getExternalReferences(this.serviceVertexUuid, COMPONENT_ID, "FAKE_OBJECT_TYPE"); + assertThat(getResult.left().value()).isEmpty(); + } + + @Test + public void testDeleteComponentInstanceExternalRef(){ + //Test the precondition + assertThat(getServiceExternalRefs()).contains(REF_5); + + //Remove REF 5 + Either<String, ActionStatus> deleteStatus = externalReferenceOperation.deleteExternalReference(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); + assertThat(deleteStatus.isLeft()).isEqualTo(true); + + //commit changes to titan + final TitanOperationStatus commit = this.titanDao.commit(); + assertThat(commit).isEqualTo(TitanOperationStatus.OK); + + //Check that ref does not exist anymore + assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3); + } + + @Test + public void testUpdateComponentInstanceExternalRef(){ + //Test the precondition + assertThat(getServiceExternalRefs()).contains(REF_5).doesNotContain(REF_4); + + //Update REF 5 with REF_4 + Either<String, ActionStatus> updateResult = externalReferenceOperation.updateExternalReference(this.serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5, REF_4); + + assertThat(updateResult.isLeft()).isEqualTo(true); + + //commit changes to titan + final TitanOperationStatus commit = this.titanDao.commit(); + assertThat(commit).isEqualTo(TitanOperationStatus.OK); + + //Check that ref does not exist anymore + assertThat(getServiceExternalRefs()).doesNotContain(REF_5).contains(REF_1, REF_2, REF_3, REF_4); + } + + private List<String> getServiceExternalRefs(){ + //Get service vertex + final Either<GraphVertex, TitanOperationStatus> externalRefsVertexResult = this.titanDao.getChildVertex(this.serviceVertex, EdgeLabelEnum.EXTERNAL_REFS, JsonParseFlagEnum.ParseJson); + assertThat(externalRefsVertexResult.isLeft()).isEqualTo(true); + + GraphVertex externalRefVertex = externalRefsVertexResult.left().value(); + + //Get the full map + Map<String, MapComponentInstanceExternalRefs> componentInstancesMap = (Map<String, MapComponentInstanceExternalRefs>) externalRefVertex.getJson(); + assertThat(componentInstancesMap).isNotNull(); + + //Get Map of external refs by object type + final MapComponentInstanceExternalRefs mapComponentInstanceExternalRefs = componentInstancesMap.get(COMPONENT_ID); + + //Get List of references + //final List<String> externalRefsByObjectType = mapComponentInstanceExternalRefs.externalRefsByObjectType(objectType); + final List<String> externalRefsByObjectType = mapComponentInstanceExternalRefs.getExternalRefsByObjectType(MONITORING_OBJECT_TYPE); + + return externalRefsByObjectType; + } + + private void initGraphForTest() { + //create a service and add 1 ref + this.serviceVertex = GraphTestUtils.createServiceVertex(titanDao, new HashMap<>()); + this.serviceVertexUuid = this.serviceVertex.getUniqueId(); + + //monitoring references + externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_1); + externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_2); + externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_3); + externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, MONITORING_OBJECT_TYPE, REF_5); + + //workflow references + externalReferenceOperation.addExternalReference(serviceVertexUuid, COMPONENT_ID, WORKFLOW_OBJECT_TYPE, REF_6); + + final TitanOperationStatus commit = this.titanDao.commit(); + assertThat(commit).isEqualTo(TitanOperationStatus.OK); + } +}
\ No newline at end of file diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/NodeTemplateOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/NodeTemplateOperationTest.java index a1a257b893..71d54bca73 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/NodeTemplateOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/NodeTemplateOperationTest.java @@ -25,7 +25,6 @@ import org.openecomp.sdc.be.model.RelationshipImpl; import org.openecomp.sdc.be.model.RelationshipInfo; import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; - import com.google.common.collect.Lists; import fj.data.Either; diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/PolicyOperationIntegrationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/PolicyOperationIntegrationTest.java new file mode 100644 index 0000000000..d4188744d8 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/PolicyOperationIntegrationTest.java @@ -0,0 +1,133 @@ +package org.openecomp.sdc.be.model.jsontitan.operations; + +import fj.data.Either; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openecomp.sdc.be.dao.config.TitanSpringConfig; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +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.PolicyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.ModelTestBase; +import org.openecomp.sdc.be.model.PolicyDefinition; +import org.openecomp.sdc.be.model.config.ModelOperationsSpringConfig; +import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate; +import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.annotation.Resource; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import static org.apache.commons.collections.ListUtils.union; +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {TitanSpringConfig.class, ModelOperationsSpringConfig.class}) +public class PolicyOperationIntegrationTest extends ModelTestBase { + + private static final String CONTAINER_ID = "container"; + public static final String POLICY_ID = "policy"; + @Resource + private TopologyTemplateOperation topologyTemplateOperation; + @Resource + private TitanDao titanDao; + @Resource + private PolicyOperation policyOperation; + private PropertyDataDefinition prop1, prop2; + private PolicyDefinition policy; + + @BeforeClass + public static void setupBeforeClass() { + + ModelTestBase.init(); + } + + @Before + public void setUp() throws Exception { + prop1 = new PropertyDataDefinition(); + prop1.setUniqueId("prop1"); + prop1.setName("prop1"); + prop1.setValue("prop1"); + + prop2 = new PropertyDataDefinition(); + prop2.setUniqueId("prop2"); + prop2.setName("prop2"); + prop2.setValue("prop2"); + + policy = new PolicyDefinition(); + policy.setUniqueId(POLICY_ID); + + policy.setProperties(Arrays.asList(prop1, prop2)); + + GraphVertex resource = new GraphVertex(VertexTypeEnum.TOPOLOGY_TEMPLATE); + resource.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, CONTAINER_ID); + titanDao.createVertex(resource); + GraphVertex loadedResource = titanDao.getVertexById(CONTAINER_ID).left().value(); + topologyTemplateOperation.addToscaDataToToscaElement(loadedResource, EdgeLabelEnum.POLICIES, VertexTypeEnum.POLICIES, policy, JsonPresentationFields.UNIQUE_ID); + titanDao.commit(); + } + + @After + public void tearDown() { + titanDao.rollback(); + } + + @Test + public void testUpdatePolicyProperties_singleProperty() { + PropertyDataDefinition prop1Copy = new PropertyDataDefinition(prop1); + prop1Copy.setValue("prop1NewValue"); + testUpdatePolicyProperties(Collections.singletonList(prop1Copy), Collections.singletonList(prop2)); + } + + @Test + public void testUpdatePolicyProperties_multipleProperties() { + PropertyDataDefinition prop1Copy = new PropertyDataDefinition(prop1); + prop1Copy.setValue("prop1NewValue"); + + PropertyDataDefinition prop2Copy = new PropertyDataDefinition(prop2); + prop2Copy.setValue("prop2NewValue"); + + testUpdatePolicyProperties(Arrays.asList(prop1Copy, prop2Copy), Collections.emptyList()); + } + + private void testUpdatePolicyProperties(List<PropertyDataDefinition> updatedProperties, List<PropertyDataDefinition> nonUpdatedPropeties) { + Component cmpt = new org.openecomp.sdc.be.model.Resource(); + cmpt.setUniqueId(CONTAINER_ID); + cmpt.setPolicies(Collections.singletonMap(POLICY_ID, policy)); + StorageOperationStatus storageOperationStatus = policyOperation.updatePolicyProperties(cmpt, POLICY_ID, updatedProperties); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK); + + ComponentParametersView componentParametersView = new ComponentParametersView(); + componentParametersView.disableAll(); + componentParametersView.setIgnorePolicies(false); + Either<ToscaElement, StorageOperationStatus> loadedCmptEither = topologyTemplateOperation.getToscaElement(CONTAINER_ID, componentParametersView); + + assertThat(loadedCmptEither.isLeft()); + ToscaElement loadedCmpt = loadedCmptEither.left().value(); + assertThat(loadedCmpt).isInstanceOf(TopologyTemplate.class); + @SuppressWarnings("unchecked") List<PropertyDataDefinition> allProperties = union(updatedProperties, nonUpdatedPropeties); + verifyPolicyPropertiesValuesUpdated((TopologyTemplate) loadedCmpt, allProperties); + } + + private void verifyPolicyPropertiesValuesUpdated(TopologyTemplate toscaElement, List<PropertyDataDefinition> expectedUpdatedProperties) { + Map<String, PolicyDataDefinition> policies = toscaElement.getPolicies(); + PolicyDataDefinition policy = policies.get(POLICY_ID); + List<PropertyDataDefinition> policyProperties = policy.getProperties(); + assertThat(policyProperties).usingElementComparatorOnFields("value") + .containsAll(expectedUpdatedProperties); + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/PolicyOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/PolicyOperationTest.java new file mode 100644 index 0000000000..136f95b987 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/PolicyOperationTest.java @@ -0,0 +1,93 @@ +package org.openecomp.sdc.be.model.jsontitan.operations; + +import fj.data.Either; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.PolicyDefinition; +import org.openecomp.sdc.be.model.Resource; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; + +import java.util.Arrays; +import java.util.Collections; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class PolicyOperationTest { + + private static final String CONTAINER_ID = "containerId"; + private static final String POLICY_ID = "policyId"; + @InjectMocks + private PolicyOperation testInstance; + @Mock + private TitanDao titanDao; + @Mock + private TopologyTemplateOperation topologyTemplateOperation; + @Captor + private ArgumentCaptor<PolicyDefinition> policyDefCaptor; + private Component component; + private PolicyDefinition policyDefinition; + private PropertyDataDefinition prop1, prop2; + + @Before + public void setUp() throws Exception { + component = new Resource(); + component.setUniqueId(CONTAINER_ID); + policyDefinition = new PolicyDefinition(); + policyDefinition.setUniqueId(POLICY_ID); + prop1 = new PropertyDataDefinition(); + prop1.setName("prop1"); + prop1.setValue("prop1"); + + prop2 = new PropertyDataDefinition(); + prop2.setName("prop2"); + prop2.setValue("prop2"); + policyDefinition.setProperties(Arrays.asList(prop1, prop2)); + component.setPolicies(Collections.singletonMap(POLICY_ID, policyDefinition)); + } + + @Test + public void updatePolicyProperties_failedToFetchContainer() { + when(titanDao.getVertexById(CONTAINER_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND)); + StorageOperationStatus storageOperationStatus = testInstance.updatePolicyProperties(component, POLICY_ID, Collections.emptyList()); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.NOT_FOUND); + verifyZeroInteractions(topologyTemplateOperation); + } + + @Test + public void updatePolicyProperties_updateFailed() { + GraphVertex cmptVertex = new GraphVertex(); + when(titanDao.getVertexById(CONTAINER_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(cmptVertex)); + when(topologyTemplateOperation.updatePolicyOfToscaElement(cmptVertex, policyDefinition)).thenReturn(StorageOperationStatus.GENERAL_ERROR); + StorageOperationStatus storageOperationStatus = testInstance.updatePolicyProperties(component, POLICY_ID, Collections.emptyList()); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.GENERAL_ERROR); + } + + @Test + public void updatePolicyProperties() { + GraphVertex cmptVertex = new GraphVertex(); + when(titanDao.getVertexById(CONTAINER_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(cmptVertex)); + when(topologyTemplateOperation.updatePolicyOfToscaElement(eq(cmptVertex), policyDefCaptor.capture())).thenReturn(StorageOperationStatus.OK); + PropertyDataDefinition prop1Copy = new PropertyDataDefinition(prop1); + prop1Copy.setValue("prop1Copy"); + StorageOperationStatus storageOperationStatus = testInstance.updatePolicyProperties(component, POLICY_ID, Collections.singletonList(prop1Copy)); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK); + assertThat(policyDefCaptor.getValue().getProperties()).usingElementComparatorOnFields("value") + .containsExactlyInAnyOrder(prop1Copy, prop2); + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperationCapabilityIntegrationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperationCapabilityIntegrationTest.java new file mode 100644 index 0000000000..103b27d3e6 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperationCapabilityIntegrationTest.java @@ -0,0 +1,199 @@ +package org.openecomp.sdc.be.model.jsontitan.operations; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openecomp.sdc.be.dao.config.TitanSpringConfig; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +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.MapCapabiltyProperty; +import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.ModelTestBase; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.model.config.ModelOperationsSpringConfig; +import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.annotation.Resource; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {TitanSpringConfig.class, ModelOperationsSpringConfig.class}) +public class TopologyTemplateOperationCapabilityIntegrationTest extends ModelTestBase { + + private static final String CONTAINER_ID = "id"; + private Map<String, MapCapabiltyProperty> capabilitiesPropsMap; + private List<CapabilityPropertyDataObject> capabilitiesProperties; + + @Resource + private TopologyTemplateOperation topologyTemplateOperation; + + @Resource + private TitanDao titanDao; + private CapabilityPropertyDataObject capabilityProperty1; + private CapabilityPropertyDataObject capabilityProperty2; + private CapabilityPropertyDataObject capabilityProperty3; + + @BeforeClass + public static void setupBeforeClass() { + + ModelTestBase.init(); + } + + @Before + public void setUp() throws Exception { + + capabilitiesPropsMap = new HashMap<>(); + capabilityProperty1 = new CapabilityPropertyDataObject("instance1", "capability1", "prop1", "val1"); + capabilityProperty2 = new CapabilityPropertyDataObject("instance1", "capability2", "prop2", "val2"); + capabilityProperty3 = new CapabilityPropertyDataObject("instance2", "capability3", "prop3", "val3"); + capabilitiesProperties = Arrays.asList(capabilityProperty1, capabilityProperty2, capabilityProperty3); + + + //capablities props == Map<instance id, Map<capability id, Map<prop id, property>>> + capabilitiesProperties.forEach(capabilitiesProperty -> { + capabilitiesPropsMap.computeIfAbsent(capabilitiesProperty.getInstanceId(), k -> new MapCapabiltyProperty(new HashMap<>())) + .getMapToscaDataDefinition().computeIfAbsent(capabilitiesProperty.getCapabilityId(), k -> new MapPropertiesDataDefinition(new HashMap<>())) + .getMapToscaDataDefinition().computeIfAbsent(capabilitiesProperty.getPropName(), k -> new PropertyDefinition(createPropWithValue(capabilitiesProperty.getPropValue()))); + }); + + GraphVertex resource = new GraphVertex(VertexTypeEnum.TOPOLOGY_TEMPLATE); + resource.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, CONTAINER_ID); + titanDao.createVertex(resource); + GraphVertex loadedResource = titanDao.getVertexById(CONTAINER_ID).left().value(); + topologyTemplateOperation.assosiateElementToData(loadedResource, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, capabilitiesPropsMap).left().value(); + titanDao.commit(); + + } + + @After + public void tearDown() throws Exception { + titanDao.rollback(); + } + + @Test + public void overrideCalculatedCapabilityProperties() { + Map<String, MapCapabiltyProperty> loadedCapPropsMap = fetchCapabilitiesProps(CONTAINER_ID); + compareCapabilitiesProperties(capabilitiesProperties, loadedCapPropsMap); + capabilityProperty1.setPropValue("newVal1"); + capabilityProperty3.setPropValue("newVal3"); + setPropertyValue(capabilitiesPropsMap, capabilityProperty1); + setPropertyValue(capabilitiesPropsMap, capabilityProperty3); + StorageOperationStatus storageOperationStatus = topologyTemplateOperation.overrideToscaDataOfToscaElement(CONTAINER_ID, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, capabilitiesPropsMap); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK); + Map<String, MapCapabiltyProperty> updatedCapPropsMap = fetchCapabilitiesProps(CONTAINER_ID); + compareCapabilitiesProperties(capabilitiesProperties, updatedCapPropsMap); + } + + @Test + public void updateToscaDataDeepElementsBlockToToscaElement() { + assertCapabilityPropValue(capabilityProperty1, "val1"); + assertCapabilityPropValue(capabilityProperty2, "val2"); + assertCapabilityPropValue(capabilityProperty3, "val3"); + + MapCapabiltyProperty instance1Props = capabilitiesPropsMap.get("instance1"); + capabilityProperty1.setPropValue("newVal1"); + setPropertyValue(capabilitiesPropsMap, capabilityProperty1); + + capabilityProperty3.setPropValue("newVal3"); + setPropertyValue(capabilitiesPropsMap, capabilityProperty3); + + Component component = new org.openecomp.sdc.be.model.Resource(); + component.setUniqueId(CONTAINER_ID); + StorageOperationStatus updateStatus = topologyTemplateOperation.updateToscaDataDeepElementsBlockToToscaElement(CONTAINER_ID, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, instance1Props, "instance1"); + + assertThat(updateStatus).isEqualTo(StorageOperationStatus.OK); + assertCapabilityPropValue(capabilityProperty1, "newVal1"); + assertCapabilityPropValue(capabilityProperty2, "val2"); + assertCapabilityPropValue(capabilityProperty3, "val3");//only instance1 props should be updated + } + + private Map<String, MapCapabiltyProperty> fetchCapabilitiesProps(String containerId) { + ComponentParametersView capabilityPropsFilter = new ComponentParametersView(true); + capabilityPropsFilter.setIgnoreCapabiltyProperties(false); + return ((TopologyTemplate) topologyTemplateOperation.getToscaElement(containerId, capabilityPropsFilter).left().value()).getCalculatedCapabilitiesProperties(); + } + + private void compareCapabilitiesProperties(List<CapabilityPropertyDataObject> expected, Map<String, MapCapabiltyProperty> actual) { + expected.forEach(expectedCapabilityProp -> { + assertThat(getPropertyValue(actual, expectedCapabilityProp.instanceId, expectedCapabilityProp.capabilityId, expectedCapabilityProp.propName)) + .isEqualTo(expectedCapabilityProp.propValue); + }); + } + + private String getPropertyValue(Map<String, MapCapabiltyProperty> capabilityPropertyMap, String instance, String capability, String prop) { + return capabilityPropertyMap.get(instance).getMapToscaDataDefinition().get(capability).getMapToscaDataDefinition().get(prop).getValue(); + } + + private void setPropertyValue(Map<String, MapCapabiltyProperty> capabilityPropertyMap, CapabilityPropertyDataObject capabilityProperty) { + setPropertyValue(capabilityPropertyMap.get(capabilityProperty.getInstanceId()), capabilityProperty); + + } + + private void setPropertyValue(MapCapabiltyProperty capabilitiesInstanceProperties, CapabilityPropertyDataObject capabilityProperty) { + capabilitiesInstanceProperties.getMapToscaDataDefinition().get(capabilityProperty.getCapabilityId()) + .getMapToscaDataDefinition().get(capabilityProperty.getPropName()) + .setValue(capabilityProperty.getPropValue()); + } + + private void assertCapabilityPropValue(CapabilityPropertyDataObject prop, String expectedValue) { + Map<String, MapCapabiltyProperty> loadedCapPropsMap = fetchCapabilitiesProps(CONTAINER_ID); + String propertyValue = getPropertyValue(loadedCapPropsMap, prop.getInstanceId(), prop.getCapabilityId(), prop.getPropName()); + assertThat(propertyValue).isEqualTo(expectedValue); + } + + + private PropertyDefinition createPropWithValue(String val) { + PropertyDefinition propertyDefinition = new PropertyDefinition(); + propertyDefinition.setValue(val); + return propertyDefinition; + } + + private static class CapabilityPropertyDataObject { + private String instanceId; + private String capabilityId; + private String propName; + private String propValue; + + CapabilityPropertyDataObject(String instanceId, String capabilityId, String propName, String propValue) { + this.instanceId = instanceId; + this.capabilityId = capabilityId; + this.propName = propName; + this.propValue = propValue; + } + + String getInstanceId() { + return instanceId; + } + + String getCapabilityId() { + return capabilityId; + } + + String getPropName() { + return propName; + } + + String getPropValue() { + return propValue; + } + + void setPropValue(String propValue) { + this.propValue = propValue; + } + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperationIntegrationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperationIntegrationTest.java new file mode 100644 index 0000000000..d104cd3d42 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperationIntegrationTest.java @@ -0,0 +1,199 @@ +package org.openecomp.sdc.be.model.jsontitan.operations; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openecomp.sdc.be.dao.config.TitanSpringConfig; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +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.MapCapabiltyProperty; +import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.ModelTestBase; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.model.config.ModelOperationsSpringConfig; +import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.annotation.Resource; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {TitanSpringConfig.class, ModelOperationsSpringConfig.class}) +public class TopologyTemplateOperationIntegrationTest extends ModelTestBase { + + private static final String CONTAINER_ID = "id"; + private Map<String, MapCapabiltyProperty> capabilitiesPropsMap; + private List<CapabilityPropertyDataObject> capabilitiesProperties; + + @Resource + private TopologyTemplateOperation topologyTemplateOperation; + + @Resource + private TitanDao titanDao; + private CapabilityPropertyDataObject capabilityProperty1; + private CapabilityPropertyDataObject capabilityProperty2; + private CapabilityPropertyDataObject capabilityProperty3; + + @BeforeClass + public static void setupBeforeClass() { + + ModelTestBase.init(); + } + + @Before + public void setUp() throws Exception { + + capabilitiesPropsMap = new HashMap<>(); + capabilityProperty1 = new CapabilityPropertyDataObject("instance1", "capability1", "prop1", "val1"); + capabilityProperty2 = new CapabilityPropertyDataObject("instance1", "capability2", "prop2", "val2"); + capabilityProperty3 = new CapabilityPropertyDataObject("instance2", "capability3", "prop3", "val3"); + capabilitiesProperties = Arrays.asList(capabilityProperty1, capabilityProperty2, capabilityProperty3); + + + //capablities props == Map<instance id, Map<capability id, Map<prop id, property>>> + capabilitiesProperties.forEach(capabilitiesProperty -> { + capabilitiesPropsMap.computeIfAbsent(capabilitiesProperty.getInstanceId(), k -> new MapCapabiltyProperty(new HashMap<>())) + .getMapToscaDataDefinition().computeIfAbsent(capabilitiesProperty.getCapabilityId(), k -> new MapPropertiesDataDefinition(new HashMap<>())) + .getMapToscaDataDefinition().computeIfAbsent(capabilitiesProperty.getPropName(), k -> new PropertyDefinition(createPropWithValue(capabilitiesProperty.getPropValue()))); + }); + + GraphVertex resource = new GraphVertex(VertexTypeEnum.TOPOLOGY_TEMPLATE); + resource.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, CONTAINER_ID); + titanDao.createVertex(resource); + GraphVertex loadedResource = titanDao.getVertexById(CONTAINER_ID).left().value(); + topologyTemplateOperation.assosiateElementToData(loadedResource, VertexTypeEnum.CALCULATED_CAP_PROPERTIES, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, capabilitiesPropsMap).left().value(); + titanDao.commit(); + + } + + @After + public void tearDown() throws Exception { + titanDao.rollback(); + } + + @Test + public void overrideCalculatedCapabilityProperties() { + Map<String, MapCapabiltyProperty> loadedCapPropsMap = fetchCapabilitiesProps(CONTAINER_ID); + compareCapabilitiesProperties(capabilitiesProperties, loadedCapPropsMap); + capabilityProperty1.setPropValue("newVal1"); + capabilityProperty3.setPropValue("newVal3"); + setPropertyValue(capabilitiesPropsMap, capabilityProperty1); + setPropertyValue(capabilitiesPropsMap, capabilityProperty3); + StorageOperationStatus storageOperationStatus = topologyTemplateOperation.overrideToscaDataOfToscaElement(CONTAINER_ID, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, capabilitiesPropsMap); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK); + Map<String, MapCapabiltyProperty> updatedCapPropsMap = fetchCapabilitiesProps(CONTAINER_ID); + compareCapabilitiesProperties(capabilitiesProperties, updatedCapPropsMap); + } + + @Test + public void updateToscaDataDeepElementsBlockToToscaElement() { + assertCapabilityPropValue(capabilityProperty1, "val1"); + assertCapabilityPropValue(capabilityProperty2, "val2"); + assertCapabilityPropValue(capabilityProperty3, "val3"); + + MapCapabiltyProperty instance1Props = capabilitiesPropsMap.get("instance1"); + capabilityProperty1.setPropValue("newVal1"); + setPropertyValue(capabilitiesPropsMap, capabilityProperty1); + + capabilityProperty3.setPropValue("newVal3"); + setPropertyValue(capabilitiesPropsMap, capabilityProperty3); + + Component component = new org.openecomp.sdc.be.model.Resource(); + component.setUniqueId(CONTAINER_ID); + StorageOperationStatus updateStatus = topologyTemplateOperation.updateToscaDataDeepElementsBlockToToscaElement(CONTAINER_ID, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, instance1Props, "instance1"); + + assertThat(updateStatus).isEqualTo(StorageOperationStatus.OK); + assertCapabilityPropValue(capabilityProperty1, "newVal1"); + assertCapabilityPropValue(capabilityProperty2, "val2"); + assertCapabilityPropValue(capabilityProperty3, "val3");//only instance1 props should be updated + } + + private Map<String, MapCapabiltyProperty> fetchCapabilitiesProps(String containerId) { + ComponentParametersView capabilityPropsFilter = new ComponentParametersView(true); + capabilityPropsFilter.setIgnoreCapabiltyProperties(false); + return ((TopologyTemplate) topologyTemplateOperation.getToscaElement(containerId, capabilityPropsFilter).left().value()).getCalculatedCapabilitiesProperties(); + } + + private void compareCapabilitiesProperties(List<CapabilityPropertyDataObject> expected, Map<String, MapCapabiltyProperty> actual) { + expected.forEach(expectedCapabilityProp -> { + assertThat(getPropertyValue(actual, expectedCapabilityProp.instanceId, expectedCapabilityProp.capabilityId, expectedCapabilityProp.propName)) + .isEqualTo(expectedCapabilityProp.propValue); + }); + } + + private String getPropertyValue(Map<String, MapCapabiltyProperty> capabilityPropertyMap, String instance, String capability, String prop) { + return capabilityPropertyMap.get(instance).getMapToscaDataDefinition().get(capability).getMapToscaDataDefinition().get(prop).getValue(); + } + + private void setPropertyValue(Map<String, MapCapabiltyProperty> capabilityPropertyMap, CapabilityPropertyDataObject capabilityProperty) { + setPropertyValue(capabilityPropertyMap.get(capabilityProperty.getInstanceId()), capabilityProperty); + + } + + private void setPropertyValue(MapCapabiltyProperty capabilitiesInstanceProperties, CapabilityPropertyDataObject capabilityProperty) { + capabilitiesInstanceProperties.getMapToscaDataDefinition().get(capabilityProperty.getCapabilityId()) + .getMapToscaDataDefinition().get(capabilityProperty.getPropName()) + .setValue(capabilityProperty.getPropValue()); + } + + private void assertCapabilityPropValue(CapabilityPropertyDataObject prop, String expectedValue) { + Map<String, MapCapabiltyProperty> loadedCapPropsMap = fetchCapabilitiesProps(CONTAINER_ID); + String propertyValue = getPropertyValue(loadedCapPropsMap, prop.getInstanceId(), prop.getCapabilityId(), prop.getPropName()); + assertThat(propertyValue).isEqualTo(expectedValue); + } + + + private PropertyDefinition createPropWithValue(String val) { + PropertyDefinition propertyDefinition = new PropertyDefinition(); + propertyDefinition.setValue(val); + return propertyDefinition; + } + + private static class CapabilityPropertyDataObject { + private String instanceId; + private String capabilityId; + private String propName; + private String propValue; + + CapabilityPropertyDataObject(String instanceId, String capabilityId, String propName, String propValue) { + this.instanceId = instanceId; + this.capabilityId = capabilityId; + this.propName = propName; + this.propValue = propValue; + } + + String getInstanceId() { + return instanceId; + } + + String getCapabilityId() { + return capabilityId; + } + + String getPropName() { + return propName; + } + + String getPropValue() { + return propValue; + } + + void setPropValue(String propValue) { + this.propValue = propValue; + } + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperationTest.java new file mode 100644 index 0000000000..7756a08cc1 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperationTest.java @@ -0,0 +1,125 @@ +package org.openecomp.sdc.be.model.jsontitan.operations; + +import fj.data.Either; + +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Property; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.VertexProperty.Cardinality; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.MapCapabiltyProperty; +import org.openecomp.sdc.be.datatypes.elements.MapDataDefinition; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.model.PolicyDefinition; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; +import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; + +import com.thinkaurelius.titan.core.PropertyKey; +import com.thinkaurelius.titan.core.TitanEdge; +import com.thinkaurelius.titan.core.TitanTransaction; +import com.thinkaurelius.titan.core.TitanVertex; +import com.thinkaurelius.titan.core.TitanVertexProperty; +import com.thinkaurelius.titan.core.TitanVertexQuery; +import com.thinkaurelius.titan.core.VertexLabel; +import com.thinkaurelius.titan.diskstorage.EntryMetaData.Map; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +//error scenarios +public class TopologyTemplateOperationTest { + + private static final String CONTAINER_ID = "containerId"; + @InjectMocks + private TopologyTemplateOperation topologyTemplateOperation; + @Mock + private TitanDao titanDao; + + @Test + public void overrideToscaDataOfToscaElement_failedToFetchContainerVertex() { + when(titanDao.getVertexById(CONTAINER_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.INVALID_ID)); + StorageOperationStatus storageOperationStatus = topologyTemplateOperation.overrideToscaDataOfToscaElement(CONTAINER_ID, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, Collections.emptyMap()); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.INVALID_ID); + } + + @Test + public void overrideToscaDataOfToscaElement_failedToFetchDataVertex() { + GraphVertex containerVertex = new GraphVertex(); + when(titanDao.getVertexById(CONTAINER_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(containerVertex)); + when(titanDao.getChildVertex(containerVertex, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, JsonParseFlagEnum.ParseJson)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND)); + StorageOperationStatus storageOperationStatus = topologyTemplateOperation.overrideToscaDataOfToscaElement(CONTAINER_ID, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, Collections.emptyMap()); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.NOT_FOUND); + } + + @Test + public void updateToscaDataDeepElements_failedToFetchContainerVertex() { + when(titanDao.getVertexById(CONTAINER_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.right(TitanOperationStatus.INVALID_ID)); + StorageOperationStatus storageOperationStatus = topologyTemplateOperation.updateToscaDataDeepElementsBlockToToscaElement(CONTAINER_ID, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, new MapCapabiltyProperty(), ""); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.INVALID_ID); + } + + @Test + public void updateToscaDataDeepElements_failedToFetchDataVertex() { + GraphVertex containerVertex = new GraphVertex(); + when(titanDao.getVertexById(CONTAINER_ID, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(containerVertex)); + when(titanDao.getChildVertex(containerVertex, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, JsonParseFlagEnum.ParseJson)).thenReturn(Either.right(TitanOperationStatus.NOT_FOUND)); + StorageOperationStatus storageOperationStatus = topologyTemplateOperation.updateToscaDataDeepElementsBlockToToscaElement(CONTAINER_ID, EdgeLabelEnum.CALCULATED_CAP_PROPERTIES, new MapCapabiltyProperty(), ""); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.NOT_FOUND); + } + + @Test + public void addPolicyToToscaElementSuccessTest(){ + TitanOperationStatus status = TitanOperationStatus.OK; + StorageOperationStatus result = addPolicyToToscaElementWithStatus(status); + assertThat(result).isEqualTo(StorageOperationStatus.OK); + } + + @Test + public void addPolicyToToscaElementFailureTest(){ + TitanOperationStatus status = TitanOperationStatus.ALREADY_EXIST; + StorageOperationStatus result = addPolicyToToscaElementWithStatus(status); + assertThat(result).isEqualTo(StorageOperationStatus.ENTITY_ALREADY_EXISTS); + } + + @SuppressWarnings("unchecked") + private StorageOperationStatus addPolicyToToscaElementWithStatus(TitanOperationStatus status) { + GraphVertex componentV = new GraphVertex(); + componentV.setVertex(Mockito.mock(TitanVertex.class)); + GraphVertex dataV = new GraphVertex(); + dataV.setVertex(Mockito.mock(TitanVertex.class)); + String componentName = "componentName"; + String componentId = UniqueIdBuilder.buildResourceUniqueId(); + String policyTypeName = "org.openecomp.policies.placement.valet.Affinity"; + componentV.setJsonMetadataField(JsonPresentationFields.NAME, componentName); + componentV.setUniqueId(componentId); + PolicyDefinition policy = new PolicyDefinition(); + policy.setPolicyTypeName(policyTypeName); + int counter = 0; + Either<GraphVertex, TitanOperationStatus> toscaDataVertexRes = Either.right(TitanOperationStatus.NOT_FOUND); + when(titanDao.getChildVertex(eq(componentV), eq(EdgeLabelEnum.POLICIES), eq(JsonParseFlagEnum.ParseJson))).thenReturn(toscaDataVertexRes); + Either<GraphVertex, TitanOperationStatus> createVertex = Either.left(dataV); + when(titanDao.createVertex(any(GraphVertex.class))).thenReturn(createVertex); + when(titanDao.createEdge(any(TitanVertex.class), any(TitanVertex.class), any(EdgeLabelEnum.class), any(HashMap.class))).thenReturn(status); + return topologyTemplateOperation.addPolicyToToscaElement(componentV, policy, counter); + } + +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperationTest.java index 806c22a8fb..d9148a18e1 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperationTest.java @@ -22,6 +22,7 @@ import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.model.LifecycleStateEnum; import org.openecomp.sdc.be.model.ModelTestBase; +import org.openecomp.sdc.be.model.catalog.CatalogComponent; import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement; import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; @@ -81,6 +82,9 @@ public class ToscaElementOperationTest extends ModelTestBase{ }, null); assertEquals(highestResources.stream().count(), highestResourcesExpectedCount); } + + + @Test public void testGetAllResourcesCertifiedNoFilter() { @@ -268,6 +272,8 @@ public class ToscaElementOperationTest extends ModelTestBase{ } private void initGraphForTest() { + GraphTestUtils.createRootCatalogVertex(titanDao); + Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() { { put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacadePoliciesTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacadePoliciesTest.java new file mode 100644 index 0000000000..6a4a117360 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacadePoliciesTest.java @@ -0,0 +1,144 @@ +package org.openecomp.sdc.be.model.jsontitan.operations; + +import fj.data.Either; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openecomp.sdc.be.dao.config.TitanSpringConfig; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType; +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.Component; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.ModelTestBase; +import org.openecomp.sdc.be.model.PolicyDefinition; +import org.openecomp.sdc.be.model.config.ModelOperationsSpringConfig; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {TitanSpringConfig.class, ModelOperationsSpringConfig.class}) +public class ToscaOperationFacadePoliciesTest extends ModelTestBase { + + private static final String CONTAINER_ID = "containerId"; + private static final String CONTAINER_NAME = "containerName"; + @Autowired + private ToscaOperationFacade toscaOperationFacade; + @Autowired + private TitanDao titanDao; + + private PolicyDefinition policy1, policy2; + + @BeforeClass + public static void setupBeforeClass() { + ModelTestBase.init(); + } + + @Before + public void setUp() throws Exception { + policy1 = createPolicyDefinition("type1"); + policy2 = createPolicyDefinition("type2"); + createContainerVertexInDB(); + createPoliciesOnGraph(policy1, policy2); + } + + private void createPoliciesOnGraph(PolicyDefinition ... policies) { + for (int i = 0; i < policies.length; i++) { + PolicyDefinition policy = policies[i]; + Either<PolicyDefinition, StorageOperationStatus> createdPolicy = toscaOperationFacade.associatePolicyToComponent(CONTAINER_ID, policy, i); + assertTrue(createdPolicy.isLeft()); + } + } + + @After + public void tearDown() { + titanDao.rollback(); + } + + @Test + public void updatePoliciesTargetsOfComponent_updateSinglePolicy() { + List<String> updatedTargetIds = asList("instance1new", "instance2"); + PolicyDefinition originalPolicy2 = clonePolicyWithTargets(policy2); + updatePolicyTypeTargetsIds(policy1, PolicyTargetType.COMPONENT_INSTANCES, updatedTargetIds); + updatePolicyTypeTargetsIds(policy2, PolicyTargetType.COMPONENT_INSTANCES, updatedTargetIds); + + StorageOperationStatus storageOperationStatus = toscaOperationFacade.updatePoliciesOfComponent(CONTAINER_ID, singletonList(policy1)); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK); + Component updatedComponent = fetchComponentFromDB(); + verifyPolicyTargets(updatedComponent.getPolicyById(policy1.getUniqueId()), policy1); + verifyPolicyTargets(updatedComponent.getPolicyById(policy2.getUniqueId()), originalPolicy2); + } + + @Test + public void updatePoliciesTargetsOfComponent_updateMultiplePolicies() { + List<String> updatedTargetIds = asList("instance1new", "instance2"); + updatePolicyTypeTargetsIds(policy1, PolicyTargetType.COMPONENT_INSTANCES, updatedTargetIds); + updatePolicyTypeTargetsIds(policy2, PolicyTargetType.COMPONENT_INSTANCES, updatedTargetIds); + StorageOperationStatus storageOperationStatus = toscaOperationFacade.updatePoliciesOfComponent(CONTAINER_ID, asList(policy1, policy2)); + assertThat(storageOperationStatus).isEqualTo(StorageOperationStatus.OK); + Component updatedComponent = fetchComponentFromDB(); + verifyPolicyTargets(updatedComponent.getPolicyById(policy1.getUniqueId()), policy1); + verifyPolicyTargets(updatedComponent.getPolicyById(policy2.getUniqueId()), policy2); + } + + private PolicyDefinition clonePolicyWithTargets(PolicyDefinition policy) { + PolicyDefinition originalPolicy = new PolicyDefinition(policy); + Map<PolicyTargetType, List<String>> clonedTargetMap = policy.getTargets().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> new ArrayList<>(entry.getValue()))); + originalPolicy.setTargets(clonedTargetMap); + return originalPolicy; + } + + private void verifyPolicyTargets(PolicyDefinition updatedPolicy, PolicyDefinition expectedPolicy) { + assertThat(updatedPolicy.getTargets()) + .isEqualTo(expectedPolicy.getTargets()); + } + + private void updatePolicyTypeTargetsIds(PolicyDefinition policy, PolicyTargetType targetType, List<String> updatedTargetIds) { + policy.getTargets().put(targetType, updatedTargetIds); + } + + private Component fetchComponentFromDB() { + ComponentParametersView componentParametersView = new ComponentParametersView(); + componentParametersView.disableAll(); + componentParametersView.setIgnorePolicies(false); + return toscaOperationFacade.getToscaElement(CONTAINER_ID, componentParametersView).left().value(); + } + + private void createContainerVertexInDB() { + 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()); + Either<GraphVertex, TitanOperationStatus> container = titanDao.createVertex(resource); + assertTrue(container.isLeft()); + } + + private PolicyDefinition createPolicyDefinition(String type) { + PolicyDefinition policy = new PolicyDefinition(); + policy.setPolicyTypeName(type); + policy.setTargets(new HashMap<>()); + policy.getTargets().put(PolicyTargetType.COMPONENT_INSTANCES, asList("instance1", "instance2")); + return policy; + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacadeTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacadeTest.java index 4a7d27812d..37d466c648 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacadeTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacadeTest.java @@ -1,15 +1,6 @@ package org.openecomp.sdc.be.model.jsontitan.operations; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - +import fj.data.Either; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,11 +20,24 @@ import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.PolicyDefinition; import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate; import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import fj.data.Either; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class ToscaOperationFacadeTest { @@ -58,16 +62,18 @@ public class ToscaOperationFacadeTest { @Test public void fetchMetaDataByResourceType() throws Exception { ArgumentCaptor<Map> criteriaCapture = ArgumentCaptor.forClass(Map.class); + ArgumentCaptor<Map> criteriaNotCapture = ArgumentCaptor.forClass(Map.class); ComponentParametersView dataFilter = new ComponentParametersView(); List<GraphVertex> mockVertices = getMockVertices(2); Either<List<GraphVertex>, TitanOperationStatus> returnedVertices = Either.left(mockVertices); - when(titanDaoMock.getByCriteria(Mockito.eq(null), criteriaCapture.capture(), Mockito.eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(returnedVertices); + when(titanDaoMock.getByCriteria(eq(null), criteriaCapture.capture(), criteriaNotCapture.capture(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(returnedVertices); when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(0), dataFilter)).thenReturn(Either.left(getResourceToscaElement("0"))); when(topologyTemplateOperationMock.getToscaElement(mockVertices.get(1), dataFilter)).thenReturn(Either.left(getResourceToscaElement("1"))); Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), dataFilter); verifyCriteriaForHighestVersionAndVfResourceType(criteriaCapture); + verifyCriteriaNotIsDeleted(criteriaNotCapture); assertTrue(fetchedComponents.isLeft()); List<Component> cmpts = fetchedComponents.left().value(); @@ -83,17 +89,93 @@ public class ToscaOperationFacadeTest { assertEquals(criteria.get(GraphPropertyEnum.IS_HIGHEST_VERSION), true); } + private void verifyCriteriaNotIsDeleted(ArgumentCaptor<Map> criteriaNotCapture) { + Map<GraphPropertyEnum, Object> notCriteria = (Map<GraphPropertyEnum, Object>)criteriaNotCapture.getValue(); + assertEquals(1, notCriteria.size()); + assertEquals(notCriteria.get(GraphPropertyEnum.IS_DELETED), true); + } + @SuppressWarnings("unchecked") @Test public void fetchMetaDataByResourceType_failedToGetData() throws Exception { - when(titanDaoMock.getByCriteria(Mockito.eq(null), Mockito.anyMap(), Mockito.eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(TitanOperationStatus.GENERAL_ERROR)); + when(titanDaoMock.getByCriteria(eq(null), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.right(TitanOperationStatus.GENERAL_ERROR)); Either<List<Component>, StorageOperationStatus> fetchedComponents = testInstance.fetchMetaDataByResourceType(ResourceTypeEnum.VF.getValue(), new ComponentParametersView()); assertTrue(fetchedComponents.isRight()); assertEquals(StorageOperationStatus.GENERAL_ERROR, fetchedComponents.right().value()); } + + @Test + public void associatePolicyToComponentSuccessTest(){ + Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.OK); + assertTrue(result.isLeft()); + } + + @Test + public void associatePolicyToComponentFailureTest(){ + Either<PolicyDefinition, StorageOperationStatus> result = associatePolicyToComponentWithStatus(StorageOperationStatus.BAD_REQUEST); + assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.BAD_REQUEST); + } + + @Test + public void updatePolicyOfComponentSuccessTest(){ + Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.OK); + assertTrue(result.isLeft()); + } + + @Test + public void updatePolicyOfComponentFailureTest(){ + Either<PolicyDefinition, StorageOperationStatus> result = updatePolicyOfComponentWithStatus(StorageOperationStatus.NOT_FOUND); + assertTrue(result.isRight() && result.right().value() == StorageOperationStatus.NOT_FOUND); + } + + @Test + public void removePolicyFromComponentSuccessTest(){ + removePolicyFromComponentWithStatus(StorageOperationStatus.OK); + } + + @Test + public void removePolicyFromComponentFailureTest(){ + removePolicyFromComponentWithStatus(StorageOperationStatus.NOT_FOUND); + } + + private Either<PolicyDefinition, StorageOperationStatus> associatePolicyToComponentWithStatus(StorageOperationStatus status) { + PolicyDefinition policy = new PolicyDefinition(); + String componentId = "componentId"; + int counter = 0; + GraphVertex vertex; + if(status == StorageOperationStatus.OK){ + vertex = getTopologyTemplateVertex(); + } else { + vertex = getNodeTypeVertex(); + } + Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex); + when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(getVertexEither); + when(topologyTemplateOperationMock.addPolicyToToscaElement(eq(vertex), any(PolicyDefinition.class), anyInt())).thenReturn(status); + return testInstance.associatePolicyToComponent(componentId, policy, counter); + } + + private Either<PolicyDefinition, StorageOperationStatus> updatePolicyOfComponentWithStatus(StorageOperationStatus status) { + PolicyDefinition policy = new PolicyDefinition(); + String componentId = "componentId"; + GraphVertex vertex = getTopologyTemplateVertex(); + when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(Either.left(vertex)); + when(topologyTemplateOperationMock.updatePolicyOfToscaElement(eq(vertex), any(PolicyDefinition.class))).thenReturn(status); + return testInstance.updatePolicyOfComponent(componentId, policy); + } + private void removePolicyFromComponentWithStatus(StorageOperationStatus status) { + String componentId = "componentId"; + String policyId = "policyId"; + GraphVertex vertex = getTopologyTemplateVertex(); + Either<GraphVertex, TitanOperationStatus> getVertexEither = Either.left(vertex); + when(titanDaoMock.getVertexById(eq(componentId), eq(JsonParseFlagEnum.NoParse))).thenReturn(getVertexEither); + when(topologyTemplateOperationMock.removePolicyFromToscaElement(eq(vertex), eq(policyId))).thenReturn(status); + StorageOperationStatus result = testInstance.removePolicyFromComponent(componentId, policyId); + assertTrue(result == status); + } + private List<GraphVertex> getMockVertices(int numOfVertices) { - return IntStream.range(0, numOfVertices).mapToObj(i -> getMockVertex()).collect(Collectors.toList()); + return IntStream.range(0, numOfVertices).mapToObj(i -> getTopologyTemplateVertex()).collect(Collectors.toList()); } private ToscaElement getResourceToscaElement(String id) { @@ -104,9 +186,15 @@ public class ToscaOperationFacadeTest { return toscaElement; } - private GraphVertex getMockVertex() { + private GraphVertex getTopologyTemplateVertex() { GraphVertex graphVertex = new GraphVertex(); graphVertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE); return graphVertex; } + + private GraphVertex getNodeTypeVertex() { + GraphVertex graphVertex = new GraphVertex(); + graphVertex.setLabel(VertexTypeEnum.NODE_TYPE); + return graphVertex; + } }
\ No newline at end of file |