Search in sources :

Example 6 with DataType

use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.

the class ConstraintPropertyServiceTest method testInvalidMapPropertyInComplex.

@Test(expected = ConstraintValueDoNotMatchPropertyTypeException.class)
public void testInvalidMapPropertyInComplex() throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
    // given
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType("alien.test.ComplexStruct");
    PropertyDefinition subPropertyDefinition = new PropertyDefinition();
    subPropertyDefinition.setType(ToscaTypes.MAP);
    PropertyDefinition entrySchema = new PropertyDefinition();
    entrySchema.setType(ToscaTypes.STRING);
    subPropertyDefinition.setEntrySchema(entrySchema);
    DataType dataType = new DataType();
    dataType.setProperties(Maps.newHashMap());
    dataType.getProperties().put("myMap", subPropertyDefinition);
    dataType.setElementId("alien.test.ComplexStruct");
    ICSARRepositorySearchService originalCsarRepositorySearchService = ToscaContext.getCsarRepositorySearchService();
    ToscaContext.init(new HashSet<>());
    ICSARRepositorySearchService mockSearchService = Mockito.mock(ICSARRepositorySearchService.class);
    Mockito.when(mockSearchService.getRequiredElementInDependencies(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(dataType);
    Mockito.when(mockSearchService.getElementInDependencies(Mockito.any(), Mockito.any(), Mockito.anySet())).thenReturn(dataType);
    try {
        ToscaContext.setCsarRepositorySearchService(mockSearchService);
        // when
        Object propertyValue = ImmutableMap.builder().put("myMap", "aa").build();
        // then -> ConstraintViolationException
        ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
    } finally {
        ToscaContext.setCsarRepositorySearchService(originalCsarRepositorySearchService);
        ToscaContext.destroy();
    }
}
Also used : DataType(org.alien4cloud.tosca.model.types.DataType) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ICSARRepositorySearchService(alien4cloud.component.ICSARRepositorySearchService) Test(org.junit.Test)

Example 7 with DataType

use of org.alien4cloud.tosca.model.types.DataType 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 8 with DataType

use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.

the class PropertyDefinitionPostProcessor method validateType.

private void validateType(PropertyDefinition propertyDefinition) {
    String propertyType = propertyDefinition.getType();
    if (propertyType == null) {
        Node node = ParsingContextExecution.getObjectToNodeMap().get(propertyType);
        if (node == null) {
            node = ParsingContextExecution.getObjectToNodeMap().get(propertyDefinition);
        }
        ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.VALIDATION_ERROR, "ToscaPropertyType", node.getStartMark(), "Property type must be defined", node.getEndMark(), "type"));
    } else if (!ToscaTypes.isSimple(propertyType)) {
        if (ToscaTypes.LIST.equals(propertyType) || ToscaTypes.MAP.equals(propertyType)) {
            PropertyDefinition entrySchema = propertyDefinition.getEntrySchema();
            if (entrySchema == null) {
                Node node = ParsingContextExecution.getObjectToNodeMap().get(propertyDefinition);
                ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.VALIDATION_ERROR, "ToscaPropertyType", node.getStartMark(), "Type " + propertyType + " must define entry schema", node.getEndMark(), "type"));
            } else {
                validateType(entrySchema);
            }
        } else {
            // It's data type
            ArchiveRoot archiveRoot = ParsingContextExecution.getRootObj();
            if (!archiveRoot.getDataTypes().containsKey(propertyType)) {
                DataType dataType = ToscaContext.get(DataType.class, propertyType);
                if (dataType == null) {
                    Node node = ParsingContextExecution.getObjectToNodeMap().get(propertyType);
                    ParsingContextExecution.getParsingErrors().add(new ParsingError(ErrorCode.TYPE_NOT_FOUND, "ToscaPropertyType", node.getStartMark(), "Type " + propertyType + " is not found.", node.getEndMark(), "type"));
                }
            }
        }
    }
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) Node(org.yaml.snakeyaml.nodes.Node) DataType(org.alien4cloud.tosca.model.types.DataType) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 9 with DataType

use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.

the class ArchiveRootPostProcessor method processRepositoriesDefinitions.

private void processRepositoriesDefinitions(Map<String, RepositoryDefinition> repositories) {
    if (MapUtils.isNotEmpty(repositories)) {
        DataType credentialType = ToscaContext.get(DataType.class, NormativeCredentialConstant.DATA_TYPE);
        repositories.values().forEach(repositoryDefinition -> {
            if (repositoryDefinition.getCredential() != null) {
                credentialType.getProperties().forEach((propertyName, propertyDefinition) -> {
                    // Fill with default value
                    if (!repositoryDefinition.getCredential().getValue().containsKey(propertyName)) {
                        AbstractPropertyValue defaultValue = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(propertyDefinition);
                        if (defaultValue instanceof PropertyValue) {
                            repositoryDefinition.getCredential().getValue().put(propertyName, ((PropertyValue) defaultValue).getValue());
                        }
                    }
                });
                Node credentialNode = ParsingContextExecution.getObjectToNodeMap().get(repositoryDefinition.getCredential());
                PropertyDefinition propertyDefinition = new PropertyDefinition();
                propertyDefinition.setType(NormativeCredentialConstant.DATA_TYPE);
                propertyValueChecker.checkProperty("credential", credentialNode, repositoryDefinition.getCredential(), propertyDefinition, repositoryDefinition.getId());
            }
        });
    }
}
Also used : Node(org.yaml.snakeyaml.nodes.Node) DataType(org.alien4cloud.tosca.model.types.DataType) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 10 with DataType

use of org.alien4cloud.tosca.model.types.DataType in project alien4cloud by alien4cloud.

the class ToscaPropertyFormDescriptorGenerator method doGenerateDescriptor.

private Map<String, Object> doGenerateDescriptor(Set<String> processedDataTypes, PropertyDefinition propertyDefinition, Set<CSARDependency> dependencies) {
    Map<String, Object> dataTypeDescriptors;
    if (ToscaTypes.isSimple(propertyDefinition.getType())) {
        dataTypeDescriptors = generateDescriptorForSimpleType(propertyDefinition);
    } else if (ToscaTypes.LIST.equals(propertyDefinition.getType())) {
        PropertyDefinition entryDefinition = propertyDefinition.getEntrySchema();
        if (entryDefinition == null) {
            throw new InvalidArgumentException("List type without entry schema");
        }
        return generateDescriptorForListType(processedDataTypes, entryDefinition, dependencies);
    } else if (ToscaTypes.MAP.equals(propertyDefinition.getType())) {
        PropertyDefinition entryDefinition = propertyDefinition.getEntrySchema();
        if (entryDefinition == null) {
            throw new InvalidArgumentException("Map type without entry schema");
        }
        dataTypeDescriptors = generateDescriptorForMapType(processedDataTypes, entryDefinition, dependencies);
    } else {
        DataType dataType = csarRepositorySearchService.getElementInDependencies(DataType.class, propertyDefinition.getType(), dependencies);
        if (dataType == null) {
            throw new InvalidArgumentException("Data type <" + propertyDefinition.getType() + "> do not exist in dependencies " + dependencies);
        }
        if (processedDataTypes.add(dataType.getElementId())) {
            dataTypeDescriptors = generateDescriptorForDataType(processedDataTypes, dataType, dependencies);
        } else {
            dataTypeDescriptors = generateDescriptorForSimpleType(propertyDefinition);
        }
    }
    if (propertyDefinition.isRequired()) {
        dataTypeDescriptors.put(NOT_NULL_KEY, true);
    }
    return dataTypeDescriptors;
}
Also used : InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) DataType(org.alien4cloud.tosca.model.types.DataType) PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Aggregations

DataType (org.alien4cloud.tosca.model.types.DataType)16 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)11 Map (java.util.Map)6 PrimitiveDataType (org.alien4cloud.tosca.model.types.PrimitiveDataType)6 Test (org.junit.Test)6 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)5 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)5 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)4 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)4 Application (alien4cloud.model.application.Application)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Maps (com.google.common.collect.Maps)3 Arrays (java.util.Arrays)3 List (java.util.List)3 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)3 NodeType (org.alien4cloud.tosca.model.types.NodeType)3 ToscaTypes (org.alien4cloud.tosca.normative.types.ToscaTypes)3 PropertiesYamlParser (org.alien4cloud.tosca.utils.PropertiesYamlParser)3 InputsMappingFileVariableResolverConfigured (org.alien4cloud.tosca.variable.InputsMappingFileVariableResolver.InputsMappingFileVariableResolverConfigured)3 InputsMappingFileVariableResolver.configure (org.alien4cloud.tosca.variable.InputsMappingFileVariableResolver.configure)3