2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2018 Nokia Solutions and Networks
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
26 package org.onap.appc.util;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertNull;
32 import static org.junit.Assert.assertTrue;
34 import com.google.common.collect.Lists;
35 import com.google.common.collect.Maps;
36 import java.util.Arrays;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.Properties;
42 import org.apache.commons.lang3.StringUtils;
43 import org.junit.Test;
44 import org.onap.appc.util.StringHelper;
47 public class StringHelperTest {
49 //TODO write more tests for convertToRegex method
52 public void convertToRegex_should_return_regex_matching_all_string_when_given_null_or_empty_string(){
53 assertEquals(".*", StringHelper.convertToRegex(null));
54 assertEquals(".*", StringHelper.convertToRegex(""));
55 assertEquals(".*", StringHelper.convertToRegex(" "));
59 public void getShortenedString_should_return_null_when_given_null(){
60 assertNull(StringHelper.getShortenedString(null, 2));
64 public void getShortenedString_should_return_given_string_when_length_is_lower_than_4(){
65 assertEquals("str", StringHelper.getShortenedString("str", 3));
66 assertEquals("str", StringHelper.getShortenedString("str", 2));
67 assertEquals("test", StringHelper.getShortenedString("test", 3));
71 public void getShortenedString_should_shorten_string_and_append_ellipsis(){
73 assertEquals("s...", StringHelper.getShortenedString("sample", 4));
74 assertEquals("test...", StringHelper.getShortenedString("test string", 7));
78 public void isNotNullNotEmpty_should_return_true_if_string_is_not_null_or_not_empty(){
79 assertFalse(StringHelper.isNotNullNotEmpty(null));
80 assertFalse(StringHelper.isNotNullNotEmpty(""));
81 assertFalse(StringHelper.isNotNullNotEmpty(" "));
82 assertTrue(StringHelper.isNotNullNotEmpty("test"));
86 public void isNullOrEmpty_should_return_true_if_string_is_null_or_empty(){
87 assertTrue(StringHelper.isNullOrEmpty(null));
88 assertTrue(StringHelper.isNullOrEmpty(""));
89 assertTrue(StringHelper.isNullOrEmpty(" "));
90 assertFalse(StringHelper.isNullOrEmpty("test"));
94 public void areEqual_should_return_true_when_both_null(){
95 assertTrue(StringHelper.areEqual(null, null));
99 public void areEqual_should_return_false_when_one_string_is_null(){
100 assertFalse(StringHelper.areEqual(null, "test"));
104 public void areEqual_should_compare_two_string(){
105 assertFalse(StringHelper.areEqual("test2", "test"));
106 assertFalse(StringHelper.areEqual("test", "Test"));
110 public void equalsIgnoreCase_should_compare_two_string_case_insensitive(){
111 assertFalse(StringHelper.equalsIgnoreCase("test2", "test"));
112 assertTrue(StringHelper.equalsIgnoreCase("test", "Test"));
116 public void mangleName_should_pad_string_when_given_null_or_empty(){
117 assertEquals("aaaa", StringHelper.mangleName(null, 3, 6));
118 assertEquals("aaaa", StringHelper.mangleName(StringUtils.EMPTY, 3, 6));
120 assertEquals("aa", StringHelper.mangleName(null, 1, 6));
121 assertEquals("aa", StringHelper.mangleName(StringUtils.EMPTY, 1, 6));
123 assertEquals("aaaaaaaaa", StringHelper.mangleName(null, 8, 12));
124 assertEquals("aaaaaaaaa", StringHelper.mangleName(StringUtils.EMPTY, 8, 12));
129 public void mangleName_should_remove_all_illegal_characters(){
130 assertEquals("ab45", StringHelper.mangleName("ab45 ", 3, 6));
131 assertEquals("ab45", StringHelper.mangleName("ab!45", 3, 6));
132 assertEquals("ab45", StringHelper.mangleName("a b!45", 3, 6));
136 public void mangleName_should_convert_all_character_to_lowercase(){
137 assertEquals("test", StringHelper.mangleName("TeSt", 3, 6));
138 assertEquals("abb45fgr", StringHelper.mangleName("abB!4 5FGR", 6, 8));
142 public void mangleName_should_pad_string_when_result_is_too_short(){
143 assertEquals("testaaa", StringHelper.mangleName("TeSt", 6, 10));
144 assertEquals("abb45fgraaaaa", StringHelper.mangleName("abB!4 5FGR", 12, 15));
148 public void mangleName_should_truncate_string_when_too_long(){
149 assertEquals("tst", StringHelper.mangleName("TeSt", 0, 3));
150 assertEquals("tt", StringHelper.mangleName("TeSt", 0, 2));
151 assertEquals("agr", StringHelper.mangleName("abb45fgr", 0, 3));
152 assertEquals("abgr", StringHelper.mangleName("abb45fgr", 0, 4));
156 public void normalizeString_should_return_null_when_given_null_or_empty_string(){
157 assertNull(StringHelper.normalizeString(null));
158 assertNull(StringHelper.normalizeString(StringUtils.EMPTY));
162 public void normalizeString_should_trim_string(){
163 assertEquals("this is test sequence",
164 StringHelper.normalizeString(" this is test sequence "));
165 assertEquals("this is test sequence",
166 StringHelper.normalizeString(" this is test sequence "));
170 public void stripCRLFs_should_return_null_when_given_null() {
171 assertNull(StringHelper.stripCRLF(null));
175 public void stripCRLF_should_strip_all_CRLF_and_LF() {
176 assertEquals(StringUtils.EMPTY, StringHelper.toUnixLines(StringUtils.EMPTY));
177 assertEquals("this is test sequence", StringHelper.stripCRLF("this is test sequence"));
178 assertEquals("this is testsequence", StringHelper.stripCRLF("this is test\nsequence"));
179 assertEquals("this istestsequence", StringHelper.stripCRLF("this is\ntest\r\nsequence"));
180 assertEquals("this istestsequence", StringHelper.stripCRLF("this is\r\ntest\n\rsequence"));
184 public void toDOSLines_should_return_null_when_given_null() {
185 assertNull(StringHelper.toDOSLines(null));
189 public void toUnixLines_should_replace_LF_with_CRLF() {
190 assertEquals(StringUtils.EMPTY, StringHelper.toUnixLines(StringUtils.EMPTY));
191 assertEquals("this is test sequence", StringHelper.toDOSLines("this is test sequence"));
192 assertEquals("this is test\r\nsequence", StringHelper.toDOSLines("this is test\nsequence"));
193 assertEquals("this is test\rsequence", StringHelper.toDOSLines("this is test\rsequence"));
194 assertEquals("this is\r\ntest\n\rsequence", StringHelper.toDOSLines("this is\r\ntest\n\rsequence"));
198 public void toUnixLines_should_return_null_when_given_null() {
199 assertNull(StringHelper.toUnixLines(null));
203 public void toUnixLines_should_replace_CRLF_with_LF() {
204 assertEquals(StringUtils.EMPTY, StringHelper.toUnixLines(StringUtils.EMPTY));
205 assertEquals("this is test sequence", StringHelper.toUnixLines("this is test sequence"));
206 assertEquals("this is test\nsequence", StringHelper.toUnixLines("this is test\nsequence"));
207 assertEquals("this is\ntest\nsequence", StringHelper.toUnixLines("this is\r\ntest\n\rsequence"));
211 public void translate_should_return_null_when_given_null_sequence() {
212 assertNull(StringHelper.translate(null, "abc", "def"));
216 public void translate_should_translate_sequence() {
218 assertEquals(StringUtils.EMPTY, StringHelper.translate(StringUtils.EMPTY, "abc", "def"));
219 assertEquals("ahis is absa sbqubccb",
220 StringHelper.translate("this is test sequence", "ten", "abc"));
225 public void translate_should_translate_sequence_given_replacement_longer_then_match() {
226 assertEquals("ahis is absa sbqubccb",
227 StringHelper.translate("this is test sequence", "ten", "abcde"));
231 public void translate_should_translate_sequence_given_replacement_shorter_then_match() {
232 assertEquals("ahas as absa sbqubccb",
233 StringHelper.translate("this is test sequence", "teni", "abc"));
237 public void validIdentifier_should_return_null_when_given_null() {
238 assertNull(StringHelper.validIdentifier(null));
243 public void validIdentifier_should_return_valid_identifier() {
244 assertEquals(StringUtils.EMPTY, StringHelper.validIdentifier(StringUtils.EMPTY));
245 assertEquals("abcd", StringHelper.validIdentifier("abcd"));
246 assertEquals("abc_", StringHelper.validIdentifier("abc!"));
247 assertEquals("ab_cd", StringHelper.validIdentifier("ab cd"));
248 assertEquals("ab_cd_", StringHelper.validIdentifier("ab cd!"));
252 public void verify_should_return_null_when_given_null_sequence() {
253 assertNull(StringHelper.verify(null, "abc", 'r'));
257 public void verify_should_return_empty_string_when_given_empty_sequence() {
258 assertEquals(StringUtils.EMPTY, StringHelper.verify("", "abc", 'r'));
262 public void verify_should_replace_illegal_characters() {
263 assertEquals("trir ir tert rerterre",
264 StringHelper.verify("this is test sentence", "iet ", 'r'));
268 public void toList_should_return_empty_string_when_given_null_or_empty_list() {
269 assertEquals(StringUtils.EMPTY, StringHelper.asList((List<String>) null));
270 assertEquals(StringUtils.EMPTY, StringHelper.asList((Lists.newArrayList())));
274 public void toList_should_return_element_when_given_one_element_list() {
275 assertEquals("test", StringHelper.asList(Lists.newArrayList("test")));
279 public void toList_should_convert_to_string_given_list() {
280 assertEquals("test, test2, test3",
281 StringHelper.asList(Lists.newArrayList("test", "test2", "test3")));
285 public void toList_should_return_empty_string_when_given_null_or_empty_map() {
286 assertEquals(StringUtils.EMPTY, StringHelper.asList((Map<String, String>) null));
287 assertEquals(StringUtils.EMPTY, StringHelper.asList((Maps.newHashMap())));
291 public void toList_should_return_entry_when_given_one_entry_map() {
292 Map<String, String> testMap = new HashMap<>();
293 testMap.put("key1", "value1");
295 assertEquals("key1=value1", StringHelper.asList(testMap));
299 public void toList_should_convert_to_string_given_map() {
300 Map<String, String> testMap = new HashMap<>();
301 testMap.put("key1", "value1");
302 testMap.put("key2", "value2");
304 assertEquals("key1=value1, key2=value2", StringHelper.asList(testMap));
308 public void toList_should_return_string_representation_of_empty_array_when_given_null() {
309 String value = StringHelper.asList((String[]) null);
310 assertNotNull(value);
311 assertEquals("[]", value);
315 public void toList_should_return_string_representation_of_empty_array_when_given_empty_array() {
316 String value = StringHelper.asList(new String[]{});
317 assertNotNull(value);
318 assertEquals("[]", value);
322 public void toList_should_return_string_representation_of_one_element_array() {
323 String value = StringHelper.asList("one");
324 assertNotNull(value);
325 assertEquals("[one]", value);
329 public void toList_should_return_string_representation_of_array() {
330 String value = StringHelper.asList("one", "two", "three", "four", "five");
331 assertNotNull(value);
332 assertEquals("[one,two,three,four,five]", value);
336 public void propertiesToString_should_return_null_when_given_null_properties() {
338 assertEquals(null, StringHelper.propertiesToString(null));
342 public void propertiesToString_should_return_string_representation_of_empty_array_when_given_empty_properties() {
344 Properties props = new Properties();
346 String result = StringHelper.propertiesToString(props);
347 assertNotNull(result);
348 assertEquals("[ ]", result);
352 public void propertiesToString_should_return_convert_properties_to_string() {
353 Properties props = new Properties();
354 props.setProperty("key1", "value1");
355 props.setProperty("key2", "value2");
357 String result = StringHelper.propertiesToString(props);
359 assertTrue(result.startsWith("[ "));
360 assertTrue(result.contains("key1"));
361 assertTrue(result.contains("value1"));
362 assertTrue(result.contains("key2"));
363 assertTrue(result.contains("value2"));
364 assertTrue(result.endsWith(" ]"));
365 assertTrue(result.lastIndexOf(",") < result.length() - 3);