From 95cf22da1854a991c756e46240cbab52a33eaa84 Mon Sep 17 00:00:00 2001 From: Dan Timoney Date: Tue, 16 Jun 2020 10:15:13 -0400 Subject: 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 --- .../sli/core/slipluginutils/SliPluginUtils.java | 55 ++-------------------- .../SliPluginUtils_StaticFunctionsTest.java | 13 ----- 2 files changed, 5 insertions(+), 63 deletions(-) (limited to 'sliPluginUtils') 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 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 @@ -283,19 +283,6 @@ public class SliPluginUtils_StaticFunctionsTest { SliPluginUtils.printContext(parameters, ctx); } - @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", ""); -- cgit 1.2.3-korg