*/
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() {}
/**
* @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;
}
/**
* @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;
}
/**
* @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;
}
/**
* @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;
}
/**
* @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"))));
}
/**
* @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")));
}
/**
* @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);
}
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;
+ }
}
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;
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);
+ }
+
}
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;
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);
+ }
+
}