Search in sources :

Example 91 with PropertyDefinition

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"));
}
Also used : PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) List(java.util.List) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Test(org.junit.Test)

Example 92 with PropertyDefinition

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());
}
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)

Example 93 with PropertyDefinition

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);
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 94 with PropertyDefinition

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;
}
Also used : PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 95 with 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);
            }
        }
    }
}
Also used : DataType(org.alien4cloud.tosca.model.types.DataType) PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) Map(java.util.Map) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

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