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.openecomp.sdnc.sli.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.openecomp.sdnc.sli.SvcLogicContext;
37 import org.openecomp.sdnc.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 void equalsIgnoreCaseTrue() throws SvcLogicException {
95 String sourceString = "HeLlOwORLD";
96 String targetSTring = "HELLOWORLD";
97 param.put("source", sourceString);
98 param.put("target", targetSTring);
99 assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
103 public void equalsIgnoreCaseFalse() throws SvcLogicException {
104 String sourceString = "HeLlOwORLD";
105 String targetSTring = "goodbyeWORLD";
106 param.put("source", sourceString);
107 param.put("target", targetSTring);
108 assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
112 public void toUpper() throws SvcLogicException {
113 String sourceString = "HeLlOwORLD";
114 param.put("source", sourceString);
115 String path = "my.unique.path.";
116 param.put("outputPath", path);
117 SliStringUtils.toUpper(param, ctx);
118 assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
122 public void toLower() throws SvcLogicException {
123 String sourceString = "HeLlOwORLD";
124 param.put("source", sourceString);
125 String path = "my.unique.path.";
126 param.put("outputPath", path);
127 SliStringUtils.toLower(param, ctx);
128 assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
132 public void containsTrue() throws SvcLogicException {
133 String sourceString = "Pizza";
134 String targetSTring = "izza";
135 param.put("source", sourceString);
136 param.put("target", targetSTring);
137 assertEquals("true", SliStringUtils.contains(param, ctx));
141 public void containsFalse() throws SvcLogicException {
142 String sourceString = "Pizza";
143 String targetSTring = "muffin";
144 param.put("source", sourceString);
145 param.put("target", targetSTring);
146 assertEquals("false", SliStringUtils.contains(param, ctx));
150 public void endsWithTrue() throws SvcLogicException {
151 String sourceString = "Pizza";
152 String targetSTring = "za";
153 param.put("source", sourceString);
154 param.put("target", targetSTring);
155 assertEquals("true", SliStringUtils.endsWith(param, ctx));
159 public void endsWithFalse() throws SvcLogicException {
160 String sourceString = "Pizza";
161 String targetSTring = "muffin";
162 param.put("source", sourceString);
163 param.put("target", targetSTring);
164 assertEquals("false", SliStringUtils.endsWith(param, ctx));
168 public void trim() throws SvcLogicException {
169 String sourceString = " H E L L O W O R L D";
170 String outputPath = "muffin";
171 param.put("source", sourceString);
172 param.put("outputPath", outputPath);
173 SliStringUtils.trim(param, ctx);
174 assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
178 public void getLength() throws SvcLogicException {
179 String sourceString = "SomeRandomString";
180 String outputPath = "muffin";
181 param.put("source", sourceString);
182 param.put("outputPath", outputPath);
183 SliStringUtils.getLength(param, ctx);
184 assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
188 public void startsWithFalse() throws SvcLogicException {
189 String sourceString = "Java";
190 String targetSTring = "DG";
191 param.put("source", sourceString);
192 param.put("target", targetSTring);
193 assertEquals("false", SliStringUtils.startsWith(param, ctx));
197 public void startsWithTrue() throws SvcLogicException {
198 String sourceString = "Java";
199 String targetSTring = "Ja";
200 param.put("source", sourceString);
201 param.put("target", targetSTring);
202 assertEquals("true", SliStringUtils.startsWith(param, ctx));
206 public void replace() throws SvcLogicException {
207 String sourceString = "cat Hello World cat";
210 String outputPath = "out";
212 param.put("source", sourceString);
213 param.put("target", old);
214 param.put("replacement", neww);
215 param.put("outputPath", outputPath);
216 SliStringUtils.replace(param, ctx);
217 assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
221 public void concat() throws SvcLogicException {
222 String sourceString = "cat";
223 String targetString = "dog";
224 String outputPath = "out";
226 param.put("source", sourceString);
227 param.put("target", targetString);
228 param.put("outputPath", outputPath);
229 SliStringUtils.concat(param, ctx);
230 assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
234 public void urlEncode() throws SvcLogicException {
235 String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
236 String outputPath = "out";
238 param.put("source", sourceString);
239 param.put("outputPath", outputPath);
240 SliStringUtils.urlEncode(param, ctx);
241 assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));