Search in sources :

Example 26 with PropertyDefinition

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

the class ConstraintPropertyServiceTest method testInvalidStringConstraintProperty.

@Test(expected = ConstraintViolationException.class)
public void testInvalidStringConstraintProperty() 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", "value", 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 27 with PropertyDefinition

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

the class ConstraintPropertyServiceTest method testInvalidConstraintProperty.

@Test(expected = ConstraintValueDoNotMatchPropertyTypeException.class)
public void testInvalidConstraintProperty() 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();
    // invalid length constraint
    LessOrEqualConstraint lengthConstraint = new LessOrEqualConstraint();
    lengthConstraint.setLessOrEqual("aa");
    List<PropertyConstraint> constraints = Lists.newArrayList();
    constraints.add(lengthConstraint);
    propertyDefinition.setConstraints(constraints);
    ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
}
Also used : LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 28 with PropertyDefinition

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

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

the class ConstraintPropertyServiceTest method testInvalidMapMinLengthConstraintProperty.

@Test(expected = ConstraintViolationException.class)
public void testInvalidMapMinLengthConstraintProperty() 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();
    // invalid length constraint
    MinLengthConstraint lengthConstraint = new MinLengthConstraint();
    lengthConstraint.setMinLength(4);
    List<PropertyConstraint> constraints = Lists.newArrayList();
    constraints.add(lengthConstraint);
    propertyDefinition.setConstraints(constraints);
    ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 30 with PropertyDefinition

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

the class ConstraintPropertyServiceTest method testInvalidMapPropertyInComplex.

@Test(expected = ConstraintValueDoNotMatchPropertyTypeException.class)
public void testInvalidMapPropertyInComplex() throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
    // given
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType("alien.test.ComplexStruct");
    PropertyDefinition subPropertyDefinition = new PropertyDefinition();
    subPropertyDefinition.setType(ToscaTypes.MAP);
    PropertyDefinition entrySchema = new PropertyDefinition();
    entrySchema.setType(ToscaTypes.STRING);
    subPropertyDefinition.setEntrySchema(entrySchema);
    DataType dataType = new DataType();
    dataType.setProperties(Maps.newHashMap());
    dataType.getProperties().put("myMap", subPropertyDefinition);
    dataType.setElementId("alien.test.ComplexStruct");
    ICSARRepositorySearchService originalCsarRepositorySearchService = ToscaContext.getCsarRepositorySearchService();
    ToscaContext.init(new HashSet<>());
    ICSARRepositorySearchService mockSearchService = Mockito.mock(ICSARRepositorySearchService.class);
    Mockito.when(mockSearchService.getRequiredElementInDependencies(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(dataType);
    Mockito.when(mockSearchService.getElementInDependencies(Mockito.any(), Mockito.any(), Mockito.anySet())).thenReturn(dataType);
    try {
        ToscaContext.setCsarRepositorySearchService(mockSearchService);
        // when
        Object propertyValue = ImmutableMap.builder().put("myMap", "aa").build();
        // then -> ConstraintViolationException
        ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
    } finally {
        ToscaContext.setCsarRepositorySearchService(originalCsarRepositorySearchService);
        ToscaContext.destroy();
    }
}
Also used : DataType(org.alien4cloud.tosca.model.types.DataType) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ICSARRepositorySearchService(alien4cloud.component.ICSARRepositorySearchService) Test(org.junit.Test)

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