Search in sources :

Example 36 with PropertyDefinition

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

the class FunctionEvaluator method getPropertyValue.

private static AbstractPropertyValue getPropertyValue(Map<String, AbstractPropertyValue> properties, Map<String, PropertyDefinition> propertyDefinitions, String propertyAccessPath) {
    if (properties == null || !properties.containsKey(propertyAccessPath)) {
        String propertyName = PropertyUtil.getPropertyNameFromComplexPath(propertyAccessPath);
        if (propertyName == null) {
            // Non complex
            return PropertyUtil.getDefaultFromPropertyDefinitions(propertyAccessPath, propertyDefinitions);
        } else {
            // Complex
            PropertyDefinition propertyDefinition = propertyDefinitions.get(propertyName);
            AbstractPropertyValue rawValue;
            if (propertyDefinition == null) {
                return null;
            } else if (ToscaTypes.isSimple(propertyDefinition.getType())) {
                // It's a complex path (with '.') but the type in definition is finally simple
                return null;
            } else if (properties != null && (rawValue = properties.get(propertyName)) != null) {
                if (!(rawValue instanceof PropertyValue)) {
                    throw new NotSupportedException("Only support static value in a get_property");
                }
                Object value = MapUtil.get(((PropertyValue) rawValue).getValue(), propertyAccessPath.substring(propertyName.length() + 1));
                return new ScalarPropertyValue(PropertyUtil.serializePropertyValue(value));
            } else {
                return null;
            }
        }
    } else {
        return properties.get(propertyAccessPath);
    }
}
Also used : ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) NotSupportedException(alien4cloud.paas.exception.NotSupportedException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 37 with PropertyDefinition

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

the class ToscaParserAlien140Test method testCapabilitiesComplexProperty.

@Test
public void testCapabilitiesComplexProperty() throws ParsingException {
    Mockito.reset(csarRepositorySearchService);
    Csar csar = new Csar("tosca-normative-types", "1.0.0-ALIEN14");
    Mockito.when(csarRepositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar);
    NodeType mockedResult = Mockito.mock(NodeType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
    CapabilityType mockedCapabilityResult = Mockito.mock(CapabilityType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Root"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    DataType mockedDataType = new DataType();
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(DataType.class), Mockito.eq("tosca.datatypes.Root"), Mockito.any(Set.class))).thenReturn(mockedDataType);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "capa_complex_props.yml"));
    Assert.assertEquals(0, parsingResult.getContext().getParsingErrors().size());
    ArchiveRoot archiveRoot = parsingResult.getResult();
    // check the capabilityType
    // ////////////
    CapabilityType capaType = archiveRoot.getCapabilityTypes().values().stream().findFirst().get();
    assertNotNull(capaType.getProperties());
    Assert.assertEquals(3, capaType.getProperties().size());
    // map property
    String map = "map";
    PropertyDefinition propertyDefinition = capaType.getProperties().get(map);
    assertNotNull(propertyDefinition.getDefault());
    assertTrue(propertyDefinition.getDefault() instanceof ComplexPropertyValue);
    Map<String, Object> propertyMapValue = ((ComplexPropertyValue) propertyDefinition.getDefault()).getValue();
    assertNotNull(propertyMapValue);
    Assert.assertEquals(2, propertyMapValue.size());
    Assert.assertEquals("toto_value", propertyMapValue.get("toto"));
    Assert.assertEquals("tata_value", propertyMapValue.get("tata"));
    // custom property
    String custom = "custom";
    propertyDefinition = capaType.getProperties().get(custom);
    assertEquals("alien.test.datatypes.Custom", propertyDefinition.getType());
    assertNull(propertyDefinition.getDefault());
    // custom_with_default property
    String custom_with_default = "custom_with_default";
    propertyDefinition = capaType.getProperties().get(custom_with_default);
    assertNotNull(propertyDefinition.getDefault());
    assertTrue(propertyDefinition.getDefault() instanceof ComplexPropertyValue);
    propertyMapValue = ((ComplexPropertyValue) propertyDefinition.getDefault()).getValue();
    assertNotNull(propertyMapValue);
    assertEquals(2, propertyMapValue.size());
    assertEquals("defaultName", propertyMapValue.get("name"));
    Object list = propertyMapValue.get("groups");
    assertTrue(list instanceof List);
    assertEquals(2, ((List) list).size());
    assertTrue(CollectionUtils.containsAll((List) list, Lists.newArrayList("alien", "fastconnect")));
    // check the node template capability
    // ////////////
    NodeTemplate nodeTemplate = archiveRoot.getTopology().getNodeTemplates().values().stream().findFirst().get();
    Capability capability = nodeTemplate.getCapabilities().values().stream().findFirst().get();
    assertNotNull(capability);
    Assert.assertEquals(3, capability.getProperties().size());
    // map property
    AbstractPropertyValue propertyValue = capability.getProperties().get(map);
    assertNotNull(propertyValue);
    assertTrue(propertyValue instanceof ComplexPropertyValue);
    propertyMapValue = ((ComplexPropertyValue) propertyValue).getValue();
    assertNotNull(propertyMapValue);
    Assert.assertEquals(2, propertyMapValue.size());
    Assert.assertEquals("toto_value", propertyMapValue.get("toto"));
    Assert.assertEquals("tata_value", propertyMapValue.get("tata"));
    // custom property
    propertyValue = capability.getProperties().get(custom);
    assertNotNull(propertyValue);
    assertTrue(propertyValue instanceof ComplexPropertyValue);
    propertyMapValue = ((ComplexPropertyValue) propertyValue).getValue();
    assertNotNull(propertyMapValue);
    assertEquals(2, propertyMapValue.size());
    assertEquals("manual", propertyMapValue.get("name"));
    list = propertyMapValue.get("groups");
    assertTrue(list instanceof List);
    assertEquals(2, ((List) list).size());
    assertTrue(CollectionUtils.containsAll((List) list, Lists.newArrayList("manual_alien", "manual_fastconnect")));
    // custom_with_default property
    propertyValue = capability.getProperties().get(custom_with_default);
    assertNotNull(propertyValue);
    assertTrue(propertyValue instanceof ComplexPropertyValue);
    propertyMapValue = ((ComplexPropertyValue) propertyValue).getValue();
    assertNotNull(propertyMapValue);
    assertEquals(2, propertyMapValue.size());
    assertEquals("defaultName", propertyMapValue.get("name"));
    list = propertyMapValue.get("groups");
    assertTrue(list instanceof List);
    assertEquals(2, ((List) list).size());
    assertTrue(CollectionUtils.containsAll((List) list, Lists.newArrayList("alien", "fastconnect")));
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) Capability(org.alien4cloud.tosca.model.templates.Capability) 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) DataType(org.alien4cloud.tosca.model.types.DataType) List(java.util.List) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Test(org.junit.Test)

Example 38 with PropertyDefinition

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

the class ToscaParserSimpleProfileAlien120Test method testRelationshipType.

@SuppressWarnings("unchecked")
@Test
public void testRelationshipType() throws FileNotFoundException, ParsingException {
    RelationshipType mockedResult = Mockito.mock(RelationshipType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.Relationship"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(mockedResult.getDerivedFrom()).thenReturn(Lists.newArrayList("tosca.capabilities.Root"));
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-relationship-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.getRelationshipTypes().size());
    Entry<String, RelationshipType> entry = archiveRoot.getRelationshipTypes().entrySet().iterator().next();
    Assert.assertEquals("mycompany.mytypes.myapplication.MyRelationship", entry.getKey());
    RelationshipType relationship = entry.getValue();
    Assert.assertEquals(Lists.newArrayList("tosca.relationships.Relationship", "tosca.capabilities.Root"), relationship.getDerivedFrom());
    Assert.assertEquals("a custom relationship", relationship.getDescription());
    // properties
    Assert.assertEquals(2, relationship.getProperties().size());
    Assert.assertTrue(relationship.getProperties().containsKey("my_feature_setting"));
    PropertyDefinition pd = relationship.getProperties().get("my_feature_setting");
    Assert.assertEquals("string", pd.getType());
    Assert.assertTrue(relationship.getProperties().containsKey("my_feature_value"));
    pd = relationship.getProperties().get("my_feature_value");
    Assert.assertEquals("integer", pd.getType());
    // valid targets
    Assert.assertEquals(2, relationship.getValidTargets().length);
    Assert.assertEquals("tosca.capabilities.Feature1", relationship.getValidTargets()[0]);
    Assert.assertEquals("tosca.capabilities.Feature2", relationship.getValidTargets()[1]);
}
Also used : Set(java.util.Set) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 39 with PropertyDefinition

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

the class PropertyDefinitionConstraintsTest method testCheckIfCompatibleOrFailConstraintSatisfiedAllNull.

@Test
public void testCheckIfCompatibleOrFailConstraintSatisfiedAllNull() throws ConstraintViolationException, IncompatiblePropertyDefinitionException {
    PropertyDefinition propDef1 = new PropertyDefinition();
    PropertyDefinition propDef2 = new PropertyDefinition();
    propDef1.setType(ToscaTypes.STRING);
    propDef2.setType(ToscaTypes.STRING);
    propDef1.checkIfCompatibleOrFail(propDef2);
}
Also used : PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 40 with PropertyDefinition

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

the class PropertyDefinitionConstraintsTest method testCheckIfCompatibleOrFailConstraintNotSatisfiedSetOfScamblePropertyConstraintWithDifferentType.

@Test(expected = IncompatiblePropertyDefinitionException.class)
public void testCheckIfCompatibleOrFailConstraintNotSatisfiedSetOfScamblePropertyConstraintWithDifferentType() 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");
    InRangeConstraint constraint1Bis = new InRangeConstraint();
    constraint1Bis.setRangeMaxValue("4");
    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)

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