From 3a325d8192addf27423494d8ed8b621dedcf7888 Mon Sep 17 00:00:00 2001 From: Enbo Wang Date: Fri, 21 Feb 2020 14:57:01 +0800 Subject: Support updating JSON object string for sliPluginUtils Issue-ID: CCSDK-2111 Signed-off-by: Enbo Wang Change-Id: Ide6bb06a05fcbb3b88e73cc0fbec6fd46bd29033 --- .../sli/core/slipluginutils/SliPluginUtils.java | 55 ++++++++++++++++++++++ .../SliPluginUtils_StaticFunctionsTest.java | 37 +++++++++++++++ .../provider/src/test/resources/JsonObject.json | 5 ++ 3 files changed, 97 insertions(+) create mode 100644 sliPluginUtils/provider/src/test/resources/JsonObject.json (limited to 'sliPluginUtils/provider/src') 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 0d9ab217e..ce0f50800 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 @@ -33,12 +33,15 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; import java.util.Properties; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; + import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; import org.onap.ccsdk.sli.core.sli.SvcLogicConstants; @@ -865,6 +868,58 @@ public class SliPluginUtils implements SvcLogicJavaPlugin { } } + /** + * updateJsonObjectString takes a json object string, and adds or deletes the properties of it + * @param parameters - requires source, outputPath and keys to be added or deleted. + * The key of parameter starts with "add.", e.g. "add.A", and then "A" and its value will be added + * to the JSON object. + * The key of parameter starts with "delete.", e.g. "delete.B", and then "B" will be deleted from + * the JSON object. + * @param ctx Reference to context memory + * @throws SvcLogicException if a required parameter is missing an exception is thrown + */ + public static void updateJsonObjectString(Map parameters, SvcLogicContext ctx) throws SvcLogicException { + checkParameters(parameters, new String[] {"source", "outputPath"}, LOG); + try { + String source = ctx.getAttribute(parameters.get("source")); + JsonParser jp = new JsonParser(); + JsonElement element = jp.parse(source); + if (element.isJsonObject()) { + JsonObject jsonObject = element.getAsJsonObject(); + updateJsonObject(jsonObject, parameters); + + String target = jsonObject.toString(); + ctx.setAttribute(parameters.get("outputPath"), target); + } else { + throw new SvcLogicException("just update JSON object string"); + } + } catch (Exception ex) { + throw new SvcLogicException("problem with updateJsonObjectString", ex); + } + } + + protected static void updateJsonObject(JsonObject jsonObject, Map parameters) throws SvcLogicException { + List deleted_params = parameters.keySet().stream().filter(param -> param.startsWith("delete.")). + collect(Collectors.toList()); + for (String param: deleted_params) { + String[] action_key = param.split("\\.", 2); + if (action_key.length < 2) { + throw new SvcLogicException("error parameter format: " + param + ", must be \"delete.\""); + } + jsonObject.remove(action_key[1]); + } + + List added_params = parameters.keySet().stream().filter(param -> param.startsWith("add.")). + collect(Collectors.toList()); + for (String param: added_params) { + String[] action_key = param.split("\\.", 2); + if (action_key.length < 2) { + throw new SvcLogicException("error parameter format: " + param + ", must be \"add.\""); + } + jsonObject.addProperty(action_key[1], parameters.get(param)); + } + } + /** * getAttributeValue takes a ctx memory path as a string, gets the value stored at this path and set this value in context memory at * outputPath 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 5c222c83f..ad039d7f4 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 @@ -495,6 +495,43 @@ public class SliPluginUtils_StaticFunctionsTest { assertEquals("200", ctx.getAttribute("testPath.widget.window.width")); } + @Test + public void testUpdateJsonObjectString() throws Exception { + String path = "src/test/resources/JsonObject.json"; + String content = new String(Files.readAllBytes(Paths.get(path))); + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("input", content); + + Map parametersUpdateJson = new HashMap(); + parametersUpdateJson.put("source", "input"); + parametersUpdateJson.put("outputPath", "newJsonString"); + + // add key "ccc" and its value + parametersUpdateJson.put("add.ccc", "abcxyz"); + + // update keys and their values of "aaa" and "c.d" + parametersUpdateJson.put("add.aaa", "4567"); + parametersUpdateJson.put("add.c.d", "defg"); + + // delete key "bbb" + parametersUpdateJson.put("delete.bbb", ""); + + SliPluginUtils.updateJsonObjectString(parametersUpdateJson, ctx); + + Map parametersJsonToCtx = new HashMap(); + parametersJsonToCtx.put("source", "newJsonString"); + parametersJsonToCtx.put("outputPath", "testPath"); + parametersJsonToCtx.put("isEscaped", "false"); + + SliPluginUtils.jsonStringToCtx(parametersJsonToCtx, ctx); + + assertEquals("abcxyz", ctx.getAttribute("testPath.ccc")); + assertEquals("4567", ctx.getAttribute("testPath.aaa")); + assertEquals("defg", ctx.getAttribute("testPath.c.d")); + assertEquals(null, ctx.getAttribute("testPath.bbb")); + } + @Test public void testEmbeddedEscapedJsonJsonStringToCtx() throws Exception { String path = "src/test/resources/EmbeddedEscapedJson.json"; diff --git a/sliPluginUtils/provider/src/test/resources/JsonObject.json b/sliPluginUtils/provider/src/test/resources/JsonObject.json new file mode 100644 index 000000000..0578368f8 --- /dev/null +++ b/sliPluginUtils/provider/src/test/resources/JsonObject.json @@ -0,0 +1,5 @@ +{ + "aaa": "123", + "bbb": "xyz", + "c.d": "abc" +} \ No newline at end of file -- cgit 1.2.3-korg