From a522b7eb8346617883227c0a6621420cfdf44dc5 Mon Sep 17 00:00:00 2001 From: vasraz Date: Wed, 25 Jan 2023 19:09:41 +0000 Subject: 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 --- .../validation/NodeFilterValidatorTest.java | 54 +++++----- .../sdc/be/tosca/PropertyConvertorTest.java | 115 ++++++++++++++++++--- 2 files changed, 128 insertions(+), 41 deletions(-) (limited to 'catalog-be/src/test/java') 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; } } -- cgit 1.2.3-korg