d57cefa0d7d246e8d1ab27d3a924c5b6a1402159
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / test / java / org / openecomp / sdnc / sli / SliPluginUtils / SliStringUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
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.openecomp.sdnc.sli.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.openecomp.sdnc.sli.SvcLogicContext;
37 import org.openecomp.sdnc.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 void equalsIgnoreCaseTrue() throws SvcLogicException {
95         String sourceString = "HeLlOwORLD";
96         String targetSTring = "HELLOWORLD";
97         param.put("source", sourceString);
98         param.put("target", targetSTring);
99         assertEquals("true", SliStringUtils.equalsIgnoreCase(param, ctx));
100     }
101
102     @Test
103     public void equalsIgnoreCaseFalse() throws SvcLogicException {
104         String sourceString = "HeLlOwORLD";
105         String targetSTring = "goodbyeWORLD";
106         param.put("source", sourceString);
107         param.put("target", targetSTring);
108         assertEquals("false", SliStringUtils.equalsIgnoreCase(param, ctx));
109     }
110
111     @Test
112     public void toUpper() throws SvcLogicException {
113         String sourceString = "HeLlOwORLD";
114         param.put("source", sourceString);
115         String path = "my.unique.path.";
116         param.put("outputPath", path);
117         SliStringUtils.toUpper(param, ctx);
118         assertEquals(sourceString.toUpperCase(), ctx.getAttribute(path));
119     }
120
121     @Test
122     public void toLower() throws SvcLogicException {
123         String sourceString = "HeLlOwORLD";
124         param.put("source", sourceString);
125         String path = "my.unique.path.";
126         param.put("outputPath", path);
127         SliStringUtils.toLower(param, ctx);
128         assertEquals(sourceString.toLowerCase(), ctx.getAttribute(path));
129     }
130
131     @Test
132     public void containsTrue() throws SvcLogicException {
133         String sourceString = "Pizza";
134         String targetSTring = "izza";
135         param.put("source", sourceString);
136         param.put("target", targetSTring);
137         assertEquals("true", SliStringUtils.contains(param, ctx));
138     }
139
140     @Test
141     public void containsFalse() throws SvcLogicException {
142         String sourceString = "Pizza";
143         String targetSTring = "muffin";
144         param.put("source", sourceString);
145         param.put("target", targetSTring);
146         assertEquals("false", SliStringUtils.contains(param, ctx));
147     }
148
149     @Test
150     public void endsWithTrue() throws SvcLogicException {
151         String sourceString = "Pizza";
152         String targetSTring = "za";
153         param.put("source", sourceString);
154         param.put("target", targetSTring);
155         assertEquals("true", SliStringUtils.endsWith(param, ctx));
156     }
157
158     @Test
159     public void endsWithFalse() throws SvcLogicException {
160         String sourceString = "Pizza";
161         String targetSTring = "muffin";
162         param.put("source", sourceString);
163         param.put("target", targetSTring);
164         assertEquals("false", SliStringUtils.endsWith(param, ctx));
165     }
166
167     @Test
168     public void trim() throws SvcLogicException {
169         String sourceString = " H E L L O W O R L D";
170         String outputPath = "muffin";
171         param.put("source", sourceString);
172         param.put("outputPath", outputPath);
173         SliStringUtils.trim(param, ctx);
174         assertEquals(sourceString.trim(), ctx.getAttribute(outputPath));
175     }
176
177     @Test
178     public void getLength() throws SvcLogicException {
179         String sourceString = "SomeRandomString";
180         String outputPath = "muffin";
181         param.put("source", sourceString);
182         param.put("outputPath", outputPath);
183         SliStringUtils.getLength(param, ctx);
184         assertEquals(String.valueOf(sourceString.length()), ctx.getAttribute(outputPath));
185     }
186
187     @Test
188     public void startsWithFalse() throws SvcLogicException {
189         String sourceString = "Java";
190         String targetSTring = "DG";
191         param.put("source", sourceString);
192         param.put("target", targetSTring);
193         assertEquals("false", SliStringUtils.startsWith(param, ctx));
194     }
195
196     @Test
197     public void startsWithTrue() throws SvcLogicException {
198         String sourceString = "Java";
199         String targetSTring = "Ja";
200         param.put("source", sourceString);
201         param.put("target", targetSTring);
202         assertEquals("true", SliStringUtils.startsWith(param, ctx));
203     }
204
205     @Test
206     public void replace() throws SvcLogicException {
207         String sourceString = "cat Hello World cat";
208         String old = "cat";
209         String neww = "dog";
210         String outputPath = "out";
211
212         param.put("source", sourceString);
213         param.put("target", old);
214         param.put("replacement", neww);
215         param.put("outputPath", outputPath);
216         SliStringUtils.replace(param, ctx);
217         assertEquals(sourceString.replace(old, neww), ctx.getAttribute(outputPath));
218     }
219
220     @Test
221     public void concat() throws SvcLogicException {
222         String sourceString = "cat";
223         String targetString = "dog";
224         String outputPath = "out";
225
226         param.put("source", sourceString);
227         param.put("target", targetString);
228         param.put("outputPath", outputPath);
229         SliStringUtils.concat(param, ctx);
230         assertEquals(sourceString + targetString, ctx.getAttribute(outputPath));
231     }
232
233     @Test
234     public void urlEncode() throws SvcLogicException {
235         String sourceString = "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4";
236         String outputPath = "out";
237
238         param.put("source", sourceString);
239         param.put("outputPath", outputPath);
240         SliStringUtils.urlEncode(param, ctx);
241         assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath));
242     }
243
244 }