Search in sources :

Example 6 with MaxLengthConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint 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)

Example 7 with MaxLengthConstraint

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

the class ToscaParserSimpleProfileAlien130Test method testNodeType.

@SuppressWarnings("unchecked")
@Test
public void testNodeType() throws FileNotFoundException, ParsingException {
    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();
    assertNotNull(archiveRoot.getArchive());
    Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
    Assert.assertEquals(1, archiveRoot.getNodeTypes().size());
    // check node type.
    Entry<String, NodeType> entry = archiveRoot.getNodeTypes().entrySet().iterator().next();
    Assert.assertEquals("my_company.my_types.MyAppNodeType", entry.getKey());
    NodeType nodeType = entry.getValue();
    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());
}
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) 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

MaxLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint)7 MinLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)6 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)5 GreaterThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint)5 LessThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint)5 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)4 LengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint)4 Test (org.junit.Test)4 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)3 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)3 GreaterOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)3 InRangeConstraint (org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint)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 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)2 Set (java.util.Set)2 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)2 NodeType (org.alien4cloud.tosca.model.types.NodeType)2 RelationshipType (org.alien4cloud.tosca.model.types.RelationshipType)2