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);
}
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"))));
}
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"));
}
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"));
}
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;
}
Aggregations