Search in sources :

Example 11 with ComplexPropertyValue

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

the class FunctionEvaluatorTest method setPropertiesValues.

private void setPropertiesValues(Map<String, AbstractPropertyValue> properties, String prefix) {
    ScalarPropertyValue scalarPropValue = new ScalarPropertyValue(prefix + "scalar value");
    Map<String, Object> complex = Maps.newHashMap();
    complex.put("scalar", prefix + "complex scalar value");
    complex.put("map", Maps.newHashMap());
    ((Map) complex.get("map")).put("element_1", prefix + "element 1 value");
    ((Map) complex.get("map")).put("element_2", prefix + "element 2 value");
    complex.put("list", Lists.newArrayList(prefix + "list value 1", prefix + "list value 2"));
    ComplexPropertyValue complexPropValue = new ComplexPropertyValue(complex);
    FunctionPropertyValue getInputPropValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_INPUT, Lists.newArrayList("scalar_input"));
    FunctionPropertyValue getSecretPropValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_SECRET, Lists.newArrayList("my/path"));
    FunctionPropertyValue getScalarPropValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_PROPERTY, Lists.newArrayList("SELF", "scalar_prop"));
    FunctionPropertyValue getComplexPropValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_PROPERTY, Lists.newArrayList("SELF", "complex_prop.scalar"));
    FunctionPropertyValue getComplexPropListValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_PROPERTY, Lists.newArrayList("SELF", "complex_prop.list[1]"));
    FunctionPropertyValue getComplexPropMapValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_PROPERTY, Lists.newArrayList("SELF", "complex_prop.map.element_1"));
    ConcatPropertyValue concatPropValue = new ConcatPropertyValue();
    concatPropValue.setFunction_concat("concat");
    concatPropValue.setParameters(Lists.newArrayList(new ScalarPropertyValue("input is: "), getInputPropValue, new ScalarPropertyValue(" property is: "), getScalarPropValue));
    ConcatPropertyValue concatGetSecretPropValue = new ConcatPropertyValue();
    concatGetSecretPropValue.setFunction_concat("concat");
    concatGetSecretPropValue.setParameters(Lists.newArrayList(new ScalarPropertyValue("input is: "), getSecretPropValue));
    FunctionPropertyValue getConcatPropValue = new FunctionPropertyValue(ToscaFunctionConstants.GET_PROPERTY, Lists.newArrayList("SELF", "concat_prop"));
    ConcatPropertyValue concatGetConcatPropValue = new ConcatPropertyValue();
    concatGetConcatPropValue.setFunction_concat("concat");
    concatGetConcatPropValue.setParameters(Lists.newArrayList(new ScalarPropertyValue("get concat is: "), getConcatPropValue));
    properties.put("scalar_prop", scalarPropValue);
    properties.put("complex_prop", complexPropValue);
    properties.put("get_input_prop", getInputPropValue);
    properties.put("get_secret_prop", getSecretPropValue);
    properties.put("get_scalar_prop", getScalarPropValue);
    properties.put("get_complex_prop", getComplexPropValue);
    properties.put("get_complex_prop_list", getComplexPropListValue);
    properties.put("get_complex_prop_map", getComplexPropMapValue);
    properties.put("concat_prop", concatPropValue);
    properties.put("concat_prop_and_get_secret", concatGetSecretPropValue);
    properties.put("get_concat_prop", getConcatPropValue);
    properties.put("concat_get_concat_prop", concatGetConcatPropValue);
}
Also used : FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)

Example 12 with ComplexPropertyValue

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

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

the class ToscaParserSimpleProfileAlien120Test method testDataTypesComplexWithDefault.

@Test
public void testDataTypesComplexWithDefault() throws ParsingException {
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-data-types-complex-default.yml"));
    ParserTestUtil.displayErrors(parsingResult);
    Assert.assertEquals(2, 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.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"));
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Test(org.junit.Test)

Example 14 with ComplexPropertyValue

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

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

the class PropertyUtil method merge.

/**
 * Merge the map of two node properties recursively.
 * @param source    the source map
 * @param target    the target map
 * @param overrideNull  a boolean value for override the null value of target
 * @param untouched the list of unmodified key //TODO not used and it needs to be reorganised
 * @return  a merged map
 */
public static Map<String, AbstractPropertyValue> merge(Map<String, AbstractPropertyValue> source, Map<String, AbstractPropertyValue> target, boolean overrideNull, Set<String> untouched) {
    if (target == null || target.isEmpty()) {
        return source;
    }
    for (Map.Entry<String, AbstractPropertyValue> entry : safe(source).entrySet()) {
        String sourceKey = entry.getKey();
        AbstractPropertyValue sourceValue = entry.getValue();
        AbstractPropertyValue targetValue = target.get(sourceKey);
        if ((overrideNull && targetValue == null) || !target.containsKey(sourceKey)) {
            target.put(sourceKey, sourceValue);
        } else if (target.containsKey(sourceKey) && targetValue != null) {
            if (sourceValue instanceof ComplexPropertyValue) {
                Map<String, Object> mergedMap = mergeMap(((ComplexPropertyValue) sourceValue).getValue(), ((ComplexPropertyValue) targetValue).getValue(), overrideNull, untouched);
                target.put(sourceKey, new ComplexPropertyValue(mergedMap));
            } else if (sourceValue instanceof Map) {
                Map<String, Object> mergedMap = mergeMap((Map<String, Object>) sourceValue, (Map<String, Object>) targetValue, overrideNull, untouched);
                target.put(sourceKey, (AbstractPropertyValue) mergedMap);
            }
        }
    }
    return target;
}
Also used : ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

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