diff options
2 files changed, 58 insertions, 0 deletions
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 574178e3..888ef883 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 @@ -437,4 +437,42 @@ public class SliStringUtils implements SvcLogicJavaPlugin { source = StringEscapeUtils.escapeXml(source); ctx.setAttribute(target, source); } + + /** + * unescapeJsonString takes an escaped json string stored as a single property in context memory and unescapes it storing it as a single property + * @param parameters - requires source and outputPath to not be null. + * @param ctx Reference to context memory + * @throws SvcLogicException if a required parameter is missing an exception is thrown + */ + public static void unescapeJsonString(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException { + SliPluginUtils.checkParameters(parameters, new String[] { INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET }, LOG); + try { + String source = parameters.get(INPUT_PARAM_SOURCE); + String target = parameters.get(INPUT_PARAM_TARGET); + String unescapedJson = StringEscapeUtils.unescapeJson(source); + ctx.setAttribute(target, unescapedJson); + } catch (Exception ex) { + ex.printStackTrace(); + throw new SvcLogicException("problem with unescapeJsonString", ex); + } + } + + /** + * escapeJsonString takes json stored as a single string in context memory and escapes it storing it as a single property + * @param parameters - requires source and outputPath to not be null. + * @param ctx Reference to context memory + * @throws SvcLogicException if a required parameter is missing an exception is thrown + */ + public static void escapeJsonString(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException { + SliPluginUtils.checkParameters(parameters, new String[] { INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET }, LOG); + try { + String source = parameters.get(INPUT_PARAM_SOURCE); + String target = parameters.get(INPUT_PARAM_TARGET); + String unescapedJson = StringEscapeUtils.escapeJson(source); + ctx.setAttribute(target, unescapedJson); + } catch (Exception ex) { + ex.printStackTrace(); + throw new SvcLogicException("problem with escapeJsonString", ex); + } + } } diff --git a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtilsTest.java b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtilsTest.java index 085d46ef..b88e8d2c 100644 --- a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtilsTest.java +++ b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtilsTest.java @@ -297,4 +297,24 @@ public class SliStringUtilsTest { utils.substring(param, ctx); } + @Test + public void testUnescapeJsonString() throws Exception { + String source = "{\\\"image_name\\\":\\\"Ubuntu 14.04\\\",\\\"service-instance-id\\\":\\\"1\\\",\\\"vnf-model-customization-uuid\\\":\\\"2f\\\",\\\"vnf-id\\\":\\\"3b\\\"}"; + param.put(SliStringUtils.INPUT_PARAM_SOURCE, source); + String outputPath = "unescaped"; + param.put(SliStringUtils.INPUT_PARAM_TARGET, outputPath); + SliStringUtils.unescapeJsonString(param, ctx); + assertEquals("{\"image_name\":\"Ubuntu 14.04\",\"service-instance-id\":\"1\",\"vnf-model-customization-uuid\":\"2f\",\"vnf-id\":\"3b\"}", ctx.getAttribute(outputPath)); + } + + @Test + public void testEscapeJsonString() throws Exception { + String source = "{\"image_name\":\"Ubuntu 14.04\",\"service-instance-id\":\"1\",\"vnf-model-customization-uuid\":\"2f\",\"vnf-id\":\"3b\"}"; + param.put(SliStringUtils.INPUT_PARAM_SOURCE, source); + String outputPath = "unescaped"; + param.put(SliStringUtils.INPUT_PARAM_TARGET, outputPath); + SliStringUtils.escapeJsonString(param, ctx); + assertEquals("{\\\"image_name\\\":\\\"Ubuntu 14.04\\\",\\\"service-instance-id\\\":\\\"1\\\",\\\"vnf-model-customization-uuid\\\":\\\"2f\\\",\\\"vnf-id\\\":\\\"3b\\\"}", ctx.getAttribute(outputPath)); + } + } |