diff options
author | MichaelMorris <michael.morris@est.tech> | 2021-05-25 09:32:31 +0100 |
---|---|---|
committer | Christophe Closset <christophe.closset@intl.att.com> | 2021-06-08 06:57:55 +0000 |
commit | b08daeee5442d8422e2e23047ed13aa10d8303f9 (patch) | |
tree | c40477f7e3f200b4af3021f1523962de04315af3 /catalog-model | |
parent | 2a62c13ecfcf3fe0c22d8cd21d66d46e58105018 (diff) |
Support adding data types to model
Signed-off-by: MichaelMorris <michael.morris@est.tech>
Issue-ID: SDC-3611
Change-Id: Ia9fd2437b71a2cd4fc853ff6a1f4fd37fe09a685
Diffstat (limited to 'catalog-model')
6 files changed, 177 insertions, 111 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java index b61aa5a4c2..01f6cd1b40 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java @@ -36,6 +36,7 @@ public class DataTypeDefinition extends DataTypeDataDefinition { private DataTypeDefinition derivedFrom; private List<PropertyConstraint> constraints; private List<PropertyDefinition> properties; + private String model; public DataTypeDefinition(final DataTypeDataDefinition dataTypeDataDefinition) { super(dataTypeDataDefinition); @@ -49,6 +50,7 @@ public class DataTypeDefinition extends DataTypeDataDefinition { this.setUniqueId(dataTypeDefinition.getUniqueId()); this.setConstraints(dataTypeDefinition.getConstraints()); this.setDescription(dataTypeDefinition.getDescription()); + this.setModel(dataTypeDefinition.getModel()); } public List<PropertyConstraint> safeGetConstraints() { diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/IPropertyOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/IPropertyOperation.java index 163ee43655..c0cc097c49 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/IPropertyOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/IPropertyOperation.java @@ -50,7 +50,7 @@ public interface IPropertyOperation { public boolean isPropertyDefaultValueValid(IComplexDefaultValue propertyDefinition, Map<String, DataTypeDefinition> dataTypes); - public boolean isPropertyTypeValid(IComplexDefaultValue propertyDefinition); + public boolean isPropertyTypeValid(IComplexDefaultValue propertyDefinition, String model); public ImmutablePair<String, Boolean> isPropertyInnerTypeValid(IComplexDefaultValue propertyDefinition, Map<String, DataTypeDefinition> dataTypes); @@ -65,11 +65,9 @@ public interface IPropertyOperation { * @param name * @return */ - public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(String name); + public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(String name, String validForModel); - public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(String name, boolean inTransaction); - - public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByNameWithoutDerived(String name); + public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(String name, String validForModel, boolean inTransaction); public StorageOperationStatus validateAndUpdateProperty(IComplexDefaultValue propertyDefinition, Map<String, DataTypeDefinition> dataTypes); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/AttributeOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/AttributeOperation.java index 74dc4207a1..685dc85675 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/AttributeOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/AttributeOperation.java @@ -78,7 +78,7 @@ public class AttributeOperation extends AbstractOperation { } private Either<Boolean, JanusGraphOperationStatus> isDefinedInDataTypes(final String propertyType) { - final String dataTypeUid = UniqueIdBuilder.buildDataTypeUid(propertyType); + final String dataTypeUid = UniqueIdBuilder.buildDataTypeUid(null, propertyType); final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = getDataTypeByUid(dataTypeUid); if (dataTypeByUid.isRight()) { final JanusGraphOperationStatus status = dataTypeByUid.right().value(); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java index 06323644b7..1f4fca0685 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java @@ -19,20 +19,8 @@ */ package org.openecomp.sdc.be.model.operations.impl; -import com.fasterxml.jackson.core.ObjectCodec; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.collect.Maps; -import com.google.gson.JsonArray; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonParser; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import fj.data.Either; +import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR; + import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; @@ -101,10 +89,25 @@ 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; +import com.fasterxml.jackson.core.ObjectCodec; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.google.common.collect.Maps; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonParser; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import fj.data.Either; @Component("property-operation") public class PropertyOperation extends AbstractOperation implements IPropertyOperation { + private static final String AFTER_RETRIEVING_DERIVED_FROM_NODE_OF_STATUS_IS = "After retrieving DERIVED_FROM node of {}. status is {}"; private static final String FAILED_TO_FETCH_PROPERTIES_OF_DATA_TYPE = "Failed to fetch properties of data type {}"; private static final String DATA_TYPE_CANNOT_BE_FOUND_IN_GRAPH_STATUS_IS = "Data type {} cannot be found in graph. status is {}"; private static final String GOING_TO_EXECUTE_COMMIT_ON_GRAPH = "Going to execute commit on graph."; @@ -809,12 +812,12 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } } - public boolean isPropertyTypeValid(IComplexDefaultValue property) { + public boolean isPropertyTypeValid(final IComplexDefaultValue property, final String model) { if (property == null) { return false; } if (ToscaPropertyType.isValidType(property.getType()) == null) { - Either<Boolean, JanusGraphOperationStatus> definedInDataTypes = isDefinedInDataTypes(property.getType()); + Either<Boolean, JanusGraphOperationStatus> definedInDataTypes = isDefinedInDataTypes(property.getType(), model); if (definedInDataTypes.isRight()) { return false; } else { @@ -1115,7 +1118,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe */ private Either<DataTypeData, JanusGraphOperationStatus> addDataTypeToGraph(DataTypeDefinition dataTypeDefinition) { log.debug("Got data type {}", dataTypeDefinition); - String dtUniqueId = UniqueIdBuilder.buildDataTypeUid(dataTypeDefinition.getName()); + String dtUniqueId = UniqueIdBuilder.buildDataTypeUid(dataTypeDefinition.getModel(), dataTypeDefinition.getName()); DataTypeData dataTypeData = buildDataTypeData(dataTypeDefinition, dtUniqueId); log.debug("Before adding data type to graph. dataTypeData = {}", dataTypeData); Either<DataTypeData, JanusGraphOperationStatus> createDataTypeResult = janusGraphGenericDao.createNode(dataTypeData, DataTypeData.class); @@ -1128,17 +1131,28 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } DataTypeData resultCTD = createDataTypeResult.left().value(); List<PropertyDefinition> properties = dataTypeDefinition.getProperties(); - Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToDataType = addPropertiesToDataType(resultCTD.getUniqueId(), + Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToDataType = addPropertiesToDataType(resultCTD.getUniqueId(), dataTypeDefinition.getModel(), properties); if (addPropertiesToDataType.isRight()) { log.debug("Failed add properties {} to data type {}", properties, dataTypeDefinition.getName()); return Either.right(addPropertiesToDataType.right().value()); } + + final Either<GraphRelation, JanusGraphOperationStatus> modelRelationship = addDataTypeToModel(dataTypeDefinition); + if (modelRelationship.isRight()) { + return Either.right(modelRelationship.right().value()); + } + String derivedFrom = dataTypeDefinition.getDerivedFromName(); if (derivedFrom != null) { + final Either<DataTypeDefinition, JanusGraphOperationStatus> derivedFromDataType = getDataTypeByNameValidForModel(derivedFrom, dataTypeDefinition.getModel()); + if (derivedFromDataType.isRight()) { + return Either.right(derivedFromDataType.right().value()); + } + log.debug("Before creating relation between data type {} to its parent {}", dtUniqueId, derivedFrom); UniqueIdData from = new UniqueIdData(NodeTypeEnum.DataType, dtUniqueId); - String deriveFromUid = UniqueIdBuilder.buildDataTypeUid(derivedFrom); + final String deriveFromUid = derivedFromDataType.left().value().getUniqueId(); UniqueIdData to = new UniqueIdData(NodeTypeEnum.DataType, deriveFromUid); Either<GraphRelation, JanusGraphOperationStatus> createRelation = janusGraphGenericDao .createRelation(from, to, GraphEdgeLabels.DERIVED_FROM, null); @@ -1149,6 +1163,17 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } return Either.left(createDataTypeResult.left().value()); } + + private Either<GraphRelation, JanusGraphOperationStatus> addDataTypeToModel(final DataTypeDefinition dataTypeDefinition) { + final String model = dataTypeDefinition.getModel(); + if (model == null) { + return Either.left(null); + } + final GraphNode from = new UniqueIdData(NodeTypeEnum.Model, UniqueIdBuilder.buildModelUid(model)); + final GraphNode to = new UniqueIdData(NodeTypeEnum.DataType, dataTypeDefinition.getUniqueId()); + log.info("Connecting model {} to type {}", from, to); + return janusGraphGenericDao.createRelation(from , to, GraphEdgeLabels.MODEL_ELEMENT, Collections.emptyMap()); + } private DataTypeData buildDataTypeData(DataTypeDefinition dataTypeDefinition, String ctUniqueId) { DataTypeData dataTypeData = new DataTypeData(dataTypeDefinition); @@ -1171,14 +1196,14 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe * @param properties * @return */ - private Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToDataType(String uniqueId, - List<PropertyDefinition> properties) { + private Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToDataType(final String uniqueId, final String modelName, + final List<PropertyDefinition> properties) { Map<String, PropertyData> propertiesData = new HashMap<>(); if (properties != null && !properties.isEmpty()) { for (PropertyDefinition propertyDefinition : properties) { String propertyName = propertyDefinition.getName(); String propertyType = propertyDefinition.getType(); - Either<Boolean, JanusGraphOperationStatus> validPropertyType = isValidPropertyType(propertyType); + Either<Boolean, JanusGraphOperationStatus> validPropertyType = isValidPropertyType(propertyType, modelName); if (validPropertyType.isRight()) { log.debug("Data type {} contains invalid property type {}", uniqueId, propertyType); return Either.right(validPropertyType.right().value()); @@ -1217,6 +1242,45 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } return Either.left(propertiesData); } + + public Either<DataTypeDefinition, JanusGraphOperationStatus> getDataTypeByNameValidForModel(final String name, final String modelName) { + final Either<DataTypeData, JanusGraphOperationStatus> dataTypesRes = janusGraphGenericDao + .getNode(GraphPropertiesDictionary.NAME.getProperty(), name, DataTypeData.class, modelName); + if (dataTypesRes.isRight()) { + final JanusGraphOperationStatus status = dataTypesRes.right().value(); + log.debug(DATA_TYPE_CANNOT_BE_FOUND_IN_GRAPH_STATUS_IS, name, status); + return Either.right(status); + } + final DataTypeData dataType = dataTypesRes.left().value(); + final DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(dataType.getDataTypeDataDefinition()); + final JanusGraphOperationStatus propertiesStatus = fillProperties(dataTypeDefinition.getUniqueId(), dataTypeDefinition); + if (propertiesStatus != JanusGraphOperationStatus.OK) { + log.error(BUSINESS_PROCESS_ERROR, FAILED_TO_FETCH_PROPERTIES_OF_DATA_TYPE, dataTypeDefinition.getUniqueId()); + return Either.right(propertiesStatus); + } + final Either<ImmutablePair<DataTypeData, GraphEdge>, JanusGraphOperationStatus> parentNode = janusGraphGenericDao + .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), dataTypeDefinition.getUniqueId(), GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.DataType, + DataTypeData.class); + log.debug(AFTER_RETRIEVING_DERIVED_FROM_NODE_OF_STATUS_IS, dataTypeDefinition.getUniqueId(), parentNode); + if (parentNode.isRight()) { + final JanusGraphOperationStatus janusGraphOperationStatus = parentNode.right().value(); + if (janusGraphOperationStatus != JanusGraphOperationStatus.NOT_FOUND) { + log.error(BUSINESS_PROCESS_ERROR, "Failed to find the parent data type of data type {}. status is {}", dataTypeDefinition.getUniqueId(), janusGraphOperationStatus); + return Either.right(janusGraphOperationStatus); + } + } else { + // derived from node was found + final ImmutablePair<DataTypeData, GraphEdge> immutablePair = parentNode.left().value(); + final DataTypeData parentDataType = immutablePair.getKey(); + final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = getDataTypeByUid(parentDataType.getUniqueId()); + if (dataTypeByUid.isRight()) { + return Either.right(dataTypeByUid.right().value()); + } + DataTypeDefinition parentDataTypeDefinition = dataTypeByUid.left().value(); + dataTypeDefinition.setDerivedFrom(parentDataTypeDefinition); + } + return Either.left(dataTypeDefinition); + } /** * Build Data type object from graph by unique id @@ -1243,7 +1307,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe Either<ImmutablePair<DataTypeData, GraphEdge>, JanusGraphOperationStatus> parentNode = janusGraphGenericDao .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.DataType, DataTypeData.class); - log.debug("After retrieving DERIVED_FROM node of {}. status is {}", uniqueId, parentNode); + log.debug(AFTER_RETRIEVING_DERIVED_FROM_NODE_OF_STATUS_IS, uniqueId, parentNode); if (parentNode.isRight()) { JanusGraphOperationStatus janusGraphOperationStatus = parentNode.right().value(); if (janusGraphOperationStatus != JanusGraphOperationStatus.NOT_FOUND) { @@ -1330,11 +1394,10 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } @Override - public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(String name, boolean inTransaction) { + public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(final String name, final String validForModel, final boolean inTransaction) { Either<DataTypeDefinition, StorageOperationStatus> result = null; try { - String dtUid = UniqueIdBuilder.buildDataTypeUid(name); - Either<DataTypeDefinition, JanusGraphOperationStatus> ctResult = this.getDataTypeByUid(dtUid); + Either<DataTypeDefinition, JanusGraphOperationStatus> ctResult = this.getDataTypeByNameValidForModel(name, validForModel); if (ctResult.isRight()) { JanusGraphOperationStatus status = ctResult.right().value(); if (status != JanusGraphOperationStatus.NOT_FOUND) { @@ -1359,24 +1422,18 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } @Override - public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(String name) { - return getDataTypeByName(name, true); - } - - @Override - public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByNameWithoutDerived(String name) { - return getDataTypeByNameWithoutDerived(name, true); + public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(final String name, final String validForModel) { + return getDataTypeByName(name, validForModel, true); } - - private Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByNameWithoutDerived(String name, boolean inTransaction) { + + public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByUidWithoutDerived(String uid, boolean inTransaction) { Either<DataTypeDefinition, StorageOperationStatus> result = null; try { - String uid = UniqueIdBuilder.buildDataTypeUid(name); Either<DataTypeDefinition, JanusGraphOperationStatus> ctResult = this.getDataTypeByUidWithoutDerivedDataTypes(uid); if (ctResult.isRight()) { JanusGraphOperationStatus status = ctResult.right().value(); if (status != JanusGraphOperationStatus.NOT_FOUND) { - log.error("Failed to retrieve information on capability type {} status is {}", name, status); + log.error(BUSINESS_PROCESS_ERROR, "Failed to retrieve information on data type {} status is {}", uid, status); } result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(ctResult.right().value())); return result; @@ -1425,23 +1482,22 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe return new DataTypeDefinition(dataTypeData.getDataTypeDataDefinition()); } - private Either<Boolean, JanusGraphOperationStatus> isValidPropertyType(String propertyType) { + private Either<Boolean, JanusGraphOperationStatus> isValidPropertyType(String propertyType, final String modelName) { if (propertyType == null || propertyType.isEmpty()) { return Either.left(false); } ToscaPropertyType toscaPropertyType = ToscaPropertyType.isValidType(propertyType); if (toscaPropertyType == null) { - return isDefinedInDataTypes(propertyType); + return isDefinedInDataTypes(propertyType, modelName); } else { return Either.left(true); } } - public Either<Boolean, JanusGraphOperationStatus> isDefinedInDataTypes(String propertyType) { - String dataTypeUid = UniqueIdBuilder.buildDataTypeUid(propertyType); - Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = getDataTypeByUid(dataTypeUid); - if (dataTypeByUid.isRight()) { - JanusGraphOperationStatus status = dataTypeByUid.right().value(); + public Either<Boolean, JanusGraphOperationStatus> isDefinedInDataTypes(final String propertyType, final String modelName) { + Either<DataTypeDefinition, JanusGraphOperationStatus> dataType = getDataTypeByNameValidForModel(propertyType, modelName); + if (dataType.isRight()) { + JanusGraphOperationStatus status = dataType.right().value(); if (status == JanusGraphOperationStatus.NOT_FOUND) { return Either.left(false); } @@ -1531,7 +1587,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe Either<ImmutablePair<DataTypeData, GraphEdge>, JanusGraphOperationStatus> parentNode = janusGraphGenericDao .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.DataType, DataTypeData.class); - log.debug("After retrieving DERIVED_FROM node of {}. status is {}", uniqueId, parentNode); + log.debug(AFTER_RETRIEVING_DERIVED_FROM_NODE_OF_STATUS_IS, uniqueId, parentNode); if (parentNode.isRight()) { JanusGraphOperationStatus janusGraphOperationStatus = parentNode.right().value(); if (janusGraphOperationStatus != JanusGraphOperationStatus.NOT_FOUND) { @@ -1752,7 +1808,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } } Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToDataType = addPropertiesToDataType( - oldDataTypeDefinition.getUniqueId(), propertiesToAdd); + oldDataTypeDefinition.getUniqueId(), oldDataTypeDefinition.getModel(), propertiesToAdd); if (addPropertiesToDataType.isRight()) { log.debug("Failed to update data type {} to Graph. Status is {}", oldDataTypeDefinition, addPropertiesToDataType.right().value().name()); 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 77b28c7561..6e3d968556 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 @@ -176,8 +176,8 @@ public class UniqueIdBuilder { return resourceUniqueId + DOT + "additionalinformation"; } - static String buildDataTypeUid(String name) { - return name + DOT + "datatype"; + public static String buildDataTypeUid(final String modelName, final String name) { + return StringUtils.isEmpty(modelName) ? name + DOT + "datatype" : modelName + DOT + name + DOT + "datatype"; } public static String buildInvariantUUID() { diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java index 254208e06c..6812b44b41 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java @@ -26,11 +26,15 @@ import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.openecomp.sdc.be.dao.impl.HealingPipelineDao; +import org.mockito.Mockito; +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.JanusGraphClient; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao; 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.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyRule; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; @@ -44,7 +48,6 @@ import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint; import org.openecomp.sdc.be.resources.data.DataTypeData; import org.openecomp.sdc.be.resources.data.PropertyData; import org.openecomp.sdc.be.resources.data.PropertyValueData; - import java.util.*; import static org.junit.Assert.*; @@ -774,7 +777,7 @@ public class PropertyOperationTest extends ModelTestBase { // test 1 testSubject = createTestSubject(); property = null; - result = testSubject.isPropertyTypeValid(property); + result = testSubject.isPropertyTypeValid(property, null); Assert.assertEquals(false, result); } @@ -852,52 +855,71 @@ public class PropertyOperationTest extends ModelTestBase { @Test - public void testAddDataType() throws Exception { - PropertyOperation testSubject; + public void testAddAndGetDataType() throws Exception { + final String dataTypeName = "myDataType"; DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(); + dataTypeDefinition.setName("myDataType"); Either<DataTypeDefinition, StorageOperationStatus> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.addDataType(dataTypeDefinition); + + Mockito.doReturn(Either.left(new DataTypeData(dataTypeDefinition))).when(janusGraphGenericDao) + .createNode(Mockito.any(), Mockito.eq(DataTypeData.class)); + + Mockito.doReturn(Either.left(new DataTypeData(dataTypeDefinition))).when(janusGraphGenericDao) + .getNode(GraphPropertiesDictionary.NAME.getProperty(), dataTypeName, DataTypeData.class, null); + + Mockito.doReturn(Either.left(Collections.EMPTY_LIST)).when(janusGraphGenericDao) + .getChildrenNodes(Mockito.anyString(), Mockito.anyString(), Mockito.eq(GraphEdgeLabels.PROPERTY), Mockito.eq(NodeTypeEnum.Property), Mockito.eq(PropertyData.class)); + + result = propertyOperation.addDataType(dataTypeDefinition); + assertTrue(result.isLeft()); + + Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(janusGraphGenericDao) + .getChild(Mockito.anyString(), Mockito.anyString(), Mockito.eq(GraphEdgeLabels.DERIVED_FROM), Mockito.eq(NodeTypeEnum.DataType), Mockito.eq(DataTypeData.class)); + + result = propertyOperation.getDataTypeByName(dataTypeName, null, false); + assertTrue(result.isLeft()); + + result = propertyOperation.getDataTypeByName(dataTypeName, null); + assertTrue(result.isLeft()); + + Mockito.doReturn(Either.left(new DataTypeData(dataTypeDefinition))).when(janusGraphGenericDao) + .getNode(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), dataTypeName + ".datatype", DataTypeData.class); + + Either<DataTypeDefinition, JanusGraphOperationStatus> resultGetByUid = propertyOperation.getDataTypeByUid("myDataType.datatype"); + assertTrue(resultGetByUid.isLeft()); + + Either<Boolean, JanusGraphOperationStatus> resultIsDefinedDataType = propertyOperation.isDefinedInDataTypes(dataTypeName, null); + assertTrue(resultIsDefinedDataType.isLeft()); } - - @Test - public void testGetDataTypeByName() throws Exception { - PropertyOperation testSubject; - String name = ""; - boolean inTransaction = false; - Either<DataTypeDefinition, StorageOperationStatus> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getDataTypeByName(name, inTransaction); - } - - - @Test - public void testGetDataTypeByName_1() throws Exception { - PropertyOperation testSubject; - String name = ""; - Either<DataTypeDefinition, StorageOperationStatus> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getDataTypeByName(name); - } - - - @Test - public void testGetDataTypeByNameWithoutDerived() throws Exception { - PropertyOperation testSubject; - String name = ""; - Either<DataTypeDefinition, StorageOperationStatus> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getDataTypeByNameWithoutDerived(name); - } + @Test + public void testAddDataTypeToModel() throws Exception { + DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(); + dataTypeDefinition.setName("testName"); + dataTypeDefinition.setModel("testModel"); + Either<DataTypeDefinition, StorageOperationStatus> result; + + Mockito.doReturn(Either.left(new DataTypeData(dataTypeDefinition))).when(janusGraphGenericDao) + .createNode(Mockito.any(), Mockito.eq(DataTypeData.class)); + + Mockito.doReturn(Either.left(new GraphRelation())).when(janusGraphGenericDao) + .createRelation(Mockito.any(), Mockito.any(), Mockito.eq(GraphEdgeLabels.MODEL_ELEMENT), Mockito.any()); + + result = propertyOperation.addDataType(dataTypeDefinition); + assertTrue(result.isLeft()); + + Mockito.doReturn(Either.left(new DataTypeData(dataTypeDefinition))).when(janusGraphGenericDao) + .getNode(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), "testModel.testName.datatype", DataTypeData.class); + + Mockito.doReturn(Either.left(Collections.EMPTY_LIST)).when(janusGraphGenericDao) + .getChildrenNodes(Mockito.anyString(), Mockito.anyString(), Mockito.eq(GraphEdgeLabels.PROPERTY), Mockito.eq(NodeTypeEnum.Property), Mockito.eq(PropertyData.class)); + + Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(janusGraphGenericDao) + .getChild(Mockito.anyString(), Mockito.anyString(), Mockito.eq(GraphEdgeLabels.DERIVED_FROM), Mockito.eq(NodeTypeEnum.DataType), Mockito.eq(DataTypeData.class)); + + Either<DataTypeDefinition, JanusGraphOperationStatus> resultGetByUid = propertyOperation.getDataTypeByUid("testModel.testName.datatype"); + assertTrue(resultGetByUid.isLeft()); + } @Test @@ -913,18 +935,6 @@ public class PropertyOperationTest extends ModelTestBase { @Test - public void testIsDefinedInDataTypes() throws Exception { - PropertyOperation testSubject; - String propertyType = ""; - Either<Boolean, JanusGraphOperationStatus> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.isDefinedInDataTypes(propertyType); - } - - - @Test public void testGetAllDataTypes() throws Exception { PropertyOperation testSubject; Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> result; |