Support updating JSON object string for sliPluginUtils 19/102119/2
authorEnbo Wang <wangenbo@huawei.com>
Fri, 21 Feb 2020 06:57:01 +0000 (14:57 +0800)
committerDan Timoney <dtimoney@att.com>
Fri, 21 Feb 2020 20:21:32 +0000 (20:21 +0000)
Issue-ID: CCSDK-2111
Signed-off-by: Enbo Wang <wangenbo@huawei.com>
Change-Id: Ide6bb06a05fcbb3b88e73cc0fbec6fd46bd29033

sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java
sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java
sliPluginUtils/provider/src/test/resources/JsonObject.json [new file with mode: 0644]

index 0d9ab21..ce0f508 100644 (file)
@@ -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<String, String> 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<String, String> parameters) throws SvcLogicException {
+        List<String> 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.<key>\"");
+            }
+            jsonObject.remove(action_key[1]);
+        }
+
+        List<String> 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.<key>\"");
+            }
+            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
index 5c222c8..ad039d7 100644 (file)
@@ -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<String, String> parametersUpdateJson = new HashMap<String, String>();
+        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<String, String> parametersJsonToCtx = new HashMap<String, String>();
+        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 (file)
index 0000000..0578368
--- /dev/null
@@ -0,0 +1,5 @@
+{\r
+  "aaa": "123",\r
+  "bbb": "xyz",\r
+  "c.d": "abc"\r
+}
\ No newline at end of file