use of org.alien4cloud.tosca.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class ToscaTypeConverterTest method convert_time_to_property_value.
@Test
public void convert_time_to_property_value() throws Exception {
PropertyDefinition propertyDefinition = new PropertyDefinition();
propertyDefinition.setType(ToscaTypes.TIME);
PropertyValue propertyValue = converter.toPropertyValue("2 d", propertyDefinition);
Object time = ToscaTypes.fromYamlTypeName(propertyDefinition.getType()).parse(propertyValue.getValue().toString());
assertThat(time).isInstanceOf(Time.class);
assertThat(time).isEqualTo(new Time(2, TimeUnit.D));
}
use of org.alien4cloud.tosca.model.definitions.PropertyValue 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.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class ConstraintPropertyService method checkPropertyConstraint.
/**
* Check the constraints on an unwrapped property value (basically a string, map or list) and get events through the given consumer parameter when missing
* properties on complex data type are found.
* Note that the property value cannot be null and the required characteristic of the initial property definition will NOT be checked.
*
* @param propertyName The name of the property.
* @param propertyValue The value of the property to check.
* @param propertyDefinition The property definition that defines the property to check.
* @param missingPropertyConsumer A consumer to receive events when a required property is not defined on a complex type sub-field.
* @throws ConstraintValueDoNotMatchPropertyTypeException In case the value type doesn't match the type of the property as defined.
* @throws ConstraintViolationException In case the value doesn't match one of the constraints defined on the property.
*/
public static void checkPropertyConstraint(String propertyName, Object propertyValue, PropertyDefinition propertyDefinition, Consumer<String> missingPropertyConsumer) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
Object value = propertyValue;
if (propertyValue instanceof PropertyValue) {
value = ((PropertyValue) propertyValue).getValue();
}
boolean isTypeDerivedFromPrimitive = false;
DataType dataType = null;
String typeName = propertyDefinition.getType();
if (!ToscaTypes.isPrimitive(typeName)) {
dataType = ToscaContext.get(DataType.class, typeName);
if (dataType instanceof PrimitiveDataType) {
// the type is derived from a primitive type
isTypeDerivedFromPrimitive = true;
}
}
if (value instanceof String) {
if (ToscaTypes.isSimple(typeName)) {
checkSimplePropertyConstraint(propertyName, (String) value, propertyDefinition);
} else if (isTypeDerivedFromPrimitive) {
checkComplexPropertyDerivedFromPrimitiveTypeConstraints(propertyName, (String) value, propertyDefinition, dataType);
} else {
throwConstraintValueDoNotMatchPropertyTypeException("Property value is a String while the expected data type is the complex type " + propertyDefinition.getType(), propertyName, propertyDefinition.getType(), value);
}
} else if (value instanceof Map) {
if (ToscaTypes.MAP.equals(typeName)) {
checkMapPropertyConstraint(propertyName, (Map<String, Object>) value, propertyDefinition, missingPropertyConsumer);
} else {
checkDataTypePropertyConstraint(propertyName, (Map<String, Object>) value, propertyDefinition, missingPropertyConsumer);
}
} else if (value instanceof List) {
// Range type is a specific primitive type that is actually wrapped
if (ToscaTypes.RANGE.equals(typeName)) {
checkRangePropertyConstraint(propertyName, (List<Object>) value, propertyDefinition);
} else {
checkListPropertyConstraint(propertyName, (List<Object>) value, propertyDefinition, missingPropertyConsumer);
}
} else {
throw new InvalidArgumentException("Not expecting to receive constraint validation for other types than String, Map or List, but got " + value.getClass().getName());
}
}
Aggregations