diff options
author | Michael Lando <ml636r@att.com> | 2017-07-17 21:12:03 +0300 |
---|---|---|
committer | Michael Lando <ml636r@att.com> | 2017-07-17 21:12:03 +0300 |
commit | 75aacbbe1acf78fa53378f07f0a8c7769449a17e (patch) | |
tree | 68a9799eb8f4704dd9a3d513401df9bb11af7739 /catalog-model/src/test/java/org | |
parent | dec02e7cc74e1c401be51bd9d266575e1e008f5f (diff) |
[SDC] rebase 1710 code
Change-Id: I532ed68979fee7840ea8a5395e7e965b155fb9f9
Signed-off-by: Michael Lando <ml636r@att.com>
Diffstat (limited to 'catalog-model/src/test/java/org')
4 files changed, 617 insertions, 0 deletions
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ArtifactsOperationsTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ArtifactsOperationsTest.java new file mode 100644 index 0000000000..3f80806292 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ArtifactsOperationsTest.java @@ -0,0 +1,85 @@ +package org.openecomp.sdc.be.model.jsontitan.operations; + +import fj.data.Either; +import org.junit.Test; +import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition; +import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; +import org.openecomp.sdc.be.model.ArtifactDefinition; +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.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.*; + +public class ArtifactsOperationsTest { + + private static final String SERVICE_ID = "serviceId"; + private static final String INSTANCE_ID = "instanceId"; + private ArtifactsOperations testInstance = mock(ArtifactsOperations.class, CALLS_REAL_METHODS); + + @Test + public void getInstanceArtifacts_collectAllInstanceArtifacts() throws Exception { + Map<String, ToscaDataDefinition> instanceArtifacts = Collections.singletonMap(INSTANCE_ID, getArtifactsByInstance("name1")); + + Map<String, ToscaDataDefinition> instanceDeploymentArtifacts = new HashMap<>(); + instanceDeploymentArtifacts.put(INSTANCE_ID, getArtifactsByInstance("name2", "name3")); + instanceDeploymentArtifacts.put("instanceId2", getArtifactsByInstance("name4")); + + 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<Map<String, ArtifactDefinition>, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID); + + assertTrue(allInstArtifacts.isLeft()); + assertEquals(allInstArtifacts.left().value().size(), 3); + assertTrue(allInstArtifacts.left().value().containsKey("name1")); + assertTrue(allInstArtifacts.left().value().containsKey("name2")); + assertTrue(allInstArtifacts.left().value().containsKey("name3")); + assertFalse(allInstArtifacts.left().value().containsKey("name4"));//this key is of different instance + } + + @Test + public void getInstanceArtifacts_noArtifactsForInstance() throws Exception { + Map<String, ToscaDataDefinition> instanceArtifacts = Collections.singletonMap(INSTANCE_ID, getArtifactsByInstance("name1")); + + 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<Map<String, ArtifactDefinition>, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, "someOtherInstance"); + + assertTrue(allInstArtifacts.isLeft()); + assertTrue(allInstArtifacts.left().value().isEmpty()); + } + + @Test + public void getInstanceArtifacts_errorGettingInstanceArtifacts() throws Exception { + doReturn(Either.right(TitanOperationStatus.GENERAL_ERROR)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INSTANCE_ARTIFACTS); + Either<Map<String, ArtifactDefinition>, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID); + verify(testInstance, times(0)).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + assertTrue(allInstArtifacts.isRight()); + } + + @Test + public void getAllInstanceArtifacts_errorGettingDeploymentArtifacts() throws Exception { + doReturn(Either.left(new HashMap<>())).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INSTANCE_ARTIFACTS); + doReturn(Either.right(TitanOperationStatus.GENERAL_ERROR)).when(testInstance).getDataFromGraph(SERVICE_ID, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + Either<Map<String, ArtifactDefinition>, StorageOperationStatus> allInstArtifacts = testInstance.getAllInstanceArtifacts(SERVICE_ID, INSTANCE_ID); + assertTrue(allInstArtifacts.isRight()); + } + + private ToscaDataDefinition getArtifactsByInstance(String ... artifactsNames) { + MapArtifactDataDefinition artifactsByInstance = new MapArtifactDataDefinition(); + Map<String, ArtifactDataDefinition> artifactsByName = new HashMap<>(); + for (String artifactName : artifactsNames) { + artifactsByName.put(artifactName, new ArtifactDataDefinition()); + } + artifactsByInstance.setMapToscaDataDefinition(artifactsByName); + return artifactsByInstance; + } +} 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 new file mode 100644 index 0000000000..339023f9a4 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperationTest.java @@ -0,0 +1,344 @@ +package org.openecomp.sdc.be.model.jsontitan.operations; + +import fj.data.Either; +import org.apache.cassandra.cql3.CQL3Type; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; +import org.junit.runner.RunWith; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.TitanDao; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +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.jsontitan.datamodel.ToscaElement; +import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.*; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by chaya on 6/12/2017. + */ + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:application-context-test.xml") +public class ToscaElementOperationTest extends ModelTestBase{ + + private List<GraphVertex> allVertices = new ArrayList<>(); + private boolean isInitialized = false; + + @javax.annotation.Resource + ToscaElementOperationTestImpl toscaElementOperation; + + @javax.annotation.Resource + TitanDao titanDao; + + @BeforeClass + public static void initTest(){ + ModelTestBase.init(); + + } + + @Rule + public TestName testName = new TestName(); + + @Before + public void beforeTest() { + if (!isInitialized) { + GraphTestUtils.clearGraph(titanDao); + //exportGraphMl(titanDao.getGraph().left().value(),""); + initGraphForTest(); + isInitialized = true; + } + } + + @Test + public void testGetAllHighestResourcesNoFilter() { + + Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, true); + assertTrue(highestResourcesRes.isLeft()); + List<ToscaElement> highestResources = highestResourcesRes.left().value(); + // calculate expected count value + long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + } + }, null); + assertEquals(highestResources.stream().count(), highestResourcesExpectedCount); + } + + @Test + public void testGetAllResourcesCertifiedNoFilter() { + Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, null, false); + assertTrue(highestResourcesRes.isLeft()); + List<ToscaElement> highestResources = highestResourcesRes.left().value(); + // calculate expected count value + long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + } + }, null); + highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + } + }, new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + } + }); + assertEquals(highestResources.stream().count(), highestResourcesExpectedCount); + } + + @Test + public void testGetHighestResourcesExclude() { + + // exclude VFCMT + List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT); + assertTrue(genericTestGetResourcesWithExcludeList(excludeList)); + + // exclude CP & VL + excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP); + assertTrue(genericTestGetResourcesWithExcludeList(excludeList)); + + // exclude CP & VL & VF & VFC + excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC); + assertTrue(genericTestGetResourcesWithExcludeList(excludeList)); + } + + @Test + public void testGetAllResourcesCertifiedExclude() { + // exclude VFCMT + List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VFCMT); + assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList)); + + // exclude CP & VL + excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP); + assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList)); + + // exclude CP & VL & VF & VFC + excludeList = Arrays.asList(ResourceTypeEnum.VL, ResourceTypeEnum.CP, ResourceTypeEnum.VF, ResourceTypeEnum.VFC); + assertTrue(genericTestGetCertifiedResourcesWithExcludeList(excludeList)); + } + + @Test + public void testGetAllHighestServicesNoFilter() { + Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, true); + assertTrue(highestResourcesRes.isLeft()); + List<ToscaElement> highestResources = highestResourcesRes.left().value(); + // calculate expected count value + long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + } + }, null); + assertEquals(highestResources.stream().count(), highestResourcesExpectedCount); + } + + @Test + public void testGetAllCertifiedServicesNoFilter() { + Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, null, false); + assertTrue(highestResourcesRes.isLeft()); + List<ToscaElement> highestResources = highestResourcesRes.left().value(); + // calculate expected count value + long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + } + }, null); + highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + } + }, new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + } + }); + assertEquals(highestResources.stream().count(), highestResourcesExpectedCount); + } + + @Test + public void testGetServicesExcludeList() { + List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VF, ResourceTypeEnum.VFCMT); + Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, true); + assertTrue(highestResourcesRes.isLeft()); + List<ToscaElement> highestResources = highestResourcesRes.left().value(); + // calculate expected count value + long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + } + }, null); + assertEquals(highestResources.stream().count(), highestResourcesExpectedCount); + } + + @Test + public void testGetCertifiedServicesExcludeList() { + List<ResourceTypeEnum> excludeList = Arrays.asList(ResourceTypeEnum.VL); + Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.SERVICE, excludeList, false); + assertTrue(highestResourcesRes.isLeft()); + List<ToscaElement> highestResources = highestResourcesRes.left().value(); + // calculate expected count value + long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + } + }, null); + highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + } + }, new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + } + }); + assertEquals(highestResources.stream().count(), highestResourcesExpectedCount); + } + + private boolean genericTestGetResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) { + Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE,excludeList, true); + assertTrue(highestResourcesRes.isLeft()); + List<ToscaElement> highestResources = highestResourcesRes.left().value(); + // calculate expected count value + long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + } + }, new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.RESOURCE_TYPE, excludeList); + } + }); + return highestResources.stream().count() == (highestResourcesExpectedCount); + } + + private boolean genericTestGetCertifiedResourcesWithExcludeList(List<ResourceTypeEnum> excludeList) { + Either<List<ToscaElement>, StorageOperationStatus> highestResourcesRes = toscaElementOperation.getElementCatalogData(ComponentTypeEnum.RESOURCE, excludeList, false); + assertTrue(highestResourcesRes.isLeft()); + List<ToscaElement> highestResources = highestResourcesRes.left().value(); + // calculate expected count value + long highestResourcesExpectedCount = calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + } + }, new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.RESOURCE_TYPE, excludeList); + } + }); + highestResourcesExpectedCount += calculateCount(new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + } + }, new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + put(GraphPropertyEnum.RESOURCE_TYPE, excludeList); + } + }); + return highestResources.stream().count() == highestResourcesExpectedCount; + } + + private void initGraphForTest() { + Map<GraphPropertyEnum, Object> highstVerticesProps = new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + } + }; + + Map<GraphPropertyEnum, Object> certifiedVerticesProps = new HashMap<GraphPropertyEnum, Object>() { + { + put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name()); + } + }; + + // add vertices with higestVersion = true + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VF)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VFC)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VFCMT)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.VL)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, highstVerticesProps, ResourceTypeEnum.CP)); + allVertices.add(GraphTestUtils.createServiceVertex(titanDao, highstVerticesProps)); + + // add vertices with non-additional properties + for (int i=0 ; i<2 ; i++) { + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VF)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VFC)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VFCMT)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.VL)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, new HashMap<>(), ResourceTypeEnum.CP)); + allVertices.add(GraphTestUtils.createServiceVertex(titanDao, new HashMap<>())); + } + + // add certified vertices + for (int i=0; i<3; i++) { + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VF)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VFC)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VFCMT)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.VL)); + allVertices.add(GraphTestUtils.createResourceVertex(titanDao, certifiedVerticesProps, ResourceTypeEnum.CP)); + allVertices.add(GraphTestUtils.createServiceVertex(titanDao, certifiedVerticesProps)); + } + //allVertices.stream().forEach( v -> System.out.println("type: "+v.getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE))); + //String result = GraphTestUtils.exportGraphMl(titanDao.getGraph().left().value(), ""); + //System.out.println("graph is: " + result); + } + + private long calculateCount(HashMap<GraphPropertyEnum, Object> hasProps, Map<GraphPropertyEnum, Object> doesntHaveProps){ + return allVertices.stream(). + filter(v -> { + Map<GraphPropertyEnum, Object> vertexProps = v.getMetadataProperties(); + if (hasProps != null) { + for (Map.Entry<GraphPropertyEnum, Object> prop: hasProps.entrySet()){ + Object value = vertexProps.get(prop.getKey()); + if ( value == null || !value.equals(prop.getValue())) { + return false; + } + } + } + + if (doesntHaveProps != null) { + for (Map.Entry<GraphPropertyEnum, Object> prop : doesntHaveProps.entrySet()) { + Object value = vertexProps.get(prop.getKey()); + Object propValue = prop.getValue(); + if ( value != null && propValue != null && propValue instanceof List ) { + for (ResourceTypeEnum propVal : (List<ResourceTypeEnum>)propValue) { + if (propVal.name().equals(value)) { + return false; + } + } + } + else if (value != null && value.equals(propValue)){ + return false; + } + } + } + return true; + }).count(); + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperationTestImpl.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperationTestImpl.java new file mode 100644 index 0000000000..27fbce4776 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperationTestImpl.java @@ -0,0 +1,74 @@ +package org.openecomp.sdc.be.model.jsontitan.operations; + +import fj.data.Either; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; +import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; +import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.model.ComponentParametersView; +import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; + +/** + * Created by cb478c on 6/13/2017. + */ +@org.springframework.stereotype.Component("test-tosca-element-operation") +public class ToscaElementOperationTestImpl extends ToscaElementOperation { + + @Override + protected <T extends ToscaElement> Either<T, StorageOperationStatus> getLightComponent(GraphVertex vertexComponent, ComponentTypeEnum nodeType, ComponentParametersView parametersFilter) { + titanDao.parseVertexProperties(vertexComponent, JsonParseFlagEnum.ParseMetadata); + T toscaElement = convertToComponent(vertexComponent); + return Either.left(toscaElement); + } + + @Override + public <T extends ToscaElement> Either<T, StorageOperationStatus> getToscaElement(String uniqueId, ComponentParametersView componentParametersView) { + return null; + } + + @Override + public <T extends ToscaElement> Either<T, StorageOperationStatus> getToscaElement(GraphVertex toscaElementVertex, ComponentParametersView componentParametersView) { + return null; + } + + @Override + public <T extends ToscaElement> Either<T, StorageOperationStatus> deleteToscaElement(GraphVertex toscaElementVertex) { + return null; + } + + @Override + public <T extends ToscaElement> Either<T, StorageOperationStatus> createToscaElement(ToscaElement toscaElement) { + return null; + } + + @Override + protected <T extends ToscaElement> TitanOperationStatus setCategoriesFromGraph(GraphVertex vertexComponent, T toscaElement) { + return null; + } + + @Override + protected <T extends ToscaElement> TitanOperationStatus setCapabilitiesFromGraph(GraphVertex componentV, T toscaElement) { + return null; + } + + @Override + protected <T extends ToscaElement> TitanOperationStatus setRequirementsFromGraph(GraphVertex componentV, T toscaElement) { + return null; + } + + @Override + protected <T extends ToscaElement> StorageOperationStatus validateCategories(T toscaElementToUpdate, GraphVertex elementV) { + return null; + } + + @Override + protected <T extends ToscaElement> StorageOperationStatus updateDerived(T toscaElementToUpdate, GraphVertex updateElementV) { + return null; + } + + @Override + public <T extends ToscaElement> void fillToscaElementVertexData(GraphVertex elementV, T toscaElementToUpdate, JsonParseFlagEnum flag) { + + } +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/utils/GraphTestUtils.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/utils/GraphTestUtils.java new file mode 100644 index 0000000000..8e2a283559 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/jsontitan/utils/GraphTestUtils.java @@ -0,0 +1,114 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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.jsontitan.utils; + +import com.thinkaurelius.titan.core.TitanGraph; +import com.thinkaurelius.titan.core.TitanVertex; +import fj.data.Either; +import org.apache.tinkerpop.gremlin.structure.io.IoCore; +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.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.util.Iterator; +import java.util.Map; +import java.util.UUID; + +public final class GraphTestUtils { + + public static GraphVertex createResourceVertex(TitanDao titanDao, Map<GraphPropertyEnum,Object> metadataProps, ResourceTypeEnum type) { + GraphVertex vertex = new GraphVertex(); + if (type == ResourceTypeEnum.VF) { + vertex.setLabel(VertexTypeEnum.TOPOLOGY_TEMPLATE); + vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE); + } else { + vertex.setLabel(VertexTypeEnum.NODE_TYPE); + vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.NODE_TYPE); + } + String uuid = UUID.randomUUID().toString(); + vertex.setUniqueId(uuid); + + vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid); + vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name()); + vertex.addMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE, type.name()); + vertex.addMetadataProperty(GraphPropertyEnum.IS_ABSTRACT, false); + for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) { + vertex.addMetadataProperty(prop.getKey(), prop.getValue()); + } + titanDao.createVertex(vertex); + titanDao.commit(); + return vertex; + } + + public static GraphVertex createServiceVertex(TitanDao titanDao, Map<GraphPropertyEnum, Object> metadataProps){ + GraphVertex vertex = new GraphVertex(VertexTypeEnum.TOPOLOGY_TEMPLATE); + String uuid = UUID.randomUUID().toString(); + vertex.setUniqueId(uuid); + vertex.addMetadataProperty(GraphPropertyEnum.LABEL, VertexTypeEnum.TOPOLOGY_TEMPLATE); + vertex.addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uuid); + vertex.addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name()); + for (Map.Entry<GraphPropertyEnum, Object> prop : metadataProps.entrySet()) { + vertex.addMetadataProperty(prop.getKey(), prop.getValue()); + } + titanDao.createVertex(vertex); + titanDao.commit(); + return vertex; + } + + public static void clearGraph(TitanDao titanDao) { + Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph(); + TitanGraph graph = graphResult.left().value(); + + Iterable<TitanVertex> vertices = graph.query().vertices(); + if (vertices != null) { + Iterator<TitanVertex> iterator = vertices.iterator(); + while (iterator.hasNext()) { + TitanVertex vertex = iterator.next(); + vertex.remove(); + } + } + titanDao.commit(); + } + + public static String exportGraphMl(TitanGraph graph, String outputDirectory) { + String result = null; + String outputFile = outputDirectory + File.separator + "exportGraph." + System.currentTimeMillis() + ".graphml"; + try { + try (final OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) { + graph.io(IoCore.graphml()).writer().normalize(true).create().writeGraph(os, graph); + } + result = outputFile; + graph.tx().commit(); + } catch (Exception e) { + graph.tx().rollback(); + e.printStackTrace(); + } + return result; + } +} |