diff options
Diffstat (limited to 'catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MinLengthConstraint.java')
-rw-r--r-- | catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MinLengthConstraint.java | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MinLengthConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MinLengthConstraint.java index bc51f4fa5a..4f55970d79 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MinLengthConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MinLengthConstraint.java @@ -20,11 +20,16 @@ package org.openecomp.sdc.be.model.tosca.constraints; +import java.util.List; +import java.util.Map; + +import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import javax.validation.constraints.NotNull; -public class MinLengthConstraint extends AbstractStringPropertyConstraint { +public class MinLengthConstraint extends AbstractPropertyConstraint { @NotNull private Integer minLength; @@ -37,10 +42,13 @@ public class MinLengthConstraint extends AbstractStringPropertyConstraint { super(); } - @Override - protected void doValidate(String propertyValue) throws ConstraintViolationException { - if (propertyValue.length() < minLength) { + protected void doValidate(Object propertyValue) throws ConstraintViolationException { + if (propertyValue instanceof String && String.valueOf(propertyValue).length() < minLength) { throw new ConstraintViolationException("The length of the value is less than [" + minLength + "]"); + } else if (propertyValue instanceof List && ((List) propertyValue).size() < minLength) { + throw new ConstraintViolationException("The size of the list is less than [" + minLength + "]"); + } else if (propertyValue instanceof Map && ((Map) propertyValue).size() < minLength) { + throw new ConstraintViolationException("The size of the map is less than [" + minLength + "]"); } } @@ -52,4 +60,19 @@ public class MinLengthConstraint extends AbstractStringPropertyConstraint { this.minLength = minLength; } + @Override + public void validate(Object propertyValue) throws ConstraintViolationException { + if (propertyValue == null) { + throw new ConstraintViolationException("Value to validate is null"); + } + if(!(propertyValue instanceof String || propertyValue instanceof List || propertyValue instanceof Map)) { + throw new ConstraintViolationException("This constraint can only be applied on String/List/Map value"); + } + doValidate(propertyValue); + } + + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s minimum length must be [%s]", String.valueOf(minLength)); + } } |