diff options
Diffstat (limited to 'sliPluginUtils/provider')
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")); |