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 testQuotedOrNull() throws SvcLogicException {
95 //param.put("nullString",null);
96 assertEquals("NULL",SliStringUtils.quotedOrNULL(null));
100 public void equalsIgnoreCaseTrue() throws SvcLogicException {
101 String sourceString = "HeLlOwORLD";
102 String targetSTring = "HELLOWORLD";
103 param.put("source", sourceString);
104 param.put("target", targetSTring);
105 assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
109 public void equalsIgnoreCaseFalse() throws SvcLogicException {
110 String sourceString = "HeLlOwORLD";
111 String targetSTring = "goodbyeWORLD";
112 param.put("source", sourceString);
113 param.put("target", targetSTring);
114 assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
118 public void toUpper() throws SvcLogicException {
119 String sourceString = "HeLlOwORLD";
120 param.put("source", sourceString);
121 String path = "my.unique.path.";
122 param.put("outputPath", path);
123 SliStringUtils.toUpper(param, ctx);
124 assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
128 public void toLower() throws SvcLogicException {
129 String sourceString = "HeLlOwORLD";
130 param.put("source", sourceString);
131 String path = "my.unique.path.";
132 param.put("outputPath", path);
133 SliStringUtils.toLower(param, ctx);
134 assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
138 public void containsTrue() throws SvcLogicException {
139 String sourceString = "Pizza";
140 String targetSTring = "izza";
141 param.put("source", sourceString);
142 param.put("target", targetSTring);
143 assertEquals("true", SliStringUtils.contains(param, ctx));
147 public void containsFalse() throws SvcLogicException {
148 String sourceString = "Pizza";
149 String targetSTring = "muffin";
150 param.put("source", sourceString);
151 param.put("target", targetSTring);
152 assertEquals("false", SliStringUtils.contains(param, ctx));
156 public void endsWithTrue() throws SvcLogicException {
157 String sourceString = "Pizza";
158 String targetSTring = "za";
159 param.put("source", sourceString);
160 param.put("target", targetSTring);
161 assertEquals("true", SliStringUtils.endsWith(param, ctx));
165 public void endsWithFalse() throws SvcLogicException {
166 String sourceString = "Pizza";
167 String targetSTring = "muffin";
168 param.put("source", sourceString);
169 param.put("target", targetSTring);
170 assertEquals("false", SliStringUtils.endsWith(param, ctx));
174 public void trim() throws SvcLogicException {
175 String sourceString = " H E L L O W O R L D";
176 String outputPath = "muffin";
177 param.put("source", sourceString);
178 param.put("outputPath", outputPath);
179 SliStringUtils.trim(param, ctx);
180 assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
184 public void getLength() throws SvcLogicException {
185 String sourceString = "SomeRandomString";
186 String outputPath = "muffin";
187 param.put("source", sourceString);
188 param.put("outputPath", outputPath);
189 SliStringUtils.getLength(param, ctx);
190 assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
194 public void startsWithFalse() throws SvcLogicException {
195 String sourceString = "Java";
196 String targetSTring = "DG";
197 param.put("source", sourceString);
198 param.put("target", targetSTring);
199 assertEquals("false", SliStringUtils.startsWith(param, ctx));
203 public void startsWithTrue() throws SvcLogicException {
204 String sourceString = "Java";
205 String targetSTring = "Ja";
206 param.put("source", sourceString);
207 param.put("target", targetSTring);
208 assertEquals("true", SliStringUtils.startsWith(param, ctx));
212 public void replace() throws SvcLogicException {
213 String sourceString = "cat Hello World cat";
216 String outputPath = "out";
218 param.put("source", sourceString);
219 param.put("target", old);
220 param.put("replacement", neww);
221 param.put("outputPath", outputPath);
222 SliStringUtils.replace(param, ctx);
223 assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
227 public void concat() throws SvcLogicException {
228 String sourceString = "cat";
229 String targetString = "dog";
230 String outputPath = "out";
232 param.put("source", sourceString);
233 param.put("target", targetString);
234 param.put("outputPath", outputPath);
235 SliStringUtils.concat(param, ctx);
236 assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
240 public void urlEncode() throws SvcLogicException {
241 String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
242 String outputPath = "out";
244 param.put("source", sourceString);
245 param.put("outputPath", outputPath);
246 SliStringUtils.urlEncode(param, ctx);
247 assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));