ToscaTypeConverter { @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; return new ComplexPropertyValue(map); } else { throw new IllegalStateException( "Property value: expected type [" + propertyDefinition.getType() + "] but got [" + resolvedPropertyValue.getClass().getName() + "]"); } } } ToscaTypeConverter(DataTypesFetcher.DataTypeFinder dataTypeFinder); @SuppressWarnings("unchecked") PropertyValue toPropertyValue(Object resolvedPropertyValue, PropertyDefinition propertyDefinition);  }
@Test public void convert_time_to_property_value() throws Exception { PropertyDefinition propertyDefinition = new PropertyDefinition(); propertyDefinition.setType(ToscaTypes.TIME); PropertyValue propertyValue = converter.toPropertyValue("2 d", propertyDefinition); Object time = ToscaTypes.fromYamlTypeName(propertyDefinition.getType()).parse(propertyValue.getValue().toString()); assertThat(time).isInstanceOf(Time.class); assertThat(time).isEqualTo(new Time(2, TimeUnit.D)); }
@Test public void convert_version_to_property_value() throws Exception { PropertyDefinition propertyDefinition = new PropertyDefinition(); propertyDefinition.setType(ToscaTypes.VERSION); PropertyValue propertyValue = converter.toPropertyValue("3.4-SNAPSHOT", propertyDefinition); Object version = ToscaTypes.fromYamlTypeName(propertyDefinition.getType()).parse(propertyValue.getValue().toString()); assertThat(version).isInstanceOf(Version.class); assertThat(version).isEqualTo(new Version("3.4-SNAPSHOT")); }
@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"); }
@Test public void convert_version() throws Exception { PropertyDefinition propertyDefinition = new PropertyDefinition(); propertyDefinition.setType(ToscaTypes.VERSION); Object value = converter.toPropertyValue("3.4-SNAPSHOT", propertyDefinition); assertThat(value).isInstanceOf(ScalarPropertyValue.class); assertThat(value).isEqualTo(new ScalarPropertyValue("3.4-SNAPSHOT")); }