diff options
author | Smokowski, Kevin (ks6305) <kevin.smokowski@att.com> | 2019-12-17 18:27:42 +0000 |
---|---|---|
committer | Smokowski, Kevin (ks6305) <kevin.smokowski@att.com> | 2019-12-18 16:44:13 +0000 |
commit | 2fa35ec7af06c9f1d1982dc500726de703f7dede (patch) | |
tree | 1830d0bc7426422dc022d7cb3dd040b8c334052a /sliPluginUtils/provider/src/main/java | |
parent | a55a4e30507c25c21c7f1df830f5d5189e03c038 (diff) |
enhance SliPluginUtils
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
Diffstat (limited to 'sliPluginUtils/provider/src/main/java')
2 files changed, 42 insertions, 13 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 b8e88f652..1fc052532 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 @@ -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++; } } diff --git a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtils.java b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtils.java index 269c3766c..d343ce25f 100644 --- a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtils.java +++ b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtils.java @@ -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. * |