Search in sources :

Example 6 with LengthConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint in project alien4cloud by alien4cloud.

the class ConstraintPropertyServiceTest method testValidStringConstraintProperty.

// constraint test
@Test
public void testValidStringConstraintProperty() throws Exception {
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType(ToscaTypes.STRING);
    propertyDefinition.setConstraints(new ArrayList<PropertyConstraint>());
    LengthConstraint lengthConstraint = new LengthConstraint();
    lengthConstraint.setLength(3);
    propertyDefinition.getConstraints().add(lengthConstraint);
    ConstraintPropertyService.checkPropertyConstraint("test", "val", propertyDefinition);
}
Also used : LengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 7 with LengthConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint in project alien4cloud by alien4cloud.

the class PropertyDefinitionConverter method convert.

public PropertyDefinition convert(FormPropertyDefinition definitionAnnotation) {
    if (definitionAnnotation == null) {
        return null;
    }
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType(definitionAnnotation.type());
    // FIXME ? can be other than a scalar here ?
    propertyDefinition.setDefault(new ScalarPropertyValue(definitionAnnotation.defaultValue()));
    propertyDefinition.setDescription(definitionAnnotation.description());
    propertyDefinition.setPassword(definitionAnnotation.isPassword());
    propertyDefinition.setRequired(definitionAnnotation.isRequired());
    List<PropertyConstraint> constraints = Lists.newArrayList();
    if (!definitionAnnotation.constraints().equal().isEmpty()) {
        EqualConstraint equalConstraint = new EqualConstraint();
        equalConstraint.setEqual(definitionAnnotation.constraints().equal());
        constraints.add(equalConstraint);
    }
    if (!definitionAnnotation.constraints().greaterOrEqual().isEmpty()) {
        GreaterOrEqualConstraint greaterOrEqualConstraint = new GreaterOrEqualConstraint();
        greaterOrEqualConstraint.setGreaterOrEqual(definitionAnnotation.constraints().greaterOrEqual());
        constraints.add(greaterOrEqualConstraint);
    }
    if (!definitionAnnotation.constraints().greaterThan().isEmpty()) {
        GreaterThanConstraint greaterThanConstraint = new GreaterThanConstraint();
        greaterThanConstraint.setGreaterThan(definitionAnnotation.constraints().greaterThan());
        constraints.add(greaterThanConstraint);
    }
    if (!definitionAnnotation.constraints().inRange().isEmpty()) {
        String inRangeText = definitionAnnotation.constraints().inRange();
        Matcher matcher = IN_RANGE_REGEXP.matcher(inRangeText);
        if (matcher.matches()) {
            InRangeConstraint inRangeConstraint = new InRangeConstraint();
            inRangeConstraint.setRangeMinValue(matcher.group(1).trim());
            inRangeConstraint.setRangeMaxValue(matcher.group(2).trim());
            constraints.add(inRangeConstraint);
        } else {
            throw new FormDescriptorGenerationException("In range constraint definition must be in this format '[ $min - $max ]'");
        }
    }
    if (definitionAnnotation.constraints().length() >= 0) {
        LengthConstraint lengthConstraint = new LengthConstraint();
        lengthConstraint.setLength(definitionAnnotation.constraints().length());
        constraints.add(lengthConstraint);
    }
    if (!definitionAnnotation.constraints().lessOrEqual().isEmpty()) {
        LessOrEqualConstraint lessOrEqualConstraint = new LessOrEqualConstraint();
        lessOrEqualConstraint.setLessOrEqual(definitionAnnotation.constraints().lessOrEqual());
        constraints.add(lessOrEqualConstraint);
    }
    if (!definitionAnnotation.constraints().lessThan().isEmpty()) {
        LessThanConstraint lessThanConstraint = new LessThanConstraint();
        lessThanConstraint.setLessThan(definitionAnnotation.constraints().lessThan());
        constraints.add(lessThanConstraint);
    }
    if (definitionAnnotation.constraints().maxLength() >= 0) {
        MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint();
        maxLengthConstraint.setMaxLength(definitionAnnotation.constraints().maxLength());
        constraints.add(maxLengthConstraint);
    }
    if (definitionAnnotation.constraints().minLength() >= 0) {
        MinLengthConstraint minLengthConstraint = new MinLengthConstraint();
        minLengthConstraint.setMinLength(definitionAnnotation.constraints().minLength());
        constraints.add(minLengthConstraint);
    }
    if (!definitionAnnotation.constraints().pattern().isEmpty()) {
        PatternConstraint patternConstraint = new PatternConstraint();
        patternConstraint.setPattern(definitionAnnotation.constraints().pattern());
        constraints.add(patternConstraint);
    }
    if (definitionAnnotation.constraints().validValues().length > 0) {
        ValidValuesConstraint validValuesConstraint = new ValidValuesConstraint();
        validValuesConstraint.setValidValues(Lists.newArrayList(definitionAnnotation.constraints().validValues()));
        constraints.add(validValuesConstraint);
    }
    if (!constraints.isEmpty()) {
        propertyDefinition.setConstraints(constraints);
    }
    return propertyDefinition;
}
Also used : LengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) FormDescriptorGenerationException(alien4cloud.ui.form.exception.FormDescriptorGenerationException) Matcher(java.util.regex.Matcher) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) FormPropertyDefinition(alien4cloud.ui.form.annotation.FormPropertyDefinition) LessThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) ValidValuesConstraint(org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) GreaterThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint) InRangeConstraint(org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint) PatternConstraint(org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)

Example 8 with LengthConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint in project alien4cloud by alien4cloud.

the class ConstraintPropertyServiceTest method testValidMapProperty.

@Test
public void testValidMapProperty() throws Exception {
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType(ToscaTypes.MAP);
    PropertyDefinition entrySchema = new PropertyDefinition();
    entrySchema.setType(ToscaTypes.STRING);
    propertyDefinition.setEntrySchema(entrySchema);
    Object propertyValue = ImmutableMap.builder().put("aa", "bb").build();
    ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
    // test length constraint
    LengthConstraint lengthConstraint = new LengthConstraint();
    lengthConstraint.setLength(1);
    List<PropertyConstraint> constraints = Lists.newArrayList();
    constraints.add(lengthConstraint);
    propertyDefinition.setConstraints(constraints);
    ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
}
Also used : LengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 9 with LengthConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint in project alien4cloud by alien4cloud.

the class ToscaPropertyDefaultValueConstraintsValidatorTest method createLenghtDefinition.

private PropertyDefinition createLenghtDefinition(String propertyType, int constraintValue, String defaultValue) {
    LengthConstraint constraint = new LengthConstraint();
    constraint.setLength(constraintValue);
    return createDefinitions(propertyType, constraint, defaultValue);
}
Also used : LengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)

Example 10 with LengthConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint in project alien4cloud by alien4cloud.

the class ToscaSerializerUtilsTest method testRenderConstraint.

@Test
public void testRenderConstraint() {
    GreaterOrEqualConstraint greaterOrEqualConstraint = new GreaterOrEqualConstraint();
    Assert.assertEquals("greater_or_equal: null", utils.renderConstraint(greaterOrEqualConstraint));
    greaterOrEqualConstraint.setGreaterOrEqual("1");
    Assert.assertEquals("greater_or_equal: 1", utils.renderConstraint(greaterOrEqualConstraint));
    GreaterThanConstraint greaterThanConstraint = new GreaterThanConstraint();
    Assert.assertEquals("greater_than: null", utils.renderConstraint(greaterThanConstraint));
    greaterThanConstraint.setGreaterThan("1");
    Assert.assertEquals("greater_than: 1", utils.renderConstraint(greaterThanConstraint));
    LessOrEqualConstraint lessOrEqualConstraint = new LessOrEqualConstraint();
    Assert.assertEquals("less_or_equal: null", utils.renderConstraint(lessOrEqualConstraint));
    lessOrEqualConstraint.setLessOrEqual("1");
    Assert.assertEquals("less_or_equal: 1", utils.renderConstraint(lessOrEqualConstraint));
    LessThanConstraint lessThanConstraint = new LessThanConstraint();
    Assert.assertEquals("less_than: null", utils.renderConstraint(lessThanConstraint));
    lessThanConstraint.setLessThan("1");
    Assert.assertEquals("less_than: 1", utils.renderConstraint(lessThanConstraint));
    LengthConstraint lengthConstraint = new LengthConstraint();
    Assert.assertEquals("length: null", utils.renderConstraint(lengthConstraint));
    lengthConstraint.setLength(1);
    Assert.assertEquals("length: 1", utils.renderConstraint(lengthConstraint));
    MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint();
    Assert.assertEquals("max_length: null", utils.renderConstraint(maxLengthConstraint));
    maxLengthConstraint.setMaxLength(1);
    Assert.assertEquals("max_length: 1", utils.renderConstraint(maxLengthConstraint));
    MinLengthConstraint minLengthConstraint = new MinLengthConstraint();
    Assert.assertEquals("min_length: null", utils.renderConstraint(minLengthConstraint));
    minLengthConstraint.setMinLength(1);
    Assert.assertEquals("min_length: 1", utils.renderConstraint(minLengthConstraint));
    PatternConstraint patternConstraint = new PatternConstraint();
    Assert.assertEquals("pattern: null", utils.renderConstraint(patternConstraint));
    patternConstraint.setPattern("a");
    Assert.assertEquals("pattern: a", utils.renderConstraint(patternConstraint));
    patternConstraint.setPattern("[.*]");
    Assert.assertEquals("pattern: \"[.*]\"", utils.renderConstraint(patternConstraint));
    EqualConstraint equalConstraint = new EqualConstraint();
    Assert.assertEquals("equal: null", utils.renderConstraint(equalConstraint));
    equalConstraint.setEqual("value");
    Assert.assertEquals("equal: value", utils.renderConstraint(equalConstraint));
    equalConstraint.setEqual(" value");
    Assert.assertEquals("equal: \" value\"", utils.renderConstraint(equalConstraint));
    InRangeConstraint inRangeConstraint = new InRangeConstraint();
    Assert.assertEquals("in_range: []", utils.renderConstraint(inRangeConstraint));
    List<String> inRange = Lists.newArrayList();
    inRangeConstraint.setInRange(inRange);
    Assert.assertEquals("in_range: []", utils.renderConstraint(inRangeConstraint));
    inRange.add("1");
    Assert.assertEquals("in_range: [1]", utils.renderConstraint(inRangeConstraint));
    inRange.add("2");
    Assert.assertEquals("in_range: [1, 2]", utils.renderConstraint(inRangeConstraint));
    ValidValuesConstraint validValuesConstraint = new ValidValuesConstraint();
    Assert.assertEquals("valid_values: []", utils.renderConstraint(validValuesConstraint));
    List<String> validValues = Lists.newArrayList();
    validValuesConstraint.setValidValues(validValues);
    Assert.assertEquals("valid_values: []", utils.renderConstraint(validValuesConstraint));
    validValues.add("value1");
    Assert.assertEquals("valid_values: [value1]", utils.renderConstraint(validValuesConstraint));
    validValues.add("value2 ");
    Assert.assertEquals("valid_values: [value1, \"value2 \"]", utils.renderConstraint(validValuesConstraint));
    validValues.add("value3,value4");
    Assert.assertEquals("valid_values: [value1, \"value2 \", \"value3,value4\"]", utils.renderConstraint(validValuesConstraint));
    // finally test an unknown constraint
    AbstractPropertyConstraint abstractPropertyConstraint = new AbstractPropertyConstraint() {

        @Override
        public void validate(Object propertyValue) throws ConstraintViolationException {
        }

        @Override
        public void initialize(IPropertyType<?> propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
        }
    };
    Assert.assertEquals("", utils.renderConstraint(abstractPropertyConstraint));
}
Also used : MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) LengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) LessThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) ValidValuesConstraint(org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint) AbstractPropertyConstraint(org.alien4cloud.tosca.model.definitions.constraints.AbstractPropertyConstraint) IPropertyType(org.alien4cloud.tosca.normative.types.IPropertyType) GreaterThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint) PatternConstraint(org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint) InRangeConstraint(org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) Test(org.junit.Test)

Aggregations

LengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint)10 MaxLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint)10 MinLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)10 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)8 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)7 Test (org.junit.Test)7 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)3 GreaterOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)3 GreaterThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint)3 InRangeConstraint (org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint)3 LessOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint)3 LessThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint)3 PatternConstraint (org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint)3 ValidValuesConstraint (org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint)3 FormPropertyDefinition (alien4cloud.ui.form.annotation.FormPropertyDefinition)1 FormDescriptorGenerationException (alien4cloud.ui.form.exception.FormDescriptorGenerationException)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)1 AbstractPropertyConstraint (org.alien4cloud.tosca.model.definitions.constraints.AbstractPropertyConstraint)1