diff options
author | andre.schmid <andre.schmid@est.tech> | 2022-03-07 18:48:09 +0000 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2022-03-11 16:48:13 +0000 |
commit | e5488e5e3623646125802b8ab7e12b7159a2c0d3 (patch) | |
tree | 58c896b9d2f434041cff1cafad7835dd9cd691f3 /catalog-model | |
parent | f13f58eb867c763e6ed1c3b674fd99b1081a0664 (diff) |
Support complex types in artifact properties
Adds support to complex types in artifact properties of an interface
operation implementation.
Change-Id: I7a82a3652541b35230fe4ce87bf703a1dbe72d50
Issue-ID: SDC-3899
Signed-off-by: andre.schmid <andre.schmid@est.tech>
Diffstat (limited to 'catalog-model')
3 files changed, 38 insertions, 23 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java index 4093e9d364..67894bdade 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java @@ -32,17 +32,18 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import org.jetbrains.annotations.Nullable; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.common.log.wrappers.Logger; +import org.openecomp.sdc.common.util.JsonUtils; public class ToscaListValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter { private static final Logger log = Logger.getLogger(ToscaListValueConverter.class.getName()); - private static ToscaListValueConverter listConverter = new ToscaListValueConverter(); - private JsonParser jsonParser = new JsonParser(); + private static final ToscaListValueConverter listConverter = new ToscaListValueConverter(); private ToscaListValueConverter() { } @@ -77,16 +78,8 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements return value; } } - JsonElement jsonElement = null; - try { - StringReader reader = new StringReader(value); - JsonReader jsonReader = new JsonReader(reader); - jsonReader.setLenient(true); - jsonElement = jsonParser.parse(jsonReader); - } catch (JsonSyntaxException e) { - log.debug("convertToToscaValue failed to parse json value :", e); - return null; - } + JsonElement jsonElement; + jsonElement = parseToJson(value); if (jsonElement == null || jsonElement.isJsonNull()) { log.debug("convertToToscaValue json element is null"); return null; @@ -122,7 +115,6 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements if (propertyDefinition == null) { log.debug("The property {} was not found under data type {}", propName, dataTypeDefinition.getName()); continue; - // return null; } String type = propertyDefinition.getType(); ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type); @@ -132,13 +124,17 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter valueConverter = propertyType.getValueConverter(); convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type, dataTypes); } else { - if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) { - ToscaValueConverter valueConverter = propertyType.getValueConverter(); - String json = gson.toJson(elementValue); - String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType(); - convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes); + if (JsonUtils.isEmptyJson(elementValue)) { + convValue = null; } else { - convValue = handleComplexJsonValue(elementValue); + if (ToscaPropertyType.MAP == propertyType || ToscaPropertyType.LIST == propertyType) { + ToscaValueConverter valueConverter = propertyType.getValueConverter(); + String json = gson.toJson(elementValue); + String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType(); + convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes); + } else { + convValue = handleComplexJsonValue(elementValue); + } } } } else { @@ -158,4 +154,16 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements return null; } } + + private JsonElement parseToJson(final String value) { + try { + final StringReader reader = new StringReader(value); + final JsonReader jsonReader = new JsonReader(reader); + jsonReader.setLenient(true); + return JsonParser.parseReader(jsonReader); + } catch (final JsonSyntaxException e) { + log.debug("convertToToscaValue failed to parse json value :", e); + return null; + } + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java index ee254811b1..7505d2af88 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java @@ -98,12 +98,19 @@ public class ToscaValueBaseConverter { } private Map<String, Object> handleJsonObject(final JsonElement jsonElement) { - final Map<String, Object> jsonObjectAsMap = new HashMap<>(); final JsonObject jsonObject = jsonElement.getAsJsonObject(); + if (jsonObject.entrySet().isEmpty()) { + return null; + } + final Map<String, Object> jsonObjectAsMap = new HashMap<>(); for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) { - jsonObjectAsMap.put(entry.getKey(), handleComplexJsonValue(entry.getValue())); + final Object value = handleComplexJsonValue(entry.getValue()); + if (value != null) { + jsonObjectAsMap.put(entry.getKey(), value); + } + } - return jsonObjectAsMap; + return jsonObjectAsMap.isEmpty() ? null : jsonObjectAsMap; } private List<Object> handleJsonArray(final JsonElement entry) { diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverterTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverterTest.java index 179d3cf7a1..5387b46dd6 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverterTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverterTest.java @@ -138,7 +138,7 @@ class ToscaValueBaseConverterTest { final Object objectProperty = jsonObjectAsMap.get("objectProperty"); assertTrue(objectProperty instanceof Map); final Map<String, Object> objectPropertyAsMap = (Map<String, Object>) objectProperty; - assertEquals(4, objectPropertyAsMap.keySet().size()); + assertEquals(3, objectPropertyAsMap.keySet().size()); assertTrue(objectPropertyAsMap.containsKey("string")); assertEquals(innerObject.get("string").getAsString(), objectPropertyAsMap.get("string")); assertEquals(innerObject.get("int").getAsInt(), objectPropertyAsMap.get("int")); |