summaryrefslogtreecommitdiffstats
path: root/catalog-model/src
diff options
context:
space:
mode:
Diffstat (limited to 'catalog-model/src')
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java23
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java34
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java32
-rw-r--r--catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverterTest.java60
4 files changed, 107 insertions, 42 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 67894bdade..f449f7841b 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
@@ -17,6 +17,7 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
+
package org.openecomp.sdc.be.model.tosca.converters;
import com.google.gson.JsonArray;
@@ -32,7 +33,6 @@ 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;
@@ -93,12 +93,11 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements
final boolean isScalarF = isScalar;
final ToscaValueConverter innerConverterFinal = innerConverter;
asJsonArray.forEach(e -> {
- Object convertedValue = null;
+ Object convertedValue;
if (isScalarF) {
- if (e.isJsonPrimitive()) {
- String jsonAsString = e.getAsString();
- log.debug("try to convert scalar value {}", jsonAsString);
- convertedValue = innerConverterFinal.convertToToscaValue(jsonAsString, innerType, dataTypes);
+ if (isJsonElementAJsonPrimitive(e)) {
+ log.debug("try to convert scalar value {}", e.getAsString());
+ convertedValue = innerConverterFinal.convertToToscaValue(e.getAsString(), innerType, dataTypes);
} else {
convertedValue = handleComplexJsonValue(e);
}
@@ -154,16 +153,4 @@ 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/ToscaMapValueConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java
index 2cc868b17c..76d9d2bf47 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
@@ -17,6 +17,7 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
+
package org.openecomp.sdc.be.model.tosca.converters;
import com.google.gson.JsonArray;
@@ -67,7 +68,7 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T
} else {
DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
if (dataTypeDefinition != null) {
- ToscaPropertyType toscaPropertyType = null;
+ ToscaPropertyType toscaPropertyType;
if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
innerConverter = toscaPropertyType.getValueConverter();
} else {
@@ -85,15 +86,7 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T
}
}
JsonElement jsonElement;
- try {
- StringReader reader = new StringReader(value);
- JsonReader jsonReader = new JsonReader(reader);
- jsonReader.setLenient(true);
- jsonElement = JsonParser.parseReader(jsonReader);
- } catch (JsonSyntaxException e) {
- log.debug("convertToToscaValue failed to parse json value :", e);
- return null;
- }
+ jsonElement = parseToJson(value);
if (jsonElement == null || jsonElement.isJsonNull()) {
log.debug("convertToToscaValue json element is null");
return null;
@@ -127,9 +120,7 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T
propType = pd.getType();
final DataTypeDefinition pdDataType = dataTypes.get(propType);
final ToscaPropertyType toscaPropType = isScalarType(pdDataType);
- if (toscaPropType == null) {
- scalar = false;
- } else {
+ if (toscaPropType != null) {
scalar = true;
propType = toscaPropType.getType();
innerConverterProp = toscaPropType.getValueConverter();
@@ -144,14 +135,9 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T
public Object convertDataTypeToToscaObject(String innerType, Map<String, DataTypeDefinition> dataTypes, ToscaValueConverter innerConverter,
final boolean isScalarF, JsonElement entryValue, boolean preserveEmptyValue) {
- Object convertedValue = null;
- if (isScalarF && entryValue.isJsonPrimitive()) {
- log.debug("try convert scalar value ");
- if (entryValue.getAsString() == null) {
- convertedValue = null;
- } else {
- convertedValue = innerConverter.convertToToscaValue(entryValue.getAsString(), innerType, dataTypes);
- }
+ Object convertedValue;
+ if (isScalarF && isJsonElementAJsonPrimitive(entryValue)) {
+ return innerConverter.convertToToscaValue(entryValue.getAsString(), innerType, dataTypes);
} else {
if (entryValue.isJsonPrimitive()) {
return handleComplexJsonValue(entryValue);
@@ -201,11 +187,11 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T
String type = propertyDefinition.getType();
ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
if (propertyType != null) {
- if (elementValue.isJsonPrimitive()) {
+ if (isJsonElementAJsonPrimitive(elementValue)) {
ToscaValueConverter valueConverter = propertyType.getValueConverter();
convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type, dataTypes);
} else {
- if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) {
+ if (ToscaPropertyType.MAP.equals(propertyType) || ToscaPropertyType.LIST.equals(propertyType)) {
ToscaValueConverter valueConverter = propertyType.getValueConverter();
String json = gson.toJson(elementValue);
String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType();
@@ -219,7 +205,7 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T
}
}
} else {
- if (elementValue.isJsonPrimitive()) {
+ if (isJsonElementAJsonPrimitive(elementValue)) {
convValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
} else {
convValue = handleComplexJsonValue(elementValue);
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 7505d2af88..c9b5db8e87 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
@@ -17,13 +17,18 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
+
package org.openecomp.sdc.be.model.tosca.converters;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
+import com.google.gson.JsonSyntaxException;
+import com.google.gson.stream.JsonReader;
+import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -91,6 +96,9 @@ public class ToscaValueBaseConverter {
return handleJsonArray(jsonElement);
}
if (jsonElement.isJsonPrimitive()) {
+ if (!isJsonElementAJsonPrimitive(jsonElement)) {
+ return handleComplexJsonValue(parseToJson(jsonElement.getAsString()));
+ }
return json2JavaPrimitive(jsonElement.getAsJsonPrimitive());
}
log.debug("JSON type '{}' not supported", jsonElement);
@@ -137,4 +145,28 @@ public class ToscaValueBaseConverter {
}
throw new IllegalStateException(String.format("JSON primitive not supported: %s", jsonPrimitive));
}
+
+ public 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;
+ }
+ }
+
+ public boolean isJsonElementAJsonPrimitive(JsonElement jsonElement) {
+ if (!jsonElement.isJsonPrimitive()) {
+ return false;
+ }
+ String elementAsString = jsonElement.getAsString();
+ JsonElement elementAsJson = parseToJson(elementAsString);
+ if (elementAsJson.isJsonPrimitive() || elementAsJson.isJsonNull()) {
+ return true;
+ }
+ return false;
+ }
}
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 5387b46dd6..13b2be5ce5 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
@@ -26,6 +26,8 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import java.util.HashMap;
@@ -145,4 +147,62 @@ class ToscaValueBaseConverterTest {
assertEquals(innerObject.get("float").getAsDouble(), objectPropertyAsMap.get("float"));
assertNull(objectPropertyAsMap.get("null"));
}
+
+ @Test
+ void testParseToJson() {
+ final JsonElement result1 = converter.parseToJson("");
+ assertTrue(result1.isJsonNull());
+
+ final JsonElement result2 = converter.parseToJson("testString");
+ assertTrue(result2.isJsonPrimitive());
+
+ final JsonElement result3 = converter.parseToJson("1");
+ assertTrue(result3.isJsonPrimitive());
+
+ final JsonElement result4 = converter.parseToJson("true");
+ assertTrue(result4.isJsonPrimitive());
+
+ final JsonElement result5 = converter.parseToJson("{\"get_property\":[\"relatedParty_0\",\"name\"]}");
+ assertTrue(result5.isJsonObject());
+
+ final JsonElement result6 = converter.parseToJson("[\"relatedParty_0\",\"name\"]");
+ assertTrue(result6.isJsonArray());
+
+ }
+
+ @Test
+ void testIsJsonElementAJsonPrimitive() {
+
+ JsonElement emptyStringEle = new JsonPrimitive("");
+ final Boolean result1 = converter.isJsonElementAJsonPrimitive(emptyStringEle);
+ assertTrue(result1);
+
+ JsonElement stringEle = new JsonPrimitive("testString");
+ final Boolean result2 = converter.isJsonElementAJsonPrimitive(stringEle);
+ assertTrue(result2);
+
+ JsonElement toscaFunctionEle = new JsonPrimitive("{\"get_property\":[\"relatedParty_0\",\"name\"]}");
+ final Boolean result3 = converter.isJsonElementAJsonPrimitive(toscaFunctionEle);
+ assertFalse(result3);
+
+ JsonElement jsonObjectEle = new JsonObject();
+ final Boolean result4 = converter.isJsonElementAJsonPrimitive(jsonObjectEle);
+ assertFalse(result4);
+
+ JsonElement jsonArrayEle = new JsonArray();
+ final Boolean result5 = converter.isJsonElementAJsonPrimitive(jsonArrayEle);
+ assertFalse(result5);
+
+ JsonElement intEle = new JsonPrimitive(123);
+ final Boolean result6 = converter.isJsonElementAJsonPrimitive(intEle);
+ assertTrue(result6);
+
+ JsonElement boolEle = new JsonPrimitive(true);
+ final Boolean result7 = converter.isJsonElementAJsonPrimitive(boolEle);
+ assertTrue(result7);
+
+ JsonElement nullEle = new JsonNull();
+ final Boolean result8 = converter.isJsonElementAJsonPrimitive(nullEle);
+ assertFalse(result8);
+ }
}