use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.
the class ToscaParserSimpleProfileAlien120Test method testDataTypesVeryComplexWithDefault.
@Test
public void testDataTypesVeryComplexWithDefault() throws ParsingException {
ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-data-types-very-complex-default.yml"));
ParserTestUtil.displayErrors(parsingResult);
Assert.assertEquals(3, parsingResult.getResult().getDataTypes().size());
Assert.assertEquals(2, parsingResult.getResult().getNodeTypes().size());
Assert.assertEquals(0, parsingResult.getContext().getParsingErrors().size());
NodeType commandType = parsingResult.getResult().getNodeTypes().get("alien.test.Command");
Assert.assertNotNull(commandType);
PropertyDefinition pd = commandType.getProperties().get("customer");
Assert.assertNotNull(pd);
// check the default value
Object defaultValue = pd.getDefault();
Assert.assertNotNull(defaultValue);
Assert.assertTrue(defaultValue instanceof ComplexPropertyValue);
ComplexPropertyValue cpv = (ComplexPropertyValue) defaultValue;
Map<String, Object> valueAsMap = cpv.getValue();
Assert.assertNotNull(valueAsMap);
Assert.assertTrue(valueAsMap.containsKey("first_name"));
Assert.assertEquals("Foo", valueAsMap.get("first_name"));
Assert.assertTrue(valueAsMap.containsKey("last_name"));
Assert.assertEquals("Bar", valueAsMap.get("last_name"));
Assert.assertTrue(valueAsMap.containsKey("address"));
Object addressObj = valueAsMap.get("address");
Assert.assertNotNull(addressObj);
Assert.assertTrue(addressObj instanceof Map);
Map<String, Object> addressMap = (Map<String, Object>) addressObj;
Assert.assertTrue(addressMap.containsKey("street_name"));
Assert.assertEquals("rue des peupliers", addressMap.get("street_name"));
Assert.assertTrue(addressMap.containsKey("zipcode"));
Assert.assertEquals("92130", addressMap.get("zipcode"));
Assert.assertTrue(addressMap.containsKey("city_name"));
Assert.assertEquals("ISSY LES MOULES", addressMap.get("city_name"));
Assert.assertTrue(valueAsMap.containsKey("emails"));
Object emailsObj = valueAsMap.get("emails");
Assert.assertNotNull(emailsObj);
Assert.assertTrue(emailsObj instanceof List);
List<Object> emailsList = (List<Object>) emailsObj;
Assert.assertEquals(2, emailsList.size());
Assert.assertEquals("contact@fastconnect.fr", emailsList.get(0));
Assert.assertEquals("info@fastconnect.fr", emailsList.get(1));
Object accountsObj = valueAsMap.get("accounts");
Assert.assertNotNull(accountsObj);
Assert.assertTrue(accountsObj instanceof Map);
Map<String, Object> accountsMap = (Map<String, Object>) accountsObj;
Assert.assertEquals(2, accountsMap.size());
Assert.assertTrue(accountsMap.containsKey("main"));
Assert.assertEquals("root", accountsMap.get("main"));
Assert.assertTrue(accountsMap.containsKey("secondary"));
Assert.assertEquals("user", accountsMap.get("secondary"));
Assert.assertEquals(1, parsingResult.getResult().getTopology().getNodeTemplates().size());
NodeTemplate nodeTemplate = parsingResult.getResult().getTopology().getNodeTemplates().values().iterator().next();
// on the node, the default value should be set
Assert.assertNotNull(nodeTemplate.getProperties());
Assert.assertTrue(nodeTemplate.getProperties().containsKey("customer"));
AbstractPropertyValue apv = nodeTemplate.getProperties().get("customer");
Assert.assertNotNull(apv);
Assert.assertTrue(apv instanceof ComplexPropertyValue);
cpv = (ComplexPropertyValue) apv;
valueAsMap = cpv.getValue();
Assert.assertNotNull(valueAsMap);
Assert.assertTrue(valueAsMap.containsKey("first_name"));
Assert.assertEquals("Foo", valueAsMap.get("first_name"));
Assert.assertTrue(valueAsMap.containsKey("last_name"));
Assert.assertEquals("Bar", valueAsMap.get("last_name"));
Assert.assertTrue(valueAsMap.containsKey("address"));
addressObj = valueAsMap.get("address");
Assert.assertNotNull(addressObj);
Assert.assertTrue(addressObj instanceof Map);
addressMap = (Map<String, Object>) addressObj;
Assert.assertTrue(addressMap.containsKey("street_name"));
Assert.assertEquals("rue des peupliers", addressMap.get("street_name"));
Assert.assertTrue(addressMap.containsKey("zipcode"));
Assert.assertEquals("92130", addressMap.get("zipcode"));
Assert.assertTrue(addressMap.containsKey("city_name"));
Assert.assertEquals("ISSY LES MOULES", addressMap.get("city_name"));
Assert.assertTrue(valueAsMap.containsKey("emails"));
emailsObj = valueAsMap.get("emails");
Assert.assertNotNull(emailsObj);
Assert.assertTrue(emailsObj instanceof List);
emailsList = (List<Object>) emailsObj;
Assert.assertEquals(2, emailsList.size());
Assert.assertEquals("contact@fastconnect.fr", emailsList.get(0));
Assert.assertEquals("info@fastconnect.fr", emailsList.get(1));
accountsObj = valueAsMap.get("accounts");
Assert.assertNotNull(accountsObj);
Assert.assertTrue(accountsObj instanceof Map);
accountsMap = (Map<String, Object>) accountsObj;
Assert.assertEquals(2, accountsMap.size());
Assert.assertTrue(accountsMap.containsKey("main"));
Assert.assertEquals("root", accountsMap.get("main"));
Assert.assertTrue(accountsMap.containsKey("secondary"));
Assert.assertEquals("user", accountsMap.get("secondary"));
}
use of org.alien4cloud.tosca.model.definitions.PropertyDefinition 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());
}
use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.
the class PropertyDefinitionConstraintsTest method testCheckIfCompatibleOrFailConstraintNotSatisfiedSetOfScamblePropertyConstraintWithDifferentValues.
@Test(expected = IncompatiblePropertyDefinitionException.class)
public void testCheckIfCompatibleOrFailConstraintNotSatisfiedSetOfScamblePropertyConstraintWithDifferentValues() 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.INTEGER);
EqualConstraint constraint1 = new EqualConstraint();
constraint1.setEqual("test");
EqualConstraint constraint1Bis = new EqualConstraint();
constraint1Bis.setEqual("testShoulFailed");
LessThanConstraint constraint2 = new LessThanConstraint();
constraint2.setLessThan("5");
constraintsProp1.add(constraint1);
constraintsProp1.add(constraint2);
constraintsProp2.add(constraint2);
constraintsProp2.add(constraint1Bis);
propDef1.setConstraints(constraintsProp1);
propDef2.setConstraints(constraintsProp2);
propDef1.checkIfCompatibleOrFail(propDef2);
}
use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.
the class ToscaPropertyConstraintValidatorTest method createDefinitions.
private PropertyDefinition createDefinitions(String propertyType, PropertyConstraint constraint) {
PropertyDefinition propertyDefinition = new PropertyDefinition();
propertyDefinition.setType(propertyType);
propertyDefinition.setConstraints(Lists.newArrayList(constraint));
return propertyDefinition;
}
use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.
the class ConstraintPropertyService method checkDataTypePropertyConstraint.
private static void checkDataTypePropertyConstraint(String propertyName, Map<String, Object> complexPropertyValue, PropertyDefinition propertyDefinition, Consumer<String> missingPropertyConsumer) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
DataType dataType = ToscaContext.get(DataType.class, propertyDefinition.getType());
if (dataType == null) {
throw new ConstraintViolationException("Complex type " + propertyDefinition.getType() + " is not complex or it cannot be found in the archive nor in Alien");
}
for (Map.Entry<String, Object> complexPropertyValueEntry : complexPropertyValue.entrySet()) {
if (!safe(dataType.getProperties()).containsKey(complexPropertyValueEntry.getKey())) {
throw new ConstraintViolationException("Complex type <" + propertyDefinition.getType() + "> do not have nested property with name <" + complexPropertyValueEntry.getKey() + "> for property <" + propertyName + ">");
} else {
Object nestedPropertyValue = complexPropertyValueEntry.getValue();
PropertyDefinition nestedPropertyDefinition = dataType.getProperties().get(complexPropertyValueEntry.getKey());
checkPropertyConstraint(propertyName + "." + complexPropertyValueEntry.getKey(), nestedPropertyValue, nestedPropertyDefinition, missingPropertyConsumer);
}
}
// check if the data type has required missing properties
if (missingPropertyConsumer != null) {
for (Map.Entry<String, PropertyDefinition> dataTypeDefinition : safe(dataType.getProperties()).entrySet()) {
if (dataTypeDefinition.getValue().isRequired() && !complexPropertyValue.containsKey(dataTypeDefinition.getKey())) {
// A required property is missing
String missingPropertyName = propertyName + "." + dataTypeDefinition.getKey();
missingPropertyConsumer.accept(missingPropertyName);
}
}
}
}
Aggregations