SliStringUtils 81/97281/3
authorSmokowski, Kevin (ks6305) <kevin.smokowski@att.com>
Thu, 17 Oct 2019 19:03:35 +0000 (19:03 +0000)
committerKevin Smokowski <kevin.smokowski@att.com>
Wed, 30 Oct 2019 18:45:34 +0000 (18:45 +0000)
additional functions in SliStringUtils added

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

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/SliStringUtilsTest.java

index fd3364c..b8e88f6 100644 (file)
@@ -39,8 +39,8 @@ import java.util.Objects;
 import java.util.Properties;
 import java.util.Set;
 import java.util.UUID;
-import org.apache.commons.text.StringEscapeUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.text.StringEscapeUtils;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
@@ -1110,4 +1110,13 @@ public class SliPluginUtils implements SvcLogicJavaPlugin {
                return changeFlag;
        }
 
+    public static String containsKey(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
+        String key = parameters.get("key");
+        Boolean keyFound = ctx.getAttributeKeySet().contains(key);
+        if (keyFound) {
+            return "true";
+        }
+        return "false";
+    }
+
 }
index 888ef88..269c376 100644 (file)
@@ -39,10 +39,12 @@ import org.slf4j.LoggerFactory;
  */
 public class SliStringUtils implements SvcLogicJavaPlugin {
        private static final Logger LOG = LoggerFactory.getLogger(SliStringUtils.class);
-
+       public static final String INPUT_PARAM_KEY = "key";
        public static final String INPUT_PARAM_SOURCE = "source";
        public static final String INPUT_PARAM_TARGET = "target";
-       
+    public static final String TRUE_CONSTANT = "true";
+    public static final String FALSE_CONSTANT = "false";
+
        public SliStringUtils() {}
 
        /**
@@ -125,11 +127,11 @@ public class SliStringUtils implements SvcLogicJavaPlugin {
      * @since 11.0.2
      */
     public static String equalsIgnoreCase(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
-        SliPluginUtils.checkParameters(parameters, new String[]{INPUT_PARAM_SOURCE,"target"}, LOG);
-        if(parameters.get(INPUT_PARAM_SOURCE).equalsIgnoreCase(parameters.get("target"))){
-            return "true";
+        SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET}, LOG);
+        if (parameters.get(INPUT_PARAM_SOURCE).equalsIgnoreCase(parameters.get(INPUT_PARAM_TARGET))) {
+            return TRUE_CONSTANT;
         }
-        return "false";
+        return FALSE_CONSTANT;
     }
 
     /**
@@ -189,11 +191,11 @@ public class SliStringUtils implements SvcLogicJavaPlugin {
      * @since 11.0.2
      */
     public static String contains(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
-        SliPluginUtils.checkParameters(parameters, new String[]{INPUT_PARAM_SOURCE,"target"}, LOG);
-        if(parameters.get(INPUT_PARAM_SOURCE).contains(parameters.get("target"))){
-            return "true";
+        SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET}, LOG);
+        if (parameters.get(INPUT_PARAM_SOURCE).contains(parameters.get(INPUT_PARAM_TARGET))) {
+            return TRUE_CONSTANT;
         }
-        return "false";
+        return FALSE_CONSTANT;
     }
 
     /**
@@ -213,11 +215,11 @@ public class SliStringUtils implements SvcLogicJavaPlugin {
      * @since 11.0.2
      */
     public static String endsWith(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
-        SliPluginUtils.checkParameters(parameters, new String[]{INPUT_PARAM_SOURCE,"target"}, LOG);
-        if(parameters.get(INPUT_PARAM_SOURCE).endsWith(parameters.get("target"))){
-            return "true";
+        SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET}, LOG);
+        if (parameters.get(INPUT_PARAM_SOURCE).endsWith(parameters.get(INPUT_PARAM_TARGET))) {
+            return TRUE_CONSTANT;
         }
-        return "false";
+        return FALSE_CONSTANT;
     }
 
     /**
@@ -237,11 +239,11 @@ public class SliStringUtils implements SvcLogicJavaPlugin {
      * @since 11.0.2
      */
     public static String startsWith(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
-        SliPluginUtils.checkParameters(parameters, new String[]{INPUT_PARAM_SOURCE,"target"}, LOG);
-        if(parameters.get(INPUT_PARAM_SOURCE).startsWith(parameters.get("target"))){
-            return "true";
+        SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET}, LOG);
+        if (parameters.get(INPUT_PARAM_SOURCE).startsWith(parameters.get(INPUT_PARAM_TARGET))) {
+            return TRUE_CONSTANT;
         }
-        return "false";
+        return FALSE_CONSTANT;
     }
 
     /**
@@ -302,8 +304,10 @@ public class SliStringUtils implements SvcLogicJavaPlugin {
      * @since 11.0.2
      */
     public static void replace(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
-        SliPluginUtils.checkParameters(parameters, new String[]{INPUT_PARAM_SOURCE,"outputPath","target","replacement"}, LOG);
-        ctx.setAttribute(parameters.get("outputPath"), (parameters.get(INPUT_PARAM_SOURCE).replace(parameters.get("target"), parameters.get("replacement"))));
+        SliPluginUtils.checkParameters(parameters,
+                new String[] {INPUT_PARAM_SOURCE, "outputPath", INPUT_PARAM_TARGET, "replacement"}, LOG);
+        ctx.setAttribute(parameters.get("outputPath"), (parameters.get(INPUT_PARAM_SOURCE)
+                .replace(parameters.get(INPUT_PARAM_TARGET), parameters.get("replacement"))));
     }
 
     /**
@@ -324,8 +328,10 @@ public class SliStringUtils implements SvcLogicJavaPlugin {
      * @since 11.0.2
      */
     public static void replaceAll(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
-        SliPluginUtils.checkParameters(parameters, new String[]{INPUT_PARAM_SOURCE,"outputPath","target","replacement"}, LOG);
-        ctx.setAttribute(parameters.get("outputPath"), parameters.get(INPUT_PARAM_SOURCE).replaceAll(parameters.get("target"), parameters.get("replacement")));
+        SliPluginUtils.checkParameters(parameters,
+                new String[] {INPUT_PARAM_SOURCE, "outputPath", INPUT_PARAM_TARGET, "replacement"}, LOG);
+        ctx.setAttribute(parameters.get("outputPath"), parameters.get(INPUT_PARAM_SOURCE)
+                .replaceAll(parameters.get(INPUT_PARAM_TARGET), parameters.get("replacement")));
     }
     
     /**
@@ -385,8 +391,9 @@ public class SliStringUtils implements SvcLogicJavaPlugin {
      * @since 11.0.2
      */
     public static void concat( Map<String, String> parameters, SvcLogicContext ctx ) throws SvcLogicException {
-            SliPluginUtils.checkParameters( parameters, new String[]{INPUT_PARAM_SOURCE,"target","outputPath"}, LOG );
-            String result = parameters.get(INPUT_PARAM_SOURCE).concat(parameters.get("target"));
+        SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, INPUT_PARAM_TARGET, "outputPath"},
+                LOG);
+        String result = parameters.get(INPUT_PARAM_SOURCE).concat(parameters.get(INPUT_PARAM_TARGET));
             ctx.setAttribute(parameters.get("outputPath"), result);
     }
 
@@ -475,4 +482,31 @@ public class SliStringUtils implements SvcLogicJavaPlugin {
            throw new SvcLogicException("problem with escapeJsonString", ex);
        }
     }
+
+    public static String isBlank(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
+        String ctxLocation = parameters.get(INPUT_PARAM_KEY);
+        String str = ctx.getAttribute(ctxLocation);
+        if (str == null || str.isEmpty() || " ".equals(str)) {
+            return TRUE_CONSTANT;
+        }
+        return FALSE_CONSTANT;
+    }
+
+    public static String isEmpty(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
+        String ctxLocation = parameters.get(INPUT_PARAM_KEY);
+        String str = ctx.getAttribute(ctxLocation);
+        if (str == null || str.isEmpty()) {
+            return TRUE_CONSTANT;
+        }
+        return FALSE_CONSTANT;
+    }
+
+    public static String isNull(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
+        String ctxLocation = parameters.get(INPUT_PARAM_KEY);
+        String str = ctx.getAttribute(ctxLocation);
+        if (str == null) {
+            return TRUE_CONSTANT;
+        }
+        return FALSE_CONSTANT;
+    }
 }
index 774aa1c..08adc97 100644 (file)
@@ -24,10 +24,8 @@ 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 java.util.HashMap;
 import java.util.Map;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
@@ -247,4 +245,23 @@ public class SliPluginUtils_StaticFunctionsTest {
         SliPluginUtils.setTime(parameters, ctx);
         assertNotNull(ctx.getAttribute(outputPath));
     }
+
+    @Test
+    public void containsKey() throws Exception {
+        ctx = new SvcLogicContext();
+        parameters.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
+        String result = SliPluginUtils.containsKey(parameters, ctx);
+        assertEquals(SliStringUtils.FALSE_CONSTANT, result);
+
+        ctx.setAttribute("a", null);
+        parameters.put(SliStringUtils.INPUT_PARAM_KEY, "a");
+        result = SliPluginUtils.containsKey(parameters, ctx);
+        assertEquals(SliStringUtils.FALSE_CONSTANT, result);
+
+        ctx.setAttribute("a", "hellworld");
+        parameters.put(SliStringUtils.INPUT_PARAM_KEY, "a");
+        result = SliPluginUtils.containsKey(parameters, ctx);
+        assertEquals(SliStringUtils.TRUE_CONSTANT, result);
+    }
+
 }
index b88e8d2..d8d78a0 100644 (file)
@@ -26,10 +26,8 @@ package org.onap.ccsdk.sli.core.slipluginutils;
 import static org.hamcrest.Matchers.equalTo;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
-
 import java.util.HashMap;
 import java.util.Map;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
@@ -317,4 +315,70 @@ public class SliStringUtilsTest {
                assertEquals("{\\\"image_name\\\":\\\"Ubuntu 14.04\\\",\\\"service-instance-id\\\":\\\"1\\\",\\\"vnf-model-customization-uuid\\\":\\\"2f\\\",\\\"vnf-id\\\":\\\"3b\\\"}", ctx.getAttribute(outputPath));
        }
 
+    @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);
+
+        ctx.setAttribute("a", null);
+        param.put(SliStringUtils.INPUT_PARAM_KEY, "a");
+        result = SliStringUtils.isEmpty(param, ctx);
+        assertEquals(SliStringUtils.TRUE_CONSTANT, result);
+
+        ctx.setAttribute("a", "");
+        result = SliStringUtils.isEmpty(param, ctx);
+        assertEquals(SliStringUtils.TRUE_CONSTANT, result);
+
+        ctx.setAttribute("a", " ");
+        result = SliStringUtils.isEmpty(param, ctx);
+        assertEquals(SliStringUtils.FALSE_CONSTANT, result);
+    }
+
+    @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);
+
+        ctx.setAttribute("a", null);
+        param.put(SliStringUtils.INPUT_PARAM_KEY, "a");
+        result = SliStringUtils.isBlank(param, ctx);
+        assertEquals(SliStringUtils.TRUE_CONSTANT, result);
+
+        ctx.setAttribute("a", "");
+        result = SliStringUtils.isBlank(param, ctx);
+        assertEquals(SliStringUtils.TRUE_CONSTANT, result);
+
+        ctx.setAttribute("a", " ");
+        result = SliStringUtils.isBlank(param, ctx);
+        assertEquals(SliStringUtils.TRUE_CONSTANT, result);
+    }
+
+    @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);
+
+        ctx.setAttribute("a", null);
+        param.put(SliStringUtils.INPUT_PARAM_KEY, "a");
+        result = SliStringUtils.isNull(param, ctx);
+        assertEquals(SliStringUtils.TRUE_CONSTANT, result);
+
+        ctx.setAttribute("a", "");
+        result = SliStringUtils.isNull(param, ctx);
+        assertEquals(SliStringUtils.FALSE_CONSTANT, result);
+
+        ctx.setAttribute("a", " ");
+        result = SliStringUtils.isNull(param, ctx);
+        assertEquals(SliStringUtils.FALSE_CONSTANT, result);
+    }
+
 }