Search in sources :

Example 26 with PropertyValue

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));
}
Also used : ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) Time(org.alien4cloud.tosca.normative.primitives.Time) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 27 with PropertyValue

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;
}
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 28 with PropertyValue

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());
    }
}
Also used : PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) DataType(org.alien4cloud.tosca.model.types.DataType) PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) List(java.util.List) Map(java.util.Map)

Aggregations

PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)28 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)20 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)17 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)16 Test (org.junit.Test)15 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)13 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)11 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)10 Map (java.util.Map)9 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)9 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)5 DataType (org.alien4cloud.tosca.model.types.DataType)5 Application (alien4cloud.model.application.Application)3 Location (alien4cloud.model.orchestrators.locations.Location)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Maps (com.google.common.collect.Maps)3 Arrays (java.util.Arrays)3 PreconfiguredInputsConfiguration (org.alien4cloud.alm.deployment.configuration.model.PreconfiguredInputsConfiguration)3 ToscaTypes (org.alien4cloud.tosca.normative.types.ToscaTypes)3 PropertiesYamlParser (org.alien4cloud.tosca.utils.PropertiesYamlParser)3