diff options
Diffstat (limited to 'catalog-model/src')
4 files changed, 205 insertions, 67 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/RelationshipTypeDefinition.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/RelationshipTypeDefinition.java index fd988fa565..08a04422ae 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/RelationshipTypeDefinition.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/RelationshipTypeDefinition.java @@ -18,14 +18,21 @@ package org.openecomp.sdc.be.model; import java.util.Map; import org.openecomp.sdc.be.datatypes.elements.RelationshipInstDataDefinition; import org.openecomp.sdc.be.resources.data.RelationshipTypeData; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; /** * Specifies the capabilities that the Node Type exposes. */ +@Getter +@Setter +@ToString(callSuper = true) public class RelationshipTypeDefinition extends RelationshipInstDataDefinition { private String derivedFrom; private Map<String, PropertyDefinition> properties; + private String model; public RelationshipTypeDefinition() { super(); @@ -42,24 +49,4 @@ public class RelationshipTypeDefinition extends RelationshipInstDataDefinition { this.setValidSourceTypes(relationshipTypeData.getRelationshipTypeDataDefinition().getValidSourceTypes()); } - public String getDerivedFrom() { - return derivedFrom; - } - - public void setDerivedFrom(String derivedFrom) { - this.derivedFrom = derivedFrom; - } - - public Map<String, PropertyDefinition> getProperties() { - return properties; - } - - public void setProperties(Map<String, PropertyDefinition> properties) { - this.properties = properties; - } - - @Override - public String toString() { - return super.toString() + " [ derivedFrom=" + derivedFrom + ", properties=" + properties + " ]"; - } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperation.java index 7fab326be7..f02fc2da7d 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperation.java @@ -19,24 +19,31 @@ */ package org.openecomp.sdc.be.model.operations.impl; +import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR; + import fj.data.Either; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge; +import org.openecomp.sdc.be.dao.graph.datatype.GraphNode; import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels; +import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.RelationshipTypeDefinition; import org.openecomp.sdc.be.model.operations.api.DerivedFromOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.resources.data.ModelData; import org.openecomp.sdc.be.resources.data.PropertyData; import org.openecomp.sdc.be.resources.data.RelationshipTypeData; +import org.openecomp.sdc.be.resources.data.UniqueIdData; import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -53,19 +60,6 @@ public class RelationshipTypeOperation extends AbstractOperation { @Autowired private DerivedFromOperation derivedFromOperation; - public Either<RelationshipTypeDefinition, JanusGraphOperationStatus> getRelationshipTypeByName(String name) { - String uid = UniqueIdBuilder.buildRelationshipTypeUid(name); - Either<RelationshipTypeDefinition, JanusGraphOperationStatus> result = getRelationshipTypeByUid(uid); - if (result.isRight()) { - JanusGraphOperationStatus status = result.right().value(); - if (status != JanusGraphOperationStatus.NOT_FOUND) { - logger.error("Failed to get information on relationship type {} status is {}", name, status); - } - return Either.right(status); - } - return Either.left(result.left().value()); - } - public Either<RelationshipTypeDefinition, JanusGraphOperationStatus> getRelationshipTypeByUid(String uniqueId) { Either<RelationshipTypeDefinition, JanusGraphOperationStatus> result; Either<RelationshipTypeData, JanusGraphOperationStatus> relationshipTypesRes = janusGraphGenericDao @@ -75,28 +69,30 @@ public class RelationshipTypeOperation extends AbstractOperation { logger.debug("Relationship type {} cannot be found in graph. status is {}", uniqueId, status); return Either.right(status); } - RelationshipTypeData relationshipTypeData = relationshipTypesRes.left().value(); + return getRelationshipTypeDefinition(relationshipTypesRes.left().value()); + } + + public Either<RelationshipTypeDefinition, JanusGraphOperationStatus> getRelationshipTypeDefinition(final RelationshipTypeData relationshipTypeData) { RelationshipTypeDefinition relationshipTypeDefinition = new RelationshipTypeDefinition( relationshipTypeData.getRelationshipTypeDataDefinition()); Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> propertiesStatus = OperationUtils - .fillProperties(uniqueId, propertyOperation, NodeTypeEnum.RelationshipType); + .fillProperties(relationshipTypeData.getUniqueId(), propertyOperation, NodeTypeEnum.RelationshipType); if (propertiesStatus.isRight() && propertiesStatus.right().value() != JanusGraphOperationStatus.OK) { - logger.error("Failed to fetch properties of relationship type {}", uniqueId); + logger.error(BUSINESS_PROCESS_ERROR, "Failed to fetch properties of relationship type {}", relationshipTypeData.getUniqueId()); return Either.right(propertiesStatus.right().value()); } if (propertiesStatus.isLeft()) { relationshipTypeDefinition.setProperties(propertiesStatus.left().value()); } Either<ImmutablePair<RelationshipTypeData, GraphEdge>, JanusGraphOperationStatus> parentNode = janusGraphGenericDao - .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.RelationshipType), uniqueId, GraphEdgeLabels.DERIVED_FROM, + .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.RelationshipType), relationshipTypeData.getUniqueId(), GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.RelationshipType, RelationshipTypeData.class); - logger.debug("After retrieving DERIVED_FROM node of {}. status is {}", uniqueId, parentNode); + logger.debug("After retrieving DERIVED_FROM node of {}. status is {}", relationshipTypeData.getUniqueId(), parentNode); if (parentNode.isRight()) { JanusGraphOperationStatus janusGraphOperationStatus = parentNode.right().value(); if (janusGraphOperationStatus != JanusGraphOperationStatus.NOT_FOUND) { - logger.error("Failed to find the parent relationship of relationship type {}. status is {}", uniqueId, janusGraphOperationStatus); - result = Either.right(janusGraphOperationStatus); - return result; + logger.error(BUSINESS_PROCESS_ERROR, "Failed to find the parent relationship of relationship type {}. status is {}", relationshipTypeData.getUniqueId(), janusGraphOperationStatus); + return Either.right(janusGraphOperationStatus); } } else { // derived from node was found @@ -104,16 +100,28 @@ public class RelationshipTypeOperation extends AbstractOperation { RelationshipTypeData parentCT = immutablePair.getKey(); relationshipTypeDefinition.setDerivedFrom(parentCT.getRelationshipTypeDataDefinition().getType()); } - result = Either.left(relationshipTypeDefinition); - return result; + + final Either<ImmutablePair<ModelData, GraphEdge>, JanusGraphOperationStatus> model = janusGraphGenericDao.getParentNode( + UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.RelationshipType), relationshipTypeData.getUniqueId(), GraphEdgeLabels.MODEL_ELEMENT, + NodeTypeEnum.Model, ModelData.class); + if (model.isLeft()) { + relationshipTypeDefinition.setModel(model.left().value().getLeft().getName()); + } + return Either.left(relationshipTypeDefinition); } private Either<RelationshipTypeDefinition, StorageOperationStatus> validateUpdateProperties( RelationshipTypeDefinition relationshipTypeDefinition) { JanusGraphOperationStatus error = null; if (MapUtils.isNotEmpty(relationshipTypeDefinition.getProperties()) && relationshipTypeDefinition.getDerivedFrom() != null) { + final Either<RelationshipTypeData, JanusGraphOperationStatus> derivedFromNode = janusGraphGenericDao.getNode(GraphPropertiesDictionary.TYPE.getProperty(), + relationshipTypeDefinition.getDerivedFrom(), RelationshipTypeData.class, relationshipTypeDefinition.getModel()); + if (derivedFromNode.isRight()) { + logger.error(BUSINESS_PROCESS_ERROR, "Failed to find the derived from type for {}. status is {}", relationshipTypeDefinition.getUniqueId(), derivedFromNode.right().value()); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(derivedFromNode.right().value())); + } Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> allPropertiesRes = getAllRelationshipTypePropertiesFromAllDerivedFrom( - relationshipTypeDefinition.getDerivedFrom()); + derivedFromNode.left().value().getUniqueId()); if (allPropertiesRes.isRight() && !JanusGraphOperationStatus.NOT_FOUND.equals(allPropertiesRes.right().value())) { error = allPropertiesRes.right().value(); logger.debug("Couldn't fetch derived from property nodes for relationship type {}, error: {}", relationshipTypeDefinition.getType(), @@ -146,8 +154,8 @@ public class RelationshipTypeOperation extends AbstractOperation { } private Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> getAllRelationshipTypePropertiesFromAllDerivedFrom( - String firstParentType) { - return propertyOperation.getAllTypePropertiesFromAllDerivedFrom(firstParentType, NodeTypeEnum.RelationshipType, RelationshipTypeData.class); + String firstParentUid) { + return propertyOperation.getAllTypePropertiesFromAllDerivedFrom(firstParentUid, NodeTypeEnum.RelationshipType, RelationshipTypeData.class); } public Either<RelationshipTypeDefinition, StorageOperationStatus> addRelationshipType(RelationshipTypeDefinition relationshipTypeDefinition, @@ -204,7 +212,7 @@ public class RelationshipTypeOperation extends AbstractOperation { private Either<RelationshipTypeData, StorageOperationStatus> addRelationshipTypeToGraph(RelationshipTypeDefinition relationshipTypeDefinition) { logger.debug("Got relationship type {}", relationshipTypeDefinition); - String ctUniqueId = UniqueIdBuilder.buildRelationshipTypeUid(relationshipTypeDefinition.getType()); + String ctUniqueId = UniqueIdBuilder.buildRelationshipTypeUid(relationshipTypeDefinition.getModel(), relationshipTypeDefinition.getType()); RelationshipTypeData relationshipTypeData = buildRelationshipTypeData(relationshipTypeDefinition, ctUniqueId); logger.debug("Before adding relationship type to graph. relationshipTypeData = {}", relationshipTypeData); Either<RelationshipTypeData, JanusGraphOperationStatus> createCTResult = janusGraphGenericDao @@ -223,6 +231,11 @@ public class RelationshipTypeOperation extends AbstractOperation { logger.error("Failed add properties {} to relationship {}", propertiesMap, relationshipTypeDefinition.getType()); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(addPropertiesToRelationshipType.right().value())); } + final Either<GraphRelation, StorageOperationStatus> modelRelationship = addRelationshipToModel(relationshipTypeDefinition); + if (modelRelationship.isRight()) { + return Either.right(modelRelationship.right().value()); + } + return addDerivedFromRelation(relationshipTypeDefinition, ctUniqueId).left().map(updatedDerivedFrom -> createCTResult.left().value()); } @@ -246,14 +259,31 @@ public class RelationshipTypeOperation extends AbstractOperation { } logger.debug("#addDerivedFromRelation - adding derived from relation between relationship type {} to its parent " + "{}", relationshipTypeDefinition.getType(), derivedFrom); - return getRelationshipTypeByType(derivedFrom).right().map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus).left().bind( + return getRelationshipTypeByTypeAndModel(derivedFrom, relationshipTypeDefinition.getModel()).right().map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus).left().bind( derivedFromRelationship -> derivedFromOperation .addDerivedFromRelation(relationshipTypeUniqueId, derivedFromRelationship.getUniqueId(), NodeTypeEnum.RelationshipType)); } + + private Either<GraphRelation, StorageOperationStatus> addRelationshipToModel(final RelationshipTypeDefinition relationshipTypeDefinition) { + final String model = relationshipTypeDefinition.getModel(); + if (model == null) { + return Either.left(null); + } + final GraphNode from = new UniqueIdData(NodeTypeEnum.Model, UniqueIdBuilder.buildModelUid(model)); + final GraphNode to = new UniqueIdData(NodeTypeEnum.RelationshipType, relationshipTypeDefinition.getUniqueId()); + logger.info("Connecting model {} to type {}", from, to); + return janusGraphGenericDao.createRelation(from , to, GraphEdgeLabels.MODEL_ELEMENT, Collections.emptyMap()).right().map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus); + } - private Either<RelationshipTypeDefinition, JanusGraphOperationStatus> getRelationshipTypeByType(String relationshipType) { - // Optimization: In case of Relationship Type its unique ID is the same as type - return getRelationshipTypeByUid(relationshipType); + private Either<RelationshipTypeDefinition, JanusGraphOperationStatus> getRelationshipTypeByTypeAndModel(final String relationshipType, final String model) { + final Either<RelationshipTypeData, JanusGraphOperationStatus> relationshipTypesRes = janusGraphGenericDao + .getNode(GraphPropertiesDictionary.TYPE.getProperty(), relationshipType, RelationshipTypeData.class, model); + if (relationshipTypesRes.isRight()) { + final JanusGraphOperationStatus status = relationshipTypesRes.right().value(); + logger.debug("Relationship type {} cannot be found in graph. status is {}", relationshipType, status); + return Either.right(status); + } + return getRelationshipTypeDefinition(relationshipTypesRes.left().value()); } public Either<RelationshipTypeDefinition, StorageOperationStatus> updateRelationshipType(RelationshipTypeDefinition newRelationshipTypeDefinition, @@ -300,7 +330,7 @@ public class RelationshipTypeOperation extends AbstractOperation { logger.debug("#updateRelationshipTypeDerivedFrom - updating relationship derived from relation for relationship " + "type with id {}. old derived type {}. new derived type {}", relationshipTypeId, currDerivedFromRelationshipType, newRelationshipTypeDefinition.getDerivedFrom()); - StorageOperationStatus deleteDerivedRelationStatus = deleteDerivedFromRelationshipType(relationshipTypeId, currDerivedFromRelationshipType); + StorageOperationStatus deleteDerivedRelationStatus = deleteDerivedFromRelationshipType(relationshipTypeId, newRelationshipTypeDefinition.getModel(), currDerivedFromRelationshipType); if (deleteDerivedRelationStatus != StorageOperationStatus.OK) { return Either.right(deleteDerivedRelationStatus); } @@ -322,14 +352,14 @@ public class RelationshipTypeOperation extends AbstractOperation { .map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus); } - private StorageOperationStatus deleteDerivedFromRelationshipType(String relationshipTypeId, String derivedFromType) { + private StorageOperationStatus deleteDerivedFromRelationshipType(final String relationshipTypeId, final String modelName, final String derivedFromType) { if (derivedFromType == null) { return StorageOperationStatus.OK; } logger .debug("#deleteDerivedFromRelationshipType - deleting derivedFrom relation for relationship type with id " + "{} and its derived type {}", relationshipTypeId, derivedFromType); - return getRelationshipTypeByType(derivedFromType).either(derivedFromNode -> derivedFromOperation + return getRelationshipTypeByTypeAndModel(derivedFromType, modelName).either(derivedFromNode -> derivedFromOperation .removeDerivedFromRelation(relationshipTypeId, derivedFromNode.getUniqueId(), NodeTypeEnum.RelationshipType), DaoStatusConverter::convertJanusGraphStatusToStorageStatus); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/UniqueIdBuilder.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/UniqueIdBuilder.java index 62404aa409..77b28c7561 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/UniqueIdBuilder.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/UniqueIdBuilder.java @@ -22,6 +22,7 @@ package org.openecomp.sdc.be.model.operations.impl; import java.util.HashMap; import java.util.Map; import java.util.UUID; +import org.apache.commons.lang.StringUtils; import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; @@ -92,8 +93,8 @@ public class UniqueIdBuilder { return type; } - static String buildRelationshipTypeUid(String type) { - return type; + public static String buildRelationshipTypeUid(final String modelName, final String type) { + return StringUtils.isEmpty(modelName) ? type : modelName + DOT + type; } public static String buildAttributeUid(String resourceId, String attName) { diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java index 30463e9982..1cc13364a5 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java @@ -43,9 +43,11 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.mockito.Spy; +import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge; import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation; import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels; import org.openecomp.sdc.be.datatypes.elements.RelationshipInstDataDefinition; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.model.ModelTestBase; @@ -58,6 +60,7 @@ import org.openecomp.sdc.be.model.tosca.ToscaType; import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint; import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint; import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint; +import org.openecomp.sdc.be.resources.data.ModelData; import org.openecomp.sdc.be.resources.data.RelationshipTypeData; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -68,6 +71,8 @@ public class RelationshipTypeOperationTest extends ModelTestBase { private static final String PROP = "prop"; + private static final String DOT = "."; + @Mock HealingJanusGraphGenericDao janusGraphGenericDao; @@ -105,16 +110,6 @@ public class RelationshipTypeOperationTest extends ModelTestBase { } @Test - public void getRelationshipTypeByNameNotCreated() { - Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_CREATED)) - .when(relationshipTypeOperation).getRelationshipTypeByUid(Mockito.anyString()); - - Either<RelationshipTypeDefinition, JanusGraphOperationStatus> either = - relationshipTypeOperation.getRelationshipTypeByName("name"); - assertTrue(either.isRight()); - } - - @Test public void testDummy() { assertNotNull(relationshipTypeOperation); } @@ -124,6 +119,12 @@ public class RelationshipTypeOperationTest extends ModelTestBase { Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_CONNECTED)) .when(propertyOperation) .getAllTypePropertiesFromAllDerivedFrom(Mockito.anyString(), Mockito.any(), Mockito.any()); + + Mockito.doReturn(Either.left(new RelationshipTypeData(relationshipTypeDefinition))) + .when(janusGraphGenericDao).getNode(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()); + + Mockito.doReturn(Either.left(Collections.singletonMap("derivedFromProp1", new PropertyDefinition()))).when(propertyOperation) + .findPropertiesOfNode(Mockito.any(), Mockito.anyString()); Either<RelationshipTypeDefinition, StorageOperationStatus> addRelationshipType = @@ -138,6 +139,12 @@ public class RelationshipTypeOperationTest extends ModelTestBase { .getAllTypePropertiesFromAllDerivedFrom(Mockito.anyString(), Mockito.any(), Mockito.any()); Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(propertyOperation) .validatePropertiesUniqueness(Mockito.any(), Mockito.any()); + + Mockito.doReturn(Either.left(new RelationshipTypeData(relationshipTypeDefinition))) + .when(janusGraphGenericDao).getNode(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()); + + Mockito.doReturn(Either.left(Collections.singletonMap("derivedFromProp1", new PropertyDefinition()))).when(propertyOperation) + .findPropertiesOfNode(Mockito.any(), Mockito.anyString()); Either<RelationshipTypeDefinition, StorageOperationStatus> addRelationshipType = relationshipTypeOperation.addRelationshipType(relationshipTypeDefinition, false); @@ -346,6 +353,8 @@ public class RelationshipTypeOperationTest extends ModelTestBase { RelationshipTypeDefinition relationshipTypeDefinition = new RelationshipTypeDefinition(relationshipTypeData); relationshipTypeDefinition.setProperties(createPropertyData("prop1")); relationshipTypeDefinition.setDerivedFrom("tosca.relationships.Root"); + + Mockito.doReturn(Either.left(Collections.singletonMap("prop1", new PropertyDefinition()))).when(propertyOperation) .getAllTypePropertiesFromAllDerivedFrom(Mockito.anyString(), Mockito.any(), Mockito.any()); @@ -355,7 +364,7 @@ public class RelationshipTypeOperationTest extends ModelTestBase { Mockito.doReturn(Either.left(relationshipTypeData)).when(janusGraphGenericDao) .createNode(Mockito.any(), Mockito.eq(RelationshipTypeData.class)); - + Mockito.doReturn(Either.left(new HashMap())).when(propertyOperation) .addPropertiesToElementType(Mockito.anyString(), Mockito.any(), Mockito.anyMap()); @@ -369,6 +378,98 @@ public class RelationshipTypeOperationTest extends ModelTestBase { Mockito.doReturn(Either.left(relationshipTypeDefinition)) .when(relationshipTypeOperation).getRelationshipType(Mockito.anyString(), Mockito.anyBoolean()); + Mockito.doReturn(Either.left(Collections.singletonMap("derivedFromProp1", new PropertyDefinition()))).when(propertyOperation) + .findPropertiesOfNode(Mockito.any(), Mockito.anyString()); + + Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(janusGraphGenericDao) + .getChild(Mockito.any(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.eq(RelationshipTypeData.class)); + + Mockito.doReturn(Either.left(new RelationshipTypeData(relationshipTypeDefinition))) + .when(janusGraphGenericDao).getNode(Mockito.anyString(), Mockito.any(), Mockito.eq(RelationshipTypeData.class), Mockito.any()); + + + ModelData modelData = new ModelData("modelA", "modelA"); + ImmutablePair<ModelData, GraphEdge> pair = new ImmutablePair<>(modelData, new GraphEdge()); + Mockito.doReturn(Either.left(pair)) + .when(janusGraphGenericDao).getParentNode("uid", relationshipInstDataDefinition1.getUniqueId(), GraphEdgeLabels.MODEL_ELEMENT, NodeTypeEnum.Model, ModelData.class); + + Either<RelationshipTypeDefinition, StorageOperationStatus> either = + relationshipTypeOperation.addRelationshipType(relationshipTypeDefinition, true); + + assertTrue(either.isLeft()); + } + + @Test + public void testAddRelationshipTypeToModel() { + + final String relationshipName = "tosca.relationships.MyRelationship"; + final String derivedFromRelationshipName = "tosca.relationships.Root"; + final String modelName = "modelA"; + + RelationshipTypeDefinition relationshipTypeDefinition = new RelationshipTypeDefinition(); + relationshipTypeDefinition.setProperties(createPropertyData("prop1")); + relationshipTypeDefinition.setUniqueId(modelName + DOT + relationshipName); + relationshipTypeDefinition.setType(relationshipName); + relationshipTypeDefinition.setModel(modelName); + relationshipTypeDefinition.setDerivedFrom(derivedFromRelationshipName); + + RelationshipTypeData derivedFromRelationshipTypeData = new RelationshipTypeData(); + RelationshipInstDataDefinition dervideFromRelationshipInstDataDefinition = new RelationshipInstDataDefinition(); + dervideFromRelationshipInstDataDefinition.setUniqueId("modelA.tosca.relationships.Root"); + dervideFromRelationshipInstDataDefinition.setType("tosca.relationships.Root"); + derivedFromRelationshipTypeData.setRelationshipTypeDataDefinition(dervideFromRelationshipInstDataDefinition); + + Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)) + .when(janusGraphGenericDao).getNode("uid", relationshipTypeDefinition.getUniqueId(), RelationshipTypeData.class); + + Mockito.doReturn(Either.left(derivedFromRelationshipTypeData)) + .when(janusGraphGenericDao).getNode("type", "tosca.relationships.Root", RelationshipTypeData.class, "modelA"); + + Mockito.doReturn(Either.left(Collections.singletonMap("prop1", new PropertyDefinition()))).when(propertyOperation) + .getAllTypePropertiesFromAllDerivedFrom(modelName + DOT + derivedFromRelationshipName, NodeTypeEnum.RelationshipType, RelationshipTypeData.class); + + Mockito.doReturn(Either.left(new ArrayList<>(relationshipTypeDefinition.getProperties().values()))).when(propertyOperation) + .validatePropertiesUniqueness(Mockito.any(), Mockito.any()); + + Mockito.doReturn(Either.left(new RelationshipTypeData(relationshipTypeDefinition))).when(janusGraphGenericDao) + .createNode(Mockito.any(), Mockito.eq(RelationshipTypeData.class)); + + Mockito.doReturn(Either.left(new HashMap())).when(propertyOperation) + .addPropertiesToElementType(Mockito.anyString(), Mockito.any(), Mockito.anyMap()); + + Mockito.doReturn(Either.left(new GraphRelation())).when(janusGraphGenericDao) + .createRelation(Mockito.any(), Mockito.any(), Mockito.eq(GraphEdgeLabels.MODEL_ELEMENT), Mockito.any()); + + Mockito.doReturn(Either.left(Collections.singletonMap("derivedFromProp1", new PropertyDefinition()))).when(propertyOperation) + .findPropertiesOfNode(NodeTypeEnum.RelationshipType, derivedFromRelationshipTypeData.getUniqueId()); + + Mockito.doReturn(Either.left(Collections.singletonMap("prop1", new PropertyDefinition()))).when(propertyOperation) + .findPropertiesOfNode(NodeTypeEnum.RelationshipType, relationshipTypeDefinition.getUniqueId()); + + Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(janusGraphGenericDao) + .getChild("uid", derivedFromRelationshipTypeData.getUniqueId(), GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.RelationshipType, RelationshipTypeData.class); + + Mockito.doReturn(Either.left(new ImmutablePair(new RelationshipTypeData(relationshipTypeDefinition), null))).when(janusGraphGenericDao) + .getChild("uid", relationshipTypeDefinition.getUniqueId(), GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.RelationshipType, RelationshipTypeData.class); + + + Mockito.doReturn(Either.left(new GraphRelation())).when(derivedFromOperation) + .addDerivedFromRelation(relationshipTypeDefinition.getUniqueId(), derivedFromRelationshipTypeData.getUniqueId(), NodeTypeEnum.RelationshipType); + + ModelData modelData = new ModelData("modelA", "modelA"); + ImmutablePair<ModelData, GraphEdge> pair = new ImmutablePair<>(modelData, new GraphEdge()); + Mockito.doReturn(Either.left(pair)) + .when(janusGraphGenericDao).getParentNode("uid", dervideFromRelationshipInstDataDefinition.getUniqueId(), GraphEdgeLabels.MODEL_ELEMENT, NodeTypeEnum.Model, ModelData.class); + + Mockito.doReturn(Either.left(pair)) + .when(janusGraphGenericDao).getParentNode("uid", relationshipTypeDefinition.getUniqueId(), GraphEdgeLabels.MODEL_ELEMENT, NodeTypeEnum.Model, ModelData.class); + + Mockito.doReturn(Either.left(new RelationshipTypeData(relationshipTypeDefinition))) + .when(janusGraphGenericDao).getNode("uid", relationshipTypeDefinition.getUniqueId(), RelationshipTypeData.class); + + Mockito.doReturn(Either.left(new RelationshipTypeData(relationshipTypeDefinition))) + .when(janusGraphGenericDao).getNode("type", relationshipTypeDefinition.getUniqueId(), RelationshipTypeData.class, "modelA"); + Either<RelationshipTypeDefinition, StorageOperationStatus> either = relationshipTypeOperation.addRelationshipType(relationshipTypeDefinition, true); @@ -425,6 +526,20 @@ public class RelationshipTypeOperationTest extends ModelTestBase { Mockito.doReturn(Either.left(new GraphRelation())) .when(derivedFromOperation) .addDerivedFromRelation(Mockito.anyString(), Mockito.anyString(), Mockito.any()); + + Mockito.doReturn(Either.left(new RelationshipTypeData(relationshipTypeDefinition))) + .when(janusGraphGenericDao).getNode(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()); + + Mockito.doReturn(Either.left(Collections.singletonMap("derivedFromProp1", new PropertyDefinition()))).when(propertyOperation) + .findPropertiesOfNode(Mockito.any(), Mockito.anyString()); + + Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(janusGraphGenericDao) + .getChild(Mockito.any(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.eq(RelationshipTypeData.class)); + + ModelData modelData = new ModelData("modelA", "modelA"); + ImmutablePair<ModelData, GraphEdge> pair = new ImmutablePair<>(modelData, new GraphEdge()); + Mockito.doReturn(Either.left(pair)) + .when(janusGraphGenericDao).getParentNode("uid", newRelationshipTypeDefinition.getUniqueId(), GraphEdgeLabels.MODEL_ELEMENT, NodeTypeEnum.Model, ModelData.class); Either<RelationshipTypeDefinition, StorageOperationStatus> either = relationshipTypeOperation.updateRelationshipType(relationshipTypeDefinition, @@ -450,6 +565,11 @@ public class RelationshipTypeOperationTest extends ModelTestBase { janusGraphGenericDao) .getChild(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.eq(RelationshipTypeData.class)); + + ModelData modelData = new ModelData("modelA", "modelA"); + ImmutablePair<ModelData, GraphEdge> pair = new ImmutablePair<>(modelData, new GraphEdge()); + Mockito.doReturn(Either.left(pair)) + .when(janusGraphGenericDao).getParentNode("uid", relationshipTypeDefinition.getUniqueId(), GraphEdgeLabels.MODEL_ELEMENT, NodeTypeEnum.Model, ModelData.class); Either<RelationshipTypeDefinition, JanusGraphOperationStatus> either = relationshipTypeOperation.getRelationshipTypeByUid("tosca.relationships.Container1"); |