09683ac36836d07528e333e93c2b3068ee5ff01d
[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 import org.onap.ccsdk.sli.core.slipluginutils.SliStringUtils;
39
40 /**
41  * @author km991u
42  *
43  */
44 public class SliStringUtilsTest {
45     private SvcLogicContext ctx;
46     private HashMap<String, String> param;
47     private SliStringUtils stringUtils = new SliStringUtils();
48
49     /**
50      * @throws java.lang.Exception
51      */
52     @Before
53     public void setUp() throws Exception {
54         this.ctx = new SvcLogicContext();
55         param = new HashMap<String, String>();
56     }
57
58     /**
59      * @throws SvcLogicException
60      * @see SliStringUtils#split(Map, SvcLogicContext)
61      */
62     @Test
63     public final void testSplit() throws SvcLogicException {
64         param.put("original_string", "one ## two ## three");
65         param.put("regex", " ## ");
66         param.put("ctx_memory_result_key", "result");
67
68         stringUtils.split(param, ctx);
69
70         assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
71         assertThat(ctx.getAttribute("result[1]"), equalTo("two"));
72         assertThat(ctx.getAttribute("result[2]"), equalTo("three"));
73         assertThat(ctx.getAttribute("result_length"), equalTo("3"));
74     }
75
76     /**
77      * @throws SvcLogicException
78      * @see SliStringUtils#split(Map, SvcLogicContext)
79      */
80     @Test
81     public final void testSplit_limit() throws SvcLogicException {
82         param.put("original_string", "one ## two ## three");
83         param.put("regex", " ## ");
84         param.put("limit", "2");
85         param.put("ctx_memory_result_key", "result");
86
87         stringUtils.split(param, ctx);
88
89         assertThat(ctx.getAttribute("result[0]"), equalTo("one"));
90         assertThat(ctx.getAttribute("result[1]"), equalTo("two ## three"));
91         assertThat(ctx.getAttribute("result_length"), equalTo("2"));
92     }
93
94     @Test
95     public void equalsIgnoreCaseTrue() throws SvcLogicException {
96         String sourceString = "HeLlOwORLD";
97         String targetSTring = "HELLOWORLD";
98         param.put("source", sourceString);
99         param.put("target", targetSTring);
100         assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
101     }
102
103     @Test
104     public void equalsIgnoreCaseFalse() throws SvcLogicException {
105         String sourceString = "HeLlOwORLD";
106         String targetSTring = "goodbyeWORLD";
107         param.put("source", sourceString);
108         param.put("target", targetSTring);
109         assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
110     }
111
112     @Test
113     public void toUpper() throws SvcLogicException {
114         String sourceString = "HeLlOwORLD";
115         param.put("source", sourceString);
116         String path = "my.unique.path.";
117         param.put("outputPath", path);
118         SliStringUtils.toUpper(param, ctx);
119         assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
120     }
121
122     @Test
123     public void toLower() throws SvcLogicException {
124         String sourceString = "HeLlOwORLD";
125         param.put("source", sourceString);
126         String path = "my.unique.path.";
127         param.put("outputPath", path);
128         SliStringUtils.toLower(param, ctx);
129         assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
130     }
131
132     @Test
133     public void containsTrue() throws SvcLogicException {
134         String sourceString = "Pizza";
135         String targetSTring = "izza";
136         param.put("source", sourceString);
137         param.put("target", targetSTring);
138         assertEquals("true", SliStringUtils.contains(param, ctx));
139     }
140
141     @Test
142     public void containsFalse() throws SvcLogicException {
143         String sourceString = "Pizza";
144         String targetSTring = "muffin";
145         param.put("source", sourceString);
146         param.put("target", targetSTring);
147         assertEquals("false", SliStringUtils.contains(param, ctx));
148     }
149
150     @Test
151     public void endsWithTrue() throws SvcLogicException {
152         String sourceString = "Pizza";
153         String targetSTring = "za";
154         param.put("source", sourceString);
155         param.put("target", targetSTring);
156         assertEquals("true", SliStringUtils.endsWith(param, ctx));
157     }
158
159     @Test
160     public void endsWithFalse() throws SvcLogicException {
161         String sourceString = "Pizza";
162         String targetSTring = "muffin";
163         param.put("source", sourceString);
164         param.put("target", targetSTring);
165         assertEquals("false", SliStringUtils.endsWith(param, ctx));
166     }
167
168     @Test
169     public void trim() throws SvcLogicException {
170         String sourceString = " H E L L O W O R L D";
171         String outputPath = "muffin";
172         param.put("source", sourceString);
173         param.put("outputPath", outputPath);
174         SliStringUtils.trim(param, ctx);
175         assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
176     }
177
178     @Test
179     public void getLength() throws SvcLogicException {
180         String sourceString = "SomeRandomString";
181         String outputPath = "muffin";
182         param.put("source", sourceString);
183         param.put("outputPath", outputPath);
184         SliStringUtils.getLength(param, ctx);
185         assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
186     }
187
188     @Test
189     public void startsWithFalse() throws SvcLogicException {
190         String sourceString = "Java";
191         String targetSTring = "DG";
192         param.put("source", sourceString);
193         param.put("target", targetSTring);
194         assertEquals("false", SliStringUtils.startsWith(param, ctx));
195     }
196
197     @Test
198     public void startsWithTrue() throws SvcLogicException {
199         String sourceString = "Java";
200         String targetSTring = "Ja";
201         param.put("source", sourceString);
202         param.put("target", targetSTring);
203         assertEquals("true", SliStringUtils.startsWith(param, ctx));
204     }
205
206     @Test
207     public void replace() throws SvcLogicException {
208         String sourceString = "cat Hello World cat";
209         String old = "cat";
210         String neww = "dog";
211         String outputPath = "out";
212
213         param.put("source", sourceString);
214         param.put("target", old);
215         param.put("replacement", neww);
216         param.put("outputPath", outputPath);
217         SliStringUtils.replace(param, ctx);
218         assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
219     }
220
221     @Test
222     public void concat() throws SvcLogicException {
223         String sourceString = "cat";
224         String targetString = "dog";
225         String outputPath = "out";
226
227         param.put("source", sourceString);
228         param.put("target", targetString);
229         param.put("outputPath", outputPath);
230         SliStringUtils.concat(param, ctx);
231         assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
232     }
233
234     @Test
235     public void urlEncode() throws SvcLogicException {
236         String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
237         String outputPath = "out";
238
239         param.put("source", sourceString);
240         param.put("outputPath", outputPath);
241         SliStringUtils.urlEncode(param, ctx);
242         assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));
243     }
244
245 }