From: vasraz Date: Wed, 25 Jan 2023 19:09:41 +0000 (+0000) Subject: Add TCs to cover Constraints X-Git-Tag: 1.12.1~4 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=sdc.git;a=commitdiff_plain;h=a522b7eb8346617883227c0a6621420cfdf44dc5 Add TCs to cover Constraints Fix wrong Comparable Property Type and Comparable Constraint Type Signed-off-by: Vasyl Razinkov Change-Id: I59968861834a0f8859fcaf3e590c83bbf223338f Issue-ID: SDC-4347 --- diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.java index b7b2452189..86cedf26d7 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.java @@ -61,8 +61,15 @@ public class NodeFilterValidator { private static final String TARGET = "Target"; private static final String INPUT_NOT_FOUND_LOG = "Input '{}' not found in parent component '{}', unique id '{}'"; private static final Set TYPES_WITH_SCHEMA = Set.of(ToscaPropertyType.MAP.getType(), ToscaPropertyType.LIST.getType()); - private static final Set COMPARABLE_TYPES = Set - .of(ToscaPropertyType.STRING.getType(), ToscaPropertyType.INTEGER.getType(), ToscaPropertyType.FLOAT.getType()); + private static final Set COMPARABLE_TYPES = Set.of( + ToscaPropertyType.SCALAR_UNIT_SIZE.getType(), + ToscaPropertyType.SCALAR_UNIT_TIME.getType(), + ToscaPropertyType.SCALAR_UNIT_BITRATE.getType(), + ToscaPropertyType.SCALAR_UNIT_FREQUENCY.getType(), + ToscaPropertyType.BOOLEAN.getType(), + ToscaPropertyType.STRING.getType(), + ToscaPropertyType.INTEGER.getType(), + ToscaPropertyType.FLOAT.getType()); private final ComponentsUtils componentsUtils; private final ApplicationDataTypeCache applicationDataTypeCache; private final FilterConstraintValidator filterConstraintValidator; @@ -105,6 +112,7 @@ public class NodeFilterValidator { } return Either.left(true); } + public Either validateFilter(final Component parentComponent, final String componentInstanceId, final FilterConstraintDto filterConstraint) { validateFilterConstraint(filterConstraint); @@ -239,15 +247,15 @@ public class NodeFilterValidator { } return findSubProperty(propertyPath.subList(1, propertyPath.size()), propertyDefinition.getType(), modelDataTypes); } - + private Optional getInstanceProperties(final Component parentComponent, final String componentInstanceId, final String capabilityName, final String propertyName) { if (StringUtils.isEmpty(capabilityName)) { return parentComponent.getComponentInstancesProperties().get(componentInstanceId).stream() - .filter(property -> propertyName.equals(property.getName())).findFirst(); + .filter(property -> propertyName.equals(property.getName())).findFirst(); } else { final Optional componentInstanceOptional = parentComponent.getComponentInstances().stream() - .filter(componentInstance -> componentInstance.getUniqueId().equals(componentInstanceId)).findAny(); + .filter(componentInstance -> componentInstance.getUniqueId().equals(componentInstanceId)).findAny(); if (componentInstanceOptional.isPresent()) { for (final List listOfCaps : componentInstanceOptional.get().getCapabilities().values()) { final Optional capDef = listOfCaps.stream().filter(cap -> cap.getName().equals(capabilityName)).findAny(); @@ -356,7 +364,8 @@ public class NodeFilterValidator { if (componentInstanceProperty == null) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.SELECTED_PROPERTY_NOT_PRESENT, filterConstraint.getPropertyName())); } - if (filterConstraint.getOperator().isComparable() && !COMPARABLE_TYPES.contains(componentInstanceProperty.getType())) { + if (filterConstraint.getOperator().isComparable() && !TYPES_WITH_SCHEMA.contains(componentInstanceProperty.getType()) + && !COMPARABLE_TYPES.contains(componentInstanceProperty.getType())) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_OPERATOR_PROVIDED, filterConstraint.getPropertyName(), filterConstraint.getOperator().getType())); } @@ -372,7 +381,8 @@ public class NodeFilterValidator { if (componentProperty == null) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.SELECTED_PROPERTY_NOT_PRESENT, filterConstraint.getPropertyName())); } - if (filterConstraint.getOperator().isComparable() && !COMPARABLE_TYPES.contains(componentProperty.getType())) { + if (filterConstraint.getOperator().isComparable() && !TYPES_WITH_SCHEMA.contains(componentProperty.getType()) + && !COMPARABLE_TYPES.contains(componentProperty.getType())) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_OPERATOR_PROVIDED, filterConstraint.getPropertyName(), filterConstraint.getOperator().getType())); } @@ -403,16 +413,17 @@ public class NodeFilterValidator { if (componentInstanceProperty == null) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.SELECTED_PROPERTY_NOT_PRESENT, filterConstraint.getPropertyName())); } - if (filterConstraint.getOperator().isComparable() && !COMPARABLE_TYPES.contains(componentInstanceProperty.getType())) { + if (filterConstraint.getOperator().isComparable() && !TYPES_WITH_SCHEMA.contains(componentInstanceProperty.getType()) + && !COMPARABLE_TYPES.contains(componentInstanceProperty.getType())) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_OPERATOR_PROVIDED, filterConstraint.getPropertyName(), filterConstraint.getOperator().getType())); } return isValidValueCheck(componentInstanceProperty.getType(), componentInstanceProperty.getSchemaType(), parentComponent.getModel(), filterConstraint.getValue(), filterConstraint.getPropertyName()); } - - private Optional getComponentInstanceProperty(CapabilityDefinition capabilityDefinition, final String propertyName){ - return capabilityDefinition.getProperties().stream().filter(property -> property.getName().equals(propertyName)).findAny(); + + private Optional getComponentInstanceProperty(CapabilityDefinition capabilityDefinition, final String propertyName) { + return capabilityDefinition.getProperties().stream().filter(property -> property.getName().equals(propertyName)).findAny(); } private Either isValidValueCheck(final String type, final String schemaType, final String model, @@ -433,7 +444,8 @@ public class NodeFilterValidator { valueAsJsonString = new Gson().toJson(value); } catch (final Exception e) { LOGGER.debug("Unsupported property filter value", e); - return Either.right(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, type, propertyName, String.valueOf(value))); + return Either.right( + componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, type, propertyName, String.valueOf(value))); } if (toscaPropertyType != null) { if (toscaPropertyType.getValidator().isValid(valueAsJsonString, schemaType, modelDataTypesMap)) { @@ -448,7 +460,8 @@ public class NodeFilterValidator { return Either.right(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, type, propertyName, valueAsJsonString)); } - public Either validateSubstitutionFilter(final Component component, final List filterConstraintList) { + public Either validateSubstitutionFilter(final Component component, + final List filterConstraintList) { if (CollectionUtils.isEmpty(filterConstraintList)) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.CONSTRAINT_FORMAT_INCORRECT)); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java index 9b92adbe6a..75385d1f43 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java @@ -78,7 +78,6 @@ import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; import org.springframework.stereotype.Service; import org.yaml.snakeyaml.Yaml; - @Service public class PropertyConvertor { diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/NodeFilterValidatorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/NodeFilterValidatorTest.java index e1d8e46d74..67ecb7f675 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/NodeFilterValidatorTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/NodeFilterValidatorTest.java @@ -7,9 +7,9 @@ * 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. @@ -67,12 +67,12 @@ class NodeFilterValidatorTest { private static final String INNER_SERVICE = "innerService"; private static final String PROPERTY_NAME = "Prop1"; private static final String COMPONENT1_ID = "component1"; - private static final String PARENTSERVICE_ID = "parentservice"; + private static final String PARENT_SERVICE_ID = "parentservice"; private static final String COMPONENT2_ID = "component2"; private ComponentsUtils componentsUtils; @Mock - ApplicationDataTypeCache applicationDataTypeCache; + private ApplicationDataTypeCache applicationDataTypeCache; @Mock private FilterConstraintValidator filterConstraintValidator; @InjectMocks @@ -98,7 +98,7 @@ class NodeFilterValidatorTest { final ResponseFormat expectedResponse = new ResponseFormat(); when(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND, "?", INNER_SERVICE)).thenReturn(expectedResponse); Either either = - nodeFilterValidator.validateComponentInstanceExist(null, INNER_SERVICE); + nodeFilterValidator.validateComponentInstanceExist(null, INNER_SERVICE); assertTrue(either.isRight()); assertEquals(expectedResponse, either.right().value()); @@ -124,7 +124,7 @@ class NodeFilterValidatorTest { final FilterConstraintDto filterConstraintDto = buildFilterConstraintDto(PROPERTY_NAME, FilterValueType.STATIC, ConstraintType.EQUAL, PropertyFilterTargetType.PROPERTY, "true"); Either either = - nodeFilterValidator.validateFilter(service, INNER_SERVICE, filterConstraintDto); + nodeFilterValidator.validateFilter(service, INNER_SERVICE, filterConstraintDto); assertTrue(either.isRight()); filterConstraintDto.setTargetType(PropertyFilterTargetType.CAPABILITY); either = nodeFilterValidator.validateFilter(service, INNER_SERVICE, filterConstraintDto); @@ -142,7 +142,7 @@ class NodeFilterValidatorTest { createToscaGetFunction("test", PropertySource.INSTANCE, ToscaGetFunctionType.GET_PROPERTY, List.of("test2")) ); Either actualValidationResult = - nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(filterConstraint1)); + nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(filterConstraint1)); assertTrue(actualValidationResult.isRight()); final var filterConstraint2 = buildFilterConstraintDto( @@ -150,10 +150,10 @@ class NodeFilterValidatorTest { FilterValueType.GET_PROPERTY, ConstraintType.EQUAL, PropertyFilterTargetType.PROPERTY, - createToscaGetFunction(PARENTSERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of("Prop1")) + createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of("Prop1")) ); actualValidationResult = - nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(filterConstraint2)); + nodeFilterValidator.validateSubstitutionFilter(service, Collections.singletonList(filterConstraint2)); assertTrue(actualValidationResult.isLeft()); final var staticFilter1 = buildFilterConstraintDto( @@ -210,11 +210,11 @@ class NodeFilterValidatorTest { "true" ); final ResponseFormat expectedResponse = new ResponseFormat(); - when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_OPERATOR_PROVIDED, filterConstraintDto.getPropertyName(), - filterConstraintDto.getOperator().getType()) + when(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, ToscaPropertyType.BOOLEAN.getType(), + filterConstraintDto.getPropertyName(), "\"true\"") ).thenReturn(expectedResponse); final Either validationResult = - nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(filterConstraintDto)); + nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(filterConstraintDto)); assertTrue(validationResult.isRight()); assertEquals(expectedResponse, validationResult.right().value()); } @@ -225,11 +225,11 @@ class NodeFilterValidatorTest { baseFilterConstraintDto.setValue("trues"); final ResponseFormat responseFormat = new ResponseFormat(); - when(componentsUtils - .getResponseFormat(eq(ActionStatus.UNSUPPORTED_VALUE_PROVIDED), eq(ToscaPropertyType.BOOLEAN.getType()), eq(PROPERTY_NAME), any()) + when(componentsUtils.getResponseFormat + (ActionStatus.UNSUPPORTED_VALUE_PROVIDED, ToscaPropertyType.BOOLEAN.getType(), PROPERTY_NAME, "\"trues\"") ).thenReturn(responseFormat); final Either validationResult = - nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto)); + nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto)); assertTrue(validationResult.isRight()); assertEquals(responseFormat, validationResult.right().value()); @@ -241,7 +241,7 @@ class NodeFilterValidatorTest { baseFilterConstraintDto.setValue("true"); baseFilterConstraintDto.setOperator(ConstraintType.GREATER_THAN); Either either = - nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto)); + nodeFilterValidator.validateFilter(service, INNER_SERVICE, List.of(baseFilterConstraintDto)); assertTrue(either.isLeft()); } @@ -328,7 +328,7 @@ class NodeFilterValidatorTest { void testValidatePropertyConstraintParentSuccess() { final var service = createService(ToscaPropertyType.STRING.getType()); final ToscaGetFunctionDataDefinition toscaGetFunction = - createToscaGetFunction(PARENTSERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME)); + createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME)); final var filterConstraintDto = buildFilterConstraintDto( PROPERTY_NAME, FilterValueType.GET_PROPERTY, @@ -374,7 +374,7 @@ class NodeFilterValidatorTest { final Service service = createService(ToscaPropertyType.STRING.getType()); service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setType(ToscaPropertyType.INTEGER.getType()); final ToscaGetFunctionDataDefinition toscaGetFunction = - createToscaGetFunction(PARENTSERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME)); + createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME)); final var filterConstraintDto = buildFilterConstraintDto( PROPERTY_NAME, FilterValueType.GET_PROPERTY, @@ -405,7 +405,7 @@ class NodeFilterValidatorTest { .thenReturn(expectedResponse); final ToscaGetFunctionDataDefinition toscaGetFunction = - createToscaGetFunction(PARENTSERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME)); + createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME)); final var filterConstraintDto = buildFilterConstraintDto( PROPERTY_NAME, FilterValueType.GET_PROPERTY, @@ -425,7 +425,7 @@ class NodeFilterValidatorTest { Service service = createService(ToscaPropertyType.STRING.getType()); service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setName("Prop2"); final ToscaGetFunctionDataDefinition toscaGetFunction = - createToscaGetFunction(PARENTSERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME)); + createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME)); final var filterConstraintDto = buildFilterConstraintDto( PROPERTY_NAME, FilterValueType.GET_PROPERTY, @@ -467,7 +467,7 @@ class NodeFilterValidatorTest { service.getComponentInstancesProperties().get(COMPONENT1_ID).get(0).setSchema(schemaDefinition); final ToscaGetFunctionDataDefinition toscaGetFunction = - createToscaGetFunction(PARENTSERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME)); + createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME)); final var filterConstraintDto = buildFilterConstraintDto( PROPERTY_NAME, FilterValueType.GET_PROPERTY, @@ -494,12 +494,12 @@ class NodeFilterValidatorTest { private Service createService(String type, String schemaType) { Service service = new Service(); - service.setName(PARENTSERVICE_ID); + service.setName(PARENT_SERVICE_ID); PropertyDefinition propertyDefinition = new PropertyDefinition(); propertyDefinition.setName(PROPERTY_NAME); propertyDefinition.setType(type); - if (schemaType != null){ + if (schemaType != null) { SchemaDefinition schemaDefinition = new SchemaDefinition(); PropertyDataDefinition schemaProperty = new PropertyDataDefinition(); schemaProperty.setType(schemaType); @@ -518,19 +518,19 @@ class NodeFilterValidatorTest { service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2)); - ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(); + ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(); componentInstanceProperty.setName(PROPERTY_NAME); componentInstanceProperty.setType(type); - ComponentInstanceProperty componentInstanceProperty2 = new ComponentInstanceProperty(); + ComponentInstanceProperty componentInstanceProperty2 = new ComponentInstanceProperty(); componentInstanceProperty2.setName(PROPERTY_NAME); componentInstanceProperty2.setType(type); Map> componentInstancePropertyMap = new HashMap<>(); componentInstancePropertyMap.put(componentInstance.getUniqueId(), - Collections.singletonList(componentInstanceProperty)); + Collections.singletonList(componentInstanceProperty)); componentInstancePropertyMap.put(componentInstance2.getUniqueId(), - Collections.singletonList(componentInstanceProperty2)); + Collections.singletonList(componentInstanceProperty2)); componentInstancePropertyMap.put(INNER_SERVICE, Collections.singletonList(componentInstanceProperty)); service.setComponentInstancesProperties(componentInstancePropertyMap); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java index cd756ce49d..7a74a6ff7a 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java @@ -7,9 +7,9 @@ * 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. @@ -27,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import fj.data.Either; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -37,12 +38,27 @@ import org.mockito.InjectMocks; import org.mockito.MockitoAnnotations; import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder; import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; +import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; +import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint; +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.LengthConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.PatternConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint; +import org.openecomp.sdc.be.tosca.PropertyConvertor.PropertyType; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaProperty; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint; class PropertyConvertorTest { @@ -53,7 +69,7 @@ class PropertyConvertorTest { private PropertyConvertor propertyConvertor; @BeforeEach - public void setUp(){ + public void setUp() { MockitoAnnotations.openMocks(this); property = new PropertyDefinition(); property.setName("myProperty"); @@ -64,12 +80,66 @@ class PropertyConvertorTest { @Test void testConvertProperty() { - SchemaDefinition schema = new SchemaDefinition(); - schema.setProperty(property); - - property.setSchema(schema); - - propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY); + SchemaDefinition schema = new SchemaDefinition(); + schema.setProperty(property); + property.setSchema(schema); + + assertNotNull(propertyConvertor.convertProperty(dataTypes, property, PropertyType.PROPERTY)); + } + + @Test + void testConvertPropertyWithEqualConstraint() { + assertTrue(testConstraints(new EqualConstraint(123), ConstraintType.EQUAL, true)); + } + + @Test + void testConvertPropertyWithGreaterOrEqualConstraint() { + assertTrue(testConstraints(new GreaterOrEqualConstraint<>(123), ConstraintType.GREATER_OR_EQUAL, true)); + } + + @Test + void testConvertPropertyWithGreaterThanConstraint() { + assertTrue(testConstraints(new GreaterThanConstraint<>(123), ConstraintType.GREATER_THAN, true)); + } + + @Test + void testConvertPropertyWithLessOrEqualConstraint() { + assertTrue(testConstraints(new LessOrEqualConstraint<>(123), ConstraintType.LESS_OR_EQUAL, true)); + } + + @Test + void testConvertPropertyWithLessThanConstraint() { + assertTrue(testConstraints(new LessThanConstraint<>(123), ConstraintType.LESS_THAN, true)); + } + + @Test + void testConvertPropertyWithInRangeConstraint() { + assertTrue(testConstraints(new InRangeConstraint(Arrays.asList(123, 345)), ConstraintType.IN_RANGE, false)); + } + + @Test + void testConvertPropertyWithValidValuesConstraint() { + assertTrue(testConstraints(new ValidValuesConstraint(Arrays.asList(123, 345)), ConstraintType.VALID_VALUES, false)); + } + + @Test + void testConvertPropertyWithLengthConstraint() { + assertTrue(testConstraints(new LengthConstraint(), ConstraintType.LENGTH, false)); + } + + @Test + void testConvertPropertyWithMaxLengthConstraint() { + assertTrue(testConstraints(new MaxLengthConstraint(12), ConstraintType.MAX_LENGTH, false)); + } + + @Test + void testConvertPropertyWithMinLengthConstraint() { + assertTrue(testConstraints(new MinLengthConstraint(1), ConstraintType.MIN_LENGTH, false)); + } + + @Test + void testConvertPropertyWithPatternConstraint() { + assertTrue(testConstraints(new PatternConstraint("[a-z]"), ConstraintType.PATTERN, false)); } @Test @@ -87,7 +157,7 @@ class PropertyConvertorTest { assertNotNull(result); assertEquals(Integer.valueOf(def), result.getDefaultp()); } - + @Test void convertPropertyWithMetadata() { Map metadata = new HashMap<>(); @@ -160,7 +230,7 @@ class PropertyConvertorTest { for (ToscaProperty prop : result.left().value().getProperties().values()) { assertNull(prop.getDefaultp()); } - } + } @Test void convertPropertyWhichStartsWithSemiColon() { @@ -183,7 +253,7 @@ class PropertyConvertorTest { propertyConvertor.convertProperty(Collections.emptyMap(), property, PropertyConvertor.PropertyType.PROPERTY); assertEquals("/", toscaProperty.getDefaultp()); } - + @Test void convertPropertyWithYamlValue() { final PropertyDefinition property = new PropertyDataDefinitionBuilder() @@ -193,8 +263,25 @@ class PropertyConvertorTest { final ToscaProperty toscaProperty = propertyConvertor.convertProperty(Collections.emptyMap(), property, PropertyConvertor.PropertyType.PROPERTY); assertTrue(toscaProperty.getDefaultp() instanceof Map); - assertTrue(((Map)toscaProperty.getDefaultp()).get("concat") instanceof List); - assertEquals(3, ((List)((Map)toscaProperty.getDefaultp()).get("concat")).size()); + assertTrue(((Map) toscaProperty.getDefaultp()).get("concat") instanceof List); + assertEquals(3, ((List) ((Map) toscaProperty.getDefaultp()).get("concat")).size()); + } + + private boolean testConstraints(PropertyConstraint propertyConstraint, ConstraintType constraintType, boolean checkComparable) { + property.setConstraints(Collections.singletonList(propertyConstraint)); + + ToscaProperty toscaProperty = propertyConvertor.convertProperty(dataTypes, property, PropertyType.PROPERTY); + assertNotNull(toscaProperty); + List constraints = toscaProperty.getConstraints(); + assertNotNull(constraints); + ToscaPropertyConstraint constraint = constraints.get(0); + assertNotNull(constraint); + ConstraintType actualConstraintType = constraint.getConstraintType(); + assertEquals(constraintType, actualConstraintType); + if (checkComparable) { + assertTrue(actualConstraintType.isComparable()); + } + return true; } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java index 35f036b9c6..91205359ce 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java @@ -36,10 +36,10 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraint @Setter @AllArgsConstructor @EqualsAndHashCode -public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstraint { +public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstraint { @NotNull - private Object greaterOrEqual; + private T greaterOrEqual; @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { @@ -90,7 +90,7 @@ public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstrai public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { ToscaType toscaType = ToscaType.getToscaType(propertyType); try { - greaterOrEqual = toscaType.convert(String.valueOf(greaterOrEqual)); + greaterOrEqual = (T) toscaType.convert(String.valueOf(greaterOrEqual)); } catch (Exception e) { throw new ConstraintValueDoNotMatchPropertyTypeException( "greaterOrEqual constraint has invalid values <" + greaterOrEqual.toString() + "> property type is <" + propertyType + ">"); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java index d071dbaa07..b2f5a47302 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java @@ -36,10 +36,10 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraint @Setter @AllArgsConstructor @EqualsAndHashCode -public class LessThanConstraint extends AbstractComparablePropertyConstraint { +public class LessThanConstraint extends AbstractComparablePropertyConstraint { @NotNull - private Object lessThan; + private T lessThan; @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { @@ -90,7 +90,7 @@ public class LessThanConstraint extends AbstractComparablePropertyConstraint { public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { ToscaType toscaType = ToscaType.getToscaType(propertyType); try { - lessThan = toscaType.convert(String.valueOf(lessThan)); + lessThan = (T) toscaType.convert(String.valueOf(lessThan)); } catch (Exception e) { throw new ConstraintValueDoNotMatchPropertyTypeException( "lessThan constraint has invalid values <" + lessThan.toString() + "> property type is <" + propertyType + ">"); diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/ConstraintType.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/ConstraintType.java index 80672b6d73..2963b6bcd1 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/ConstraintType.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/enums/ConstraintType.java @@ -42,7 +42,12 @@ public enum ConstraintType { PATTERN("pattern"), SCHEMA("schema"); - private static final Set comparableConstraints = Set.of(ConstraintType.GREATER_THAN, ConstraintType.LESS_THAN); + private static final Set comparableConstraints = Set.of( + ConstraintType.EQUAL, + ConstraintType.GREATER_THAN, + ConstraintType.GREATER_OR_EQUAL, + ConstraintType.LESS_OR_EQUAL, + ConstraintType.LESS_THAN); private final String type; private final List typeAlias;