diff options
Diffstat (limited to 'catalog-model')
2 files changed, 222 insertions, 39 deletions
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 f2e8c83b26..74dc4207a1 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 @@ -54,7 +54,7 @@ public class AttributeOperation extends AbstractOperation { private static final String FAILED_TO_FETCH_ATTRIBUTES_OF_DATA_TYPE = "Failed to fetch attributes 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 THE_VALUE_OF_ATTRIBUTE_FROM_TYPE_IS_INVALID = "The value {} of attribute from type {} is invalid"; - private static Logger log = Logger.getLogger(AttributeOperation.class.getName()); + private static final Logger log = Logger.getLogger(AttributeOperation.class.getName()); @Autowired public AttributeOperation(HealingJanusGraphGenericDao janusGraphGenericDao) { @@ -77,7 +77,7 @@ public class AttributeOperation extends AbstractOperation { return true; } - public Either<Boolean, JanusGraphOperationStatus> isDefinedInDataTypes(final String propertyType) { + private Either<Boolean, JanusGraphOperationStatus> isDefinedInDataTypes(final String propertyType) { final String dataTypeUid = UniqueIdBuilder.buildDataTypeUid(propertyType); final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = getDataTypeByUid(dataTypeUid); if (dataTypeByUid.isRight()) { @@ -93,7 +93,7 @@ public class AttributeOperation extends AbstractOperation { /** * Build Data type object from graph by unique id */ - public Either<DataTypeDefinition, JanusGraphOperationStatus> getDataTypeByUid(final String uniqueId) { + private Either<DataTypeDefinition, JanusGraphOperationStatus> getDataTypeByUid(final String uniqueId) { final Either<DataTypeData, JanusGraphOperationStatus> dataTypesRes = janusGraphGenericDao .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, DataTypeData.class); if (dataTypesRes.isRight()) { @@ -109,14 +109,12 @@ public class AttributeOperation extends AbstractOperation { return Either.right(propertiesStatus); } final Either<ImmutablePair<DataTypeData, GraphEdge>, JanusGraphOperationStatus> parentNode = janusGraphGenericDao - .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.DataType, - DataTypeData.class); + .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); if (parentNode.isRight()) { final JanusGraphOperationStatus janusGraphOperationStatus = parentNode.right().value(); if (janusGraphOperationStatus != JanusGraphOperationStatus.NOT_FOUND) { - log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, "Failed to find the parent data type of data type {}. status is {}", uniqueId, - janusGraphOperationStatus); + log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, "Failed to find the parent data type of data type {}. status is {}", uniqueId, janusGraphOperationStatus); return Either.right(janusGraphOperationStatus); } } else { @@ -135,8 +133,7 @@ public class AttributeOperation extends AbstractOperation { } private JanusGraphOperationStatus fillProperties(final String uniqueId, final DataTypeDefinition dataTypeDefinition) { - final Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesOfNode = this - .findPropertiesOfNode(NodeTypeEnum.DataType, uniqueId); + final Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesOfNode = this.findPropertiesOfNode(NodeTypeEnum.DataType, uniqueId); if (findPropertiesOfNode.isRight()) { final JanusGraphOperationStatus janusGraphOperationStatus = findPropertiesOfNode.right().value(); log.debug("After looking for properties of vertex {}. status is {}", uniqueId, janusGraphOperationStatus); @@ -162,12 +159,11 @@ public class AttributeOperation extends AbstractOperation { } } - public Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesOfNode(final NodeTypeEnum nodeType, - final String uniqueId) { + private Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesOfNode(final NodeTypeEnum nodeType, + final String uniqueId) { final Map<String, PropertyDefinition> resourceProps = new HashMap<>(); final Either<List<ImmutablePair<PropertyData, GraphEdge>>, JanusGraphOperationStatus> childrenNodes = janusGraphGenericDao - .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(nodeType), uniqueId, GraphEdgeLabels.PROPERTY, NodeTypeEnum.Property, - PropertyData.class); + .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(nodeType), uniqueId, GraphEdgeLabels.PROPERTY, NodeTypeEnum.Property, PropertyData.class); if (childrenNodes.isRight()) { final JanusGraphOperationStatus operationStatus = childrenNodes.right().value(); return Either.right(operationStatus); @@ -187,7 +183,7 @@ public class AttributeOperation extends AbstractOperation { return Either.left(resourceProps); } - public PropertyDefinition convertPropertyDataToPropertyDefinition(final PropertyData propertyDataResult, final String propertyName) { + private PropertyDefinition convertPropertyDataToPropertyDefinition(final PropertyData propertyDataResult, final String propertyName) { log.debug("The object returned after create property is {}", propertyDataResult); final PropertyDefinition propertyDefResult = new PropertyDefinition(propertyDataResult.getPropertyDataDefinition()); propertyDefResult.setConstraints(convertConstraints(propertyDataResult.getConstraints())); @@ -304,10 +300,7 @@ public class AttributeOperation extends AbstractOperation { return StorageOperationStatus.INVALID_VALUE; } final JsonElement jsonElement = validateResult.left; - if (log.isTraceEnabled()) { - log.trace("Going to update value in attribute definition {} {}", attributeDefinition.getName(), - (jsonElement != null ? jsonElement.toString() : null)); - } + log.trace("Going to update value in attribute definition {} {}", attributeDefinition.getName(), (jsonElement != null ? jsonElement.toString() : null)); updateAttributeValue(attributeDefinition, jsonElement); return StorageOperationStatus.OK; } @@ -316,34 +309,30 @@ public class AttributeOperation extends AbstractOperation { attributeDefinition.set_default(jsonElement); } - public Either<Object, Boolean> validateAndUpdateAttributeValue(final String attributeType, - final String value, - final boolean isValidate, + public Either<Object, Boolean> validateAndUpdateAttributeValue(final AttributeDataDefinition attribute, final String innerType, final Map<String, DataTypeDefinition> dataTypes) { + final var attributeType = attribute.getType(); + final var value = attribute.getValue(); log.trace("Going to validate attribute value and its type. type = {}, value = {}", attributeType, value); - final ToscaPropertyType type = getType(attributeType); - if (isValidate) { - if (type == null) { - final DataTypeDefinition dataTypeDefinition = dataTypes.get(attributeType); - final ImmutablePair<JsonElement, Boolean> validateResult = - dataTypeValidatorConverter.validateAndUpdate(value, dataTypeDefinition, dataTypes); - if (Boolean.FALSE.equals(validateResult.right)) { - log.debug(THE_VALUE_OF_ATTRIBUTE_FROM_TYPE_IS_INVALID, value, attributeType); - return Either.right(false); - } - return Either.left(getValueFromJsonElement(validateResult.left)); - } - log.trace("before validating property type {}", attributeType); - if (!isValidValue(type, value, innerType, dataTypes)) { - log.debug(THE_VALUE_OF_ATTRIBUTE_FROM_TYPE_IS_INVALID, value, type); + final var type = getType(attributeType); + if (type == null) { + final var dataTypeDefinition = dataTypes.get(attributeType); + final var validateResult = dataTypeValidatorConverter.validateAndUpdate(value, dataTypeDefinition, dataTypes); + if (Boolean.FALSE.equals(validateResult.right)) { + log.debug(THE_VALUE_OF_ATTRIBUTE_FROM_TYPE_IS_INVALID, value, attributeType); return Either.right(false); } + return Either.left(getValueFromJsonElement(validateResult.left)); + } + log.trace("before validating property type {}", attributeType); + if (!isValidValue(type, value, innerType, dataTypes)) { + log.debug(THE_VALUE_OF_ATTRIBUTE_FROM_TYPE_IS_INVALID, value, type); + return Either.right(false); } Object convertedValue = value; - if (!isEmptyValue(value) && isValidate) { - PropertyValueConverter converter = type.getConverter(); - convertedValue = converter.convert(value, innerType, dataTypes); + if (!isEmptyValue(value)) { + convertedValue = type.getConverter().convert(value, innerType, dataTypes); } return Either.left(convertedValue); } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/AttributeOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/AttributeOperationTest.java new file mode 100644 index 0000000000..daa0e13450 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/AttributeOperationTest.java @@ -0,0 +1,194 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.openecomp.sdc.be.model.operations.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +import fj.data.Either; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge; +import org.openecomp.sdc.be.dao.graph.datatype.GraphNode; +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.AttributeDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; +import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.ModelTestBase; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter; +import org.openecomp.sdc.be.resources.data.DataTypeData; +import org.openecomp.sdc.be.resources.data.PropertyData; + +@ExtendWith(MockitoExtension.class) +class AttributeOperationTest extends ModelTestBase { + + @InjectMocks + private AttributeOperation attributeOperation; + + @Mock + private HealingJanusGraphGenericDao janusGraphGenericDao; + + @Mock + private DataTypeValidatorConverter dataTypeValidatorConverter; + + @BeforeAll + public static void setupBeforeClass() { + ModelTestBase.init(); + } + + @BeforeEach + void setUp() { + attributeOperation = new AttributeOperation(janusGraphGenericDao); + } + + @Test + void isAttributeTypeValid() { + final List<ImmutablePair<GraphNode, GraphEdge>> list = new ArrayList<>(); + final GraphEdge edge = new GraphEdge(); + final HashMap<String, Object> map = new HashMap<>(); + map.put("name", "property"); + edge.setProperties(map); + final ImmutablePair<GraphNode, GraphEdge> pairPropertyData = new ImmutablePair<>(new PropertyData(), edge); + list.add(pairPropertyData); + final ImmutablePair<GraphNode, GraphEdge> pairDataTypeData = new ImmutablePair<>(new DataTypeData(), edge); + + when(janusGraphGenericDao.getNode(any(), any(), any())).thenReturn(Either.left(new DataTypeData())); + when(janusGraphGenericDao.getChildrenNodes(eq("uid"), eq("null.datatype"), any(GraphEdgeLabels.class), any(NodeTypeEnum.class), any())) + .thenReturn(Either.<List<ImmutablePair<GraphNode, GraphEdge>>, JanusGraphOperationStatus>left(list)); + when(janusGraphGenericDao.getChild(eq("uid"), eq("null.datatype"), any(GraphEdgeLabels.class), any(NodeTypeEnum.class), any())) + .thenReturn(Either.<ImmutablePair<GraphNode, GraphEdge>, JanusGraphOperationStatus>left(pairDataTypeData)); + + when(janusGraphGenericDao.getChildrenNodes(eq("uid"), eq(null), any(GraphEdgeLabels.class), any(NodeTypeEnum.class), any())) + .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); + when(janusGraphGenericDao.getChild(eq("uid"), eq(null), any(GraphEdgeLabels.class), any(NodeTypeEnum.class), any())) + .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR)); + + final var attributeDefinition = new AttributeDataDefinition(); + assertFalse(attributeOperation.isAttributeTypeValid(attributeDefinition)); + } + + @Test + void isAttributeTypeValid_when_Null() { + assertFalse(attributeOperation.isAttributeTypeValid(null)); + } + + @Test + void isAttributeTypeValid_when_type_valid() { + final var attributeDefinition = new AttributeDataDefinition(); + attributeDefinition.setType("string"); + assertTrue(attributeOperation.isAttributeTypeValid(attributeDefinition)); + } + + @Test + void isAttributeInnerTypeValid() { + final var attributeDefinition = new AttributeDataDefinition(); + attributeDefinition.setType("string"); + final var result = attributeOperation.isAttributeInnerTypeValid(attributeDefinition, new HashMap<>()); + assertNotNull(result); + assertNull(result.getLeft()); + assertFalse(result.getRight()); + } + + @Test + void isAttributeInnerTypeValid_when_null() { + final var result = attributeOperation.isAttributeInnerTypeValid(null, new HashMap<>()); + assertNotNull(result); + assertNull(result.getLeft()); + assertFalse(result.getRight()); + } + + @Test + void isAttributeDefaultValueValid() { + final var attributeDefinition = new AttributeDataDefinition(); + attributeDefinition.setType("string"); + final var result = attributeOperation.isAttributeDefaultValueValid(attributeDefinition, new HashMap<>()); + assertTrue(result); + } + + @Test + void isAttributeDefaultValueValid_when_list() { + final var attributeDefinition = new AttributeDataDefinition(); + attributeDefinition.setType("list"); + final SchemaDefinition schema = new SchemaDefinition(); + schema.setProperty(new PropertyDataDefinition()); + attributeDefinition.setSchema(schema); + final var result = attributeOperation.isAttributeDefaultValueValid(attributeDefinition, new HashMap<>()); + assertTrue(result); + } + + @Test + void validateAndUpdateAttributeValue() { + final var attributeDefinition = new AttributeDataDefinition(); + attributeDefinition.setType("string"); + attributeDefinition.setValue("new string"); + final var result = attributeOperation.validateAndUpdateAttributeValue(attributeDefinition, "", new HashMap<>()); + assertNotNull(result); + } + + @Test + void validateAndUpdateAttributeValue_when_type_null() { + final var attributeDefinition = new AttributeDataDefinition(); + attributeDefinition.setValue("[ 'test' : 123 ]"); + final var result = attributeOperation.validateAndUpdateAttributeValue(attributeDefinition, "", new HashMap<>()); + assertNotNull(result); + assertTrue(result.isRight()); + assertFalse(result.right().value()); + } + + @Test + void validateAndUpdateAttribute() { + final var attributeDefinition = new AttributeDataDefinition(); + attributeDefinition.setType("string"); + final var result = attributeOperation.validateAndUpdateAttribute(attributeDefinition, new HashMap<>()); + assertNotNull(result); + assertEquals(StorageOperationStatus.OK, result); + } + + @Test + void validateAndUpdateAttribute_without_type() { + final var attributeDefinition = new AttributeDataDefinition(); + attributeDefinition.setType("double"); + final HashMap<String, DataTypeDefinition> dataTypes = new HashMap<>(); + dataTypes.put("double", new DataTypeDefinition()); + final var result = attributeOperation.validateAndUpdateAttribute(attributeDefinition, dataTypes); + assertNotNull(result); + assertEquals(StorageOperationStatus.OK, result); + } + +} |