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