enhance SliPluginUtils 36/99736/2
authorSmokowski, Kevin (ks6305) <kevin.smokowski@att.com>
Tue, 17 Dec 2019 18:27:42 +0000 (18:27 +0000)
committerSmokowski, Kevin (ks6305) <kevin.smokowski@att.com>
Wed, 18 Dec 2019 16:44:13 +0000 (16:44 +0000)
writeJsonToCtx now supports top level json arrays, add urlDecode to SliStringUtils, rename CheckParametersTest and move unrelated methods into another test class and Add test cases to improve test coverage

Issue-ID: CCSDK-2006
Signed-off-by: Smokowski, Kevin (ks6305) <kevin.smokowski@att.com>
Change-Id: If1f654ea9c7046b00c977434f87740e54f15b7d3

12 files changed:
sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java
sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtils.java
sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java
sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_checkParametersTest.java [moved from sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/CheckParametersTest.java with 56% similarity]
sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtilsTest.java
sliPluginUtils/provider/src/test/resources/2dArray.json [new file with mode: 0644]
sliPluginUtils/provider/src/test/resources/3dArray.json [new file with mode: 0644]
sliPluginUtils/provider/src/test/resources/ArrayMenu.json [new file with mode: 0644]
sliPluginUtils/provider/src/test/resources/EmbeddedEscapedJson.json [new file with mode: 0644]
sliPluginUtils/provider/src/test/resources/EscapedJson.json [new file with mode: 0644]
sliPluginUtils/provider/src/test/resources/ObjectMenu.json [new file with mode: 0644]
sliPluginUtils/provider/src/test/resources/Widget.json [new file with mode: 0644]

index b8e88f6..1fc0525 100644 (file)
@@ -821,32 +821,46 @@ public class SliPluginUtils implements SvcLogicJavaPlugin {
     protected static void writeJsonToCtx(String resp, SvcLogicContext ctx, String prefix){
         JsonParser jp = new JsonParser();
         JsonElement element = jp.parse(resp);
-        writeJsonObject(element.getAsJsonObject(), ctx, prefix + ".");
+        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 + entry.getKey() + ".");
+                writeJsonObject(entry.getValue().getAsJsonObject(), ctx, root + key + ".");
             } else if (entry.getValue().isJsonArray()) {
                 JsonArray array = entry.getValue().getAsJsonArray();
-                ctx.setAttribute(root + entry.getKey() + LENGTH, String.valueOf(array.size()));
-                Integer arrayIdx = 0;
-                for (JsonElement element : array) {
-                    if (element.isJsonObject()) {
-                        writeJsonObject(element.getAsJsonObject(), ctx, root + entry.getKey() + "[" + arrayIdx + "].");
-                    } else if (element.isJsonPrimitive()) {
-                        ctx.setAttribute(root + entry.getKey() + "[" + arrayIdx + "]", element.getAsString());
-                    }
-                    arrayIdx++;
-                }
+                handleJsonArray(key, array, ctx, root);
             } else {
                 //Handles when a JSON obj is nested within a JSON obj
                 if(!root.endsWith(".")){
                     root = root + ".";
                 }
-                ctx.setAttribute(root + entry.getKey(), entry.getValue().getAsString());
+                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.isJsonPrimitive()) {
+                ctx.setAttribute(prefix, element.getAsString());
             }
+            arrayIdx++;
         }
     }
 
index 269c376..d343ce2 100644 (file)
@@ -24,6 +24,7 @@
 package org.onap.ccsdk.sli.core.slipluginutils;
 
 import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.util.Map;
 import org.apache.commons.lang3.StringEscapeUtils;
@@ -427,6 +428,20 @@ public class SliStringUtils implements SvcLogicJavaPlugin {
         }
     }
 
+    public static void urlDecode(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
+        SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, "outputPath"}, LOG);
+        String encoding = parameters.get("encoding");
+        if (encoding == null) {
+            encoding = "UTF-8";
+        }
+        try {
+            String result = URLDecoder.decode(parameters.get(INPUT_PARAM_SOURCE), encoding);
+            ctx.setAttribute(parameters.get("outputPath"), result);
+        } catch (UnsupportedEncodingException e) {
+            throw new SvcLogicException("Url decode failed.", e);
+        }
+    }
+
        /**
         * xmlEscapeText() will be used to format input xml with text.
         *
index 08adc97..64645b3 100644 (file)
@@ -24,14 +24,20 @@ package org.onap.ccsdk.sli.core.slipluginutils;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.HashMap;
 import java.util.Map;
 import org.junit.Before;
 import org.junit.Test;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
+import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils.LogLevel;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import com.google.gson.JsonObject;
 
 public class SliPluginUtils_StaticFunctionsTest {
     private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_StaticFunctionsTest.class);
@@ -216,28 +222,6 @@ public class SliPluginUtils_StaticFunctionsTest {
         SliPluginUtils.ctxSetAttribute(ctx, "test", i, LOG, SliPluginUtils.LogLevel.TRACE);
     }
 
-    /*@Test
-    public void printContext() throws SvcLogicException, IOException {
-        String filePath = "/src/test/resources/printContext.txt";
-        parameters.put("filename", filePath);
-        File f = new File(filePath);
-        assert (f.exists());
-        assert (!f.isDirectory());
-        ctx.setAttribute("hello", "world");
-        ctx.setAttribute("name", "value");
-
-        SliPluginUtils.printContext(parameters, ctx);
-        BufferedReader br = new BufferedReader(new FileReader(f));
-        String line = br.readLine();
-        assertEquals("#######################################", line);
-        line = br.readLine();
-        assertEquals("hello = world", line);
-        line = br.readLine();
-        assertEquals("name = value", line);
-        br.close();
-        Files.delete(Paths.get(filePath));
-    }*/
-
     @Test
     public void setTime() throws SvcLogicException {
         String outputPath = "output";
@@ -264,4 +248,318 @@ public class SliPluginUtils_StaticFunctionsTest {
         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
     }
 
+    @Test
+    public void testGetAttributeValue() throws Exception {
+        parameters.put("outputPath", "testPath");
+        parameters.put("source", "testSource");
+        SliPluginUtils.getAttributeValue(parameters, ctx);
+        assertNull(ctx.getAttribute(parameters.get("outputPath")));
+    }
+
+    @Test
+    public void testCtxListContains() throws Exception {
+        parameters.put("list", "10_length");
+        parameters.put("keyName", "testName");
+        parameters.put("keyValue", "testValue");
+        ctx.setAttribute("10_length", "10");
+        assertEquals("false", SliPluginUtils.ctxListContains(parameters, ctx));
+
+    }
+
+    @Test(expected = SvcLogicException.class)
+    public void testPrintContextForEmptyParameters() throws SvcLogicException {
+        SliPluginUtils.printContext(parameters, ctx);
+    }
+
+    @Test(expected = SvcLogicException.class)
+    public void testPrintContextForNullParameters() throws SvcLogicException {
+        SliPluginUtils.printContext(null, ctx);
+    }
+
+    @Test
+    public void testPrintContext() throws SvcLogicException {
+        parameters.put("filename", "testFileName");
+        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", "");
+        assertTrue(SliPluginUtils.ctxKeyEmpty(ctx, "key"));
+    }
+
+    @Test
+    public void testGetArrayLength() {
+        ctx.setAttribute("key_length", "test");
+        Logger log = LoggerFactory.getLogger(getClass());
+        SliPluginUtils.getArrayLength(ctx, "key", log, LogLevel.INFO, "invalid input");
+    }
+
+    @Test
+    public void testSetPropertiesForRoot() {
+        Map<String, String> parameters = new HashMap<>();
+        parameters.put("root", "RootVal");
+        parameters.put("valueRoot", "ValueRootVal");
+        assertEquals("success", SliPluginUtils.setPropertiesForRoot(parameters, ctx));
+    }
+
+    @Test
+    public void testJsonStringToCtxToplevelArray() throws Exception {
+        String path = "src/test/resources/ArrayMenu.json";
+        String content = new String(Files.readAllBytes(Paths.get(path)));
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("input", content);
+        Map<String, String> parameters = new HashMap<String, String>();
+        parameters.put("outputPath", "testPath");
+        parameters.put("isEscaped", "false");
+        parameters.put("source", "input");
+
+        SliPluginUtils.jsonStringToCtx(parameters, ctx);
+
+        assertEquals("1000", ctx.getAttribute("testPath.[0].calories"));
+        assertEquals("1", ctx.getAttribute("testPath.[0].id"));
+        assertEquals("plain", ctx.getAttribute("testPath.[0].name"));
+        assertEquals("pizza", ctx.getAttribute("testPath.[0].type"));
+        assertEquals("true", ctx.getAttribute("testPath.[0].vegetarian"));
+        assertEquals("2000", ctx.getAttribute("testPath.[1].calories"));
+        assertEquals("2", ctx.getAttribute("testPath.[1].id"));
+        assertEquals("Tuesday Special", ctx.getAttribute("testPath.[1].name"));
+        assertEquals("1", ctx.getAttribute("testPath.[1].topping[0].id"));
+        assertEquals("onion", ctx.getAttribute("testPath.[1].topping[0].name"));
+        assertEquals("2", ctx.getAttribute("testPath.[1].topping[1].id"));
+        assertEquals("pepperoni", ctx.getAttribute("testPath.[1].topping[1].name"));
+        assertEquals("2", ctx.getAttribute("testPath.[1].topping_length"));
+        assertEquals("pizza", ctx.getAttribute("testPath.[1].type"));
+        assertEquals("false", ctx.getAttribute("testPath.[1].vegetarian"));
+        assertEquals("1500", ctx.getAttribute("testPath.[2].calories"));
+        assertEquals("3", ctx.getAttribute("testPath.[2].id"));
+        assertEquals("House Special", ctx.getAttribute("testPath.[2].name"));
+        assertEquals("3", ctx.getAttribute("testPath.[2].topping[0].id"));
+        assertEquals("basil", ctx.getAttribute("testPath.[2].topping[0].name"));
+        assertEquals("4", ctx.getAttribute("testPath.[2].topping[1].id"));
+        assertEquals("fresh mozzarella", ctx.getAttribute("testPath.[2].topping[1].name"));
+        assertEquals("5", ctx.getAttribute("testPath.[2].topping[2].id"));
+        assertEquals("tomato", ctx.getAttribute("testPath.[2].topping[2].name"));
+        assertEquals("3", ctx.getAttribute("testPath.[2].topping_length"));
+        assertEquals("pizza", ctx.getAttribute("testPath.[2].type"));
+        assertEquals("true", ctx.getAttribute("testPath.[2].vegetarian"));
+        assertEquals("3", ctx.getAttribute("testPath._length"));
+    }
+
+    @Test
+    public void testJsonStringToCtx() throws Exception {
+        String path = "src/test/resources/ObjectMenu.json";
+        String content = new String(Files.readAllBytes(Paths.get(path)));
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("input", content);
+
+        Map<String, String> parameters = new HashMap<String, String>();
+        parameters.put("outputPath", "testPath");
+        parameters.put("isEscaped", "false");
+        parameters.put("source", "input");
+
+        SliPluginUtils.jsonStringToCtx(parameters, ctx);
+
+
+        assertEquals("1000", ctx.getAttribute("testPath.menu[0].calories"));
+        assertEquals("1", ctx.getAttribute("testPath.menu[0].id"));
+        assertEquals("plain", ctx.getAttribute("testPath.menu[0].name"));
+        assertEquals("pizza", ctx.getAttribute("testPath.menu[0].type"));
+        assertEquals("true", ctx.getAttribute("testPath.menu[0].vegetarian"));
+        assertEquals("2000", ctx.getAttribute("testPath.menu[1].calories"));
+        assertEquals("2", ctx.getAttribute("testPath.menu[1].id"));
+        assertEquals("Tuesday Special", ctx.getAttribute("testPath.menu[1].name"));
+        assertEquals("1", ctx.getAttribute("testPath.menu[1].topping[0].id"));
+        assertEquals("onion", ctx.getAttribute("testPath.menu[1].topping[0].name"));
+        assertEquals("2", ctx.getAttribute("testPath.menu[1].topping[1].id"));
+        assertEquals("pepperoni", ctx.getAttribute("testPath.menu[1].topping[1].name"));
+        assertEquals("2", ctx.getAttribute("testPath.menu[1].topping_length"));
+        assertEquals("pizza", ctx.getAttribute("testPath.menu[1].type"));
+        assertEquals("false", ctx.getAttribute("testPath.menu[1].vegetarian"));
+        assertEquals("1500", ctx.getAttribute("testPath.menu[2].calories"));
+        assertEquals("3", ctx.getAttribute("testPath.menu[2].id"));
+        assertEquals("House Special", ctx.getAttribute("testPath.menu[2].name"));
+        assertEquals("3", ctx.getAttribute("testPath.menu[2].topping[0].id"));
+        assertEquals("basil", ctx.getAttribute("testPath.menu[2].topping[0].name"));
+        assertEquals("4", ctx.getAttribute("testPath.menu[2].topping[1].id"));
+        assertEquals("fresh mozzarella", ctx.getAttribute("testPath.menu[2].topping[1].name"));
+        assertEquals("5", ctx.getAttribute("testPath.menu[2].topping[2].id"));
+        assertEquals("tomato", ctx.getAttribute("testPath.menu[2].topping[2].name"));
+        assertEquals("3", ctx.getAttribute("testPath.menu[2].topping_length"));
+        assertEquals("pizza", ctx.getAttribute("testPath.menu[2].type"));
+        assertEquals("true", ctx.getAttribute("testPath.menu[2].vegetarian"));
+        assertEquals("3", ctx.getAttribute("testPath.menu_length"));
+    }
+
+    @Test
+    public void test2dJsonStringToCtx() throws Exception {
+        String path = "src/test/resources/2dArray.json";
+        String content = new String(Files.readAllBytes(Paths.get(path)));
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("input", content);
+
+        Map<String, String> parameters = new HashMap<String, String>();
+        parameters.put("outputPath", "testPath");
+        parameters.put("isEscaped", "false");
+        parameters.put("source", "input");
+
+        SliPluginUtils.jsonStringToCtx(parameters, ctx);
+        assertEquals("apple", ctx.getAttribute("testPath.[0][0]"));
+        assertEquals("orange", ctx.getAttribute("testPath.[0][1]"));
+        assertEquals("banana", ctx.getAttribute("testPath.[0][2]"));
+        assertEquals("3", ctx.getAttribute("testPath.[0]_length"));
+        assertEquals("squash", ctx.getAttribute("testPath.[1][0]"));
+        assertEquals("broccoli", ctx.getAttribute("testPath.[1][1]"));
+        assertEquals("cauliflower", ctx.getAttribute("testPath.[1][2]"));
+        assertEquals("3", ctx.getAttribute("testPath.[1]_length"));
+        assertEquals("2", ctx.getAttribute("testPath._length"));
+    }
+
+    @Test
+    public void test3dJsonStringToCtx() throws Exception {
+        String path = "src/test/resources/3dArray.json";
+        String content = new String(Files.readAllBytes(Paths.get(path)));
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("input", content);
+
+        Map<String, String> parameters = new HashMap<String, String>();
+        parameters.put("outputPath", "testPath");
+        parameters.put("isEscaped", "false");
+        parameters.put("source", "input");
+
+        SliPluginUtils.jsonStringToCtx(parameters, ctx);
+        assertEquals("a", ctx.getAttribute("testPath.[0][0][0]"));
+        assertEquals("b", ctx.getAttribute("testPath.[0][0][1]"));
+        assertEquals("c", ctx.getAttribute("testPath.[0][0][2]"));
+        assertEquals("3", ctx.getAttribute("testPath.[0][0]_length"));
+        assertEquals("d", ctx.getAttribute("testPath.[0][1][0]"));
+        assertEquals("e", ctx.getAttribute("testPath.[0][1][1]"));
+        assertEquals("f", ctx.getAttribute("testPath.[0][1][2]"));
+        assertEquals("3", ctx.getAttribute("testPath.[0][1]_length"));
+        assertEquals("2", ctx.getAttribute("testPath.[0]_length"));
+        assertEquals("x", ctx.getAttribute("testPath.[1][0][0]"));
+        assertEquals("y", ctx.getAttribute("testPath.[1][0][1]"));
+        assertEquals("z", ctx.getAttribute("testPath.[1][0][2]"));
+        assertEquals("3", ctx.getAttribute("testPath.[1][0]_length"));
+        assertEquals("1", ctx.getAttribute("testPath.[1]_length"));
+        assertEquals("2", ctx.getAttribute("testPath._length"));
+    }
+
+    @Test
+    public void testJsonWidgetStringToCtx() throws Exception {
+        String path = "src/test/resources/Widget.json";
+        String content = new String(Files.readAllBytes(Paths.get(path)));
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("input", content);
+
+        Map<String, String> parameters = new HashMap<String, String>();
+        parameters.put("outputPath", "testPath");
+        parameters.put("isEscaped", "false");
+        parameters.put("source", "input");
+
+        SliPluginUtils.jsonStringToCtx(parameters, ctx);
+        assertEquals("false", ctx.getAttribute("testPath.widget.debug"));
+        assertEquals("center", ctx.getAttribute("testPath.widget.image.alignment"));
+        assertEquals("150", ctx.getAttribute("testPath.widget.image.hOffset"));
+        assertEquals("moon", ctx.getAttribute("testPath.widget.image.name"));
+        assertEquals("images/moon.png", ctx.getAttribute("testPath.widget.image.src"));
+        assertEquals("150", ctx.getAttribute("testPath.widget.image.vOffset"));
+        assertEquals("center", ctx.getAttribute("testPath.widget.text.alignment"));
+        assertEquals("Click Me", ctx.getAttribute("testPath.widget.text.data"));
+        assertEquals("350", ctx.getAttribute("testPath.widget.text.hOffset"));
+        assertEquals("text1", ctx.getAttribute("testPath.widget.text.name"));
+        assertEquals("21", ctx.getAttribute("testPath.widget.text.size"));
+        assertEquals("bold", ctx.getAttribute("testPath.widget.text.style"));
+        assertEquals("200", ctx.getAttribute("testPath.widget.text.vOffset"));
+        assertEquals("300", ctx.getAttribute("testPath.widget.window.height"));
+        assertEquals("main_window", ctx.getAttribute("testPath.widget.window.name"));
+        assertEquals("ONAP Widget", ctx.getAttribute("testPath.widget.window.title"));
+        assertEquals("200", ctx.getAttribute("testPath.widget.window.width"));
+    }
+
+    @Test
+    public void testEmbeddedEscapedJsonJsonStringToCtx() throws Exception {
+        String path = "src/test/resources/EmbeddedEscapedJson.json";
+        String content = new String(Files.readAllBytes(Paths.get(path)));
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("input", content);
+
+        Map<String, String> parameters = new HashMap<String, String>();
+        parameters.put("outputPath", "testPath");
+        parameters.put("isEscaped", "false");
+        parameters.put("source", "input");
+
+        SliPluginUtils.jsonStringToCtx(parameters, ctx);
+
+        assertEquals("escapedJsonObject", ctx.getAttribute("testPath.input.parameters[0].name"));
+        assertEquals("[{\"id\":\"0.2.0.0/16\"},{\"id\":\"ge04::/64\"}]",
+                ctx.getAttribute("testPath.input.parameters[0].value"));
+        assertEquals("Hello/World", ctx.getAttribute("testPath.input.parameters[1].value"));
+        assertEquals("resourceName", ctx.getAttribute("testPath.input.parameters[2].name"));
+        assertEquals("The\t\"Best\"\tName", ctx.getAttribute("testPath.input.parameters[2].value"));
+        assertEquals("3", ctx.getAttribute("testPath.input.parameters_length"));
+
+
+        // Break the embedded json object into properties
+        parameters.put("outputPath", "testPath.input.parameters[0].value");
+        parameters.put("source", "testPath.input.parameters[0].value");
+        SliPluginUtils.jsonStringToCtx(parameters, ctx);
+
+        assertEquals("0.2.0.0/16", ctx.getAttribute("testPath.input.parameters[0].value.[0].id"));
+        assertEquals("ge04::/64", ctx.getAttribute("testPath.input.parameters[0].value.[1].id"));
+        assertEquals("2", ctx.getAttribute("testPath.input.parameters[0].value._length"));
+    }
+
+    @Test
+    public void testEscapedJsonStringToCtx() throws Exception {
+        String path = "src/test/resources/EscapedJson.json";
+        String content = new String(Files.readAllBytes(Paths.get(path)));
+
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("input", content);
+
+        Map<String, String> parameters = new HashMap<String, String>();
+        parameters.put("outputPath", "testPath");
+        parameters.put("isEscaped", "true"); // set to true because the entire json content has been escaped
+        parameters.put("source", "input");
+
+
+        SliPluginUtils.jsonStringToCtx(parameters, ctx);
+        assertEquals("false", ctx.getAttribute("testPath.widget.debug"));
+        assertEquals("center", ctx.getAttribute("testPath.widget.image.alignment"));
+        assertEquals("150", ctx.getAttribute("testPath.widget.image.hOffset"));
+        assertEquals("moon", ctx.getAttribute("testPath.widget.image.name"));
+        assertEquals("images/moon.png", ctx.getAttribute("testPath.widget.image.src"));
+        assertEquals("150", ctx.getAttribute("testPath.widget.image.vOffset"));
+        assertEquals("center", ctx.getAttribute("testPath.widget.text.alignment"));
+        assertEquals("Click Me", ctx.getAttribute("testPath.widget.text.data"));
+        assertEquals("350", ctx.getAttribute("testPath.widget.text.hOffset"));
+        assertEquals("text1", ctx.getAttribute("testPath.widget.text.name"));
+        assertEquals("21", ctx.getAttribute("testPath.widget.text.size"));
+        assertEquals("bold", ctx.getAttribute("testPath.widget.text.style"));
+        assertEquals("200", ctx.getAttribute("testPath.widget.text.vOffset"));
+        assertEquals("300", ctx.getAttribute("testPath.widget.window.height"));
+        assertEquals("main_window", ctx.getAttribute("testPath.widget.window.name"));
+        assertEquals("ONAP Widget", ctx.getAttribute("testPath.widget.window.title"));
+        assertEquals("200", ctx.getAttribute("testPath.widget.window.width"));
+    }
+
 }
 
 package org.onap.ccsdk.sli.core.slipluginutils;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
 import java.util.HashMap;
 import java.util.Map;
-
 import org.junit.Test;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
-import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils.LogLevel;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.slf4j.Marker;
-
-import com.google.gson.JsonObject;
 
-public class CheckParametersTest {
+public class SliPluginUtils_checkParametersTest {
 
     @Test
     public void nullRequiredParameters() throws Exception {
@@ -123,94 +114,4 @@ public class CheckParametersTest {
 
         SliPluginUtils.requiredParameters(parameters, ctx);
     }
-
-    @Test(expected = SvcLogicException.class)
-    public void testJsonStringToCtx() throws Exception {
-        SvcLogicContext ctx = new SvcLogicContext();
-        Map<String, String> parameters = new HashMap<String, String>();
-        parameters.put("outputPath", "testPath");
-        parameters.put("isEscaped", "true");
-        parameters.put("source", "//{/name1/:value1/}//");
-        SliPluginUtils.jsonStringToCtx(parameters, ctx);
-    }
-
-    @Test
-    public void testGetAttributeValue() throws Exception {
-        SvcLogicContext ctx = new SvcLogicContext();
-        Map<String, String> parameters = new HashMap<String, String>();
-        parameters.put("outputPath", "testPath");
-        parameters.put("source", "testSource");
-        SliPluginUtils.getAttributeValue(parameters, ctx);
-        assertNull(ctx.getAttribute(parameters.get("outputPath")));
-    }
-
-    @Test
-    public void testCtxListContains() throws Exception {
-        SvcLogicContext ctx = new SvcLogicContext();
-        Map<String, String> parameters = new HashMap<String, String>();
-        parameters.put("list", "10_length");
-        parameters.put("keyName", "testName");
-        parameters.put("keyValue", "testValue");
-        ctx.setAttribute("10_length", "10");
-        assertEquals("false", SliPluginUtils.ctxListContains(parameters, ctx));
-
-    }
-    
-    @Test(expected= SvcLogicException.class)
-    public void testPrintContextForNullParameters() throws SvcLogicException
-    {
-        SvcLogicContext ctx = new SvcLogicContext();
-        Map<String, String> parameters = new HashMap<String, String>();
-        SliPluginUtils.printContext(parameters, ctx);
-    }
-    
-    @Test
-    public void testPrintContext() throws SvcLogicException
-    {
-        SvcLogicContext ctx = new SvcLogicContext();
-        Map<String, String> parameters = new HashMap<String, String>();
-        parameters.put("filename","testFileName");
-        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()
-    {
-        SvcLogicContext ctx = new SvcLogicContext();
-        ctx.setAttribute("key", "");
-        assertTrue(SliPluginUtils.ctxKeyEmpty(ctx, "key"));
-    }
-    
-    @Test
-    public void testGetArrayLength()
-    {
-        SvcLogicContext ctx = new SvcLogicContext();
-        ctx.setAttribute("key_length", "test");
-        Logger log = LoggerFactory.getLogger(getClass());
-        SliPluginUtils.getArrayLength(ctx, "key", log , LogLevel.INFO, "invalid input");
-    }
-    
-    @Test
-    public void testSetPropertiesForRoot()
-    {
-        SvcLogicContext ctx = new SvcLogicContext();
-        Map<String, String> parameters= new HashMap<>();
-        parameters.put("root","RootVal");
-        parameters.put("valueRoot", "ValueRootVal");
-        assertEquals("success",SliPluginUtils.setPropertiesForRoot(parameters,ctx));
-    }
 }
index d8d78a0..3a6c31a 100644 (file)
@@ -271,6 +271,17 @@ public class SliStringUtilsTest {
                assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));
        }
 
+    @Test
+    public void urlDecode() throws SvcLogicException {
+        String sourceString = "102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4";
+        String outputPath = "out";
+
+        param.put("source", sourceString);
+        param.put("outputPath", outputPath);
+        SliStringUtils.urlDecode(param, ctx);
+        assertEquals("102/GE100/SNJSCAMCJP8/SNJSCAMCJT4", ctx.getAttribute(outputPath));
+    }
+
        @Test
        public void testXmlEscapeText() {
                param.put("source", "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4");
@@ -317,8 +328,6 @@ public class SliStringUtilsTest {
 
     @Test
     public void isEmpty() throws Exception {
-        ctx = new SvcLogicContext();
-        param = new HashMap<>();
         String result = SliStringUtils.isEmpty(param, ctx);
         param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
@@ -339,8 +348,6 @@ public class SliStringUtilsTest {
 
     @Test
     public void isBlank() throws Exception {
-        ctx = new SvcLogicContext();
-        param = new HashMap<>();
         String result = SliStringUtils.isBlank(param, ctx);
         param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
@@ -361,8 +368,6 @@ public class SliStringUtilsTest {
 
     @Test
     public void isNull() throws Exception {
-        ctx = new SvcLogicContext();
-        param = new HashMap<>();
         String result = SliStringUtils.isNull(param, ctx);
         param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
diff --git a/sliPluginUtils/provider/src/test/resources/2dArray.json b/sliPluginUtils/provider/src/test/resources/2dArray.json
new file mode 100644 (file)
index 0000000..b473864
--- /dev/null
@@ -0,0 +1,4 @@
+[\r
+       ["apple", "orange", "banana"],\r
+       ["squash", "broccoli", "cauliflower"]\r
+]
\ No newline at end of file
diff --git a/sliPluginUtils/provider/src/test/resources/3dArray.json b/sliPluginUtils/provider/src/test/resources/3dArray.json
new file mode 100644 (file)
index 0000000..1499555
--- /dev/null
@@ -0,0 +1,4 @@
+[\r
+       [["a","b","c"], ["d","e","f"]],\r
+       [["x","y","z"]]\r
+]
\ No newline at end of file
diff --git a/sliPluginUtils/provider/src/test/resources/ArrayMenu.json b/sliPluginUtils/provider/src/test/resources/ArrayMenu.json
new file mode 100644 (file)
index 0000000..b12f163
--- /dev/null
@@ -0,0 +1,41 @@
+[{\r
+               "id": "1",\r
+               "type": "pizza",\r
+               "name": "plain",\r
+               "calories": 1000,\r
+               "vegetarian": true\r
+       }, {\r
+               "id": "2",\r
+               "type": "pizza",\r
+               "name": "Tuesday Special",\r
+               "calories": 2000,\r
+               "vegetarian": false,\r
+               "topping":\r
+               [{\r
+                               "id": "1",\r
+                               "name": "onion"\r
+                       }, {\r
+                               "id": "2",\r
+                               "name": "pepperoni"\r
+                       }\r
+               ]\r
+       }, {\r
+               "id": "3",\r
+               "type": "pizza",\r
+               "name": "House Special",\r
+               "calories": 1500,\r
+               "vegetarian": true,\r
+               "topping":\r
+               [{\r
+                               "id": "3",\r
+                               "name": "basil"\r
+                       }, {\r
+                               "id": "4",\r
+                               "name": "fresh mozzarella"\r
+                       }, {\r
+                               "id": "5",\r
+                               "name": "tomato"\r
+                       }\r
+               ]\r
+       }\r
+]\r
diff --git a/sliPluginUtils/provider/src/test/resources/EmbeddedEscapedJson.json b/sliPluginUtils/provider/src/test/resources/EmbeddedEscapedJson.json
new file mode 100644 (file)
index 0000000..dbb6d8d
--- /dev/null
@@ -0,0 +1,16 @@
+{\r
+       "input": {\r
+               "parameters":\r
+               [{\r
+                               "name": "escapedJsonObject",\r
+                               "value": "[{\"id\":\"0.2.0.0\/16\"},{\"id\":\"ge04::\/64\"}]"\r
+                       }, {\r
+                               "name": "password",\r
+                               "value": "Hello\/World"\r
+                       }, {\r
+                               "name": "resourceName",\r
+                               "value": "The\t\"Best\"\tName"\r
+                       }\r
+               ]\r
+       }\r
+}
\ No newline at end of file
diff --git a/sliPluginUtils/provider/src/test/resources/EscapedJson.json b/sliPluginUtils/provider/src/test/resources/EscapedJson.json
new file mode 100644 (file)
index 0000000..a7719e8
--- /dev/null
@@ -0,0 +1 @@
+{\"widget\":{\"debug\":false,\"window\":{\"title\":\"ONAP Widget\",\"name\":\"main_window\",\"width\":200,\"height\":300},\"image\":{\"src\":\"images\/moon.png\",\"name\":\"moon\",\"hOffset\":150,\"vOffset\":150,\"alignment\":\"center\"},\"text\":{\"data\":\"Click Me\",\"size\":21,\"style\":\"bold\",\"name\":\"text1\",\"hOffset\":350,\"vOffset\":200,\"alignment\":\"center\"}}}
\ No newline at end of file
diff --git a/sliPluginUtils/provider/src/test/resources/ObjectMenu.json b/sliPluginUtils/provider/src/test/resources/ObjectMenu.json
new file mode 100644 (file)
index 0000000..56f842d
--- /dev/null
@@ -0,0 +1,43 @@
+{\r
+       "menu": [{\r
+                       "id": "1",\r
+                       "type": "pizza",\r
+                       "name": "plain",\r
+                       "calories": 1000,\r
+                       "vegetarian": true\r
+               }, {\r
+                       "id": "2",\r
+                       "type": "pizza",\r
+                       "name": "Tuesday Special",\r
+                       "calories": 2000,\r
+                       "vegetarian": false,\r
+                       "topping":\r
+                       [{\r
+                                       "id": "1",\r
+                                       "name": "onion"\r
+                               }, {\r
+                                       "id": "2",\r
+                                       "name": "pepperoni"\r
+                               }\r
+                       ]\r
+               }, {\r
+                       "id": "3",\r
+                       "type": "pizza",\r
+                       "name": "House Special",\r
+                       "calories": 1500,\r
+                       "vegetarian": true,\r
+                       "topping":\r
+                       [{\r
+                                       "id": "3",\r
+                                       "name": "basil"\r
+                               }, {\r
+                                       "id": "4",\r
+                                       "name": "fresh mozzarella"\r
+                               }, {\r
+                                       "id": "5",\r
+                                       "name": "tomato"\r
+                               }\r
+                       ]\r
+               }\r
+       ]\r
+}\r
diff --git a/sliPluginUtils/provider/src/test/resources/Widget.json b/sliPluginUtils/provider/src/test/resources/Widget.json
new file mode 100644 (file)
index 0000000..1e25282
--- /dev/null
@@ -0,0 +1,27 @@
+{\r
+       "widget": {\r
+               "debug": false,\r
+               "window": {\r
+                       "title": "ONAP Widget",\r
+                       "name": "main_window",\r
+                       "width": 200,\r
+                       "height": 300\r
+               },\r
+               "image": {\r
+                       "src": "images/moon.png",\r
+                       "name": "moon",\r
+                       "hOffset": 150,\r
+                       "vOffset": 150,\r
+                       "alignment": "center"\r
+               },\r
+               "text": {\r
+                       "data": "Click Me",\r
+                       "size": 21,\r
+                       "style": "bold",\r
+                       "name": "text1",\r
+                       "hOffset": 350,\r
+                       "vOffset": 200,\r
+                       "alignment": "center"\r
+               }\r
+       }\r
+}
\ No newline at end of file