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);
}
}
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;
}
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");
}
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;
}
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);
}
}
Aggregations