aboutsummaryrefslogtreecommitdiffstats
path: root/restapi-call-node/provider/src
diff options
context:
space:
mode:
Diffstat (limited to 'restapi-call-node/provider/src')
-rw-r--r--restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/JsonParser.java66
-rw-r--r--restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestJsonParser.java70
-rw-r--r--restapi-call-node/provider/src/test/resources/1dArray.json8
-rw-r--r--restapi-call-node/provider/src/test/resources/3dArray.json5
4 files changed, 109 insertions, 40 deletions
diff --git a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/JsonParser.java b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/JsonParser.java
index 4c3ba7f8..60d43df7 100644
--- a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/JsonParser.java
+++ b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/JsonParser.java
@@ -25,6 +25,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
@@ -42,6 +43,46 @@ public final class JsonParser {
// Preventing instantiation of the same.
}
+
+ private static void handleJsonArray(String key, Map<String, Object> jArrayMap, JSONArray jsonArr) throws JSONException {
+ JSONObject jsonObj;
+ JSONArray subJsonArr;
+ boolean stripKey = false;
+
+ for (int i = 0, length = jsonArr.length(); i < length; i++) {
+ if (stripKey)
+ key = key.substring(0, key.length()-3);
+
+ subJsonArr = jsonArr.optJSONArray(i);
+ if (subJsonArr != null) {
+ key = StringUtils.trimToEmpty(key) + "[" + i + "]";
+ jArrayMap.putIfAbsent(key + "_length", String.valueOf(subJsonArr.length()));
+ handleJsonArray(key, jArrayMap, subJsonArr);
+ stripKey = true;
+ continue;
+ }
+
+ jsonObj = jsonArr.optJSONObject(i);
+ if (jsonObj != null) {
+ Iterator<String> ii = jsonObj.keys();
+ while (ii.hasNext()) {
+ String nodeKey = ii.next();
+ String key1 = "[" + i + "]." + nodeKey;
+ String[] subKey = key1.split(":");
+ if (subKey.length == 2) {
+ jArrayMap.putIfAbsent(subKey[1], jsonObj.get(nodeKey));
+ } else {
+ jArrayMap.putIfAbsent(key1, jsonObj.get(nodeKey));
+ }
+ }
+ }
+ else {
+ jArrayMap.putIfAbsent(StringUtils.trimToEmpty(key), jsonArr);
+ break;
+ }
+ }
+ }
+
@SuppressWarnings("unchecked")
public static Map<String, String> convertToProperties(String s)
throws SvcLogicException {
@@ -50,27 +91,13 @@ public final class JsonParser {
try {
Map<String, Object> wm = new HashMap<>();
- String topLvlArrLength = null;
JSONObject json;
JSONArray jsonArr;
//support top level list in json response
if (s.startsWith("[")) {
jsonArr = new JSONArray(s);
- topLvlArrLength = String.valueOf(jsonArr.length());
- for (int i = 0, length = jsonArr.length(); i < length; i++) {
- json = jsonArr.getJSONObject(i);
- Iterator<String> ii = json.keys();
- while (ii.hasNext()) {
- String key = ii.next();
- String key1 = "[" + i + "]." + key;
- String[] subKey = key1.split(":");
- if (subKey.length == 2) {
- wm.put(subKey[1], json.get(key));
- } else {
- wm.put(key1, json.get(key));
- }
- }
- }
+ wm.put("_length", String.valueOf(jsonArr.length()));
+ handleJsonArray(null, wm, jsonArr);
} else {
json = new JSONObject(s);
Iterator<String> ii = json.keys();
@@ -86,9 +113,6 @@ public final class JsonParser {
}
Map<String, String> mm = new HashMap<>();
- if (topLvlArrLength != null) {
- mm.put("_length", topLvlArrLength);
- }
while (!wm.isEmpty()) {
for (String key : new ArrayList<>(wm.keySet())) {
Object o = wm.get(key);
@@ -96,7 +120,6 @@ public final class JsonParser {
if (o instanceof Boolean || o instanceof Number || o instanceof String) {
mm.put(key, o.toString());
-
log.info("Added property: {} : {}", key, o.toString());
} else if (o instanceof JSONObject) {
JSONObject jo = (JSONObject) o;
@@ -113,8 +136,7 @@ public final class JsonParser {
} else if (o instanceof JSONArray) {
JSONArray ja = (JSONArray) o;
mm.put(key + "_length", String.valueOf(ja.length()));
-
- log.info("Added property: {}_length: {}", key, String.valueOf(ja.length()));
+ log.info("Added property: {}_length: {}", key, ja.length());
for (int i = 0; i < ja.length(); i++) {
wm.put(key + '[' + i + ']', ja.get(i));
diff --git a/restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestJsonParser.java b/restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestJsonParser.java
index 569719d6..cdffd457 100644
--- a/restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestJsonParser.java
+++ b/restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestJsonParser.java
@@ -134,7 +134,23 @@ public class TestJsonParser {
assertEquals("3", mm.get("menu_length"));
}
- @Test(expected = SvcLogicException.class) // current behavior is multidimensional arrays are not supported
+ @Test
+ public void test1dJsonStringToCtx() throws Exception {
+ String path = "src/test/resources/1dArray.json";
+ String content = new String(Files.readAllBytes(Paths.get(path)));
+ Map<String, String> mm = JsonParser.convertToProperties(content);
+
+ System.out.println(mm);
+ assertEquals("6", mm.get("_length"));
+ assertEquals("apple", mm.get("[0]"));
+ assertEquals("orange", mm.get("[1]"));
+ assertEquals("banana", mm.get("[2]"));
+ assertEquals("squash", mm.get("[3]"));
+ assertEquals("broccoli", mm.get("[4]"));
+ assertEquals("cauliflower", mm.get("[5]"));
+ }
+
+ @Test
public void test2dJsonStringToCtx() throws Exception {
String path = "src/test/resources/2dArray.json";
String content = new String(Files.readAllBytes(Paths.get(path)));
@@ -145,6 +161,7 @@ public class TestJsonParser {
assertEquals("orange", mm.get("[0][1]"));
assertEquals("banana", mm.get("[0][2]"));
assertEquals("3", mm.get("[0]_length"));
+
assertEquals("squash", mm.get("[1][0]"));
assertEquals("broccoli", mm.get("[1][1]"));
assertEquals("cauliflower", mm.get("[1][2]"));
@@ -152,28 +169,48 @@ public class TestJsonParser {
assertEquals("2", mm.get("_length"));
}
- @Test(expected = SvcLogicException.class) // current behavior is multidimensional arrays are not supported
+ @Test
public void test3dJsonStringToCtx() throws Exception {
String path = "src/test/resources/3dArray.json";
String content = new String(Files.readAllBytes(Paths.get(path)));
Map<String, String> mm = JsonParser.convertToProperties(content);
- // code will crash before these tests
- assertEquals("a", mm.get("[0][0][0]"));
- assertEquals("b", mm.get("[0][0][1]"));
- assertEquals("c", mm.get("[0][0][2]"));
+ assertEquals("3", mm.get("_length"));
+ assertEquals("1", mm.get("[0]_length"));
assertEquals("3", mm.get("[0][0]_length"));
- assertEquals("d", mm.get("[0][1][0]"));
- assertEquals("e", mm.get("[0][1][1]"));
- assertEquals("f", mm.get("[0][1][2]"));
- assertEquals("3", mm.get("[0][1]_length"));
- assertEquals("2", mm.get("[0]_length"));
- assertEquals("x", mm.get("[1][0][0]"));
- assertEquals("y", mm.get("[1][0][1]"));
- assertEquals("z", mm.get("[1][0][2]"));
+
+ assertEquals("2", mm.get("[1]_length"));
assertEquals("3", mm.get("[1][0]_length"));
- assertEquals("1", mm.get("[1]_length"));
- assertEquals("2", mm.get("_length"));
+ assertEquals("3", mm.get("[1][1]_length"));
+
+ assertEquals("3", mm.get("[2]_length"));
+ assertEquals("3", mm.get("[2][0]_length"));
+ assertEquals("3", mm.get("[2][1]_length"));
+ assertEquals("3", mm.get("[2][2]_length"));
+
+ assertEquals("x", mm.get("[0][0][0]"));
+ assertEquals("y", mm.get("[0][0][1]"));
+ assertEquals("z", mm.get("[0][0][2]"));
+
+ assertEquals("abc", mm.get("[1][0][0]"));
+ assertEquals("def", mm.get("[1][0][1]"));
+ assertEquals("xyz", mm.get("[1][0][2]"));
+
+ assertEquals("123", mm.get("[1][1][0]"));
+ assertEquals("456", mm.get("[1][1][1]"));
+ assertEquals("789", mm.get("[1][1][2]"));
+
+ assertEquals("a", mm.get("[2][0][0]"));
+ assertEquals("b", mm.get("[2][0][1]"));
+ assertEquals("c", mm.get("[2][0][2]"));
+
+ assertEquals("d", mm.get("[2][1][0]"));
+ assertEquals("e", mm.get("[2][1][1]"));
+ assertEquals("f", mm.get("[2][1][2]"));
+
+ assertEquals("1", mm.get("[2][2][0]"));
+ assertEquals("2", mm.get("[2][2][1]"));
+ assertEquals("3", mm.get("[2][2][2]"));
}
@Test
@@ -216,6 +253,7 @@ public class TestJsonParser {
mm = JsonParser.convertToProperties(mm.get("input.parameters[0].value"));
assertEquals("0.2.0.0/16", mm.get("[0].id"));
assertEquals("ge04::/64", mm.get("[1].id"));
+ assertEquals("2", mm.get("_length"));
}
}
diff --git a/restapi-call-node/provider/src/test/resources/1dArray.json b/restapi-call-node/provider/src/test/resources/1dArray.json
new file mode 100644
index 00000000..208fb70a
--- /dev/null
+++ b/restapi-call-node/provider/src/test/resources/1dArray.json
@@ -0,0 +1,8 @@
+[
+ "apple",
+ "orange",
+ "banana",
+ "squash",
+ "broccoli",
+ "cauliflower"
+] \ No newline at end of file
diff --git a/restapi-call-node/provider/src/test/resources/3dArray.json b/restapi-call-node/provider/src/test/resources/3dArray.json
index 14995559..3f3f4fab 100644
--- a/restapi-call-node/provider/src/test/resources/3dArray.json
+++ b/restapi-call-node/provider/src/test/resources/3dArray.json
@@ -1,4 +1,5 @@
[
- [["a","b","c"], ["d","e","f"]],
- [["x","y","z"]]
+ [["x","y","z"]],
+ [["abc","def","xyz"], [123, 456, 789]],
+ [["a","b","c"], ["d","e","f"], ["1","2","3"]]
] \ No newline at end of file