085d46ef76b4cde88b32372bdad834f914103cad
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                         reserved.
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
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
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=========================================================
22  */ 
23
24 package org.onap.ccsdk.sli.core.slipluginutils;
25
26 import static org.hamcrest.Matchers.equalTo;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertThat;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
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;
37
38 /**
39  * @author km991u
40  *
41  */
42 public class SliStringUtilsTest {
43         private SvcLogicContext ctx;
44         private HashMap<String, String> param;
45         private SliStringUtils stringUtils = new SliStringUtils();
46
47         /**
48          * @throws java.lang.Exception
49          */
50         @Before
51         public void setUp() throws Exception {
52                 this.ctx = new SvcLogicContext();
53                 param = new HashMap<String, String>();
54         }
55
56         /**
57          * @throws SvcLogicException
58          * @see SliStringUtils#split(Map, SvcLogicContext)
59          */
60         @Test
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");
65
66                 stringUtils.split(param, ctx);
67
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"));
72         }
73
74         /**
75          * @throws SvcLogicException
76          * @see SliStringUtils#split(Map, SvcLogicContext)
77          */
78         @Test
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");
84
85                 stringUtils.split(param, ctx);
86
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"));
90         }
91
92         @Test
93         public final void testSubString() throws SvcLogicException {
94                 param.put("string", "splitatgivenindex");
95                 param.put("begin-index", "0");
96                 param.put("end-index", "5");
97                 param.put("result", "result");
98
99                 stringUtils.substring(param, ctx);
100
101                 assertEquals("split", ctx.getAttribute("result"));
102         }
103
104         @Test
105         public final void testQuotedOrNull() throws SvcLogicException {
106                 // param.put("nullString",null);
107                 assertEquals("NULL", SliStringUtils.quotedOrNULL(null));
108         }
109
110         @Test
111         public void equalsIgnoreCaseTrue() throws SvcLogicException {
112                 String sourceString = "HeLlOwORLD";
113                 String targetSTring = "HELLOWORLD";
114                 param.put("source", sourceString);
115                 param.put("target", targetSTring);
116                 assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
117         }
118
119         @Test
120         public void equalsIgnoreCaseFalse() throws SvcLogicException {
121                 String sourceString = "HeLlOwORLD";
122                 String targetSTring = "goodbyeWORLD";
123                 param.put("source", sourceString);
124                 param.put("target", targetSTring);
125                 assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
126         }
127
128         @Test
129         public void toUpper() throws SvcLogicException {
130                 String sourceString = "HeLlOwORLD";
131                 param.put("source", sourceString);
132                 String path = "my.unique.path.";
133                 param.put("outputPath", path);
134                 SliStringUtils.toUpper(param, ctx);
135                 assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
136         }
137
138         @Test
139         public void toLower() throws SvcLogicException {
140                 String sourceString = "HeLlOwORLD";
141                 param.put("source", sourceString);
142                 String path = "my.unique.path.";
143                 param.put("outputPath", path);
144                 SliStringUtils.toLower(param, ctx);
145                 assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
146         }
147
148         @Test
149         public void containsTrue() throws SvcLogicException {
150                 String sourceString = "Pizza";
151                 String targetSTring = "izza";
152                 param.put("source", sourceString);
153                 param.put("target", targetSTring);
154                 assertEquals("true", SliStringUtils.contains(param, ctx));
155         }
156
157         @Test
158         public void containsFalse() throws SvcLogicException {
159                 String sourceString = "Pizza";
160                 String targetSTring = "muffin";
161                 param.put("source", sourceString);
162                 param.put("target", targetSTring);
163                 assertEquals("false", SliStringUtils.contains(param, ctx));
164         }
165
166         @Test
167         public void endsWithTrue() throws SvcLogicException {
168                 String sourceString = "Pizza";
169                 String targetSTring = "za";
170                 param.put("source", sourceString);
171                 param.put("target", targetSTring);
172                 assertEquals("true", SliStringUtils.endsWith(param, ctx));
173         }
174
175         @Test
176         public void endsWithFalse() throws SvcLogicException {
177                 String sourceString = "Pizza";
178                 String targetSTring = "muffin";
179                 param.put("source", sourceString);
180                 param.put("target", targetSTring);
181                 assertEquals("false", SliStringUtils.endsWith(param, ctx));
182         }
183
184         @Test
185         public void trim() throws SvcLogicException {
186                 String sourceString = " H E L L O W O R L D";
187                 String outputPath = "muffin";
188                 param.put("source", sourceString);
189                 param.put("outputPath", outputPath);
190                 SliStringUtils.trim(param, ctx);
191                 assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
192         }
193
194         @Test
195         public void getLength() throws SvcLogicException {
196                 String sourceString = "SomeRandomString";
197                 String outputPath = "muffin";
198                 param.put("source", sourceString);
199                 param.put("outputPath", outputPath);
200                 SliStringUtils.getLength(param, ctx);
201                 assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
202         }
203
204         @Test
205         public void startsWithFalse() throws SvcLogicException {
206                 String sourceString = "Java";
207                 String targetSTring = "DG";
208                 param.put("source", sourceString);
209                 param.put("target", targetSTring);
210                 assertEquals("false", SliStringUtils.startsWith(param, ctx));
211         }
212
213         @Test
214         public void startsWithTrue() throws SvcLogicException {
215                 String sourceString = "Java";
216                 String targetSTring = "Ja";
217                 param.put("source", sourceString);
218                 param.put("target", targetSTring);
219                 assertEquals("true", SliStringUtils.startsWith(param, ctx));
220         }
221
222         @Test
223         public void replace() throws SvcLogicException {
224                 String sourceString = "cat Hello World cat";
225                 String old = "cat";
226                 String neww = "dog";
227                 String outputPath = "out";
228
229                 param.put("source", sourceString);
230                 param.put("target", old);
231                 param.put("replacement", neww);
232                 param.put("outputPath", outputPath);
233                 SliStringUtils.replace(param, ctx);
234                 assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
235         }
236
237         @Test
238         public void replaceAll() throws SvcLogicException {
239                 String source = "cat Hello World cat";
240                 String target = "\\s";
241                 String replacement = "";
242                 String outputPath = "out";
243
244                 param.put("source", source);
245                 param.put("target", target);
246                 param.put("replacement", replacement);
247                 param.put("outputPath", outputPath);
248                 SliStringUtils.replaceAll(param, ctx);
249                 assertEquals(source.replaceAll(target, replacement), ctx.getAttribute(outputPath));
250         }
251
252         @Test
253         public void concat() throws SvcLogicException {
254                 String sourceString = "cat";
255                 String targetString = "dog";
256                 String outputPath = "out";
257
258                 param.put("source", sourceString);
259                 param.put("target", targetString);
260                 param.put("outputPath", outputPath);
261                 SliStringUtils.concat(param, ctx);
262                 assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
263         }
264
265         @Test
266         public void urlEncode() throws SvcLogicException {
267                 String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
268                 String outputPath = "out";
269
270                 param.put("source", sourceString);
271                 param.put("outputPath", outputPath);
272                 SliStringUtils.urlEncode(param, ctx);
273                 assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));
274         }
275
276         @Test
277         public void testXmlEscapeText() {
278                 param.put("source", "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4");
279                 param.put("target", "target");
280                 SliStringUtils.xmlEscapeText(param, ctx);
281                 assertEquals("102/GE100/SNJSCAMCJP8/SNJSCAMCJT4", ctx.getAttribute("target"));
282         }
283
284         @Test(expected = Exception.class)
285         public void testSplitForEmptyParams() throws Exception {
286                 SliStringUtils utils = new SliStringUtils();
287                 ctx = new SvcLogicContext();
288                 param = new HashMap<>();
289                 utils.split(param, ctx);
290         }
291
292         @Test(expected = Exception.class)
293         public void testSubstringForEmptyParams() throws Exception {
294                 SliStringUtils utils = new SliStringUtils();
295                 ctx = new SvcLogicContext();
296                 param = new HashMap<>();
297                 utils.substring(param, ctx);
298         }
299
300 }