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;
38 import org.onap.ccsdk.sli.core.slipluginutils.SliStringUtils;
44 public class SliStringUtilsTest {
45 private SvcLogicContext ctx;
46 private HashMap<String, String> param;
47 private SliStringUtils stringUtils = new SliStringUtils();
50 * @throws java.lang.Exception
53 public void setUp() throws Exception {
54 this.ctx = new SvcLogicContext();
55 param = new HashMap<String, String>();
59 * @throws SvcLogicException
60 * @see SliStringUtils#split(Map, SvcLogicContext)
63 public final void testSplit() throws SvcLogicException {
64 param.put("original_string", "one ## two ## three");
65 param.put("regex", " ## ");
66 param.put("ctx_memory_result_key", "result");
68 stringUtils.split(param, ctx);
70 assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
71 assertThat(ctx.getAttribute("result[1]"), equalTo("two"));
72 assertThat(ctx.getAttribute("result[2]"), equalTo("three"));
73 assertThat(ctx.getAttribute("result_length"), equalTo("3"));
77 * @throws SvcLogicException
78 * @see SliStringUtils#split(Map, SvcLogicContext)
81 public final void testSplit_limit() throws SvcLogicException {
82 param.put("original_string", "one ## two ## three");
83 param.put("regex", " ## ");
84 param.put("limit", "2");
85 param.put("ctx_memory_result_key", "result");
87 stringUtils.split(param, ctx);
89 assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
90 assertThat(ctx.getAttribute("result[1]"), equalTo("two ## three"));
91 assertThat(ctx.getAttribute("result_length"), equalTo("2"));
95 public void equalsIgnoreCaseTrue() throws SvcLogicException {
96 String sourceString = "HeLlOwORLD";
97 String targetSTring = "HELLOWORLD";
98 param.put("source", sourceString);
99 param.put("target", targetSTring);
100 assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
104 public void equalsIgnoreCaseFalse() throws SvcLogicException {
105 String sourceString = "HeLlOwORLD";
106 String targetSTring = "goodbyeWORLD";
107 param.put("source", sourceString);
108 param.put("target", targetSTring);
109 assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
113 public void toUpper() throws SvcLogicException {
114 String sourceString = "HeLlOwORLD";
115 param.put("source", sourceString);
116 String path = "my.unique.path.";
117 param.put("outputPath", path);
118 SliStringUtils.toUpper(param, ctx);
119 assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
123 public void toLower() throws SvcLogicException {
124 String sourceString = "HeLlOwORLD";
125 param.put("source", sourceString);
126 String path = "my.unique.path.";
127 param.put("outputPath", path);
128 SliStringUtils.toLower(param, ctx);
129 assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
133 public void containsTrue() throws SvcLogicException {
134 String sourceString = "Pizza";
135 String targetSTring = "izza";
136 param.put("source", sourceString);
137 param.put("target", targetSTring);
138 assertEquals("true", SliStringUtils.contains(param, ctx));
142 public void containsFalse() throws SvcLogicException {
143 String sourceString = "Pizza";
144 String targetSTring = "muffin";
145 param.put("source", sourceString);
146 param.put("target", targetSTring);
147 assertEquals("false", SliStringUtils.contains(param, ctx));
151 public void endsWithTrue() throws SvcLogicException {
152 String sourceString = "Pizza";
153 String targetSTring = "za";
154 param.put("source", sourceString);
155 param.put("target", targetSTring);
156 assertEquals("true", SliStringUtils.endsWith(param, ctx));
160 public void endsWithFalse() throws SvcLogicException {
161 String sourceString = "Pizza";
162 String targetSTring = "muffin";
163 param.put("source", sourceString);
164 param.put("target", targetSTring);
165 assertEquals("false", SliStringUtils.endsWith(param, ctx));
169 public void trim() throws SvcLogicException {
170 String sourceString = " H E L L O W O R L D";
171 String outputPath = "muffin";
172 param.put("source", sourceString);
173 param.put("outputPath", outputPath);
174 SliStringUtils.trim(param, ctx);
175 assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
179 public void getLength() throws SvcLogicException {
180 String sourceString = "SomeRandomString";
181 String outputPath = "muffin";
182 param.put("source", sourceString);
183 param.put("outputPath", outputPath);
184 SliStringUtils.getLength(param, ctx);
185 assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
189 public void startsWithFalse() throws SvcLogicException {
190 String sourceString = "Java";
191 String targetSTring = "DG";
192 param.put("source", sourceString);
193 param.put("target", targetSTring);
194 assertEquals("false", SliStringUtils.startsWith(param, ctx));
198 public void startsWithTrue() throws SvcLogicException {
199 String sourceString = "Java";
200 String targetSTring = "Ja";
201 param.put("source", sourceString);
202 param.put("target", targetSTring);
203 assertEquals("true", SliStringUtils.startsWith(param, ctx));
207 public void replace() throws SvcLogicException {
208 String sourceString = "cat Hello World cat";
211 String outputPath = "out";
213 param.put("source", sourceString);
214 param.put("target", old);
215 param.put("replacement", neww);
216 param.put("outputPath", outputPath);
217 SliStringUtils.replace(param, ctx);
218 assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
222 public void concat() throws SvcLogicException {
223 String sourceString = "cat";
224 String targetString = "dog";
225 String outputPath = "out";
227 param.put("source", sourceString);
228 param.put("target", targetString);
229 param.put("outputPath", outputPath);
230 SliStringUtils.concat(param, ctx);
231 assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
235 public void urlEncode() throws SvcLogicException {
236 String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
237 String outputPath = "out";
239 param.put("source", sourceString);
240 param.put("outputPath", outputPath);
241 SliStringUtils.urlEncode(param, ctx);
242 assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));