4fc8d18668016227073aa62e9432a7d30234c1f3
[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 testSubString() throws SvcLogicException {
95         param.put("string","splitatgivenindex");
96         param.put("begin-index","0");
97         param.put("end-index","5");
98         param.put("result","result");
99
100         stringUtils.substring(param, ctx);
101
102         assertEquals("split", ctx.getAttribute("result"));
103     }
104
105     @Test
106     public final void testQuotedOrNull() throws SvcLogicException {
107         //param.put("nullString",null);
108         assertEquals("NULL",SliStringUtils.quotedOrNULL(null));
109     }
110
111     @Test
112     public void equalsIgnoreCaseTrue() throws SvcLogicException {
113         String sourceString = "HeLlOwORLD";
114         String targetSTring = "HELLOWORLD";
115         param.put("source", sourceString);
116         param.put("target", targetSTring);
117         assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
118     }
119
120     @Test
121     public void equalsIgnoreCaseFalse() throws SvcLogicException {
122         String sourceString = "HeLlOwORLD";
123         String targetSTring = "goodbyeWORLD";
124         param.put("source", sourceString);
125         param.put("target", targetSTring);
126         assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
127     }
128
129     @Test
130     public void toUpper() throws SvcLogicException {
131         String sourceString = "HeLlOwORLD";
132         param.put("source", sourceString);
133         String path = "my.unique.path.";
134         param.put("outputPath", path);
135         SliStringUtils.toUpper(param, ctx);
136         assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
137     }
138
139     @Test
140     public void toLower() throws SvcLogicException {
141         String sourceString = "HeLlOwORLD";
142         param.put("source", sourceString);
143         String path = "my.unique.path.";
144         param.put("outputPath", path);
145         SliStringUtils.toLower(param, ctx);
146         assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
147     }
148
149     @Test
150     public void containsTrue() throws SvcLogicException {
151         String sourceString = "Pizza";
152         String targetSTring = "izza";
153         param.put("source", sourceString);
154         param.put("target", targetSTring);
155         assertEquals("true", SliStringUtils.contains(param, ctx));
156     }
157
158     @Test
159     public void containsFalse() throws SvcLogicException {
160         String sourceString = "Pizza";
161         String targetSTring = "muffin";
162         param.put("source", sourceString);
163         param.put("target", targetSTring);
164         assertEquals("false", SliStringUtils.contains(param, ctx));
165     }
166
167     @Test
168     public void endsWithTrue() throws SvcLogicException {
169         String sourceString = "Pizza";
170         String targetSTring = "za";
171         param.put("source", sourceString);
172         param.put("target", targetSTring);
173         assertEquals("true", SliStringUtils.endsWith(param, ctx));
174     }
175
176     @Test
177     public void endsWithFalse() throws SvcLogicException {
178         String sourceString = "Pizza";
179         String targetSTring = "muffin";
180         param.put("source", sourceString);
181         param.put("target", targetSTring);
182         assertEquals("false", SliStringUtils.endsWith(param, ctx));
183     }
184
185     @Test
186     public void trim() throws SvcLogicException {
187         String sourceString = " H E L L O W O R L D";
188         String outputPath = "muffin";
189         param.put("source", sourceString);
190         param.put("outputPath", outputPath);
191         SliStringUtils.trim(param, ctx);
192         assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
193     }
194
195     @Test
196     public void getLength() throws SvcLogicException {
197         String sourceString = "SomeRandomString";
198         String outputPath = "muffin";
199         param.put("source", sourceString);
200         param.put("outputPath", outputPath);
201         SliStringUtils.getLength(param, ctx);
202         assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
203     }
204
205     @Test
206     public void startsWithFalse() throws SvcLogicException {
207         String sourceString = "Java";
208         String targetSTring = "DG";
209         param.put("source", sourceString);
210         param.put("target", targetSTring);
211         assertEquals("false", SliStringUtils.startsWith(param, ctx));
212     }
213
214     @Test
215     public void startsWithTrue() throws SvcLogicException {
216         String sourceString = "Java";
217         String targetSTring = "Ja";
218         param.put("source", sourceString);
219         param.put("target", targetSTring);
220         assertEquals("true", SliStringUtils.startsWith(param, ctx));
221     }
222
223     @Test
224     public void replace() throws SvcLogicException {
225         String sourceString = "cat Hello World cat";
226         String old = "cat";
227         String neww = "dog";
228         String outputPath = "out";
229
230         param.put("source", sourceString);
231         param.put("target", old);
232         param.put("replacement", neww);
233         param.put("outputPath", outputPath);
234         SliStringUtils.replace(param, ctx);
235         assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
236     }
237
238     @Test
239     public void concat() throws SvcLogicException {
240         String sourceString = "cat";
241         String targetString = "dog";
242         String outputPath = "out";
243
244         param.put("source", sourceString);
245         param.put("target", targetString);
246         param.put("outputPath", outputPath);
247         SliStringUtils.concat(param, ctx);
248         assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
249     }
250
251     @Test
252     public void urlEncode() throws SvcLogicException {
253         String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
254         String outputPath = "out";
255
256         param.put("source", sourceString);
257         param.put("outputPath", outputPath);
258         SliStringUtils.urlEncode(param, ctx);
259         assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));
260     }
261
262 }