From 1073da9c4dacfc87982eecf1667eb0177540acf6 Mon Sep 17 00:00:00 2001 From: vasraz Date: Tue, 11 May 2021 18:05:30 +0100 Subject: Improve test coverage Change-Id: I2b02d85d9d395d9092d43bd54e21179aff29df91 Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3584 --- .../impl/ComponentInstanceBusinessLogic.java | 5 +- .../openecomp/sdc/be/tosca/ToscaExportHandler.java | 29 +-- .../sdc/be/tosca/ToscaExportHandlerTest.java | 88 ++++------ .../model/operations/impl/AttributeOperation.java | 67 +++---- .../operations/impl/AttributeOperationTest.java | 194 +++++++++++++++++++++ 5 files changed, 261 insertions(+), 122 deletions(-) create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/AttributeOperationTest.java diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java index b8fabc307f..90716f9f86 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java @@ -2366,12 +2366,11 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { // Specific Update Logic String newValue = attribute.getValue(); - final Either isValid = attributeOperation - .validateAndUpdateAttributeValue(attributeType, attribute.getValue(), true, innerType, allDataTypesEither.left().value()); + final var isValid = attributeOperation.validateAndUpdateAttributeValue(attribute, innerType, allDataTypesEither.left().value()); if (isValid.isRight()) { final Boolean res = isValid.right().value(); if (!Boolean.TRUE.equals(res)) { - log.debug("validate and update attribute value has failed with value: {}", attribute.getValue()); + log.debug("validate and update attribute value has failed with value: {}", newValue); throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse( DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT))); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index 45180008ab..b374e62a54 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -712,13 +712,7 @@ public class ToscaExportHandler { interfacesOperationsConverter.addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent); addInputsToProperties(dataTypes, inputDef, mergedProperties); final Map toscaAttributeMap; - try { - toscaAttributeMap = convertToToscaAttributes(component.getAttributes(), dataTypes); - } catch (final ToscaConversionException e) { - log.error(EcompLoggerErrorCode.SCHEMA_ERROR, ToscaExportHandler.class.getName(), - COULD_NOT_PARSE_COMPONENT_ATTRIBUTES_COMPONENT_UNIQUE_ID, component.getName(), component.getUniqueId(), e); - return Either.right(ToscaError.GENERAL_ERROR); - } + toscaAttributeMap = convertToToscaAttributes(component.getAttributes(), dataTypes); if (!toscaAttributeMap.isEmpty()) { toscaNodeType.setAttributes(toscaAttributeMap); } @@ -763,7 +757,7 @@ public class ToscaExportHandler { } private Map convertToToscaAttributes(final List attributeList, - final Map dataTypes) throws ToscaConversionException { + final Map dataTypes) { if (CollectionUtils.isEmpty(attributeList)) { return Collections.emptyMap(); } @@ -981,9 +975,7 @@ public class ToscaExportHandler { List instanceInputsList = componentInstancesInputs.get(instanceUniqueId); if (instanceInputsList != null) { instanceInputsList.forEach(input -> { - - Supplier supplier = () -> input.getValue() != null && !Objects.isNull(input.getValue()) - ? input.getValue() : input.getDefaultValue(); + Supplier supplier = () -> input.getValue() != null && !Objects.isNull(input.getValue()) ? input.getValue() : input.getDefaultValue(); propertyConvertor.convertAndAddValue(dataTypes, props, input, supplier); }); } @@ -1010,9 +1002,7 @@ public class ToscaExportHandler { // Filters out Attributes with empty default values .filter(attributeDefinition -> StringUtils.isNotEmpty(attributeDefinition.getDefaultValue())) // Converts and adds each value to attribute map - .forEach(attributeDefinition -> { - attributeConverter.convertAndAddValue(attribs, attributeDefinition); - }); + .forEach(attributeDefinition -> attributeConverter.convertAndAddValue(attribs, attributeDefinition)); } } @@ -1037,9 +1027,7 @@ public class ToscaExportHandler { // Filters out Attributes with empty default values .filter(attrib -> StringUtils.isNotEmpty(attrib.getDefaultValue())) // Converts and adds each value to attribute map - .forEach(attributeDefinition -> { - attributeConverter.convertAndAddValue(attribs, attributeDefinition); - }); + .forEach(attributeDefinition -> attributeConverter.convertAndAddValue(attribs, attributeDefinition)); } } @@ -1237,13 +1225,11 @@ public class ToscaExportHandler { Component originComponent, Map componentCache) { - final List> toscaRequirements; final List requirementDefinitionList = filterRequirements(componentInstance, relations); if (isNotEmpty(requirementDefinitionList)) { try { - toscaRequirements = buildRequirements(component, componentInstance, - requirementDefinitionList, originComponent, componentCache); + final List> toscaRequirements = buildRequirements(component, componentInstance, requirementDefinitionList, originComponent, componentCache); if (!toscaRequirements.isEmpty()) { nodeTypeTemplate.setRequirements(toscaRequirements); } @@ -1267,8 +1253,7 @@ public class ToscaExportHandler { final List> toscaRequirements = new ArrayList<>(); for (RequirementCapabilityRelDef relationshipDefinition : filteredRelations) { final Map toscaTemplateRequirementMap = - buildRequirement(componentInstance, originComponent, component.getComponentInstances(), - relationshipDefinition, componentCache); + buildRequirement(componentInstance, originComponent, component.getComponentInstances(), relationshipDefinition, componentCache); toscaRequirements.add(toscaTemplateRequirementMap); } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java index 55c2e8972f..202cf014fd 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java @@ -76,6 +76,7 @@ import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.exception.ToscaExportException; import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.CapabilityRequirementRelationship; import org.openecomp.sdc.be.model.Component; @@ -715,6 +716,15 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { componentInstancesAttributes.put("uuid", componentInstanceAttributes); component.setComponentInstancesAttributes(componentInstancesAttributes); + ComponentInstanceProperty cip = new ComponentInstanceProperty(); + cip.setInstanceUniqueId("id"); + + List list = new ArrayList<>(); + list.add(cip); + + componentInstancesProperties.put("id", list); + component.setComponentInstancesProperties(componentInstancesProperties); + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.left(component)); when(capabilityRequirementConverter .convertComponentInstanceCapabilities(any(ComponentInstance.class), any(Map.class), any(ToscaNodeTemplate.class))) @@ -769,7 +779,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { final ComponentInstance instance = new ComponentInstance(); instance.setUniqueId("id"); instance.setComponentUid("uid"); - instance.setOriginType(OriginTypeEnum.ServiceProxy); + instance.setOriginType(OriginTypeEnum.VFC); final List groupInstances = new ArrayList<>(); final GroupInstance groupInst = new GroupInstance(); final List artifacts = new ArrayList<>(); @@ -782,6 +792,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { final List properties = new ArrayList<>(); properties.add(new PropertyDefinition()); instance.setProperties(properties); + component.setProperties(properties); instance.setUniqueId("uuid"); instance.setDescription("desc"); @@ -826,6 +837,18 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { component.setArtifacts(artifactList); component.setToscaArtifacts(artifactList); + final List attributes = new ArrayList<>(); + final var attribute = new AttributeDefinition(); + attribute.setName("mock"); + attributes.add(attribute); + component.setAttributes(attributes); + + List componentInstanceInputs = new ArrayList<>(); + componentInstanceInputs.add(new ComponentInstanceInput()); + + componentInstancesInputs.put("id", componentInstanceInputs); + component.setComponentInstancesInputs(componentInstancesInputs); + when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.left(component)); when(capabilityRequirementConverter .convertComponentInstanceCapabilities(any(ComponentInstance.class), any(Map.class), any(ToscaNodeTemplate.class))) @@ -935,6 +958,12 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { component.setUUID("uuid"); component.setDescription("desc"); + final List attributes = new ArrayList<>(); + final var attribute = new AttributeDefinition(); + attribute.setName("mock"); + attributes.add(attribute); + component.setAttributes(attributes); + componentCache.put("uid", component); when(capabilityRequirementConverter.getOriginComponent(any(Map.class), any(ComponentInstance.class))).thenReturn(Either.right(false)); @@ -1005,63 +1034,6 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { assertNotNull(result); } - @Test - public void testAddComponentInstanceInputs() throws Exception { - - Map dataTypes = new HashMap<>(); - Map> componentInstancesInputs = new HashMap<>(); - ComponentInstance componentInstance = new ComponentInstance(); - String instanceUniqueId = "id"; - Map props = new HashMap<>(); - - List componentInstanceInputs = new ArrayList<>(); - componentInstanceInputs.add(new ComponentInstanceInput()); - - componentInstancesInputs.put(instanceUniqueId, componentInstanceInputs); - - // default test - Deencapsulation.invoke(testSubject, "addComponentInstanceInputs", dataTypes, componentInstancesInputs, - instanceUniqueId, props); - } - - @Test - public void testAddPropertiesOfComponentInstance() throws Exception { - Map> componentInstancesProperties = new HashMap<>(); - Map dataTypes = new HashMap<>(); - ComponentInstance componentInstance = new ComponentInstance(); - String instanceUniqueId = "id"; - Map props = new HashMap<>(); - - ComponentInstanceProperty cip = new ComponentInstanceProperty(); - cip.setInstanceUniqueId("id"); - - List list = new ArrayList<>(); - list.add(cip); - - componentInstancesProperties.put("id", list); - - // default test - Deencapsulation.invoke(testSubject, "addPropertiesOfComponentInstance", componentInstancesProperties, dataTypes, - instanceUniqueId, props); - } - - @Test - public void testAddPropertiesOfParentComponent() throws Exception { - Map dataTypes = new HashMap<>(); - ComponentInstance componentInstance = new ComponentInstance(); - Component componentOfInstance = new Resource(); - Map props = new HashMap<>(); - - List properties = new ArrayList<>(); - properties.add(new PropertyDefinition()); - - ((Resource) componentOfInstance).setProperties(properties); - - // default test - Deencapsulation.invoke(testSubject, "addPropertiesOfParentComponent", dataTypes, - componentOfInstance, props); - } - @Test public void testCreateNodeType() throws Exception { 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 isDefinedInDataTypes(final String propertyType) { + private Either isDefinedInDataTypes(final String propertyType) { final String dataTypeUid = UniqueIdBuilder.buildDataTypeUid(propertyType); final Either 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 getDataTypeByUid(final String uniqueId) { + private Either getDataTypeByUid(final String uniqueId) { final Either 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, 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, JanusGraphOperationStatus> findPropertiesOfNode = this - .findPropertiesOfNode(NodeTypeEnum.DataType, uniqueId); + final Either, 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, JanusGraphOperationStatus> findPropertiesOfNode(final NodeTypeEnum nodeType, - final String uniqueId) { + private Either, JanusGraphOperationStatus> findPropertiesOfNode(final NodeTypeEnum nodeType, + final String uniqueId) { final Map resourceProps = new HashMap<>(); final Either>, 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 validateAndUpdateAttributeValue(final String attributeType, - final String value, - final boolean isValidate, + public Either validateAndUpdateAttributeValue(final AttributeDataDefinition attribute, final String innerType, final Map 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 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> list = new ArrayList<>(); + final GraphEdge edge = new GraphEdge(); + final HashMap map = new HashMap<>(); + map.put("name", "property"); + edge.setProperties(map); + final ImmutablePair pairPropertyData = new ImmutablePair<>(new PropertyData(), edge); + list.add(pairPropertyData); + final ImmutablePair 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.>, JanusGraphOperationStatus>left(list)); + when(janusGraphGenericDao.getChild(eq("uid"), eq("null.datatype"), any(GraphEdgeLabels.class), any(NodeTypeEnum.class), any())) + .thenReturn(Either., 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 dataTypes = new HashMap<>(); + dataTypes.put("double", new DataTypeDefinition()); + final var result = attributeOperation.validateAndUpdateAttribute(attributeDefinition, dataTypes); + assertNotNull(result); + assertEquals(StorageOperationStatus.OK, result); + } + +} -- cgit 1.2.3-korg