use of org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint in project alien4cloud by alien4cloud.
the class ToscaPropertyDefaultValueConstraintsValidatorTest method createMaxLenghtDefinition.
private PropertyDefinition createMaxLenghtDefinition(String propertyType, int constraintValue, String defaultValue) {
MaxLengthConstraint constraint = new MaxLengthConstraint();
constraint.setMaxLength(constraintValue);
return createDefinitions(propertyType, constraint, defaultValue);
}
use of org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint in project alien4cloud by alien4cloud.
the class ToscaSerializerTest method getConstraintList.
private List<PropertyConstraint> getConstraintList() {
List<PropertyConstraint> result = new ArrayList<PropertyConstraint>();
ValidValuesConstraint c1 = new ValidValuesConstraint();
c1.setValidValues(Lists.newArrayList("one", "two", "tree"));
result.add(c1);
GreaterOrEqualConstraint c2 = new GreaterOrEqualConstraint();
c2.setGreaterOrEqual("2");
result.add(c2);
GreaterThanConstraint c3 = new GreaterThanConstraint();
c3.setGreaterThan("3");
result.add(c3);
LessOrEqualConstraint c4 = new LessOrEqualConstraint();
c4.setLessOrEqual("4");
result.add(c4);
LessThanConstraint c5 = new LessThanConstraint();
c5.setLessThan("5");
result.add(c5);
LengthConstraint c6 = new LengthConstraint();
c6.setLength(6);
result.add(c6);
MaxLengthConstraint c7 = new MaxLengthConstraint();
c7.setMaxLength(7);
result.add(c7);
MinLengthConstraint c8 = new MinLengthConstraint();
c8.setMinLength(8);
result.add(c8);
PatternConstraint c9 = new PatternConstraint();
c9.setPattern("9+");
result.add(c9);
EqualConstraint c10 = new EqualConstraint();
c10.setEqual("10");
result.add(c10);
InRangeConstraint c11 = new InRangeConstraint();
c11.setInRange(Lists.newArrayList("11", "12"));
result.add(c11);
return result;
}
use of org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint in project alien4cloud by alien4cloud.
the class ConstraintPropertyServiceTest method testValidListProperty.
@Test
public void testValidListProperty() throws Exception {
PropertyDefinition propertyDefinition = new PropertyDefinition();
propertyDefinition.setType(ToscaTypes.LIST);
PropertyDefinition entrySchema = new PropertyDefinition();
entrySchema.setType(ToscaTypes.STRING);
propertyDefinition.setEntrySchema(entrySchema);
Object propertyValue = ImmutableList.builder().add("aa", "bb").build();
ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
// test length constraint
LengthConstraint lengthConstraint = new LengthConstraint();
lengthConstraint.setLength(2);
List<PropertyConstraint> constraints = Lists.newArrayList();
constraints.add(lengthConstraint);
propertyDefinition.setConstraints(constraints);
ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
// test min_length constraint
MinLengthConstraint minLengthConstraint = new MinLengthConstraint();
minLengthConstraint.setMinLength(1);
constraints.add(minLengthConstraint);
propertyDefinition.setConstraints(constraints);
ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
// test max_length constraint
MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint();
maxLengthConstraint.setMaxLength(3);
constraints.add(maxLengthConstraint);
propertyDefinition.setConstraints(constraints);
ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
}
use of org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint 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());
}
use of org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint 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;
}
Aggregations