d8d78a0b1eb29c7688f109814aca20ea17ea8f8d
[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 testXmlEscapeText() {
276                 param.put("source", "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4");
277                 param.put("target", "target");
278                 SliStringUtils.xmlEscapeText(param, ctx);
279                 assertEquals("102/GE100/SNJSCAMCJP8/SNJSCAMCJT4", ctx.getAttribute("target"));
280         }
281
282         @Test(expected = Exception.class)
283         public void testSplitForEmptyParams() throws Exception {
284                 SliStringUtils utils = new SliStringUtils();
285                 ctx = new SvcLogicContext();
286                 param = new HashMap<>();
287                 utils.split(param, ctx);
288         }
289
290         @Test(expected = Exception.class)
291         public void testSubstringForEmptyParams() throws Exception {
292                 SliStringUtils utils = new SliStringUtils();
293                 ctx = new SvcLogicContext();
294                 param = new HashMap<>();
295                 utils.substring(param, ctx);
296         }
297
298         @Test
299         public void testUnescapeJsonString() throws Exception {
300                 String source = "{\\\"image_name\\\":\\\"Ubuntu 14.04\\\",\\\"service-instance-id\\\":\\\"1\\\",\\\"vnf-model-customization-uuid\\\":\\\"2f\\\",\\\"vnf-id\\\":\\\"3b\\\"}";
301                 param.put(SliStringUtils.INPUT_PARAM_SOURCE, source);
302                 String outputPath = "unescaped";
303                 param.put(SliStringUtils.INPUT_PARAM_TARGET, outputPath);
304                 SliStringUtils.unescapeJsonString(param, ctx);
305                 assertEquals("{\"image_name\":\"Ubuntu 14.04\",\"service-instance-id\":\"1\",\"vnf-model-customization-uuid\":\"2f\",\"vnf-id\":\"3b\"}", ctx.getAttribute(outputPath));
306         }
307
308         @Test
309         public void testEscapeJsonString() throws Exception {
310                 String source = "{\"image_name\":\"Ubuntu 14.04\",\"service-instance-id\":\"1\",\"vnf-model-customization-uuid\":\"2f\",\"vnf-id\":\"3b\"}";
311                 param.put(SliStringUtils.INPUT_PARAM_SOURCE, source);
312                 String outputPath = "unescaped";
313                 param.put(SliStringUtils.INPUT_PARAM_TARGET, outputPath);
314                 SliStringUtils.escapeJsonString(param, ctx);
315                 assertEquals("{\\\"image_name\\\":\\\"Ubuntu 14.04\\\",\\\"service-instance-id\\\":\\\"1\\\",\\\"vnf-model-customization-uuid\\\":\\\"2f\\\",\\\"vnf-id\\\":\\\"3b\\\"}", ctx.getAttribute(outputPath));
316         }
317
318     @Test
319     public void isEmpty() throws Exception {
320         ctx = new SvcLogicContext();
321         param = new HashMap<>();
322         String result = SliStringUtils.isEmpty(param, ctx);
323         param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
324         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
325
326         ctx.setAttribute("a", null);
327         param.put(SliStringUtils.INPUT_PARAM_KEY, "a");
328         result = SliStringUtils.isEmpty(param, ctx);
329         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
330
331         ctx.setAttribute("a", "");
332         result = SliStringUtils.isEmpty(param, ctx);
333         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
334
335         ctx.setAttribute("a", " ");
336         result = SliStringUtils.isEmpty(param, ctx);
337         assertEquals(SliStringUtils.FALSE_CONSTANT, result);
338     }
339
340     @Test
341     public void isBlank() throws Exception {
342         ctx = new SvcLogicContext();
343         param = new HashMap<>();
344         String result = SliStringUtils.isBlank(param, ctx);
345         param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
346         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
347
348         ctx.setAttribute("a", null);
349         param.put(SliStringUtils.INPUT_PARAM_KEY, "a");
350         result = SliStringUtils.isBlank(param, ctx);
351         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
352
353         ctx.setAttribute("a", "");
354         result = SliStringUtils.isBlank(param, ctx);
355         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
356
357         ctx.setAttribute("a", " ");
358         result = SliStringUtils.isBlank(param, ctx);
359         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
360     }
361
362     @Test
363     public void isNull() throws Exception {
364         ctx = new SvcLogicContext();
365         param = new HashMap<>();
366         String result = SliStringUtils.isNull(param, ctx);
367         param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
368         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
369
370         ctx.setAttribute("a", null);
371         param.put(SliStringUtils.INPUT_PARAM_KEY, "a");
372         result = SliStringUtils.isNull(param, ctx);
373         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
374
375         ctx.setAttribute("a", "");
376         result = SliStringUtils.isNull(param, ctx);
377         assertEquals(SliStringUtils.FALSE_CONSTANT, result);
378
379         ctx.setAttribute("a", " ");
380         result = SliStringUtils.isNull(param, ctx);
381         assertEquals(SliStringUtils.FALSE_CONSTANT, result);
382     }
383
384 }