Search in sources :

Example 1 with ComplexPropertyValue

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

the class ToscaTypeConverter method toPropertyValue.

@SuppressWarnings("unchecked")
public PropertyValue toPropertyValue(Object resolvedPropertyValue, PropertyDefinition propertyDefinition) {
    if (resolvedPropertyValue == null) {
        return null;
    }
    if (ToscaTypes.isSimple(propertyDefinition.getType())) {
        return new ScalarPropertyValue(resolvedPropertyValue.toString());
    }
    switch(propertyDefinition.getType()) {
        case ToscaTypes.MAP:
            if (resolvedPropertyValue instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) resolvedPropertyValue;
                Map<String, Object> resultMap = Maps.newHashMap();
                map.forEach((key, value) -> resultMap.put(key, toPropertyValue(value, propertyDefinition.getEntrySchema())));
                return new ComplexPropertyValue(resultMap);
            } else {
                throw new IllegalStateException("Property value: expected type [" + Map.class.getSimpleName() + "] but got [" + resolvedPropertyValue.getClass().getName() + "]");
            }
        case ToscaTypes.LIST:
            if (resolvedPropertyValue instanceof Collection) {
                List list = (List) resolvedPropertyValue;
                List resultList = new LinkedList();
                for (Object item : list) {
                    resultList.add(toPropertyValue(item, propertyDefinition.getEntrySchema()));
                }
                return new ListPropertyValue(resultList);
            } else {
                throw new IllegalStateException("Property value: expected type [" + Collection.class.getSimpleName() + "] but got [" + resolvedPropertyValue.getClass().getName() + "]");
            }
        default:
            DataType dataType = findDataType(propertyDefinition.getType());
            if (dataType == null) {
                throw new NotFoundException("Data type  [" + propertyDefinition.getType() + "] cannot be found");
            }
            if (dataType.isDeriveFromSimpleType()) {
                return new ScalarPropertyValue(resolvedPropertyValue.toString());
            } else if (resolvedPropertyValue instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) resolvedPropertyValue;
                /*
                 * Map<String, Object> resultMap = Maps.newHashMap();
                 * 
                 * map.forEach((key, value) -> {
                 * PropertyDefinition entryDefinition = dataType.getProperties().get(key);
                 * if(entryDefinition == null){
                 * throw new IllegalStateException("DataType [" + propertyDefinition.getType() + "] does not contains any definition for entry [" + key + "]");
                 * }
                 * resultMap.put(key, toPropertyValue(value, entryDefinition));
                 * });
                 * return new ComplexPropertyValue(resultMap);
                 */
                return new ComplexPropertyValue(map);
            } else {
                throw new IllegalStateException("Property value: expected type [" + propertyDefinition.getType() + "] but got [" + resolvedPropertyValue.getClass().getName() + "]");
            }
    }
}
Also used : NotFoundException(alien4cloud.exception.NotFoundException) LinkedList(java.util.LinkedList) Collection(java.util.Collection) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) DataType(org.alien4cloud.tosca.model.types.DataType) PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) List(java.util.List) LinkedList(java.util.LinkedList) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map)

Example 2 with ComplexPropertyValue

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

the class InputsMappingFileVariableResolverTest method check_uber_input_can_be_parsed.

@Ignore("Update when ToscaTypeConverter behavior is clearly defined")
@Test
public // Bad test
void check_uber_input_can_be_parsed() throws Exception {
    inputsPropertyDefinitions.put("uber_input", buildPropDef("datatype.uber"));
    /*
         * 
         * uber_input:
         * simple_var:
         * int_field: ${int_variable}
         * float_field: ${float_variable}
         * string_field: ${string_variable}
         * concat_field: ${int_variable}${float_variable}
         * simple_static:
         * int_field: 51
         * float_field: 16.64
         * string_field: "leffe"
         * complex:
         * complex_with_list: ${complex_with_list}
         * int_list:
         * - ${int_variable}
         * - ${int_variable}
         * float_list:
         * - ${float_variable}
         * - ${float_variable}
         * mix_list:
         * - ${int_variable}
         * - ${float_variable}
         * - ${string_variable}
         * static_mix_list:
         * - "jenlain"
         * - 16.64
         * - "kwak"
         * complex_with_var_in_leaf: ${complex_with_var_in_leaf}
         * 
         */
    inputsMappingFileVariableResolverConfigured.customConverter(new ToscaTypeConverter((concreteType, id) -> {
        DataType dataType = null;
        switch(id) {
            case "datatype.uber":
                dataType = new DataType();
                dataType.setDeriveFromSimpleType(false);
                dataType.setProperties(// 
                ImmutableMap.of(// 
                "simple_var", // 
                buildPropDef("datatype.uber.simple_var"), // 
                "complex", // 
                buildPropDef("datatype.uber.complex"), // 
                "simple_static", // 
                buildPropDef("datatype.uber.simple_static"), // 
                "complex_with_var_in_leaf", // 
                buildPropDef("datatype.uber.complex_with_var_in_leaf")));
                break;
            case "datatype.uber.simple_var":
                dataType = new DataType();
                dataType.setDeriveFromSimpleType(false);
                dataType.setProperties(// 
                ImmutableMap.of(// 
                "int_field", // 
                buildPropDef(ToscaTypes.INTEGER), // 
                "float_field", // 
                buildPropDef(ToscaTypes.FLOAT), // 
                "string_field", // 
                buildPropDef(ToscaTypes.STRING), // 
                "concat_field", // 
                buildPropDef(ToscaTypes.STRING)));
                break;
            case "datatype.uber.simple_static":
                dataType = new DataType();
                dataType.setDeriveFromSimpleType(false);
                dataType.setProperties(// 
                ImmutableMap.of(// 
                "int_field", // 
                buildPropDef(ToscaTypes.INTEGER), // 
                "float_field", // 
                buildPropDef(ToscaTypes.FLOAT), // 
                "string_field", // 
                buildPropDef(ToscaTypes.STRING)));
                break;
            case "datatype.uber.complex":
                dataType = new DataType();
                dataType.setDeriveFromSimpleType(false);
                dataType.setProperties(// 
                ImmutableMap.<String, PropertyDefinition>builder().put("complex_with_list", buildPropDef("datatype.uber.complex.complex_with_list")).put("int_list", // 
                buildPropDef(ToscaTypes.LIST, ToscaTypes.FLOAT)).put("float_list", // 
                buildPropDef(ToscaTypes.LIST, ToscaTypes.STRING)).put("mix_list", // 
                buildPropDef(ToscaTypes.LIST, ToscaTypes.STRING)).put("static_mix_list", buildPropDef(ToscaTypes.LIST, ToscaTypes.STRING)).build());
                break;
            case "datatype.uber.complex.complex_with_list":
                dataType = new DataType();
                dataType.setDeriveFromSimpleType(false);
                dataType.setProperties(// NEED TO BE
                ImmutableMap.<String, PropertyDefinition>builder().put("subfield1", buildPropDef(ToscaTypes.STRING)).put("subfield2", buildPropDef(ToscaTypes.MAP, buildPropDef(ToscaTypes.LIST, ToscaTypes.STRING))).build());
                break;
            case "datatype.uber.complex_with_var_in_leaf":
                dataType = new DataType();
                dataType.setDeriveFromSimpleType(false);
                dataType.setProperties(// 
                ImmutableMap.of("complex", // 
                buildPropDef(ToscaTypes.MAP, ToscaTypes.STRING)));
                break;
        }
        return dataType;
    }));
    Map<String, PropertyValue> inputsMappingFileResolved = resolve("src/test/resources/alien/variables/inputs_mapping_uber.yml");
    assertThat(inputsMappingFileResolved).containsOnlyKeys("uber_input");
    assertThat(inputsMappingFileResolved.get("uber_input")).isInstanceOf(ComplexPropertyValue.class);
    ComplexPropertyValue uberInput = (ComplexPropertyValue) inputsMappingFileResolved.get("uber_input");
    assertThat(sub(ScalarPropertyValue.class, uberInput, "simple_var", "string_field").getValue()).isEqualTo("text");
    assertThat(sub(ScalarPropertyValue.class, uberInput, "simple_var", "concat_field").getValue()).isEqualTo("13.14");
    assertThat(sub(ScalarPropertyValue.class, uberInput, "simple_static", "int_field").getValue()).isEqualTo("51");
    assertThat(sub(ScalarPropertyValue.class, uberInput, "simple_static", "float_field").getValue()).isEqualTo("16.64");
    assertThat(sub(ScalarPropertyValue.class, uberInput, "simple_static", "string_field").getValue()).isEqualTo("leffe");
    assertThat(sub(ListPropertyValue.class, uberInput, "complex", "int_list").getValue()).isEqualTo(Arrays.asList(new ScalarPropertyValue("1"), new ScalarPropertyValue("1")));
    assertThat(sub(ListPropertyValue.class, uberInput, "complex", "float_list").getValue()).hasSize(2);
    assertThat(sub(ListPropertyValue.class, uberInput, "complex", "mix_list").getValue()).hasSize(3);
    assertThat(sub(ListPropertyValue.class, uberInput, "complex", "static_mix_list").getValue()).hasSize(3);
    assertThat(sub(ListPropertyValue.class, uberInput, "complex", "complex_with_list", "subfield2", "sublist").getValue()).hasSize(3);
    assertThat(sub(ScalarPropertyValue.class, uberInput, "complex_with_var_in_leaf", "complex", "subfield").getValue()).isEqualTo("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) ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ComplexPropertyValue

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

the class InputsMappingFileVariableResolverTest method should_comply_with_property_definition_even_if_does_not_match_reality.

@Test
public // Good Test
void should_comply_with_property_definition_even_if_does_not_match_reality() throws Exception {
    // Given: a simplified inputs definition
    inputsPropertyDefinitions.put("complex_input", buildPropDef(ToscaTypes.MAP, ToscaTypes.STRING));
    // When
    Map<String, PropertyValue> inputsMappingFileResolved = resolve("src/test/resources/alien/variables/inputs_mapping_without_variable.yml");
    // Then: match the simplified inputs definition
    assertThat(inputsMappingFileResolved.get("complex_input")).isInstanceOf(ComplexPropertyValue.class);
    assertThat(inputsMappingFileResolved.get("complex_input")).isEqualTo(new ComplexPropertyValue(ImmutableMap.of("sub1", new ScalarPropertyValue(ImmutableMap.of("subfield11", "11", "subfield12", "12").toString()), "sub2", new ScalarPropertyValue(ImmutableMap.of("subfield21", "21").toString()), "field01", new ScalarPropertyValue("01"))));
}
Also used : 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) Test(org.junit.Test)

Example 4 with ComplexPropertyValue

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

the class ToscaTypeConverterTest method convert_complex_data_type_to_property_value.

@Test
public void convert_complex_data_type_to_property_value() throws Exception {
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType("alien.nodes.test.ComplexDataType");
    PropertyValue propertyValue = converter.toPropertyValue(ImmutableMap.of("nested", "nested value", "nested_array", Arrays.asList("item1", "item2", "item3"), "nested_map", ImmutableMap.of("key1", "value1", "key2", "value2")), propertyDefinition);
    assertThat(propertyValue).isInstanceOf(ComplexPropertyValue.class);
    ComplexPropertyValue complexPropertyValue = (ComplexPropertyValue) propertyValue;
    assertThat(complexPropertyValue.getValue().get("nested_map")).isEqualTo(ImmutableMap.of("key1", "value1", "key2", "value2"));
    assertThat(complexPropertyValue.getValue().get("nested_array")).isEqualTo(Arrays.asList("item1", "item2", "item3"));
    assertThat(complexPropertyValue.getValue().get("nested")).isEqualTo("nested value");
}
Also used : ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 5 with ComplexPropertyValue

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

Aggregations

ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)15 Map (java.util.Map)10 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)9 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)8 Test (org.junit.Test)8 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)7 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)7 List (java.util.List)5 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)5 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)4 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)4 DataType (org.alien4cloud.tosca.model.types.DataType)4 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)3 NodeType (org.alien4cloud.tosca.model.types.NodeType)3 Application (alien4cloud.model.application.Application)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Maps (com.google.common.collect.Maps)2 Arrays (java.util.Arrays)2 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)2 ToscaTypes (org.alien4cloud.tosca.normative.types.ToscaTypes)2