diff options
author | MichaelMorris <michael.morris@est.tech> | 2023-02-17 13:05:37 +0000 |
---|---|---|
committer | JEFF VAN DAM <jeff.van.dam@est.tech> | 2023-02-22 13:33:18 +0000 |
commit | 3a7add7ec56db4a7c0d62e2c86150f9a216ad041 (patch) | |
tree | 0f1153e4ee940547e07be92f77c241b39a78efe6 /catalog-model/src/main/java | |
parent | 7b00f9af119e560d7974aab8aa405a6decbaa398 (diff) |
Fix incorrect behaviour for list valid_values
Signed-off-by: MichaelMorris <michael.morris@est.tech>
Issue-ID: SDC-4398
Change-Id: Ic8c8a6565089000d1517110283e4bb7c6f989c27
Diffstat (limited to 'catalog-model/src/main/java')
3 files changed, 56 insertions, 13 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java index 12a79ec90e..df961acb6e 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.model; import com.fasterxml.jackson.annotation.JsonIgnore; import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; @@ -28,12 +29,12 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolatio import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; public interface PropertyConstraint { - - void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException; - + + void initialize(ToscaType propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException; + void validate(Object propertyValue) throws ConstraintViolationException; - void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException; + void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException; @JsonIgnore ConstraintType getConstraintType(); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java index 43bed0af67..b8e2e26660 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java @@ -20,6 +20,8 @@ package org.openecomp.sdc.be.model.tosca.constraints; import java.util.Arrays; + +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; @@ -32,7 +34,11 @@ public abstract class AbstractPropertyConstraint implements PropertyConstraint { private static final String INVALID_VALUE_ERROR_MESSAGE = "Unsupported value provided for %s property supported value type is %s."; @Override - public void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException { + public void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException { + validate(toscaType, propertyTextValue); + } + + protected void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException { try { validate(toscaType.convert(propertyTextValue)); } catch (ApplicationVersionException e) { @@ -51,8 +57,12 @@ public abstract class AbstractPropertyConstraint implements PropertyConstraint { return String.format(INVALID_VALUE_ERROR_MESSAGE, propertyName, toscaType.getType()); } - @Override - public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + protected void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { //Initialization not needed for few constraints for now might be needed in future } + + @Override + public void initialize(ToscaType propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException { + initialize(propertyType); + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java index 1fd1adf015..4970808948 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java @@ -21,8 +21,13 @@ package org.openecomp.sdc.be.model.tosca.constraints; import static java.util.stream.Collectors.toList; +import java.util.Collection; +import java.util.Collections; + +import com.fasterxml.jackson.core.type.TypeReference; import com.google.common.collect.Sets; import java.util.List; +import java.util.Map; import java.util.Set; import javax.validation.constraints.NotNull; import lombok.EqualsAndHashCode; @@ -30,6 +35,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -53,26 +59,27 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { public ValidValuesConstraint(List<Object> validValues) { this.validValues = validValues; } - + @Override - public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + public void initialize(ToscaType propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = getValuesType(propertyType, schema); validValuesTyped = Sets.newHashSet(); if (validValues == null) { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <> property type is <" + propertyType.toString() + ">"); } for (Object value : validValues) { - if (!propertyType.isValidValue(String.valueOf(value))) { + if (!toscaType.isValidValue(String.valueOf(value))) { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType.toString() + ">"); } else { - validValuesTyped.add(propertyType.convert(String.valueOf(value))); + validValuesTyped.add(toscaType.convert(String.valueOf(value))); } } } - public void validateType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - ToscaType toscaType = ToscaType.getToscaType(propertyType); + public void validateType(String propertyType, SchemaDefinition schema) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = getValuesType(ToscaType.getToscaType(propertyType), schema); if (toscaType == null) { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid values <" + validValues.toString() + PROPERTY_TYPE_IS + propertyType + ">"); @@ -88,6 +95,10 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { } } } + + private ToscaType getValuesType(ToscaType propertyType, SchemaDefinition schema) { + return ToscaType.isCollectionType(propertyType.getType()) ? ToscaType.getToscaType(schema.getProperty().getType()) : propertyType; + } @Override public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException { @@ -100,6 +111,27 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { } } } + + @Override + public void validate(ToscaType toscaType, SchemaDefinition schema, String propertyTextValue) throws ConstraintViolationException { + try { + Collection<Object> valuesToValidate; + if (ToscaType.LIST == toscaType) { + valuesToValidate = ConstraintUtil.parseToCollection(propertyTextValue, new TypeReference<>() {}); + } else if (ToscaType.MAP == toscaType) { + final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyTextValue, new TypeReference<>() {}); + valuesToValidate = map.values(); + } else { + valuesToValidate = Collections.singleton(propertyTextValue); + } + ToscaType valuesType = getValuesType(toscaType, schema); + for (final Object value: valuesToValidate) { + validate(valuesType, value.toString()); + } + } catch (ConstraintValueDoNotMatchPropertyTypeException exception) { + throw new ConstraintViolationException("Value cannot be parsed to a list", exception); + } + } @Override public void validate(Object propertyValue) throws ConstraintViolationException { |