96c6ae4ac82a54a399018e15dbf87ee176fa501a
[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  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 /**
23  *
24  */
25 package org.onap.ccsdk.sli.core.slipluginutils;
26
27 import static org.hamcrest.Matchers.equalTo;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertThat;
30
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
38
39 /**
40  * @author km991u
41  *
42  */
43 public class SliStringUtilsTest {
44     private SvcLogicContext ctx;
45     private HashMap<String, String> param;
46     private SliStringUtils stringUtils = new SliStringUtils();
47
48     /**
49      * @throws java.lang.Exception
50      */
51     @Before
52     public void setUp() throws Exception {
53         this.ctx = new SvcLogicContext();
54         param = new HashMap<String, String>();
55     }
56
57     /**
58      * @throws SvcLogicException
59      * @see SliStringUtils#split(Map, SvcLogicContext)
60      */
61     @Test
62     public final void testSplit() throws SvcLogicException {
63         param.put("original_string", "one ## two ## three");
64         param.put("regex", " ## ");
65         param.put("ctx_memory_result_key", "result");
66
67         stringUtils.split(param, ctx);
68
69         assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
70         assertThat(ctx.getAttribute("result[1]"), equalTo("two"));
71         assertThat(ctx.getAttribute("result[2]"), equalTo("three"));
72         assertThat(ctx.getAttribute("result_length"), equalTo("3"));
73     }
74
75     /**
76      * @throws SvcLogicException
77      * @see SliStringUtils#split(Map, SvcLogicContext)
78      */
79     @Test
80     public final void testSplit_limit() throws SvcLogicException {
81         param.put("original_string", "one ## two ## three");
82         param.put("regex", " ## ");
83         param.put("limit", "2");
84         param.put("ctx_memory_result_key", "result");
85
86         stringUtils.split(param, ctx);
87
88         assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
89         assertThat(ctx.getAttribute("result[1]"), equalTo("two ## three"));
90         assertThat(ctx.getAttribute("result_length"), equalTo("2"));
91     }
92
93     @Test
94     public final void testQuotedOrNull() throws SvcLogicException {
95         //param.put("nullString",null);
96         assertEquals("NULL",SliStringUtils.quotedOrNULL(null));
97     }
98
99     @Test
100     public void equalsIgnoreCaseTrue() throws SvcLogicException {
101         String sourceString = "HeLlOwORLD";
102         String targetSTring = "HELLOWORLD";
103         param.put("source", sourceString);
104         param.put("target", targetSTring);
105         assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
106     }
107
108     @Test
109     public void equalsIgnoreCaseFalse() throws SvcLogicException {
110         String sourceString = "HeLlOwORLD";
111         String targetSTring = "goodbyeWORLD";
112         param.put("source", sourceString);
113         param.put("target", targetSTring);
114         assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
115     }
116
117     @Test
118     public void toUpper() throws SvcLogicException {
119         String sourceString = "HeLlOwORLD";
120         param.put("source", sourceString);
121         String path = "my.unique.path.";
122         param.put("outputPath", path);
123         SliStringUtils.toUpper(param, ctx);
124         assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
125     }
126
127     @Test
128     public void toLower() throws SvcLogicException {
129         String sourceString = "HeLlOwORLD";
130         param.put("source", sourceString);
131         String path = "my.unique.path.";
132         param.put("outputPath", path);
133         SliStringUtils.toLower(param, ctx);
134         assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
135     }
136
137     @Test
138     public void containsTrue() throws SvcLogicException {
139         String sourceString = "Pizza";
140         String targetSTring = "izza";
141         param.put("source", sourceString);
142         param.put("target", targetSTring);
143         assertEquals("true", SliStringUtils.contains(param, ctx));
144     }
145
146     @Test
147     public void containsFalse() throws SvcLogicException {
148         String sourceString = "Pizza";
149         String targetSTring = "muffin";
150         param.put("source", sourceString);
151         param.put("target", targetSTring);
152         assertEquals("false", SliStringUtils.contains(param, ctx));
153     }
154
155     @Test
156     public void endsWithTrue() throws SvcLogicException {
157         String sourceString = "Pizza";
158         String targetSTring = "za";
159         param.put("source", sourceString);
160         param.put("target", targetSTring);
161         assertEquals("true", SliStringUtils.endsWith(param, ctx));
162     }
163
164     @Test
165     public void endsWithFalse() throws SvcLogicException {
166         String sourceString = "Pizza";
167         String targetSTring = "muffin";
168         param.put("source", sourceString);
169         param.put("target", targetSTring);
170         assertEquals("false", SliStringUtils.endsWith(param, ctx));
171     }
172
173     @Test
174     public void trim() throws SvcLogicException {
175         String sourceString = " H E L L O W O R L D";
176         String outputPath = "muffin";
177         param.put("source", sourceString);
178         param.put("outputPath", outputPath);
179         SliStringUtils.trim(param, ctx);
180         assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
181     }
182
183     @Test
184     public void getLength() throws SvcLogicException {
185         String sourceString = "SomeRandomString";
186         String outputPath = "muffin";
187         param.put("source", sourceString);
188         param.put("outputPath", outputPath);
189         SliStringUtils.getLength(param, ctx);
190         assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
191     }
192
193     @Test
194     public void startsWithFalse() throws SvcLogicException {
195         String sourceString = "Java";
196         String targetSTring = "DG";
197         param.put("source", sourceString);
198         param.put("target", targetSTring);
199         assertEquals("false", SliStringUtils.startsWith(param, ctx));
200     }
201
202     @Test
203     public void startsWithTrue() throws SvcLogicException {
204         String sourceString = "Java";
205         String targetSTring = "Ja";
206         param.put("source", sourceString);
207         param.put("target", targetSTring);
208         assertEquals("true", SliStringUtils.startsWith(param, ctx));
209     }
210
211     @Test
212     public void replace() throws SvcLogicException {
213         String sourceString = "cat Hello World cat";
214         String old = "cat";
215         String neww = "dog";
216         String outputPath = "out";
217
218         param.put("source", sourceString);
219         param.put("target", old);
220         param.put("replacement", neww);
221         param.put("outputPath", outputPath);
222         SliStringUtils.replace(param, ctx);
223         assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
224     }
225
226     @Test
227     public void concat() throws SvcLogicException {
228         String sourceString = "cat";
229         String targetString = "dog";
230         String outputPath = "out";
231
232         param.put("source", sourceString);
233         param.put("target", targetString);
234         param.put("outputPath", outputPath);
235         SliStringUtils.concat(param, ctx);
236         assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
237     }
238
239     @Test
240     public void urlEncode() throws SvcLogicException {
241         String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
242         String outputPath = "out";
243
244         param.put("source", sourceString);
245         param.put("outputPath", outputPath);
246         SliStringUtils.urlEncode(param, ctx);
247         assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));
248     }
249
250 }