Search in sources :

Example 1 with IPropertyType

use of org.alien4cloud.tosca.normative.types.IPropertyType in project alien4cloud by alien4cloud.

the class PropertyValueService method getValueInUnit.

public static String getValueInUnit(String propertyValue, String unit, boolean ceil, String toscaType) {
    IPropertyType type = ToscaTypes.fromYamlTypeName(toscaType);
    if (type instanceof ScalarType) {
        try {
            ScalarUnit scalarUnit = ((ScalarType) type).parse(propertyValue);
            double convertedValue = scalarUnit.convert(unit);
            if (ceil) {
                convertedValue = Math.ceil(convertedValue);
            }
            return format(convertedValue);
        } catch (InvalidPropertyValueException e) {
            log.error("e");
            throw new InvalidArgumentException(e.getMessage());
        }
    }
    throw new InvalidArgumentException("Type is not a scalar type");
}
Also used : InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) ScalarType(org.alien4cloud.tosca.normative.types.ScalarType) IPropertyType(org.alien4cloud.tosca.normative.types.IPropertyType) ScalarUnit(org.alien4cloud.tosca.normative.primitives.ScalarUnit)

Example 2 with IPropertyType

use of org.alien4cloud.tosca.normative.types.IPropertyType 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

IPropertyType (org.alien4cloud.tosca.normative.types.IPropertyType)2 InvalidArgumentException (alien4cloud.exception.InvalidArgumentException)1 InvalidPropertyValueException (org.alien4cloud.tosca.exceptions.InvalidPropertyValueException)1 AbstractPropertyConstraint (org.alien4cloud.tosca.model.definitions.constraints.AbstractPropertyConstraint)1 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)1 GreaterOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)1 GreaterThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint)1 InRangeConstraint (org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint)1 LengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint)1 LessOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint)1 LessThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint)1 MaxLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint)1 MinLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)1 PatternConstraint (org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint)1 ValidValuesConstraint (org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint)1 ScalarUnit (org.alien4cloud.tosca.normative.primitives.ScalarUnit)1 ScalarType (org.alien4cloud.tosca.normative.types.ScalarType)1 Test (org.junit.Test)1