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;
29 import java.util.HashMap;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 public class SliStringUtilsTest {
41 private SvcLogicContext ctx;
42 private HashMap<String, String> param;
43 private SliStringUtils stringUtils = new SliStringUtils();
46 * @throws java.lang.Exception
49 public void setUp() throws Exception {
50 this.ctx = new SvcLogicContext();
51 param = new HashMap<String, String>();
55 * @throws SvcLogicException
56 * @see SliStringUtils#split(Map, SvcLogicContext)
59 public final void testSplit() throws SvcLogicException {
60 param.put("original_string", "one ## two ## three");
61 param.put("regex", " ## ");
62 param.put("ctx_memory_result_key", "result");
64 stringUtils.split(param, ctx);
66 assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
67 assertThat(ctx.getAttribute("result[1]"), equalTo("two"));
68 assertThat(ctx.getAttribute("result[2]"), equalTo("three"));
69 assertThat(ctx.getAttribute("result_length"), equalTo("3"));
73 * @throws SvcLogicException
74 * @see SliStringUtils#split(Map, SvcLogicContext)
77 public final void testSplit_limit() throws SvcLogicException {
78 param.put("original_string", "one ## two ## three");
79 param.put("regex", " ## ");
80 param.put("limit", "2");
81 param.put("ctx_memory_result_key", "result");
83 stringUtils.split(param, ctx);
85 assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
86 assertThat(ctx.getAttribute("result[1]"), equalTo("two ## three"));
87 assertThat(ctx.getAttribute("result_length"), equalTo("2"));
91 public final void testSubString() throws SvcLogicException {
92 param.put("string", "splitatgivenindex");
93 param.put("begin-index", "0");
94 param.put("end-index", "5");
95 param.put("result", "result");
97 stringUtils.substring(param, ctx);
99 assertEquals("split", ctx.getAttribute("result"));
103 public final void testQuotedOrNull() throws SvcLogicException {
104 // param.put("nullString",null);
105 assertEquals("NULL", SliStringUtils.quotedOrNULL(null));
109 public void equalsIgnoreCaseTrue() throws SvcLogicException {
110 String sourceString = "HeLlOwORLD";
111 String targetSTring = "HELLOWORLD";
112 param.put("source", sourceString);
113 param.put("target", targetSTring);
114 assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
118 public void equalsIgnoreCaseFalse() throws SvcLogicException {
119 String sourceString = "HeLlOwORLD";
120 String targetSTring = "goodbyeWORLD";
121 param.put("source", sourceString);
122 param.put("target", targetSTring);
123 assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
127 public void toUpper() throws SvcLogicException {
128 String sourceString = "HeLlOwORLD";
129 param.put("source", sourceString);
130 String path = "my.unique.path.";
131 param.put("outputPath", path);
132 SliStringUtils.toUpper(param, ctx);
133 assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
137 public void toLower() throws SvcLogicException {
138 String sourceString = "HeLlOwORLD";
139 param.put("source", sourceString);
140 String path = "my.unique.path.";
141 param.put("outputPath", path);
142 SliStringUtils.toLower(param, ctx);
143 assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
147 public void containsTrue() throws SvcLogicException {
148 String sourceString = "Pizza";
149 String targetSTring = "izza";
150 param.put("source", sourceString);
151 param.put("target", targetSTring);
152 assertEquals("true", SliStringUtils.contains(param, ctx));
156 public void containsFalse() throws SvcLogicException {
157 String sourceString = "Pizza";
158 String targetSTring = "muffin";
159 param.put("source", sourceString);
160 param.put("target", targetSTring);
161 assertEquals("false", SliStringUtils.contains(param, ctx));
165 public void endsWithTrue() throws SvcLogicException {
166 String sourceString = "Pizza";
167 String targetSTring = "za";
168 param.put("source", sourceString);
169 param.put("target", targetSTring);
170 assertEquals("true", SliStringUtils.endsWith(param, ctx));
174 public void endsWithFalse() throws SvcLogicException {
175 String sourceString = "Pizza";
176 String targetSTring = "muffin";
177 param.put("source", sourceString);
178 param.put("target", targetSTring);
179 assertEquals("false", SliStringUtils.endsWith(param, ctx));
183 public void trim() throws SvcLogicException {
184 String sourceString = " H E L L O W O R L D";
185 String outputPath = "muffin";
186 param.put("source", sourceString);
187 param.put("outputPath", outputPath);
188 SliStringUtils.trim(param, ctx);
189 assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
193 public void getLength() throws SvcLogicException {
194 String sourceString = "SomeRandomString";
195 String outputPath = "muffin";
196 param.put("source", sourceString);
197 param.put("outputPath", outputPath);
198 SliStringUtils.getLength(param, ctx);
199 assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
203 public void startsWithFalse() throws SvcLogicException {
204 String sourceString = "Java";
205 String targetSTring = "DG";
206 param.put("source", sourceString);
207 param.put("target", targetSTring);
208 assertEquals("false", SliStringUtils.startsWith(param, ctx));
212 public void startsWithTrue() throws SvcLogicException {
213 String sourceString = "Java";
214 String targetSTring = "Ja";
215 param.put("source", sourceString);
216 param.put("target", targetSTring);
217 assertEquals("true", SliStringUtils.startsWith(param, ctx));
221 public void replace() throws SvcLogicException {
222 String sourceString = "cat Hello World cat";
225 String outputPath = "out";
227 param.put("source", sourceString);
228 param.put("target", old);
229 param.put("replacement", neww);
230 param.put("outputPath", outputPath);
231 SliStringUtils.replace(param, ctx);
232 assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
236 public void replaceAll() throws SvcLogicException {
237 String source = "cat Hello World cat";
238 String target = "\\s";
239 String replacement = "";
240 String outputPath = "out";
242 param.put("source", source);
243 param.put("target", target);
244 param.put("replacement", replacement);
245 param.put("outputPath", outputPath);
246 SliStringUtils.replaceAll(param, ctx);
247 assertEquals(source.replaceAll(target, replacement), ctx.getAttribute(outputPath));
251 public void concat() throws SvcLogicException {
252 String sourceString = "cat";
253 String targetString = "dog";
254 String outputPath = "out";
256 param.put("source", sourceString);
257 param.put("target", targetString);
258 param.put("outputPath", outputPath);
259 SliStringUtils.concat(param, ctx);
260 assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
264 public void urlEncode() throws SvcLogicException {
265 String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
266 String outputPath = "out";
268 param.put("source", sourceString);
269 param.put("outputPath", outputPath);
270 SliStringUtils.urlEncode(param, ctx);
271 assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));
275 public void urlDecode() throws SvcLogicException {
276 String sourceString = "102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4";
277 String outputPath = "out";
279 param.put("source", sourceString);
280 param.put("outputPath", outputPath);
281 SliStringUtils.urlDecode(param, ctx);
282 assertEquals("102/GE100/SNJSCAMCJP8/SNJSCAMCJT4", ctx.getAttribute(outputPath));
286 public void testXmlEscapeText() {
287 param.put("source", "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4");
288 param.put("target", "target");
289 SliStringUtils.xmlEscapeText(param, ctx);
290 assertEquals("102/GE100/SNJSCAMCJP8/SNJSCAMCJT4", ctx.getAttribute("target"));
293 @Test(expected = Exception.class)
294 public void testSplitForEmptyParams() throws Exception {
295 SliStringUtils utils = new SliStringUtils();
296 ctx = new SvcLogicContext();
297 param = new HashMap<>();
298 utils.split(param, ctx);
301 @Test(expected = Exception.class)
302 public void testSubstringForEmptyParams() throws Exception {
303 SliStringUtils utils = new SliStringUtils();
304 ctx = new SvcLogicContext();
305 param = new HashMap<>();
306 utils.substring(param, ctx);
310 public void testUnescapeJsonString() throws Exception {
311 String source = "{\\\"image_name\\\":\\\"Ubuntu 14.04\\\",\\\"service-instance-id\\\":\\\"1\\\",\\\"vnf-model-customization-uuid\\\":\\\"2f\\\",\\\"vnf-id\\\":\\\"3b\\\"}";
312 param.put(SliStringUtils.INPUT_PARAM_SOURCE, source);
313 String outputPath = "unescaped";
314 param.put(SliStringUtils.INPUT_PARAM_TARGET, outputPath);
315 SliStringUtils.unescapeJsonString(param, ctx);
316 assertEquals("{\"image_name\":\"Ubuntu 14.04\",\"service-instance-id\":\"1\",\"vnf-model-customization-uuid\":\"2f\",\"vnf-id\":\"3b\"}", ctx.getAttribute(outputPath));
320 public void testEscapeJsonString() throws Exception {
321 String source = "{\"image_name\":\"Ubuntu 14.04\",\"service-instance-id\":\"1\",\"vnf-model-customization-uuid\":\"2f\",\"vnf-id\":\"3b\"}";
322 param.put(SliStringUtils.INPUT_PARAM_SOURCE, source);
323 String outputPath = "unescaped";
324 param.put(SliStringUtils.INPUT_PARAM_TARGET, outputPath);
325 SliStringUtils.escapeJsonString(param, ctx);
326 assertEquals("{\\\"image_name\\\":\\\"Ubuntu 14.04\\\",\\\"service-instance-id\\\":\\\"1\\\",\\\"vnf-model-customization-uuid\\\":\\\"2f\\\",\\\"vnf-id\\\":\\\"3b\\\"}", ctx.getAttribute(outputPath));
330 public void isEmpty() throws Exception {
331 String result = SliStringUtils.isEmpty(param, ctx);
332 param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
333 assertEquals(SliStringUtils.TRUE_CONSTANT, result);
335 ctx.setAttribute("a", null);
336 param.put(SliStringUtils.INPUT_PARAM_KEY, "a");
337 result = SliStringUtils.isEmpty(param, ctx);
338 assertEquals(SliStringUtils.TRUE_CONSTANT, result);
340 ctx.setAttribute("a", "");
341 result = SliStringUtils.isEmpty(param, ctx);
342 assertEquals(SliStringUtils.TRUE_CONSTANT, result);
344 ctx.setAttribute("a", " ");
345 result = SliStringUtils.isEmpty(param, ctx);
346 assertEquals(SliStringUtils.FALSE_CONSTANT, result);
350 public void isBlank() throws Exception {
351 String result = SliStringUtils.isBlank(param, ctx);
352 param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
353 assertEquals(SliStringUtils.TRUE_CONSTANT, result);
355 ctx.setAttribute("a", null);
356 param.put(SliStringUtils.INPUT_PARAM_KEY, "a");
357 result = SliStringUtils.isBlank(param, ctx);
358 assertEquals(SliStringUtils.TRUE_CONSTANT, result);
360 ctx.setAttribute("a", "");
361 result = SliStringUtils.isBlank(param, ctx);
362 assertEquals(SliStringUtils.TRUE_CONSTANT, result);
364 ctx.setAttribute("a", " ");
365 result = SliStringUtils.isBlank(param, ctx);
366 assertEquals(SliStringUtils.TRUE_CONSTANT, result);
370 public void isNull() throws Exception {
371 String result = SliStringUtils.isNull(param, ctx);
372 param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
373 assertEquals(SliStringUtils.TRUE_CONSTANT, result);
375 ctx.setAttribute("a", null);
376 param.put(SliStringUtils.INPUT_PARAM_KEY, "a");
377 result = SliStringUtils.isNull(param, ctx);
378 assertEquals(SliStringUtils.TRUE_CONSTANT, result);
380 ctx.setAttribute("a", "");
381 result = SliStringUtils.isNull(param, ctx);
382 assertEquals(SliStringUtils.FALSE_CONSTANT, result);
384 ctx.setAttribute("a", " ");
385 result = SliStringUtils.isNull(param, ctx);
386 assertEquals(SliStringUtils.FALSE_CONSTANT, result);