Search in sources :

Example 41 with PropertyDefinition

use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.

the class PropertyDefinitionConstraintsTest method testCheckIfCompatibleOrFailConstraintNotSatisfiedNull.

@Test(expected = IncompatiblePropertyDefinitionException.class)
public void testCheckIfCompatibleOrFailConstraintNotSatisfiedNull() throws ConstraintViolationException, IncompatiblePropertyDefinitionException {
    PropertyDefinition propDef1 = new PropertyDefinition();
    PropertyDefinition propDef2 = new PropertyDefinition();
    propDef1.setType(ToscaTypes.STRING);
    propDef2.setType(ToscaTypes.STRING);
    List<PropertyConstraint> constraints = Lists.newArrayList();
    constraints.add(new EqualConstraint());
    propDef1.setConstraints(constraints);
    EqualConstraint constraint = new EqualConstraint();
    constraint.setEqual("test");
    constraints.add(constraint);
    propDef1.setConstraints(constraints);
    propDef1.checkIfCompatibleOrFail(propDef2);
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 42 with PropertyDefinition

use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.

the class PropertyDefinitionConstraintsTest method testCheckIfCompatibleOrFailConstraintSatisfiedSetOfScamblePropertyConstraint.

@Test
public void testCheckIfCompatibleOrFailConstraintSatisfiedSetOfScamblePropertyConstraint() throws ConstraintViolationException, IncompatiblePropertyDefinitionException {
    PropertyDefinition propDef1 = new PropertyDefinition();
    PropertyDefinition propDef2 = new PropertyDefinition();
    List<PropertyConstraint> constraintsProp1 = Lists.newArrayList();
    List<PropertyConstraint> constraintsProp2 = Lists.newArrayList();
    propDef1.setType(ToscaTypes.STRING);
    propDef2.setType(ToscaTypes.STRING);
    EqualConstraint constraint11 = new EqualConstraint();
    constraint11.setEqual("test");
    LessThanConstraint constraint12 = new LessThanConstraint();
    constraint12.setLessThan("5");
    constraintsProp1.add(constraint11);
    constraintsProp1.add(constraint12);
    EqualConstraint constraint21 = new EqualConstraint();
    constraint21.setEqual("test");
    LessThanConstraint constraint22 = new LessThanConstraint();
    constraint22.setLessThan("5");
    constraintsProp2.add(constraint22);
    constraintsProp2.add(constraint21);
    propDef1.setConstraints(constraintsProp1);
    propDef2.setConstraints(constraintsProp2);
    propDef1.checkIfCompatibleOrFail(propDef2);
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 43 with PropertyDefinition

use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.

the class PropertyDefinitionConstraintsTest method testCheckIfCompatibleOrFailConstraintSatisfiedSetOfPropertyConstraint.

@Test
public void testCheckIfCompatibleOrFailConstraintSatisfiedSetOfPropertyConstraint() throws ConstraintViolationException, IncompatiblePropertyDefinitionException {
    PropertyDefinition propDef1 = new PropertyDefinition();
    PropertyDefinition propDef2 = new PropertyDefinition();
    List<PropertyConstraint> constraints = Lists.newArrayList();
    propDef1.setType(ToscaTypes.STRING);
    propDef2.setType(ToscaTypes.STRING);
    EqualConstraint constraint1 = new EqualConstraint();
    constraint1.setEqual("test");
    constraints.add(constraint1);
    LessThanConstraint constraint2 = new LessThanConstraint();
    constraint2.setLessThan("5");
    constraints.add(constraint2);
    propDef1.setConstraints(constraints);
    propDef2.setConstraints(constraints);
    propDef1.checkIfCompatibleOrFail(propDef2);
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 44 with PropertyDefinition

use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.

the class TemplateBuilder method fillProperties.

public static void fillProperties(Map<String, AbstractPropertyValue> properties, Map<String, PropertyDefinition> propertiesDefinitions, Map<String, AbstractPropertyValue> originalProperties, boolean adaptToType) {
    if (propertiesDefinitions == null || properties == null) {
        return;
    }
    for (Map.Entry<String, PropertyDefinition> entry : propertiesDefinitions.entrySet()) {
        AbstractPropertyValue originalValue = MapUtils.getObject(originalProperties, entry.getKey());
        if (originalValue == null) {
            AbstractPropertyValue pv = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(entry.getValue());
            properties.put(entry.getKey(), pv);
        } else if (originalValue instanceof FunctionPropertyValue || originalValue instanceof ConcatPropertyValue) {
            properties.put(entry.getKey(), originalValue);
        } else {
            // we check the property type before accepting it
            try {
                ConstraintPropertyService.checkPropertyConstraint(entry.getKey(), originalValue, entry.getValue());
                properties.put(entry.getKey(), originalValue);
            } catch (ConstraintFunctionalException e) {
                log.debug("Not able to merge property <" + entry.getKey() + "> value due to a type check exception", e);
                if (adaptToType) {
                    AbstractPropertyValue pv = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(entry.getValue());
                    properties.put(entry.getKey(), pv);
                }
            }
        }
    }
    if (!adaptToType) {
        // maybe we could put validations here actually.
        for (Map.Entry<String, AbstractPropertyValue> originalProperty : safe(originalProperties).entrySet()) {
            if (!properties.containsKey(originalProperty.getKey())) {
                properties.put(originalProperty.getKey(), originalProperty.getValue());
            }
        }
    }
}
Also used : ConstraintFunctionalException(org.alien4cloud.tosca.exceptions.ConstraintFunctionalException) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) Map(java.util.Map) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)

Example 45 with PropertyDefinition

use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.

the class ConstraintPropertyService method checkMapPropertyConstraint.

private static void checkMapPropertyConstraint(String propertyName, Map<String, Object> mapPropertyValue, PropertyDefinition propertyDefinition, Consumer<String> missingPropertyConsumer) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
    if (!ToscaTypes.MAP.equals(propertyDefinition.getType())) {
        throwConstraintValueDoNotMatchPropertyTypeException("The property definition should be a map list but we found " + propertyDefinition.getType(), propertyName, ToscaTypes.MAP, null);
    }
    PropertyDefinition entrySchema = propertyDefinition.getEntrySchema();
    if (entrySchema == null) {
        throw new ConstraintValueDoNotMatchPropertyTypeException("value is a map but type actually is <" + propertyDefinition.getType() + ">");
    }
    checkLengthConstraints(propertyDefinition.getConstraints(), mapPropertyValue);
    for (Map.Entry<String, Object> complexPropertyValueEntry : mapPropertyValue.entrySet()) {
        checkPropertyConstraint(propertyName + "." + complexPropertyValueEntry.getKey(), complexPropertyValueEntry.getValue(), entrySchema, missingPropertyConsumer);
    }
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Map(java.util.Map)

Aggregations

PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)101 Test (org.junit.Test)42 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)23 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)20 Map (java.util.Map)19 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)17 NodeType (org.alien4cloud.tosca.model.types.NodeType)15 NotFoundException (alien4cloud.exception.NotFoundException)13 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)13 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)11 MinLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)11 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)10 MaxLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint)10 Capability (org.alien4cloud.tosca.model.templates.Capability)10 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)10 DataType (org.alien4cloud.tosca.model.types.DataType)9 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)8 Set (java.util.Set)8 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)8 LengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint)8