2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   7  * ================================================================================
 
   8  * Modifications Copyright (C) 2018 IBM.
 
   9  * ================================================================================
 
  10  * Licensed under the Apache License, Version 2.0 (the "License");
 
  11  * you may not use this file except in compliance with the License.
 
  12  * You may obtain a copy of the License at
 
  14  *      http://www.apache.org/licenses/LICENSE-2.0
 
  16  * Unless required by applicable law or agreed to in writing, software
 
  17  * distributed under the License is distributed on an "AS IS" BASIS,
 
  18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  19  * See the License for the specific language governing permissions and
 
  20  * limitations under the License.
 
  21  * ============LICENSE_END=========================================================
 
  24 package org.onap.ccsdk.sli.core.slipluginutils;
 
  26 import static org.hamcrest.Matchers.equalTo;
 
  27 import static org.junit.Assert.assertEquals;
 
  28 import static org.junit.Assert.assertThat;
 
  30 import java.util.HashMap;
 
  33 import org.junit.Before;
 
  34 import org.junit.Test;
 
  35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  42 public class SliStringUtilsTest {
 
  43     private SvcLogicContext ctx;
 
  44     private HashMap<String, String> param;
 
  45     private SliStringUtils stringUtils = new SliStringUtils();
 
  48      * @throws java.lang.Exception
 
  51     public void setUp() throws Exception {
 
  52         this.ctx = new SvcLogicContext();
 
  53         param = new HashMap<String, String>();
 
  57      * @throws SvcLogicException
 
  58      * @see SliStringUtils#split(Map, SvcLogicContext)
 
  61     public final void testSplit() throws SvcLogicException {
 
  62         param.put("original_string", "one ## two ## three");
 
  63         param.put("regex", " ## ");
 
  64         param.put("ctx_memory_result_key", "result");
 
  66         stringUtils.split(param, ctx);
 
  68         assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
 
  69         assertThat(ctx.getAttribute("result[1]"), equalTo("two"));
 
  70         assertThat(ctx.getAttribute("result[2]"), equalTo("three"));
 
  71         assertThat(ctx.getAttribute("result_length"), equalTo("3"));
 
  75      * @throws SvcLogicException
 
  76      * @see SliStringUtils#split(Map, SvcLogicContext)
 
  79     public final void testSplit_limit() throws SvcLogicException {
 
  80         param.put("original_string", "one ## two ## three");
 
  81         param.put("regex", " ## ");
 
  82         param.put("limit", "2");
 
  83         param.put("ctx_memory_result_key", "result");
 
  85         stringUtils.split(param, ctx);
 
  87         assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
 
  88         assertThat(ctx.getAttribute("result[1]"), equalTo("two ## three"));
 
  89         assertThat(ctx.getAttribute("result_length"), equalTo("2"));
 
  93     public final void testSubString() throws SvcLogicException {
 
  94         param.put("string","splitatgivenindex");
 
  95         param.put("begin-index","0");
 
  96         param.put("end-index","5");
 
  97         param.put("result","result");
 
  99         stringUtils.substring(param, ctx);
 
 101         assertEquals("split", ctx.getAttribute("result"));
 
 105     public final void testQuotedOrNull() throws SvcLogicException {
 
 106         //param.put("nullString",null);
 
 107         assertEquals("NULL",SliStringUtils.quotedOrNULL(null));
 
 111     public void equalsIgnoreCaseTrue() throws SvcLogicException {
 
 112         String sourceString = "HeLlOwORLD";
 
 113         String targetSTring = "HELLOWORLD";
 
 114         param.put("source", sourceString);
 
 115         param.put("target", targetSTring);
 
 116         assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
 
 120     public void equalsIgnoreCaseFalse() throws SvcLogicException {
 
 121         String sourceString = "HeLlOwORLD";
 
 122         String targetSTring = "goodbyeWORLD";
 
 123         param.put("source", sourceString);
 
 124         param.put("target", targetSTring);
 
 125         assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
 
 129     public void toUpper() throws SvcLogicException {
 
 130         String sourceString = "HeLlOwORLD";
 
 131         param.put("source", sourceString);
 
 132         String path = "my.unique.path.";
 
 133         param.put("outputPath", path);
 
 134         SliStringUtils.toUpper(param, ctx);
 
 135         assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
 
 139     public void toLower() throws SvcLogicException {
 
 140         String sourceString = "HeLlOwORLD";
 
 141         param.put("source", sourceString);
 
 142         String path = "my.unique.path.";
 
 143         param.put("outputPath", path);
 
 144         SliStringUtils.toLower(param, ctx);
 
 145         assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
 
 149     public void containsTrue() throws SvcLogicException {
 
 150         String sourceString = "Pizza";
 
 151         String targetSTring = "izza";
 
 152         param.put("source", sourceString);
 
 153         param.put("target", targetSTring);
 
 154         assertEquals("true", SliStringUtils.contains(param, ctx));
 
 158     public void containsFalse() throws SvcLogicException {
 
 159         String sourceString = "Pizza";
 
 160         String targetSTring = "muffin";
 
 161         param.put("source", sourceString);
 
 162         param.put("target", targetSTring);
 
 163         assertEquals("false", SliStringUtils.contains(param, ctx));
 
 167     public void endsWithTrue() throws SvcLogicException {
 
 168         String sourceString = "Pizza";
 
 169         String targetSTring = "za";
 
 170         param.put("source", sourceString);
 
 171         param.put("target", targetSTring);
 
 172         assertEquals("true", SliStringUtils.endsWith(param, ctx));
 
 176     public void endsWithFalse() throws SvcLogicException {
 
 177         String sourceString = "Pizza";
 
 178         String targetSTring = "muffin";
 
 179         param.put("source", sourceString);
 
 180         param.put("target", targetSTring);
 
 181         assertEquals("false", SliStringUtils.endsWith(param, ctx));
 
 185     public void trim() throws SvcLogicException {
 
 186         String sourceString = " H E L L O W O R L D";
 
 187         String outputPath = "muffin";
 
 188         param.put("source", sourceString);
 
 189         param.put("outputPath", outputPath);
 
 190         SliStringUtils.trim(param, ctx);
 
 191         assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
 
 195     public void getLength() throws SvcLogicException {
 
 196         String sourceString = "SomeRandomString";
 
 197         String outputPath = "muffin";
 
 198         param.put("source", sourceString);
 
 199         param.put("outputPath", outputPath);
 
 200         SliStringUtils.getLength(param, ctx);
 
 201         assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
 
 205     public void startsWithFalse() throws SvcLogicException {
 
 206         String sourceString = "Java";
 
 207         String targetSTring = "DG";
 
 208         param.put("source", sourceString);
 
 209         param.put("target", targetSTring);
 
 210         assertEquals("false", SliStringUtils.startsWith(param, ctx));
 
 214     public void startsWithTrue() throws SvcLogicException {
 
 215         String sourceString = "Java";
 
 216         String targetSTring = "Ja";
 
 217         param.put("source", sourceString);
 
 218         param.put("target", targetSTring);
 
 219         assertEquals("true", SliStringUtils.startsWith(param, ctx));
 
 223     public void replace() throws SvcLogicException {
 
 224         String sourceString = "cat Hello World cat";
 
 227         String outputPath = "out";
 
 229         param.put("source", sourceString);
 
 230         param.put("target", old);
 
 231         param.put("replacement", neww);
 
 232         param.put("outputPath", outputPath);
 
 233         SliStringUtils.replace(param, ctx);
 
 234         assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
 
 238     public void replaceAll() throws SvcLogicException {
 
 239         String source = "cat Hello World cat";
 
 240         String target = "\\s";
 
 241         String replacement = "";
 
 242         String outputPath = "out";
 
 244         param.put("source", source);
 
 245         param.put("target", target);
 
 246         param.put("replacement", replacement);
 
 247         param.put("outputPath", outputPath);
 
 248         SliStringUtils.replaceAll(param, ctx);
 
 249         assertEquals(source.replaceAll(target, replacement), ctx.getAttribute(outputPath));
 
 253     public void concat() throws SvcLogicException {
 
 254         String sourceString = "cat";
 
 255         String targetString = "dog";
 
 256         String outputPath = "out";
 
 258         param.put("source", sourceString);
 
 259         param.put("target", targetString);
 
 260         param.put("outputPath", outputPath);
 
 261         SliStringUtils.concat(param, ctx);
 
 262         assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
 
 266     public void urlEncode() throws SvcLogicException {
 
 267         String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
 
 268         String outputPath = "out";
 
 270         param.put("source", sourceString);
 
 271         param.put("outputPath", outputPath);
 
 272         SliStringUtils.urlEncode(param, ctx);
 
 273         assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));
 
 277     public void testXmlEscapeText()
 
 279         param.put("source", "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4");
 
 280         param.put("target", "target");
 
 281         SliStringUtils.xmlEscapeText(param,ctx);
 
 282         assertEquals("102/GE100/SNJSCAMCJP8/SNJSCAMCJT4",ctx.getAttribute("target"));