enhance SliPluginUtils
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / test / java / org / onap / ccsdk / sli / core / slipluginutils / SliStringUtilsTest.java
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 import java.util.HashMap;
30 import java.util.Map;
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;
35
36 /**
37  * @author km991u
38  *
39  */
40 public class SliStringUtilsTest {
41         private SvcLogicContext ctx;
42         private HashMap<String, String> param;
43         private SliStringUtils stringUtils = new SliStringUtils();
44
45         /**
46          * @throws java.lang.Exception
47          */
48         @Before
49         public void setUp() throws Exception {
50                 this.ctx = new SvcLogicContext();
51                 param = new HashMap<String, String>();
52         }
53
54         /**
55          * @throws SvcLogicException
56          * @see SliStringUtils#split(Map, SvcLogicContext)
57          */
58         @Test
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");
63
64                 stringUtils.split(param, ctx);
65
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"));
70         }
71
72         /**
73          * @throws SvcLogicException
74          * @see SliStringUtils#split(Map, SvcLogicContext)
75          */
76         @Test
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");
82
83                 stringUtils.split(param, ctx);
84
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"));
88         }
89
90         @Test
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");
96
97                 stringUtils.substring(param, ctx);
98
99                 assertEquals("split", ctx.getAttribute("result"));
100         }
101
102         @Test
103         public final void testQuotedOrNull() throws SvcLogicException {
104                 // param.put("nullString",null);
105                 assertEquals("NULL", SliStringUtils.quotedOrNULL(null));
106         }
107
108         @Test
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));
115         }
116
117         @Test
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));
124         }
125
126         @Test
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));
134         }
135
136         @Test
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));
144         }
145
146         @Test
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));
153         }
154
155         @Test
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));
162         }
163
164         @Test
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));
171         }
172
173         @Test
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));
180         }
181
182         @Test
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));
190         }
191
192         @Test
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));
200         }
201
202         @Test
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));
209         }
210
211         @Test
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));
218         }
219
220         @Test
221         public void replace() throws SvcLogicException {
222                 String sourceString = "cat Hello World cat";
223                 String old = "cat";
224                 String neww = "dog";
225                 String outputPath = "out";
226
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));
233         }
234
235         @Test
236         public void replaceAll() throws SvcLogicException {
237                 String source = "cat Hello World cat";
238                 String target = "\\s";
239                 String replacement = "";
240                 String outputPath = "out";
241
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));
248         }
249
250         @Test
251         public void concat() throws SvcLogicException {
252                 String sourceString = "cat";
253                 String targetString = "dog";
254                 String outputPath = "out";
255
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));
261         }
262
263         @Test
264         public void urlEncode() throws SvcLogicException {
265                 String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
266                 String outputPath = "out";
267
268                 param.put("source", sourceString);
269                 param.put("outputPath", outputPath);
270                 SliStringUtils.urlEncode(param, ctx);
271                 assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));
272         }
273
274     @Test
275     public void urlDecode() throws SvcLogicException {
276         String sourceString = "102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4";
277         String outputPath = "out";
278
279         param.put("source", sourceString);
280         param.put("outputPath", outputPath);
281         SliStringUtils.urlDecode(param, ctx);
282         assertEquals("102/GE100/SNJSCAMCJP8/SNJSCAMCJT4", ctx.getAttribute(outputPath));
283     }
284
285         @Test
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"));
291         }
292
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);
299         }
300
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);
307         }
308
309         @Test
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));
317         }
318
319         @Test
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));
327         }
328
329     @Test
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);
334
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);
339
340         ctx.setAttribute("a", "");
341         result = SliStringUtils.isEmpty(param, ctx);
342         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
343
344         ctx.setAttribute("a", " ");
345         result = SliStringUtils.isEmpty(param, ctx);
346         assertEquals(SliStringUtils.FALSE_CONSTANT, result);
347     }
348
349     @Test
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);
354
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);
359
360         ctx.setAttribute("a", "");
361         result = SliStringUtils.isBlank(param, ctx);
362         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
363
364         ctx.setAttribute("a", " ");
365         result = SliStringUtils.isBlank(param, ctx);
366         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
367     }
368
369     @Test
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);
374
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);
379
380         ctx.setAttribute("a", "");
381         result = SliStringUtils.isNull(param, ctx);
382         assertEquals(SliStringUtils.FALSE_CONSTANT, result);
383
384         ctx.setAttribute("a", " ");
385         result = SliStringUtils.isNull(param, ctx);
386         assertEquals(SliStringUtils.FALSE_CONSTANT, result);
387     }
388
389 }