Search in sources :

Example 11 with DataType

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

the class InputsMappingFileVariableResolverTest method check_inputs_mapping_can_be_parsed_when_variable.

@Ignore("Update when ToscaTypeConverter behavior is clearly defined")
@Test
public // Bad test
void check_inputs_mapping_can_be_parsed_when_variable() throws Exception {
    inputsMappingFileVariableResolverConfigured.customConverter(new ToscaTypeConverter((concreteType, id) -> {
        if (id.equals("datatype.complex_input_entry.sub1")) {
            DataType dataType = new DataType();
            dataType.setDeriveFromSimpleType(false);
            dataType.setProperties(// 
            ImmutableMap.of(// 
            "complex", // 
            buildPropDef(ToscaTypes.MAP, ToscaTypes.STRING)));
            return dataType;
        }
        if (id.equals("datatype.complex_input_entry")) {
            DataType dataType = new DataType();
            dataType.setDeriveFromSimpleType(false);
            dataType.setProperties(// 
            ImmutableMap.of(// 
            "sub1", // 
            buildPropDef("datatype.complex_input_entry.sub1"), // 
            "sub2", // 
            buildPropDef(ToscaTypes.MAP, ToscaTypes.STRING), // 
            "field01", // 
            buildPropDef(ToscaTypes.STRING)));
            return dataType;
        }
        return null;
    }));
    Map<String, PropertyValue> inputsMappingFileResolved = resolve("src/test/resources/alien/variables/inputs_mapping_with_variables.yml");
    assertThat(inputsMappingFileResolved.get("int_input")).isEqualTo(new ScalarPropertyValue("1"));
    assertThat(inputsMappingFileResolved.get("float_input")).isEqualTo(new ScalarPropertyValue("3.14"));
    assertThat(inputsMappingFileResolved.get("string_input")).isEqualTo(new ScalarPropertyValue("text_3.14"));
    // 
    assertThat(inputsMappingFileResolved.get("complex_input")).isEqualTo(new ComplexPropertyValue(// 
    ImmutableMap.of("sub1", new ComplexPropertyValue(// 
    ImmutableMap.of("complex", // 
    new ComplexPropertyValue(ImmutableMap.of("subfield", new ScalarPropertyValue("text"))))), "sub2", new ComplexPropertyValue(// 
    ImmutableMap.of("subfield21", // 
    new ScalarPropertyValue("1"))), "field01", // 
    new ScalarPropertyValue("text"))));
}
Also used : ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Arrays(java.util.Arrays) InputsMappingFileVariableResolver.configure(org.alien4cloud.tosca.variable.InputsMappingFileVariableResolver.configure) DataType(org.alien4cloud.tosca.model.types.DataType) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ImmutableMap(com.google.common.collect.ImmutableMap) InputsMappingFileVariableResolverConfigured(org.alien4cloud.tosca.variable.InputsMappingFileVariableResolver.InputsMappingFileVariableResolverConfigured) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) FileSystemResource(org.springframework.core.io.FileSystemResource) Test(org.junit.Test) Maps(com.google.common.collect.Maps) PropertiesYamlParser(org.alien4cloud.tosca.utils.PropertiesYamlParser) Ignore(org.junit.Ignore) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) Map(java.util.Map) Application(alien4cloud.model.application.Application) PropertyDefinitionUtils.buildPropDef(org.alien4cloud.tosca.variable.PropertyDefinitionUtils.buildPropDef) ToscaTypes(org.alien4cloud.tosca.normative.types.ToscaTypes) Assert(org.junit.Assert) Before(org.junit.Before) Resource(org.springframework.core.io.Resource) DataType(org.alien4cloud.tosca.model.types.DataType) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with DataType

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

the class ConstraintPropertyServiceTest method testMapPropertyInComplex.

// ///////////////////////////////////////////////////////////////
// Tests on complex properties validation
// ///////////////////////////////////////////////////////////////
@Test
public void testMapPropertyInComplex() 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 subPropertyValue = ImmutableMap.builder().put("aa", "bb").build();
        Object propertyValue = ImmutableMap.builder().put("myMap", subPropertyValue).build();
        // then
        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 13 with DataType

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

Example 14 with DataType

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

the class ConstraintPropertyService method checkPropertyConstraint.

/**
 * Check the constraints on an unwrapped property value (basically a string, map or list) and get events through the given consumer parameter when missing
 * properties on complex data type are found.
 * Note that the property value cannot be null and the required characteristic of the initial property definition will NOT be checked.
 *
 * @param propertyName The name of the property.
 * @param propertyValue The value of the property to check.
 * @param propertyDefinition The property definition that defines the property to check.
 * @param missingPropertyConsumer A consumer to receive events when a required property is not defined on a complex type sub-field.
 * @throws ConstraintValueDoNotMatchPropertyTypeException In case the value type doesn't match the type of the property as defined.
 * @throws ConstraintViolationException In case the value doesn't match one of the constraints defined on the property.
 */
public static void checkPropertyConstraint(String propertyName, Object propertyValue, PropertyDefinition propertyDefinition, Consumer<String> missingPropertyConsumer) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
    Object value = propertyValue;
    if (propertyValue instanceof PropertyValue) {
        value = ((PropertyValue) propertyValue).getValue();
    }
    boolean isTypeDerivedFromPrimitive = false;
    DataType dataType = null;
    String typeName = propertyDefinition.getType();
    if (!ToscaTypes.isPrimitive(typeName)) {
        dataType = ToscaContext.get(DataType.class, typeName);
        if (dataType instanceof PrimitiveDataType) {
            // the type is derived from a primitive type
            isTypeDerivedFromPrimitive = true;
        }
    }
    if (value instanceof String) {
        if (ToscaTypes.isSimple(typeName)) {
            checkSimplePropertyConstraint(propertyName, (String) value, propertyDefinition);
        } else if (isTypeDerivedFromPrimitive) {
            checkComplexPropertyDerivedFromPrimitiveTypeConstraints(propertyName, (String) value, propertyDefinition, dataType);
        } else {
            throwConstraintValueDoNotMatchPropertyTypeException("Property value is a String while the expected data type is the complex type " + propertyDefinition.getType(), propertyName, propertyDefinition.getType(), value);
        }
    } else if (value instanceof Map) {
        if (ToscaTypes.MAP.equals(typeName)) {
            checkMapPropertyConstraint(propertyName, (Map<String, Object>) value, propertyDefinition, missingPropertyConsumer);
        } else {
            checkDataTypePropertyConstraint(propertyName, (Map<String, Object>) value, propertyDefinition, missingPropertyConsumer);
        }
    } else if (value instanceof List) {
        // Range type is a specific primitive type that is actually wrapped
        if (ToscaTypes.RANGE.equals(typeName)) {
            checkRangePropertyConstraint(propertyName, (List<Object>) value, propertyDefinition);
        } else {
            checkListPropertyConstraint(propertyName, (List<Object>) value, propertyDefinition, missingPropertyConsumer);
        }
    } else {
        throw new InvalidArgumentException("Not expecting to receive constraint validation for other types than String, Map or List, but got " + value.getClass().getName());
    }
}
Also used : PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) DataType(org.alien4cloud.tosca.model.types.DataType) PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) List(java.util.List) Map(java.util.Map)

Example 15 with DataType

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

the class ParserTestUtil method mockNormativeTypes.

/**
 * Utility method to mock the acess to some normative types nodes and capabilities.
 *
 * @param repositorySearchService The repositorySearchService to mock.
 */
public static void mockNormativeTypes(ICSARRepositorySearchService repositorySearchService) {
    Csar csar = new Csar("tosca-normative-types", "1.0.0-ALIEN12");
    Mockito.when(repositorySearchService.getArchive(csar.getName(), csar.getVersion())).thenReturn(csar);
    NodeType mockedNodeRoot = new NodeType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq(NormativeTypesConstant.ROOT_NODE_TYPE), Mockito.any(Set.class))).thenReturn(mockedNodeRoot);
    RelationshipType mockedRelationshipRoot = new RelationshipType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq(NormativeTypesConstant.ROOT_RELATIONSHIP_TYPE), Mockito.any(Set.class))).thenReturn(mockedRelationshipRoot);
    CapabilityType mockedCapabilityType = new CapabilityType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq(NormativeCapabilityTypes.ROOT), Mockito.any(Set.class))).thenReturn(mockedCapabilityType);
    DataType mockedDataType = new DataType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(DataType.class), Mockito.eq(NormativeTypesConstant.ROOT_DATA_TYPE), Mockito.any(Set.class))).thenReturn(mockedDataType);
    ArtifactType mockedArtifactType = new ArtifactType();
    Mockito.when(repositorySearchService.getElementInDependencies(Mockito.eq(ArtifactType.class), Mockito.eq(NormativeTypesConstant.ROOT_ARTIFACT_TYPE), Mockito.any(Set.class))).thenReturn(mockedArtifactType);
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) ArtifactType(org.alien4cloud.tosca.model.types.ArtifactType) NodeType(org.alien4cloud.tosca.model.types.NodeType) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) DataType(org.alien4cloud.tosca.model.types.DataType)

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