2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2018 Nokia Solutions and Networks
8 * =============================================================================
9 * Modifications Copyright (C) 2018 IBM
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
27 package org.onap.appc.util;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertNotNull;
32 import static org.junit.Assert.assertNull;
33 import static org.junit.Assert.assertTrue;
35 import com.google.common.collect.Lists;
36 import com.google.common.collect.Maps;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.Properties;
41 import java.util.Date;
43 import org.apache.commons.lang3.StringUtils;
44 import org.junit.Test;
45 import org.onap.appc.util.StringHelper;
48 public class StringHelperTest {
50 //TODO write more tests for convertToRegex method
53 public void convertToRegex_should_return_regex_matching_all_string_when_given_null_or_empty_string(){
54 assertEquals(".*", StringHelper.convertToRegex(null));
55 assertEquals(".*", StringHelper.convertToRegex(""));
56 assertEquals(".*", StringHelper.convertToRegex(" "));
60 public void convertToRegex_should_return_proper_regex_when_we_provide_a_proper_string_expression(){
61 String expected=".*test\\.jpg.test123\\.jpeg$";
62 assertEquals(expected,StringHelper.convertToRegex("*test.jpg+test123.jpeg"));
66 public void test_ResolveToType_with_null_as_input(){
67 assertNull(StringHelper.resolveToType(null));
71 public void test_ResolveToType_with_integer_as_input(){
72 Integer expected=-112;
73 assertEquals(expected,StringHelper.resolveToType("-112"));
77 public void test_ResolveToType_with_double_as_input(){
78 Double expected=-112.12;
79 assertEquals(expected,StringHelper.resolveToType("-112.12"));
83 public void test_ResolveToType_with_boolean_as_input(){
84 Boolean expected=true;
85 assertEquals(expected,StringHelper.resolveToType("true"));
89 public void test_ResolveToType_with_date_as_input(){
90 assertTrue(StringHelper.resolveToType("1994-11-05T08:15:30-05:00") instanceof Date);
95 public void getShortenedString_should_return_null_when_given_null(){
96 assertNull(StringHelper.getShortenedString(null, 2));
100 public void getShortenedString_should_return_given_string_when_length_is_lower_than_4(){
101 assertEquals("str", StringHelper.getShortenedString("str", 3));
102 assertEquals("str", StringHelper.getShortenedString("str", 2));
103 assertEquals("test", StringHelper.getShortenedString("test", 3));
107 public void getShortenedString_should_shorten_string_and_append_ellipsis(){
109 assertEquals("s...", StringHelper.getShortenedString("sample", 4));
110 assertEquals("test...", StringHelper.getShortenedString("test string", 7));
114 public void isNotNullNotEmpty_should_return_true_if_string_is_not_null_or_not_empty(){
115 assertFalse(StringHelper.isNotNullNotEmpty(null));
116 assertFalse(StringHelper.isNotNullNotEmpty(""));
117 assertFalse(StringHelper.isNotNullNotEmpty(" "));
118 assertTrue(StringHelper.isNotNullNotEmpty("test"));
122 public void isNullOrEmpty_should_return_true_if_string_is_null_or_empty(){
123 assertTrue(StringHelper.isNullOrEmpty(null));
124 assertTrue(StringHelper.isNullOrEmpty(""));
125 assertTrue(StringHelper.isNullOrEmpty(" "));
126 assertFalse(StringHelper.isNullOrEmpty("test"));
130 public void areEqual_should_return_true_when_both_null(){
131 assertTrue(StringHelper.areEqual(null, null));
135 public void areEqual_should_return_false_when_one_string_is_null(){
136 assertFalse(StringHelper.areEqual(null, "test"));
140 public void areEqual_should_compare_two_string(){
141 assertFalse(StringHelper.areEqual("test2", "test"));
142 assertFalse(StringHelper.areEqual("test", "Test"));
146 public void equalsIgnoreCase_should_compare_two_string_case_insensitive(){
147 assertFalse(StringHelper.equalsIgnoreCase("test2", "test"));
148 assertTrue(StringHelper.equalsIgnoreCase("test", "Test"));
152 public void mangleName_should_pad_string_when_given_null_or_empty(){
153 assertEquals("aaaa", StringHelper.mangleName(null, 3, 6));
154 assertEquals("aaaa", StringHelper.mangleName(StringUtils.EMPTY, 3, 6));
156 assertEquals("aa", StringHelper.mangleName(null, 1, 6));
157 assertEquals("aa", StringHelper.mangleName(StringUtils.EMPTY, 1, 6));
159 assertEquals("aaaaaaaaa", StringHelper.mangleName(null, 8, 12));
160 assertEquals("aaaaaaaaa", StringHelper.mangleName(StringUtils.EMPTY, 8, 12));
165 public void mangleName_should_remove_all_illegal_characters(){
166 assertEquals("ab45", StringHelper.mangleName("ab45 ", 3, 6));
167 assertEquals("ab45", StringHelper.mangleName("ab!45", 3, 6));
168 assertEquals("ab45", StringHelper.mangleName("a b!45", 3, 6));
172 public void mangleName_should_convert_all_character_to_lowercase(){
173 assertEquals("test", StringHelper.mangleName("TeSt", 3, 6));
174 assertEquals("abb45fgr", StringHelper.mangleName("abB!4 5FGR", 6, 8));
178 public void mangleName_should_pad_string_when_result_is_too_short(){
179 assertEquals("testaaa", StringHelper.mangleName("TeSt", 6, 10));
180 assertEquals("abb45fgraaaaa", StringHelper.mangleName("abB!4 5FGR", 12, 15));
184 public void mangleName_should_truncate_string_when_too_long(){
185 assertEquals("tst", StringHelper.mangleName("TeSt", 0, 3));
186 assertEquals("tt", StringHelper.mangleName("TeSt", 0, 2));
187 assertEquals("agr", StringHelper.mangleName("abb45fgr", 0, 3));
188 assertEquals("abgr", StringHelper.mangleName("abb45fgr", 0, 4));
192 public void normalizeString_should_return_null_when_given_null_or_empty_string(){
193 assertNull(StringHelper.normalizeString(null));
194 assertNull(StringHelper.normalizeString(StringUtils.EMPTY));
198 public void normalizeString_should_trim_string(){
199 assertEquals("this is test sequence",
200 StringHelper.normalizeString(" this is test sequence "));
201 assertEquals("this is test sequence",
202 StringHelper.normalizeString(" this is test sequence "));
206 public void stripCRLFs_should_return_null_when_given_null() {
207 assertNull(StringHelper.stripCRLF(null));
211 public void stripCRLF_should_strip_all_CRLF_and_LF() {
212 assertEquals(StringUtils.EMPTY, StringHelper.toUnixLines(StringUtils.EMPTY));
213 assertEquals("this is test sequence", StringHelper.stripCRLF("this is test sequence"));
214 assertEquals("this is testsequence", StringHelper.stripCRLF("this is test\nsequence"));
215 assertEquals("this istestsequence", StringHelper.stripCRLF("this is\ntest\r\nsequence"));
216 assertEquals("this istestsequence", StringHelper.stripCRLF("this is\r\ntest\n\rsequence"));
220 public void toDOSLines_should_return_null_when_given_null() {
221 assertNull(StringHelper.toDOSLines(null));
225 public void toUnixLines_should_replace_LF_with_CRLF() {
226 assertEquals(StringUtils.EMPTY, StringHelper.toUnixLines(StringUtils.EMPTY));
227 assertEquals("this is test sequence", StringHelper.toDOSLines("this is test sequence"));
228 assertEquals("this is test\r\nsequence", StringHelper.toDOSLines("this is test\nsequence"));
229 assertEquals("this is test\rsequence", StringHelper.toDOSLines("this is test\rsequence"));
230 assertEquals("this is\r\ntest\n\rsequence", StringHelper.toDOSLines("this is\r\ntest\n\rsequence"));
234 public void toUnixLines_should_return_null_when_given_null() {
235 assertNull(StringHelper.toUnixLines(null));
239 public void toUnixLines_should_replace_CRLF_with_LF() {
240 assertEquals(StringUtils.EMPTY, StringHelper.toUnixLines(StringUtils.EMPTY));
241 assertEquals("this is test sequence", StringHelper.toUnixLines("this is test sequence"));
242 assertEquals("this is test\nsequence", StringHelper.toUnixLines("this is test\nsequence"));
243 assertEquals("this is\ntest\nsequence", StringHelper.toUnixLines("this is\r\ntest\n\rsequence"));
247 public void translate_should_return_null_when_given_null_sequence() {
248 assertNull(StringHelper.translate(null, "abc", "def"));
252 public void translate_should_translate_sequence() {
254 assertEquals(StringUtils.EMPTY, StringHelper.translate(StringUtils.EMPTY, "abc", "def"));
255 assertEquals("ahis is absa sbqubccb",
256 StringHelper.translate("this is test sequence", "ten", "abc"));
261 public void translate_should_translate_sequence_given_replacement_longer_then_match() {
262 assertEquals("ahis is absa sbqubccb",
263 StringHelper.translate("this is test sequence", "ten", "abcde"));
267 public void translate_should_translate_sequence_given_replacement_shorter_then_match() {
268 assertEquals("ahas as absa sbqubccb",
269 StringHelper.translate("this is test sequence", "teni", "abc"));
273 public void validIdentifier_should_return_null_when_given_null() {
274 assertNull(StringHelper.validIdentifier(null));
279 public void validIdentifier_should_return_valid_identifier() {
280 assertEquals(StringUtils.EMPTY, StringHelper.validIdentifier(StringUtils.EMPTY));
281 assertEquals("abcd", StringHelper.validIdentifier("abcd"));
282 assertEquals("abc_", StringHelper.validIdentifier("abc!"));
283 assertEquals("ab_cd", StringHelper.validIdentifier("ab cd"));
284 assertEquals("ab_cd_", StringHelper.validIdentifier("ab cd!"));
288 public void verify_should_return_null_when_given_null_sequence() {
289 assertNull(StringHelper.verify(null, "abc", 'r'));
293 public void verify_should_return_empty_string_when_given_empty_sequence() {
294 assertEquals(StringUtils.EMPTY, StringHelper.verify("", "abc", 'r'));
298 public void verify_should_replace_illegal_characters() {
299 assertEquals("trir ir tert rerterre",
300 StringHelper.verify("this is test sentence", "iet ", 'r'));
304 public void toList_should_return_empty_string_when_given_null_or_empty_list() {
305 assertEquals(StringUtils.EMPTY, StringHelper.asList((List<String>) null));
306 assertEquals(StringUtils.EMPTY, StringHelper.asList((Lists.newArrayList())));
310 public void toList_should_return_element_when_given_one_element_list() {
311 assertEquals("test", StringHelper.asList(Lists.newArrayList("test")));
315 public void toList_should_convert_to_string_given_list() {
316 assertEquals("test, test2, test3",
317 StringHelper.asList(Lists.newArrayList("test", "test2", "test3")));
321 public void toList_should_return_empty_string_when_given_null_or_empty_map() {
322 assertEquals(StringUtils.EMPTY, StringHelper.asList((Map<String, String>) null));
323 assertEquals(StringUtils.EMPTY, StringHelper.asList((Maps.newHashMap())));
327 public void toList_should_return_entry_when_given_one_entry_map() {
328 Map<String, String> testMap = new HashMap<>();
329 testMap.put("key1", "value1");
331 assertEquals("key1=value1", StringHelper.asList(testMap));
335 public void toList_should_convert_to_string_given_map() {
336 Map<String, String> testMap = new HashMap<>();
337 testMap.put("key1", "value1");
338 testMap.put("key2", "value2");
340 assertEquals("key1=value1, key2=value2", StringHelper.asList(testMap));
344 public void toList_should_return_string_representation_of_empty_array_when_given_null() {
345 String value = StringHelper.asList((String[]) null);
346 assertNotNull(value);
347 assertEquals("[]", value);
351 public void toList_should_return_string_representation_of_empty_array_when_given_empty_array() {
352 String value = StringHelper.asList(new String[]{});
353 assertNotNull(value);
354 assertEquals("[]", value);
358 public void toList_should_return_string_representation_of_one_element_array() {
359 String value = StringHelper.asList("one");
360 assertNotNull(value);
361 assertEquals("[one]", value);
365 public void toList_should_return_string_representation_of_array() {
366 String value = StringHelper.asList("one", "two", "three", "four", "five");
367 assertNotNull(value);
368 assertEquals("[one,two,three,four,five]", value);
372 public void propertiesToString_should_return_null_when_given_null_properties() {
374 assertEquals(null, StringHelper.propertiesToString(null));
378 public void propertiesToString_should_return_string_representation_of_empty_array_when_given_empty_properties() {
380 Properties props = new Properties();
382 String result = StringHelper.propertiesToString(props);
383 assertNotNull(result);
384 assertEquals("[ ]", result);
388 public void propertiesToString_should_return_convert_properties_to_string() {
389 Properties props = new Properties();
390 props.setProperty("key1", "value1");
391 props.setProperty("key2", "value2");
393 String result = StringHelper.propertiesToString(props);
395 assertTrue(result.startsWith("[ "));
396 assertTrue(result.contains("key1"));
397 assertTrue(result.contains("value1"));
398 assertTrue(result.contains("key2"));
399 assertTrue(result.contains("value2"));
400 assertTrue(result.endsWith(" ]"));
401 assertTrue(result.lastIndexOf(",") < result.length() - 3);