summaryrefslogtreecommitdiffstats
path: root/sliPluginUtils/provider
diff options
context:
space:
mode:
authorDan Timoney <dtimoney@att.com>2020-06-16 10:15:13 -0400
committerDan Timoney <dtimoney@att.com>2020-06-19 08:00:50 -0400
commit95cf22da1854a991c756e46240cbab52a33eaa84 (patch)
tree8b3a311c2f75c0bc20db2dbd498639bca637a4ab /sliPluginUtils/provider
parent1f0a87b2f2a38e868ce5b6dee6dc1355a5284819 (diff)
Implement new method to convert SvcLogicContext to JSON
Added new method toJsonString() to SvcLogicContext class to write out service logic context properties as a JSON string Refactored static method SliPluginUtils.writeJsonToCtx to SvcLogicContext.mergeJson method Change-Id: I4fe134976f93c7d116bc54ad2bae6e486c6fac2c Issue-ID: CCSDK-1760 Signed-off-by: Dan Timoney <dtimoney@att.com>
Diffstat (limited to 'sliPluginUtils/provider')
-rw-r--r--sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java55
-rw-r--r--sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java13
2 files changed, 5 insertions, 63 deletions
diff --git a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java
index 60bb4fd31..816bb5dff 100644
--- a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java
+++ b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java
@@ -817,62 +817,17 @@ public class SliPluginUtils implements SvcLogicJavaPlugin {
if("true".equals(parameters.get("isEscaped"))){
source = StringEscapeUtils.unescapeJson(source);
}
- writeJsonToCtx(source, ctx,parameters.get("outputPath"));
+ ctx.mergeJson(parameters.get("outputPath"), source);
+ // writeJsonToCtx(source, ctx,parameters.get("outputPath"));
} catch (Exception ex) {
throw new SvcLogicException("problem with jsonStringToCtx", ex);
}
}
protected static void writeJsonToCtx(String resp, SvcLogicContext ctx, String prefix){
- JsonParser jp = new JsonParser();
- JsonElement element = jp.parse(resp);
- String root = prefix + ".";
- if (element.isJsonObject()) {
- writeJsonObject(element.getAsJsonObject(), ctx, root);
- } else if (element.isJsonArray()) {
- handleJsonArray("", element.getAsJsonArray(), ctx, root);
- }
- }
-
- protected static void writeJsonObject(JsonObject obj, SvcLogicContext ctx, String root) {
- for (Entry<String, JsonElement> entry : obj.entrySet()) {
- String key = entry.getKey();
- if (entry.getValue().isJsonObject()) {
- writeJsonObject(entry.getValue().getAsJsonObject(), ctx, root + key + ".");
- } else if (entry.getValue().isJsonArray()) {
- JsonArray array = entry.getValue().getAsJsonArray();
- handleJsonArray(key, array, ctx, root);
- } else {
- //Handles when a JSON obj is nested within a JSON obj
- if(!root.endsWith(".")){
- root = root + ".";
- }
- if(entry.getValue().isJsonNull()) {
- ctx.setAttribute(root + key, CTX_NULL_VALUE);
- }else {
- ctx.setAttribute(root + key, entry.getValue().getAsString());
- }
- }
- }
- }
-
- protected static void handleJsonArray(String key, JsonArray array, SvcLogicContext ctx, String root) {
- ctx.setAttribute(root + key + LENGTH, String.valueOf(array.size()));
- Integer arrayIdx = 0;
- for (JsonElement element : array) {
- String prefix = root + key + "[" + arrayIdx + "]";
-
- if (element.isJsonArray()) {
- handleJsonArray(key, element.getAsJsonArray(), ctx, prefix);
- } else if (element.isJsonObject()) {
- writeJsonObject(element.getAsJsonObject(), ctx, prefix + ".");
- } else if (element.isJsonNull()) {
- ctx.setAttribute(prefix, CTX_NULL_VALUE);
- } else if (element.isJsonPrimitive()) {
- ctx.setAttribute(prefix, element.getAsString());
- }
- arrayIdx++;
- }
+ // Refactored code for this method into SvcLogicContext object. Leaving this
+ // method in place for backward compatibility.
+ ctx.mergeJson(prefix, resp);
}
/**
diff --git a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java
index bc2c9226d..42e7ceb94 100644
--- a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java
+++ b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java
@@ -284,19 +284,6 @@ public class SliPluginUtils_StaticFunctionsTest {
}
@Test
- public void testWriteJsonObject() throws SvcLogicException {
- JsonObject obj = new JsonObject();
- obj.addProperty("name", "testName");
- obj.addProperty("age", 27);
- obj.addProperty("salary", 600000);
- SvcLogicContext ctx = new SvcLogicContext();
- SliPluginUtils.writeJsonObject(obj, ctx, "root");
- assertEquals("testName", ctx.getAttribute("root.name"));
- assertEquals("27", ctx.getAttribute("root.age"));
- assertEquals("600000", ctx.getAttribute("root.salary"));
- }
-
- @Test
public void testCtxKeyEmpty() {
ctx.setAttribute("key", "");
assertTrue(SliPluginUtils.ctxKeyEmpty(ctx, "key"));