From 166d45efe6de92ce067bc7b6065a0d7de94a6388 Mon Sep 17 00:00:00 2001 From: franciscovila Date: Tue, 18 Oct 2022 16:02:02 +0100 Subject: 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 Change-Id: I48ebb46b3de9ddac3d9dd91400ea0fad983aa94d --- .../PropertyValueConstraintValidationUtilTest.java | 79 +++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) (limited to 'catalog-be/src/test/java') 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 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 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 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)); -- cgit 1.2.3-korg