2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  25 package org.onap.ccsdk.sli.core.slipluginutils;
 
  27 import static org.hamcrest.Matchers.equalTo;
 
  28 import static org.junit.Assert.assertEquals;
 
  29 import static org.junit.Assert.assertThat;
 
  31 import java.util.HashMap;
 
  34 import org.junit.Before;
 
  35 import org.junit.Test;
 
  36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  37 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  43 public class SliStringUtilsTest {
 
  44     private SvcLogicContext ctx;
 
  45     private HashMap<String, String> param;
 
  46     private SliStringUtils stringUtils = new SliStringUtils();
 
  49      * @throws java.lang.Exception
 
  52     public void setUp() throws Exception {
 
  53         this.ctx = new SvcLogicContext();
 
  54         param = new HashMap<String, String>();
 
  58      * @throws SvcLogicException
 
  59      * @see SliStringUtils#split(Map, SvcLogicContext)
 
  62     public final void testSplit() throws SvcLogicException {
 
  63         param.put("original_string", "one ## two ## three");
 
  64         param.put("regex", " ## ");
 
  65         param.put("ctx_memory_result_key", "result");
 
  67         stringUtils.split(param, ctx);
 
  69         assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
 
  70         assertThat(ctx.getAttribute("result[1]"), equalTo("two"));
 
  71         assertThat(ctx.getAttribute("result[2]"), equalTo("three"));
 
  72         assertThat(ctx.getAttribute("result_length"), equalTo("3"));
 
  76      * @throws SvcLogicException
 
  77      * @see SliStringUtils#split(Map, SvcLogicContext)
 
  80     public final void testSplit_limit() throws SvcLogicException {
 
  81         param.put("original_string", "one ## two ## three");
 
  82         param.put("regex", " ## ");
 
  83         param.put("limit", "2");
 
  84         param.put("ctx_memory_result_key", "result");
 
  86         stringUtils.split(param, ctx);
 
  88         assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
 
  89         assertThat(ctx.getAttribute("result[1]"), equalTo("two ## three"));
 
  90         assertThat(ctx.getAttribute("result_length"), equalTo("2"));
 
  94     public final void testSubString() throws SvcLogicException {
 
  95         param.put("string","splitatgivenindex");
 
  96         param.put("begin-index","0");
 
  97         param.put("end-index","5");
 
  98         param.put("result","result");
 
 100         stringUtils.substring(param, ctx);
 
 102         assertEquals("split", ctx.getAttribute("result"));
 
 106     public final void testQuotedOrNull() throws SvcLogicException {
 
 107         //param.put("nullString",null);
 
 108         assertEquals("NULL",SliStringUtils.quotedOrNULL(null));
 
 112     public void equalsIgnoreCaseTrue() throws SvcLogicException {
 
 113         String sourceString = "HeLlOwORLD";
 
 114         String targetSTring = "HELLOWORLD";
 
 115         param.put("source", sourceString);
 
 116         param.put("target", targetSTring);
 
 117         assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
 
 121     public void equalsIgnoreCaseFalse() throws SvcLogicException {
 
 122         String sourceString = "HeLlOwORLD";
 
 123         String targetSTring = "goodbyeWORLD";
 
 124         param.put("source", sourceString);
 
 125         param.put("target", targetSTring);
 
 126         assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
 
 130     public void toUpper() throws SvcLogicException {
 
 131         String sourceString = "HeLlOwORLD";
 
 132         param.put("source", sourceString);
 
 133         String path = "my.unique.path.";
 
 134         param.put("outputPath", path);
 
 135         SliStringUtils.toUpper(param, ctx);
 
 136         assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
 
 140     public void toLower() throws SvcLogicException {
 
 141         String sourceString = "HeLlOwORLD";
 
 142         param.put("source", sourceString);
 
 143         String path = "my.unique.path.";
 
 144         param.put("outputPath", path);
 
 145         SliStringUtils.toLower(param, ctx);
 
 146         assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
 
 150     public void containsTrue() throws SvcLogicException {
 
 151         String sourceString = "Pizza";
 
 152         String targetSTring = "izza";
 
 153         param.put("source", sourceString);
 
 154         param.put("target", targetSTring);
 
 155         assertEquals("true", SliStringUtils.contains(param, ctx));
 
 159     public void containsFalse() throws SvcLogicException {
 
 160         String sourceString = "Pizza";
 
 161         String targetSTring = "muffin";
 
 162         param.put("source", sourceString);
 
 163         param.put("target", targetSTring);
 
 164         assertEquals("false", SliStringUtils.contains(param, ctx));
 
 168     public void endsWithTrue() throws SvcLogicException {
 
 169         String sourceString = "Pizza";
 
 170         String targetSTring = "za";
 
 171         param.put("source", sourceString);
 
 172         param.put("target", targetSTring);
 
 173         assertEquals("true", SliStringUtils.endsWith(param, ctx));
 
 177     public void endsWithFalse() throws SvcLogicException {
 
 178         String sourceString = "Pizza";
 
 179         String targetSTring = "muffin";
 
 180         param.put("source", sourceString);
 
 181         param.put("target", targetSTring);
 
 182         assertEquals("false", SliStringUtils.endsWith(param, ctx));
 
 186     public void trim() throws SvcLogicException {
 
 187         String sourceString = " H E L L O W O R L D";
 
 188         String outputPath = "muffin";
 
 189         param.put("source", sourceString);
 
 190         param.put("outputPath", outputPath);
 
 191         SliStringUtils.trim(param, ctx);
 
 192         assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
 
 196     public void getLength() throws SvcLogicException {
 
 197         String sourceString = "SomeRandomString";
 
 198         String outputPath = "muffin";
 
 199         param.put("source", sourceString);
 
 200         param.put("outputPath", outputPath);
 
 201         SliStringUtils.getLength(param, ctx);
 
 202         assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
 
 206     public void startsWithFalse() throws SvcLogicException {
 
 207         String sourceString = "Java";
 
 208         String targetSTring = "DG";
 
 209         param.put("source", sourceString);
 
 210         param.put("target", targetSTring);
 
 211         assertEquals("false", SliStringUtils.startsWith(param, ctx));
 
 215     public void startsWithTrue() throws SvcLogicException {
 
 216         String sourceString = "Java";
 
 217         String targetSTring = "Ja";
 
 218         param.put("source", sourceString);
 
 219         param.put("target", targetSTring);
 
 220         assertEquals("true", SliStringUtils.startsWith(param, ctx));
 
 224     public void replace() throws SvcLogicException {
 
 225         String sourceString = "cat Hello World cat";
 
 228         String outputPath = "out";
 
 230         param.put("source", sourceString);
 
 231         param.put("target", old);
 
 232         param.put("replacement", neww);
 
 233         param.put("outputPath", outputPath);
 
 234         SliStringUtils.replace(param, ctx);
 
 235         assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
 
 239     public void concat() throws SvcLogicException {
 
 240         String sourceString = "cat";
 
 241         String targetString = "dog";
 
 242         String outputPath = "out";
 
 244         param.put("source", sourceString);
 
 245         param.put("target", targetString);
 
 246         param.put("outputPath", outputPath);
 
 247         SliStringUtils.concat(param, ctx);
 
 248         assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
 
 252     public void urlEncode() throws SvcLogicException {
 
 253         String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
 
 254         String outputPath = "out";
 
 256         param.put("source", sourceString);
 
 257         param.put("outputPath", outputPath);
 
 258         SliStringUtils.urlEncode(param, ctx);
 
 259         assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));