Search in sources :

Example 11 with PropertyConstraint

use of org.alien4cloud.tosca.model.definitions.PropertyConstraint 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 12 with PropertyConstraint

use of org.alien4cloud.tosca.model.definitions.PropertyConstraint 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 13 with PropertyConstraint

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

the class ConstraintPropertyService method checkSimplePropertyConstraint.

/**
 * Check constraints defined on a property for a specified value
 *
 * @param propertyName Property name (mainly used to create a comprehensive error message)
 * @param stringValue Tested property value
 * @param propertyDefinition Full property definition with type, constraints, default value,...
 * @throws ConstraintViolationException
 * @throws ConstraintValueDoNotMatchPropertyTypeException
 */
private static void checkSimplePropertyConstraint(final String propertyName, final String stringValue, final PropertyDefinition propertyDefinition) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    ConstraintInformation consInformation = null;
    // check any property definition without constraints (type/value)
    checkBasicType(propertyName, propertyDefinition.getType(), stringValue);
    if (propertyDefinition.getConstraints() != null && !propertyDefinition.getConstraints().isEmpty()) {
        IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(propertyDefinition.getType());
        for (PropertyConstraint constraint : propertyDefinition.getConstraints()) {
            try {
                consInformation = ConstraintUtil.getConstraintInformation(constraint);
                consInformation.setPath(propertyName + ".constraints[" + consInformation.getName() + "]");
                constraint.initialize(toscaType);
                constraint.validate(toscaType, stringValue);
            } catch (ConstraintViolationException e) {
                throw new ConstraintViolationException(e.getMessage(), e, consInformation);
            } catch (IntrospectionException e) {
                // ConstraintValueDoNotMatchPropertyTypeException is not supposed to be raised here (only in constraint definition validation)
                log.info("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
                throw new ConstraintTechnicalException("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
            }
        }
    }
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) IntrospectionException(java.beans.IntrospectionException) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ConstraintTechnicalException(org.alien4cloud.tosca.exceptions.ConstraintTechnicalException)

Example 14 with PropertyConstraint

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

the class ConstraintPropertyService method checkConstraints.

private static void checkConstraints(final String propertyName, final String stringValue, final String typeName, List<PropertyConstraint> constraints) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    ConstraintInformation consInformation = null;
    for (PropertyConstraint constraint : constraints) {
        IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(typeName);
        try {
            consInformation = ConstraintUtil.getConstraintInformation(constraint);
            consInformation.setPath(propertyName + ".constraints[" + consInformation.getName() + "]");
            constraint.initialize(toscaType);
            constraint.validate(toscaType, stringValue);
        } catch (ConstraintViolationException e) {
            throw new ConstraintViolationException(e.getMessage(), e, consInformation);
        } catch (IntrospectionException e) {
            // ConstraintValueDoNotMatchPropertyTypeException is not supposed to be raised here (only in constraint definition validation)
            log.info("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
            throw new ConstraintTechnicalException("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
        }
    }
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) IntrospectionException(java.beans.IntrospectionException) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ConstraintTechnicalException(org.alien4cloud.tosca.exceptions.ConstraintTechnicalException)

Example 15 with PropertyConstraint

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

the class AbstractToscaParserSimpleProfileTest method testNodeType.

@SuppressWarnings("unchecked")
@Test
public void testNodeType() throws FileNotFoundException, ParsingException {
    Mockito.reset(csarRepositorySearchService);
    NodeType mockedResult = Mockito.mock(NodeType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.SoftwareComponent"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(mockedResult.getDerivedFrom()).thenReturn(Lists.newArrayList("tosca.nodes.Root"));
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedResult);
    CapabilityType mockedCapabilityResult = Mockito.mock(CapabilityType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("mytypes.mycapabilities.MyCapabilityTypeName"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("mytypes.mycapabilities.MyCapabilityTypeName"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Endpoint"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    RelationshipType hostedOn = new RelationshipType();
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.HostedOn"), Mockito.any(Set.class))).thenReturn(hostedOn);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-node-type.yml"));
    ParserTestUtil.displayErrors(parsingResult);
    assertNoBlocker(parsingResult);
    ArchiveRoot archiveRoot = parsingResult.getResult();
    Assert.assertNotNull(archiveRoot.getArchive());
    Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
    Assert.assertEquals(1, archiveRoot.getNodeTypes().size());
    // check node type.
    NodeType nodeType = archiveRoot.getNodeTypes().get("my_company.my_types.MyAppNodeType");
    Assert.assertNotNull(nodeType);
    Assert.assertEquals(Lists.newArrayList("tosca.nodes.SoftwareComponent", "tosca.nodes.Root"), nodeType.getDerivedFrom());
    Assert.assertEquals("My company’s custom applicaton", nodeType.getDescription());
    // validate properties parsing
    Assert.assertEquals(4, nodeType.getProperties().size());
    PropertyDefinition def1 = new PropertyDefinition();
    def1.setType("string");
    def1.setDefault(new ScalarPropertyValue("default"));
    def1.setDescription("application password");
    List<PropertyConstraint> constraints = Lists.newArrayList();
    constraints.add(new MinLengthConstraint(6));
    constraints.add(new MaxLengthConstraint(10));
    def1.setConstraints(constraints);
    PropertyDefinition def2 = new PropertyDefinition();
    def2.setType("integer");
    def2.setDescription("application port number");
    PropertyDefinition def3 = new PropertyDefinition();
    def3.setType("scalar-unit.size");
    def3.setDefault(new ScalarPropertyValue("1 GB"));
    LessThanConstraint ltConstraint = new LessThanConstraint();
    ltConstraint.setLessThan("1 TB");
    constraints = Lists.<PropertyConstraint>newArrayList(ltConstraint);
    def3.setConstraints(constraints);
    PropertyDefinition def4 = new PropertyDefinition();
    def4.setType("scalar-unit.time");
    def4.setDefault(new ScalarPropertyValue("1 d"));
    GreaterThanConstraint gtConstraint = new GreaterThanConstraint();
    gtConstraint.setGreaterThan("1 h");
    constraints = Lists.<PropertyConstraint>newArrayList(gtConstraint);
    def4.setConstraints(constraints);
    Assert.assertEquals(MapUtil.newHashMap(new String[] { "my_app_password", "my_app_duration", "my_app_size", "my_app_port" }, new PropertyDefinition[] { def1, def4, def3, def2 }), nodeType.getProperties());
    // check requirements
    Assert.assertEquals(2, nodeType.getRequirements().size());
    RequirementDefinition rd0 = nodeType.getRequirements().get(0);
    Assert.assertEquals("host", rd0.getId());
    Assert.assertEquals(1, rd0.getLowerBound());
    Assert.assertEquals(1, rd0.getUpperBound());
    RequirementDefinition rd1 = nodeType.getRequirements().get(1);
    Assert.assertEquals("other", rd1.getId());
    Assert.assertEquals(0, rd1.getLowerBound());
    Assert.assertEquals(Integer.MAX_VALUE, rd1.getUpperBound());
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) LessThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) GreaterThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint) Test(org.junit.Test)

Aggregations

PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)30 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)18 Test (org.junit.Test)15 MinLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)11 MaxLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint)10 LengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint)8 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)5 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)4 GreaterThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint)4 LessThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint)4 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)3 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)3 GreaterOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)3 LessOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint)3 PatternConstraint (org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint)3 ValidValuesConstraint (org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint)3 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)3 NodeType (org.alien4cloud.tosca.model.types.NodeType)3 ConstraintInformation (alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)2 IntrospectionException (java.beans.IntrospectionException)2