From 577c692dc7a78d19c5bcecdf0387bbabc1dadcd6 Mon Sep 17 00:00:00 2001 From: MichaelMorris Date: Thu, 26 May 2022 09:41:11 +0100 Subject: Fix error handling valid yaml property values Signed-off-by: MichaelMorris Issue-ID: SDC-4022 Change-Id: Ic30f29e350d47d29c00744271e1e81b08db3edb9 --- .../java/org/openecomp/sdc/be/tosca/PropertyConvertor.java | 6 ++++++ .../org/openecomp/sdc/be/tosca/PropertyConvertorTest.java | 13 +++++++++++++ 2 files changed, 19 insertions(+) (limited to 'catalog-be') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java index 5d3fe1bf77..5bb3d116f8 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.tosca; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; import fj.data.Either; @@ -48,6 +49,7 @@ import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; import org.springframework.stereotype.Service; +import org.yaml.snakeyaml.Yaml; @Service public class PropertyConvertor { @@ -176,6 +178,10 @@ public class PropertyConvertor { .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue); } return convertedValue; + + } catch (JsonParseException e) { + log.trace("{} not parsable as JSON. Convert as YAML instead", value); + return new Yaml().load(value); } catch (Exception e) { log.debug("convertToToscaValue failed to parse json value :", e); return null; diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java index 36c9eb8288..cd756ce49d 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java @@ -183,5 +183,18 @@ class PropertyConvertorTest { propertyConvertor.convertProperty(Collections.emptyMap(), property, PropertyConvertor.PropertyType.PROPERTY); assertEquals("/", toscaProperty.getDefaultp()); } + + @Test + void convertPropertyWithYamlValue() { + final PropertyDefinition property = new PropertyDataDefinitionBuilder() + .setDefaultValue("{concat: [ get_input: service_name, '--', 'WirelessService']}") + .setType(ToscaPropertyType.STRING.getType()) + .build(); + final ToscaProperty toscaProperty = + propertyConvertor.convertProperty(Collections.emptyMap(), property, PropertyConvertor.PropertyType.PROPERTY); + assertTrue(toscaProperty.getDefaultp() instanceof Map); + assertTrue(((Map)toscaProperty.getDefaultp()).get("concat") instanceof List); + assertEquals(3, ((List)((Map)toscaProperty.getDefaultp()).get("concat")).size()); + } } -- cgit 1.2.3-korg