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