2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
24 package org.openecomp.sdnc.sli.SliPluginUtils;
26 import static org.hamcrest.Matchers.equalTo;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertThat;
30 import java.util.HashMap;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
42 public class SliStringUtilsTest {
43 private SvcLogicContext ctx;
44 private HashMap<String, String> param;
45 private SliStringUtils stringUtils = new SliStringUtils();
48 * @throws java.lang.Exception
51 public void setUp() throws Exception {
52 this.ctx = new SvcLogicContext();
53 param = new HashMap<String, String>();
57 * @throws SvcLogicException
58 * @see SliStringUtils#split(Map, SvcLogicContext)
61 public final void testSplit() throws SvcLogicException {
62 param.put("original_string", "one ## two ## three");
63 param.put("regex", " ## ");
64 param.put("ctx_memory_result_key", "result");
66 stringUtils.split(param, ctx);
68 assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
69 assertThat(ctx.getAttribute("result[1]"), equalTo("two"));
70 assertThat(ctx.getAttribute("result[2]"), equalTo("three"));
71 assertThat(ctx.getAttribute("result_length"), equalTo("3"));
75 * @throws SvcLogicException
76 * @see SliStringUtils#split(Map, SvcLogicContext)
79 public final void testSplit_limit() throws SvcLogicException {
80 param.put("original_string", "one ## two ## three");
81 param.put("regex", " ## ");
82 param.put("limit", "2");
83 param.put("ctx_memory_result_key", "result");
85 stringUtils.split(param, ctx);
87 assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
88 assertThat(ctx.getAttribute("result[1]"), equalTo("two ## three"));
89 assertThat(ctx.getAttribute("result_length"), equalTo("2"));
93 public void equalsIgnoreCaseTrue() throws SvcLogicException {
94 String sourceString = "HeLlOwORLD";
95 String targetSTring = "HELLOWORLD";
96 param.put("source", sourceString);
97 param.put("target", targetSTring);
98 assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
102 public void equalsIgnoreCaseFalse() throws SvcLogicException {
103 String sourceString = "HeLlOwORLD";
104 String targetSTring = "goodbyeWORLD";
105 param.put("source", sourceString);
106 param.put("target", targetSTring);
107 assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
111 public void toUpper() throws SvcLogicException {
112 String sourceString = "HeLlOwORLD";
113 param.put("source", sourceString);
114 String path = "my.unique.path.";
115 param.put("outputPath", path);
116 SliStringUtils.toUpper(param, ctx);
117 assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
121 public void toLower() throws SvcLogicException {
122 String sourceString = "HeLlOwORLD";
123 param.put("source", sourceString);
124 String path = "my.unique.path.";
125 param.put("outputPath", path);
126 SliStringUtils.toLower(param, ctx);
127 assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
131 public void containsTrue() throws SvcLogicException {
132 String sourceString = "Pizza";
133 String targetSTring = "izza";
134 param.put("source", sourceString);
135 param.put("target", targetSTring);
136 assertEquals("true", SliStringUtils.contains(param, ctx));
140 public void containsFalse() throws SvcLogicException {
141 String sourceString = "Pizza";
142 String targetSTring = "muffin";
143 param.put("source", sourceString);
144 param.put("target", targetSTring);
145 assertEquals("false", SliStringUtils.contains(param, ctx));
149 public void endsWithTrue() throws SvcLogicException {
150 String sourceString = "Pizza";
151 String targetSTring = "za";
152 param.put("source", sourceString);
153 param.put("target", targetSTring);
154 assertEquals("true", SliStringUtils.endsWith(param, ctx));
158 public void endsWithFalse() throws SvcLogicException {
159 String sourceString = "Pizza";
160 String targetSTring = "muffin";
161 param.put("source", sourceString);
162 param.put("target", targetSTring);
163 assertEquals("false", SliStringUtils.endsWith(param, ctx));
167 public void trim() throws SvcLogicException {
168 String sourceString = " H E L L O W O R L D";
169 String outputPath = "muffin";
170 param.put("source", sourceString);
171 param.put("outputPath", outputPath);
172 SliStringUtils.trim(param, ctx);
173 assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
177 public void getLength() throws SvcLogicException {
178 String sourceString = "SomeRandomString";
179 String outputPath = "muffin";
180 param.put("source", sourceString);
181 param.put("outputPath", outputPath);
182 SliStringUtils.getLength(param, ctx);
183 assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
187 public void startsWithFalse() throws SvcLogicException {
188 String sourceString = "Java";
189 String targetSTring = "DG";
190 param.put("source", sourceString);
191 param.put("target", targetSTring);
192 assertEquals("false", SliStringUtils.startsWith(param, ctx));
196 public void startsWithTrue() throws SvcLogicException {
197 String sourceString = "Java";
198 String targetSTring = "Ja";
199 param.put("source", sourceString);
200 param.put("target", targetSTring);
201 assertEquals("true", SliStringUtils.startsWith(param, ctx));
205 public void replace() throws SvcLogicException {
206 String sourceString = "cat Hello World cat";
209 String outputPath = "out";
211 param.put("source", sourceString);
212 param.put("target", old);
213 param.put("replacement", neww);
214 param.put("outputPath", outputPath);
215 SliStringUtils.replace(param, ctx);
216 assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
220 public void concat() throws SvcLogicException {
221 String sourceString = "cat";
222 String targetString = "dog";
223 String outputPath = "out";
225 param.put("source", sourceString);
226 param.put("target", targetString);
227 param.put("outputPath", outputPath);
228 SliStringUtils.concat(param, ctx);
229 assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
233 public void urlEncode() throws SvcLogicException {
234 String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
235 String outputPath = "out";
237 param.put("source", sourceString);
238 param.put("outputPath", outputPath);
239 SliStringUtils.urlEncode(param, ctx);
240 assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));