diff options
author | franciscovila <javier.paradela.vila@est.tech> | 2022-10-18 16:02:02 +0100 |
---|---|---|
committer | Vasyl Razinkov <vasyl.razinkov@est.tech> | 2022-11-23 10:19:09 +0000 |
commit | 166d45efe6de92ce067bc7b6065a0d7de94a6388 (patch) | |
tree | 98beeddb9f19651137b9dedfd8a5c91566934f41 /catalog-be/src/test | |
parent | 83bbca087a66d5ea4cff1f5d066f7761948a2cb3 (diff) |
Fix property constraints validation
Fix property constraints validation behaviour when a property
is not required shouldnt be validated vs constraints if no
value is provided. Also add constraints validation for length
measures in list, map and string types.
Issue-ID: SDC-4222
Signed-off-by: franciscovila <javier.paradela.vila@est.tech>
Change-Id: I48ebb46b3de9ddac3d9dd91400ea0fad983aa94d
Diffstat (limited to 'catalog-be/src/test')
-rw-r--r-- | catalog-be/src/test/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtilTest.java | 79 | ||||
-rw-r--r-- | catalog-be/src/test/resources/types/datatypes/constraintTest.json | 21 |
2 files changed, 99 insertions, 1 deletions
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtilTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtilTest.java index a9350edc18..6e9c6dd784 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtilTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtilTest.java @@ -39,6 +39,8 @@ import java.util.List; import java.util.Map; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -180,7 +182,7 @@ class PropertyValueConstraintValidationUtilTest { PropertyDefinition propertyDefinition = new PropertyDefinition(); propertyDefinition.setType("org.openecomp.datatypes.heat.network.neutron.Subnet"); - propertyDefinition.setValue("{\"value_specs\":{\"key\":\"slaac\"}}"); + propertyDefinition.setValue("{\"value_specs\":{\"key\":\"slaac\", \"key2\":\"slaac\"}}"); Either<Boolean, ResponseFormat> responseEither = propertyValueConstraintValidationUtil.validatePropertyConstraints( @@ -291,6 +293,81 @@ class PropertyValueConstraintValidationUtilTest { assertEquals(schemaType, propertyDefinition.getSchemaType()); } + @ParameterizedTest + @ValueSource(strings = {"[{\"ipv6_address_mode\": \"dhcpv6-stateful\"}, {\"ipv6_address_mode\": \"dhcpv6-stateless\"}, " + + "{\"dns_nameservers\": [\"server1\"]}]", + "[{\"ipv6_address_mode\": \"dhcpv6-stateful\"}, {\"ipv6_address_mode\": \"dhcpv6-stateless\"}, " + + "{\"dns_nameservers\": [\"server1\", \"server2\", \"server3\", \"server4\", \"server5\"]}]", + "[{\"ipv6_address_mode\": \"dhcpv6-stateful\"}, {\"ipv6_address_mode\": \"dhcpv6-stateless\"}, " + + "{\"subnetpool\": \"h\"}]", + "[{\"ipv6_address_mode\": \"dhcpv6-stateful\"}, {\"ipv6_address_mode\": \"dhcpv6-stateless\"}, " + + "{\"subnetpool\": \"123456\"}]", + "{\"value_specs\":{\"key\":\"slaac\"}}"}) + void listOfComplexWithConstrainedFails(String value) { + when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(dataTypeDefinitionMap)); + final var propertyDefinition = new PropertyDefinition(); + final String type = "list"; + propertyDefinition.setType(type); + final SchemaDefinition schemaDefinition = new SchemaDefinition(); + final PropertyDataDefinition schemaProperty = new PropertyDataDefinition(); + final String schemaType = "org.openecomp.datatypes.heat.network.neutron.Subnet"; + schemaProperty.setType(schemaType); + schemaDefinition.setProperty(schemaProperty); + propertyDefinition.setSchema(schemaDefinition); + propertyDefinition.setValue(value); + final String name = "listOfComplex"; + propertyDefinition.setName(name); + + Either<Boolean, ResponseFormat> responseEither = + propertyValueConstraintValidationUtil + .validatePropertyConstraints(Collections.singletonList(propertyDefinition), applicationDataTypeCache, null); + + assertTrue(responseEither.isRight()); + //original object values should not be changed + assertEquals(name, propertyDefinition.getName()); + assertEquals(type, propertyDefinition.getType()); + assertEquals(value, propertyDefinition.getValue()); + assertEquals(schemaType, propertyDefinition.getSchemaType()); + } + + @ParameterizedTest + @ValueSource(strings = {"[{\"ipv6_address_mode\": \"dhcpv6-stateful\"}, {\"ipv6_address_mode\": \"dhcpv6-stateless\"}, " + + "{\"dns_nameservers\": [\"server1\", \"server2\", \"server3\", \"server4\"]}]", + "[{\"ipv6_address_mode\": \"dhcpv6-stateful\"}, {\"ipv6_address_mode\": \"dhcpv6-stateless\"}, " + + "{\"dns_nameservers\": [\"server1\", \"server1\"]}]", + "[{\"ipv6_address_mode\": \"dhcpv6-stateful\"}, {\"ipv6_address_mode\": \"dhcpv6-stateless\"}, " + + "{\"subnetpool\": \"123\"}]", + "[{\"ipv6_address_mode\": \"dhcpv6-stateful\"}, {\"ipv6_address_mode\": \"dhcpv6-stateless\"}, " + + "{\"subnetpool\": \"1234\"}]", + "[{\"value_specs\":{\"key1\":\"slaac\",\"key2\":\"slaac\"}}]"}) + void listOfComplexWithConstrainedSuccess(String value) { + when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(dataTypeDefinitionMap)); + + final var propertyDefinition = new PropertyDefinition(); + final String type = "list"; + propertyDefinition.setType(type); + final SchemaDefinition schemaDefinition = new SchemaDefinition(); + final PropertyDataDefinition schemaProperty = new PropertyDataDefinition(); + final String schemaType = "org.openecomp.datatypes.heat.network.neutron.Subnet"; + schemaProperty.setType(schemaType); + schemaDefinition.setProperty(schemaProperty); + propertyDefinition.setSchema(schemaDefinition); + propertyDefinition.setValue(value); + final String name = "listOfComplex"; + propertyDefinition.setName(name); + + Either<Boolean, ResponseFormat> responseEither = + propertyValueConstraintValidationUtil + .validatePropertyConstraints(Collections.singletonList(propertyDefinition), applicationDataTypeCache, null); + + assertTrue(responseEither.isLeft()); + //original object values should not be changed + assertEquals(name, propertyDefinition.getName()); + assertEquals(type, propertyDefinition.getType()); + assertEquals(value, propertyDefinition.getValue()); + assertEquals(schemaType, propertyDefinition.getSchemaType()); + } + @Test void listOfComplexSuccessTest1() { when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(dataTypeDefinitionMap)); diff --git a/catalog-be/src/test/resources/types/datatypes/constraintTest.json b/catalog-be/src/test/resources/types/datatypes/constraintTest.json index 83b4253939..a9d1469068 100644 --- a/catalog-be/src/test/resources/types/datatypes/constraintTest.json +++ b/catalog-be/src/test/resources/types/datatypes/constraintTest.json @@ -128,6 +128,11 @@ "uniqueId": "org.openecomp.datatypes.heat.network.neutron.Subnet.datatype.value_specs", "type": "map", "required": false, + "constraints": [ + { + "minLength": 2 + } + ], "definition": false, "defaultValue": "{}", "description": "Extra parameters to include in the request", @@ -186,6 +191,14 @@ "uniqueId": "org.openecomp.datatypes.heat.network.neutron.Subnet.datatype.subnetpool", "type": "string", "required": false, + "constraints": [ + { + "minLength": "2" + }, + { + "maxLength": "4" + } + ], "definition": false, "description": "The name or ID of the subnet pool", "password": false, @@ -198,6 +211,14 @@ "uniqueId": "org.openecomp.datatypes.heat.network.neutron.Subnet.datatype.dns_nameservers", "type": "list", "required": false, + "constraints": [ + { + "minLength": "2" + }, + { + "maxLength": "4" + } + ], "definition": false, "defaultValue": "[]", "description": "A specified set of DNS name servers to be used", |