diff options
author | andre.schmid <andre.schmid@est.tech> | 2022-02-09 19:00:35 +0000 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2022-03-11 15:25:28 +0000 |
commit | f13f58eb867c763e6ed1c3b674fd99b1081a0664 (patch) | |
tree | c0ccc70b8fdf4362bce26efa0a5bb1c435f98575 /catalog-model/src | |
parent | 767b122ea026099e17a2ffde30e6718d2abf150f (diff) |
Support complex types in interface operation inputs
Issue-ID: SDC-3897
Change-Id: Ieac2d74ad340de1d9f6e4cd3ac830e2ec8c35d5b
Signed-off-by: andre.schmid <andre.schmid@est.tech>
Signed-off-by: vasraz <vasyl.razinkov@est.tech>
Signed-off-by: MichaelMorris <michael.morris@est.tech>
Diffstat (limited to 'catalog-model/src')
4 files changed, 176 insertions, 119 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java index 1d0354f749..9ee287b333 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java @@ -43,8 +43,7 @@ import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; public class ToscaMapValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter { private static final Logger log = Logger.getLogger(ToscaMapValueConverter.class.getName()); - private static ToscaMapValueConverter mapConverter = new ToscaMapValueConverter(); - private JsonParser jsonParser = new JsonParser(); + private static final ToscaMapValueConverter mapConverter = new ToscaMapValueConverter(); private ToscaMapValueConverter() { } @@ -56,7 +55,7 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T @Override public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) { if (value == null) { - return value; + return null; } try { ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType); @@ -85,12 +84,12 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T return value; } } - JsonElement jsonElement = null; + JsonElement jsonElement; try { StringReader reader = new StringReader(value); JsonReader jsonReader = new JsonReader(reader); jsonReader.setLenient(true); - jsonElement = jsonParser.parse(jsonReader); + jsonElement = JsonParser.parseReader(jsonReader); } catch (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 b0598971df..ee254811b1 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 @@ -26,11 +26,9 @@ import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; @@ -82,74 +80,54 @@ public class ToscaValueBaseConverter { return result; } - public Object handleComplexJsonValue(JsonElement elementValue) { - Object jsonValue = null; - Map<String, Object> value = new HashMap<>(); - if (elementValue.isJsonObject()) { - JsonObject jsonOb = elementValue.getAsJsonObject(); - Set<Entry<String, JsonElement>> entrySet = jsonOb.entrySet(); - Iterator<Entry<String, JsonElement>> iteratorEntry = entrySet.iterator(); - while (iteratorEntry.hasNext()) { - Entry<String, JsonElement> entry = iteratorEntry.next(); - if (entry.getValue().isJsonArray()) { - List<Object> array = handleJsonArray(entry.getValue()); - value.put(entry.getKey(), array); - } else { - Object object; - if (entry.getValue().isJsonPrimitive()) { - object = json2JavaPrimitive(entry.getValue().getAsJsonPrimitive()); - } else { - object = handleComplexJsonValue(entry.getValue()); - } - value.put(entry.getKey(), object); - } - } - jsonValue = value; - } else { - if (elementValue.isJsonArray()) { - jsonValue = handleJsonArray(elementValue); - } else { - if (elementValue.isJsonPrimitive()) { - jsonValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive()); - } else { - log.debug("not supported json type "); - } - } + public Object handleComplexJsonValue(final JsonElement jsonElement) { + if (jsonElement.isJsonNull()) { + return null; + } + if (jsonElement.isJsonObject()) { + return handleJsonObject(jsonElement); } - return jsonValue; + if (jsonElement.isJsonArray()) { + return handleJsonArray(jsonElement); + } + if (jsonElement.isJsonPrimitive()) { + return json2JavaPrimitive(jsonElement.getAsJsonPrimitive()); + } + log.debug("JSON type '{}' not supported", jsonElement); + return null; } - private List<Object> handleJsonArray(JsonElement entry) { - List<Object> array = new ArrayList<>(); - JsonArray jsonArray = entry.getAsJsonArray(); - Iterator<JsonElement> iterator = jsonArray.iterator(); - while (iterator.hasNext()) { - Object object; - JsonElement element = iterator.next(); - if (element.isJsonPrimitive()) { - object = json2JavaPrimitive(element.getAsJsonPrimitive()); - } else { - object = handleComplexJsonValue(element); - } - array.add(object); + private Map<String, Object> handleJsonObject(final JsonElement jsonElement) { + final Map<String, Object> jsonObjectAsMap = new HashMap<>(); + final JsonObject jsonObject = jsonElement.getAsJsonObject(); + for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) { + jsonObjectAsMap.put(entry.getKey(), handleComplexJsonValue(entry.getValue())); } - return array; + return jsonObjectAsMap; } - public Object json2JavaPrimitive(JsonPrimitive prim) { - if (prim.isBoolean()) { - return prim.getAsBoolean(); - } else if (prim.isString()) { - return prim.getAsString(); - } else if (prim.isNumber()) { - String strRepesentation = prim.getAsString(); - if (strRepesentation.contains(".")) { - return prim.getAsDouble(); - } else { - return prim.getAsInt(); + private List<Object> handleJsonArray(final JsonElement entry) { + final List<Object> jsonAsArray = new ArrayList<>(); + final JsonArray jsonArray = entry.getAsJsonArray(); + for (final JsonElement jsonElement : jsonArray) { + jsonAsArray.add(handleComplexJsonValue(jsonElement)); + } + return jsonAsArray; + } + + public Object json2JavaPrimitive(final JsonPrimitive jsonPrimitive) { + if (jsonPrimitive.isBoolean()) { + return jsonPrimitive.getAsBoolean(); + } + if (jsonPrimitive.isString()) { + return jsonPrimitive.getAsString(); + } + if (jsonPrimitive.isNumber()) { + if (jsonPrimitive.getAsString().contains(".")) { + return jsonPrimitive.getAsDouble(); } - } else { - throw new IllegalStateException(); + return jsonPrimitive.getAsInt(); } + throw new IllegalStateException(String.format("JSON primitive not supported: %s", jsonPrimitive)); } } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverterTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverterTest.java index cf2172d2b0..5d933ea3f6 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverterTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverterTest.java @@ -20,31 +20,36 @@ package org.openecomp.sdc.be.model.tosca.converters; -import com.google.gson.JsonObject; -import org.junit.Before; -import org.junit.Test; -import org.openecomp.sdc.be.model.DataTypeDefinition; -import org.openecomp.sdc.be.model.PropertyDefinition; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import com.google.gson.JsonObject; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.PropertyDefinition; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -public class DataTypePropertyConverterTest { +class DataTypePropertyConverterTest { - private static final String EMPTY_JSON_STR = "{}"; - public static final String PROPERTY2_DEFAULT = "{\"prop1\":\"def1\",\"prop3\":\"def3\"}"; - private DataTypePropertyConverter testInstance = DataTypePropertyConverter.getInstance(); - private Map<String, DataTypeDefinition> dataTypes; - private DataTypeDefinition noDefaultValue, dataType1, dataType2, dataType3; - private PropertyDefinition prop1, prop2, prop3, noDefaultProp; + private static final DataTypePropertyConverter dataTypePropertyConverter = DataTypePropertyConverter.getInstance(); + private static final String PROPERTY2_DEFAULT = "{\"prop1\":\"def1\",\"prop3\":\"def3\"}"; - @Before - public void setUp() throws Exception { + private Map<String, DataTypeDefinition> dataTypes; + private DataTypeDefinition noDefaultValue; + private DataTypeDefinition dataType2; + private DataTypeDefinition dataType3; + private PropertyDefinition prop1; + private PropertyDefinition prop2; + private PropertyDefinition prop3; + private PropertyDefinition noDefaultProp; + + @BeforeEach + void setUp() { dataTypes = new HashMap<>(); prop1 = new PropertyDefinition(); @@ -65,7 +70,7 @@ public class DataTypePropertyConverterTest { noDefaultValue = new DataTypeDefinition(); noDefaultValue.setProperties(Collections.singletonList(noDefaultProp)); - dataType1 = new DataTypeDefinition(); + DataTypeDefinition dataType1 = new DataTypeDefinition(); dataType1.setProperties(Arrays.asList(prop1, prop3)); dataType2 = new DataTypeDefinition(); @@ -82,51 +87,51 @@ public class DataTypePropertyConverterTest { } @Test - public void testGetPropertyDefaultValuesRec_dataTypeNotExist() throws Exception { - String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("someType", dataTypes); + void testGetPropertyDefaultValuesRec_dataTypeNotExist() { + String defaultValue = dataTypePropertyConverter.getDataTypePropertiesDefaultValuesRec("someType", dataTypes); assertNull(defaultValue); } @Test - public void testGetPropertyDefaultValuesRec_NoDefaultValue() throws Exception { - String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("noDefault", dataTypes); + void testGetPropertyDefaultValuesRec_NoDefaultValue() { + String defaultValue = dataTypePropertyConverter.getDataTypePropertiesDefaultValuesRec("noDefault", dataTypes); assertNull(defaultValue); } @Test - public void testGetPropertyDefaultValuesRec() throws Exception { - String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType1", dataTypes); + void testGetPropertyDefaultValuesRec() { + String defaultValue = dataTypePropertyConverter.getDataTypePropertiesDefaultValuesRec("dataType1", dataTypes); assertEquals(PROPERTY2_DEFAULT, defaultValue); } @Test - public void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType_derivedDataTypeHasNoDefaults() throws Exception { + void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType_derivedDataTypeHasNoDefaults() { dataType2.setDerivedFrom(noDefaultValue); - String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes); + String defaultValue = dataTypePropertyConverter.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes); assertNull(defaultValue); } @Test - public void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType() throws Exception { - String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes); + void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType() { + String defaultValue = dataTypePropertyConverter.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes); assertEquals(PROPERTY2_DEFAULT, defaultValue); } @Test - public void testGetPropertyDefaultValuesRec_defaultFromDataTypesOfProperties_dataTypeOfPropertyHasNoDefault() throws Exception { + void testGetPropertyDefaultValuesRec_defaultFromDataTypesOfProperties_dataTypeOfPropertyHasNoDefault() { dataType3.getProperties().get(0).setType(noDefaultValue.getName()); - String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType3", dataTypes); + String defaultValue = dataTypePropertyConverter.getDataTypePropertiesDefaultValuesRec("dataType3", dataTypes); assertNull(defaultValue); } @Test - public void testGetPropertyDefaultValuesRec_defaultFromDataTypesOfProperties() throws Exception { - String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType3", dataTypes); + void testGetPropertyDefaultValuesRec_defaultFromDataTypesOfProperties() { + String defaultValue = dataTypePropertyConverter.getDataTypePropertiesDefaultValuesRec("dataType3", dataTypes); assertEquals("{\"prop2\":" + PROPERTY2_DEFAULT + "}", defaultValue);//data type 3 has property prop2 which has a data type with property prop1 which has a default value } @Test - public void testMergeDefaultValues_allDefaultValuesAreOverridden() throws Exception { + void testMergeDefaultValues_allDefaultValuesAreOverridden() { JsonObject value = new JsonObject(); value.addProperty(noDefaultProp.getName(), "override1"); @@ -144,12 +149,12 @@ public class DataTypePropertyConverterTest { String valBeforeMerge = value.toString(); - testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); + dataTypePropertyConverter.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); assertEquals(valBeforeMerge, value.toString()); } @Test - public void testMergeDefaultValues() throws Exception { + void testMergeDefaultValues() { JsonObject value = new JsonObject(); value.addProperty(noDefaultProp.getName(), "override1"); @@ -158,23 +163,23 @@ public class DataTypePropertyConverterTest { value.add(prop2.getName(), prop1Val); - testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); + dataTypePropertyConverter.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); assertEquals("{\"noDefaultProp\":\"override1\",\"prop2\":{\"prop1\":\"prop1Override\",\"prop3\":\"def3\"}}", value.toString());//expect to merge prop 3 default as it was not overridden } @Test - public void testMergeDefaultValues_mergeAll() throws Exception { + void testMergeDefaultValues_mergeAll() { JsonObject value = new JsonObject(); - testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); + dataTypePropertyConverter.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); assertEquals("{\"prop2\":" + PROPERTY2_DEFAULT + "}", value.toString());//expect to merge prop 3 default as it was not overridden } @Test - public void testMergeDefaultValues_doNotAddDefaultsForGetInputValues() throws Exception { + void testMergeDefaultValues_doNotAddDefaultsForGetInputValues() { JsonObject getInputValue = new JsonObject(); getInputValue.addProperty("get_input", "in1"); @@ -182,13 +187,13 @@ public class DataTypePropertyConverterTest { JsonObject value = new JsonObject(); value.add(prop2.getName(), getInputValue); - testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); + dataTypePropertyConverter.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); assertEquals("{\"prop2\":{\"get_input\":\"in1\"}}", value.toString()); } @Test - public void testMergeDefaultValues_doNotAddDefaultsForGetInputInnerValues() throws Exception { + void testMergeDefaultValues_doNotAddDefaultsForGetInputInnerValues() { JsonObject getInputValue = new JsonObject(); getInputValue.addProperty("get_input", "in1"); @@ -198,7 +203,7 @@ public class DataTypePropertyConverterTest { JsonObject value = new JsonObject(); value.add(prop2.getName(), prop1Val); - testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); + dataTypePropertyConverter.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); assertEquals("{\"prop2\":{\"prop1\":{\"get_input\":\"in1\"},\"prop3\":\"def3\"}}", value.toString()); 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 0923f8e49f..179d3cf7a1 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 @@ -20,41 +20,46 @@ package org.openecomp.sdc.be.model.tosca.converters; -import com.google.gson.JsonPrimitive; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.jupiter.api.Test; - +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; import java.util.HashMap; import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; -public class ToscaValueBaseConverterTest { +class ToscaValueBaseConverterTest { - private ToscaValueBaseConverter converter = new ToscaValueBaseConverter(); + private final ToscaValueBaseConverter converter = new ToscaValueBaseConverter(); @Test - public void testJson2JavaPrimitive() throws Exception { + void testJson2JavaPrimitive() { JsonPrimitive prim1 = new JsonPrimitive(Boolean.FALSE); Object res1 = converter.json2JavaPrimitive(prim1); assertFalse((Boolean)res1); JsonPrimitive prim2 = new JsonPrimitive("Test"); Object res2 = converter.json2JavaPrimitive(prim2); - assertTrue(res2.equals("Test")); + assertEquals("Test", res2); JsonPrimitive prim3 = new JsonPrimitive(3); Object res3 = converter.json2JavaPrimitive(prim3); - assertTrue((Integer)res3 == 3); + assertEquals(3, (int) (Integer) res3); JsonPrimitive prim4 = new JsonPrimitive(3.6); Object res4 = converter.json2JavaPrimitive(prim4); - assertTrue((Double)res4 == 3.6); + assertEquals(3.6, (Double) res4); } @Test - public void testIsEmptyObjectValue() throws Exception { + void testIsEmptyObjectValue() { boolean res1 = ToscaValueBaseConverter.isEmptyObjectValue(null); assertTrue(res1); @@ -70,4 +75,74 @@ public class ToscaValueBaseConverterTest { boolean res5 = ToscaValueBaseConverter.isEmptyObjectValue("test"); assertFalse(res5); } + + @Test + void testHandleComplexJsonValue() { + JsonObject innerObject = new JsonObject(); + innerObject.addProperty("string", "stringValue"); + innerObject.addProperty("int", 1); + innerObject.addProperty("float", 1.1); + innerObject.add("null", null); + + JsonArray jsonArray1 = new JsonArray(); + jsonArray1.add(innerObject); + jsonArray1.add(innerObject); + jsonArray1.add(innerObject); + + JsonArray jsonArray2 = new JsonArray(); + jsonArray2.add("value0"); + jsonArray2.add("value1"); + jsonArray2.add("value2"); + + JsonObject jsonObject = new JsonObject(); + jsonObject.add("objectProperty", innerObject); + jsonObject.add("arrayProperty1", jsonArray1); + jsonObject.add("arrayProperty2", jsonArray2); + jsonObject.addProperty("stringProperty", "stringPropertyValue"); + + final Object resultingObject = converter.handleComplexJsonValue(jsonObject); + assertTrue(resultingObject instanceof Map); + final Map<String, Object> jsonObjectAsMap = (Map<String, Object>) resultingObject; + assertEquals(4, jsonObjectAsMap.keySet().size()); + assertTrue(jsonObjectAsMap.containsKey("objectProperty")); + assertTrue(jsonObjectAsMap.containsKey("arrayProperty1")); + assertTrue(jsonObjectAsMap.containsKey("arrayProperty2")); + assertTrue(jsonObjectAsMap.containsKey("stringProperty")); + + final String stringProperty = (String) jsonObjectAsMap.get("stringProperty"); + assertEquals(jsonObject.get("stringProperty").getAsString(), stringProperty); + + final Object arrayProperty1 = jsonObjectAsMap.get("arrayProperty1"); + assertTrue(arrayProperty1 instanceof List); + final List<Object> arrayProperty1AsList = (List<Object>) arrayProperty1; + assertEquals(3, arrayProperty1AsList.size()); + for (int i = 0; i < arrayProperty1AsList.size(); i++) { + final Object actualElement = arrayProperty1AsList.get(i); + assertTrue(actualElement instanceof Map); + final Map<String, Object> actualElementAsMap = (Map<String, Object>) actualElement; + final JsonObject expectedJsonObject = jsonArray1.get(i).getAsJsonObject(); + assertEquals(expectedJsonObject.get("string").getAsString(), actualElementAsMap.get("string")); + assertEquals(expectedJsonObject.get("int").getAsInt(), actualElementAsMap.get("int")); + assertEquals(expectedJsonObject.get("float").getAsDouble(), actualElementAsMap.get("float")); + assertNull(actualElementAsMap.get("null")); + } + + final Object arrayProperty2 = jsonObjectAsMap.get("arrayProperty2"); + assertTrue(arrayProperty2 instanceof List); + final List<String> arrayProperty2AsList = (List<String>) arrayProperty2; + assertEquals(3, arrayProperty2AsList.size()); + for (int i = 0; i < arrayProperty2AsList.size(); i++) { + assertEquals("value" + i, arrayProperty2AsList.get(i)); + } + + final Object objectProperty = jsonObjectAsMap.get("objectProperty"); + assertTrue(objectProperty instanceof Map); + final Map<String, Object> objectPropertyAsMap = (Map<String, Object>) objectProperty; + assertEquals(4, objectPropertyAsMap.keySet().size()); + assertTrue(objectPropertyAsMap.containsKey("string")); + assertEquals(innerObject.get("string").getAsString(), objectPropertyAsMap.get("string")); + assertEquals(innerObject.get("int").getAsInt(), objectPropertyAsMap.get("int")); + assertEquals(innerObject.get("float").getAsDouble(), objectPropertyAsMap.get("float")); + assertNull(objectPropertyAsMap.get("null")); + } } |