Search in sources :

Example 1 with InvalidPropertyValueException

use of org.alien4cloud.tosca.exceptions.InvalidPropertyValueException in project alien4cloud by alien4cloud.

the class ConstraintPropertyService method checkBasicType.

/**
 * Check that a given value is matching the native type defined on the property definition.
 *
 * @param propertyName The name of the property under validation
 * @param primitiveType The primitive type to check the value against.
 * @param propertyValue The value to check.
 * @throws ConstraintValueDoNotMatchPropertyTypeException in case the value does not match the primitive type.
 */
private static void checkBasicType(final String propertyName, final String primitiveType, final String propertyValue) throws ConstraintValueDoNotMatchPropertyTypeException {
    // "string" (basic case, no exception), "float", "integer", "version"
    try {
        IPropertyType<?> propertyType = ToscaTypes.fromYamlTypeName(primitiveType);
        propertyType.parse(propertyValue);
    } catch (InvalidPropertyValueException e) {
        log.debug("The property value for property {} is not of type {}: {}", propertyName, primitiveType, propertyValue, e);
        ConstraintInformation consInformation = new ConstraintInformation(propertyName, null, propertyValue, primitiveType);
        throw new ConstraintValueDoNotMatchPropertyTypeException(e.getMessage(), e, consInformation);
    }
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)

Example 2 with InvalidPropertyValueException

use of org.alien4cloud.tosca.exceptions.InvalidPropertyValueException in project alien4cloud by alien4cloud.

the class ToscaPropertyDefaultValueConstraintsValidator method isValid.

@Override
public boolean isValid(PropertyDefinition value, ConstraintValidatorContext context) {
    PropertyValue defaultValue = value.getDefault();
    if (defaultValue == null) {
        // no default value is specified.
        return true;
    }
    // validate that the default value matches the defined constraints.
    IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(value.getType());
    if (toscaType == null) {
        return false;
    }
    if (!(defaultValue instanceof ScalarPropertyValue)) {
        // No constraint can be made on other thing than scalar values
        return false;
    }
    String defaultValueAsString = ((ScalarPropertyValue) defaultValue).getValue();
    Object parsedDefaultValue;
    try {
        parsedDefaultValue = toscaType.parse(defaultValueAsString);
    } catch (InvalidPropertyValueException e) {
        return false;
    }
    if (value.getConstraints() != null) {
        for (PropertyConstraint constraint : value.getConstraints()) {
            try {
                constraint.validate(parsedDefaultValue);
            } catch (ConstraintViolationException e) {
                return false;
            }
        }
    }
    return true;
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)

Example 3 with InvalidPropertyValueException

use of org.alien4cloud.tosca.exceptions.InvalidPropertyValueException in project alien4cloud by alien4cloud.

the class PropertyValueService method getValueInUnit.

public static String getValueInUnit(String propertyValue, String unit, boolean ceil, String toscaType) {
    IPropertyType type = ToscaTypes.fromYamlTypeName(toscaType);
    if (type instanceof ScalarType) {
        try {
            ScalarUnit scalarUnit = ((ScalarType) type).parse(propertyValue);
            double convertedValue = scalarUnit.convert(unit);
            if (ceil) {
                convertedValue = Math.ceil(convertedValue);
            }
            return format(convertedValue);
        } catch (InvalidPropertyValueException e) {
            log.error("e");
            throw new InvalidArgumentException(e.getMessage());
        }
    }
    throw new InvalidArgumentException("Type is not a scalar type");
}
Also used : InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) ScalarType(org.alien4cloud.tosca.normative.types.ScalarType) IPropertyType(org.alien4cloud.tosca.normative.types.IPropertyType) ScalarUnit(org.alien4cloud.tosca.normative.primitives.ScalarUnit)

Example 4 with InvalidPropertyValueException

use of org.alien4cloud.tosca.exceptions.InvalidPropertyValueException in project alien4cloud by alien4cloud.

the class ToscaPropertyDefaultValueTypeValidator method isValid.

@Override
public boolean isValid(PropertyDefinition value, ConstraintValidatorContext context) {
    PropertyValue defaultValue = value.getDefault();
    if (defaultValue == null) {
        // no default value is specified.
        return true;
    }
    if (!(defaultValue instanceof ScalarPropertyValue)) {
        // No constraint can be made on other thing than scalar values
        return false;
    }
    IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(value.getType());
    if (toscaType == null) {
        return false;
    }
    try {
        toscaType.parse(((ScalarPropertyValue) defaultValue).getValue());
    } catch (InvalidPropertyValueException e) {
        return false;
    }
    return true;
}
Also used : InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)

Example 5 with InvalidPropertyValueException

use of org.alien4cloud.tosca.exceptions.InvalidPropertyValueException in project alien4cloud by alien4cloud.

the class ScalarType method parse.

@Override
public T parse(String text) throws InvalidPropertyValueException {
    if (StringUtils.isEmpty(text)) {
        throw new InvalidPropertyValueException("Could not parse scalar from value " + text + " as the text is empty");
    }
    Matcher matcher = SCALAR_UNIT_PATTERN.matcher(text);
    if (matcher.matches()) {
        String valueText = matcher.group(1);
        String unitText = matcher.group(2);
        try {
            return doParse(Double.parseDouble(valueText), unitText);
        } catch (NumberFormatException e) {
            throw new InvalidPropertyValueException("Could not parse scalar from value " + text + " as this is not a valid number " + valueText, e);
        }
    } else {
        throw new InvalidPropertyValueException("Could not parse scalar from value " + text + " as it does not match pattern " + SCALAR_UNIT_PATTERN);
    }
}
Also used : Matcher(java.util.regex.Matcher) InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException)

Aggregations

InvalidPropertyValueException (org.alien4cloud.tosca.exceptions.InvalidPropertyValueException)5 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)2 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)2 InvalidArgumentException (alien4cloud.exception.InvalidArgumentException)1 ConstraintInformation (alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)1 Matcher (java.util.regex.Matcher)1 ConstraintValueDoNotMatchPropertyTypeException (org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException)1 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)1 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)1 ScalarUnit (org.alien4cloud.tosca.normative.primitives.ScalarUnit)1 IPropertyType (org.alien4cloud.tosca.normative.types.IPropertyType)1 ScalarType (org.alien4cloud.tosca.normative.types.ScalarType)1