add replaceAll to slistringutils
authorSmokowski, Kevin (ks6305) <ks6305@att.com>
Wed, 25 Apr 2018 21:13:56 +0000 (21:13 +0000)
committerSmokowski, Kevin (ks6305) <ks6305@att.com>
Wed, 25 Apr 2018 21:13:56 +0000 (21:13 +0000)
add replaceAll to slistringutils because it supports regular expressions and replace does not

Change-Id: Ibff7acd339103a6bb897d14ca3d31736a5b568ef
Issue-ID: CCSDK-253
Signed-off-by: Smokowski, Kevin (ks6305) <ks6305@att.com>
sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtils.java
sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtilsTest.java

index 6402abd..63c750c 100644 (file)
@@ -304,6 +304,28 @@ public class SliStringUtils implements SvcLogicJavaPlugin {
         ctx.setAttribute(parameters.get("outputPath"), (parameters.get("source").replace(parameters.get("target"), parameters.get("replacement"))));
     }
 
+    /**
+     * exposes replaceAll to directed graph
+     * writes the length of source to outputPath
+     * @param parameters HashMap<String,String> of parameters passed by the DG to this function
+     * <table border="1">
+     *  <thead><th>parameter</th><th>Mandatory/Optional</th><th>description</th></thead>
+     *  <tbody>
+     *      <tr><td>source</td><td>Mandatory</td><td>source string</td></tr>
+     *      <tr><td>target</td><td>Mandatory</td><td>This should be a valid regular expression</td></tr>
+     *      <tr><td>replacement</td><td>Mandatory</td><td>The replacement sequence of char values</td></tr>
+     *      <tr><td>outputPath</td><td>Mandatory</td><td>the location in context memory the result is written to</td></tr>
+     *  </tbody>
+     * </table>
+     * @param ctx Reference to context memory
+     * @throws SvcLogicException
+     * @since 11.0.2
+     */
+    public static void replaceAll(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
+        SliPluginUtils.checkParameters(parameters, new String[]{"source","outputPath","target","replacement"}, LOG);
+        ctx.setAttribute(parameters.get("outputPath"), parameters.get("source").replaceAll(parameters.get("target"), parameters.get("replacement")));
+    }
+    
     /**
      * Provides substring functionality to Directed Graphs.
      * <p>
index 4fc8d18..4c66512 100644 (file)
@@ -234,6 +234,21 @@ public class SliStringUtilsTest {
         SliStringUtils.replace(param, ctx);
         assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
     }
+    
+    @Test
+    public void replaceAll() throws SvcLogicException {
+        String source = "cat Hello World cat";
+        String target = "\\s";
+        String replacement = "";
+        String outputPath = "out";
+
+        param.put("source", source);
+        param.put("target", target);
+        param.put("replacement", replacement);
+        param.put("outputPath", outputPath);
+        SliStringUtils.replaceAll(param, ctx);
+        assertEquals(source.replaceAll(target, replacement), ctx.getAttribute(outputPath));
+    }
 
     @Test
     public void concat() throws SvcLogicException {