summaryrefslogtreecommitdiff
path: root/eaaf_core/src/test
diff options
context:
space:
mode:
authorThomas Lenz <thomas.lenz@egiz.gv.at>2019-07-23 08:51:32 +0200
committerThomas Lenz <thomas.lenz@egiz.gv.at>2019-07-23 08:51:32 +0200
commit204e7dc0195b62a33f46aefb534cd59eb54b6c44 (patch)
treeb353036f93a31256d55f357975909c2907d33851 /eaaf_core/src/test
parent7a6633b824f425efc32c1515f6ed75da0f65c57b (diff)
downloadEAAF-Components-204e7dc0195b62a33f46aefb534cd59eb54b6c44.tar.gz
EAAF-Components-204e7dc0195b62a33f46aefb534cd59eb54b6c44.tar.bz2
EAAF-Components-204e7dc0195b62a33f46aefb534cd59eb54b6c44.zip
fix nullpointer in KeyValueUtils.java and add jUnit test
Diffstat (limited to 'eaaf_core/src/test')
-rw-r--r--eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/utils/KeyValueUtilsTest.java447
1 files changed, 447 insertions, 0 deletions
diff --git a/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/utils/KeyValueUtilsTest.java b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/utils/KeyValueUtilsTest.java
new file mode 100644
index 00000000..9df11ed6
--- /dev/null
+++ b/eaaf_core/src/test/java/at/gv/egiz/eaaf/core/impl/utils/KeyValueUtilsTest.java
@@ -0,0 +1,447 @@
+package at.gv.egiz.eaaf.core.impl.utils;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.apache.commons.lang3.RandomStringUtils;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.BlockJUnit4ClassRunner;
+
+@RunWith(BlockJUnit4ClassRunner.class)
+public class KeyValueUtilsTest {
+
+ @Test
+ public void getFirstChildTest_1() {
+ final String prefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String child = RandomStringUtils.randomAlphabetic(2);
+ final String key = prefix + KeyValueUtils.KEY_DELIMITER + child + KeyValueUtils.KEY_DELIMITER + RandomStringUtils.randomAlphabetic(4);
+ final String resut = KeyValueUtils.getFirstChildAfterPrefix(key, prefix);
+ Assert.assertEquals("First child not match", child, resut);
+
+ }
+
+ @Test
+ public void getFirstChildTest_2() {
+ final String prefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String child = RandomStringUtils.randomAlphabetic(2);
+ final String key = prefix + KeyValueUtils.KEY_DELIMITER + child;
+ final String resut = KeyValueUtils.getFirstChildAfterPrefix(key, prefix);
+ Assert.assertEquals("First child not match", child, resut);
+
+ }
+
+ @Test
+ public void getFirstChildTest_3() {
+ final String prefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String child = RandomStringUtils.randomAlphabetic(2);
+ final String key = prefix + KeyValueUtils.KEY_DELIMITER + child;
+ final String resut = KeyValueUtils.getFirstChildAfterPrefix(key, key);
+ Assert.assertNull("First child not null", resut);
+
+ }
+
+ @Test
+ public void getFirstChildTest_4() {
+ final String prefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String child = RandomStringUtils.randomAlphabetic(2);
+ final String key = prefix + KeyValueUtils.KEY_DELIMITER + child;
+ final String resut = KeyValueUtils.getFirstChildAfterPrefix(
+ RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER + key,
+ key);
+ Assert.assertNull("First child not null", resut);
+
+ }
+
+ @Test
+ public void getFirstChildTest_5() {
+ final String prefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String child = RandomStringUtils.randomAlphabetic(2);
+ final String key = child + KeyValueUtils.KEY_DELIMITER + prefix;
+ final String resut = KeyValueUtils.getFirstChildAfterPrefix(key, null);
+ Assert.assertEquals("First child not match", child, resut);
+
+ }
+
+ @Test
+ public void getFirstChildTest_6() {
+ final String prefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String child = RandomStringUtils.randomAlphabetic(2);
+ final String key = prefix + KeyValueUtils.KEY_DELIMITER + child;
+ final String resut = KeyValueUtils.getFirstChildAfterPrefix(key, key);
+ Assert.assertNull("First child not null", resut);
+
+ }
+
+ @Test
+ public void getPrefixFromKey_1() {
+ final String prefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String child = RandomStringUtils.randomAlphabetic(2);
+ final String key = prefix + KeyValueUtils.KEY_DELIMITER + child;
+ final String resut = KeyValueUtils.getPrefixFromKey(key, child);
+ Assert.assertEquals("Prefix not match", prefix, resut);
+
+ }
+
+ @Test
+ public void getPrefixFromKey_2() {
+ final String child = RandomStringUtils.randomAlphabetic(2);
+ final String resut = KeyValueUtils.getPrefixFromKey(null, child);
+ Assert.assertNull("Prefix not null", resut);
+
+ }
+
+ @Test
+ public void getPrefixFromKey_3() {
+ final String prefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String key = prefix + KeyValueUtils.KEY_DELIMITER + RandomStringUtils.randomAlphabetic(4);
+ final String resut = KeyValueUtils.getPrefixFromKey(key, RandomStringUtils.randomAlphabetic(5));
+ Assert.assertNull("Prefix not null", resut);
+
+ }
+
+ @Test
+ public void getPrefixFromKey_4() {
+ final String prefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String child = KeyValueUtils.KEY_DELIMITER + RandomStringUtils.randomAlphabetic(2);
+ final String key = prefix + child;
+ final String resut = KeyValueUtils.getPrefixFromKey(key, child);
+ Assert.assertEquals("Prefix not match", prefix, resut);
+
+ }
+
+ @Test
+ public void getPrefixFromKey_5() {
+ final String key = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String resut = KeyValueUtils.getPrefixFromKey(key, null);
+ Assert.assertNull("Prefix not null", resut);
+
+ }
+
+ @Test
+ public void getRemovePrefixesFromKeys_1() {
+ final String testPrefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final Map<String, String> testMap = generateTestMap(testPrefix , 5, 5);
+
+ final Map<String, String> result = KeyValueUtils.removePrefixFromKeys(testMap, testPrefix);
+ Assert.assertNotNull("Result is null", result);
+ Assert.assertFalse("Result is empty", result.isEmpty());
+ Assert.assertEquals("Result size not match", 5, result.size());
+ final Iterator<Entry<String, String>> it = result.entrySet().iterator();
+ while(it.hasNext()) {
+ final Entry<String, String> next = it.next();
+ Assert.assertNotNull("Key is null", next.getKey());
+ Assert.assertNotNull("Value is null", next.getValue());
+ Assert.assertTrue("Key is null", testMap.containsKey(testPrefix + "." + next.getKey()));
+ Assert.assertEquals("Value not match", testMap.get(testPrefix + "." + next.getKey()), next.getValue());
+
+ }
+
+ }
+
+ @Test
+ public void getSubSetWithPrefixTest_1() {
+ final String testPrefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final Map<String, String> testMap = generateTestMap(testPrefix , 5, 5);
+
+ final Map<String, String> result = KeyValueUtils.getSubSetWithPrefix(testMap, testPrefix);
+ Assert.assertNotNull("Result is null", result);
+ Assert.assertFalse("Result is empty", result.isEmpty());
+ Assert.assertEquals("Result size not match", 5, result.size());
+ final Iterator<Entry<String, String>> it = result.entrySet().iterator();
+ while(it.hasNext()) {
+ final Entry<String, String> next = it.next();
+ Assert.assertNotNull("Key is null", next.getKey());
+ Assert.assertNotNull("Value is null", next.getValue());
+ Assert.assertTrue("Key is null", testMap.containsKey(testPrefix + "." + next.getKey()));
+ Assert.assertEquals("Value not match", testMap.get(testPrefix + "." + next.getKey()), next.getValue());
+
+ }
+
+ }
+
+ @Test
+ public void makeKeysAbsolutTest_1() {
+ final String absTestPrefixtestPrefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String prefix = absTestPrefixtestPrefix + "." + RandomStringUtils.randomAlphabetic(4);
+ final Map<String, String> testMap = generateTestMap(prefix, 5, 5);
+ final Map<String, String> result = KeyValueUtils.makeKeysAbsolut(
+ testMap,
+ absTestPrefixtestPrefix,
+ prefix);
+
+ Assert.assertNotNull("Result is null", result);
+ Assert.assertFalse("Result is empty", result.isEmpty());
+ Assert.assertEquals("Result size not match", 10, result.size());
+ final Iterator<Entry<String, String>> it = result.entrySet().iterator();
+ while(it.hasNext()) {
+ final Entry<String, String> next = it.next();
+ Assert.assertNotNull("Key is null", next.getKey());
+ Assert.assertNotNull("Value is null", next.getValue());
+ if (testMap.containsKey(next.getKey()))
+ Assert.assertEquals("Value not match", testMap.get(next.getKey()), next.getValue());
+ else {
+ Assert.assertTrue("Key not found", testMap.containsKey(
+ next.getKey().substring(
+ absTestPrefixtestPrefix.length() + 1))
+ );
+ Assert.assertEquals("Value not match", testMap.get(
+ next.getKey().substring(
+ absTestPrefixtestPrefix.length() + 1)),
+ next.getValue());
+ }
+ }
+ }
+
+ @Test
+ public void getParentKeyTest_1() {
+ final String testPrefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final String result = KeyValueUtils.getParentKey(testPrefix + "." + RandomStringUtils.randomAlphabetic(5));
+ Assert.assertNotNull("Result is null", result);
+ Assert.assertEquals("Parent not match", testPrefix, result);
+
+ }
+
+ @Test
+ public void getParentKeyTest_2() {
+ final String result = KeyValueUtils.getParentKey(RandomStringUtils.randomAlphabetic(5));
+ Assert.assertNotNull("Result is null", result);
+ Assert.assertTrue("Result not empty", result.isEmpty());
+
+ }
+
+ @Test
+ public void findNextFreeListCoutnerTest_1() {
+ final String testPrefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final Set<String> propList = new HashSet<String>();
+ propList.add(testPrefix + ".1");
+ propList.add(testPrefix + ".2");
+ propList.add(testPrefix + ".0");
+ propList.add(testPrefix + ".4");
+ propList.add(testPrefix + ".3");
+
+ final int result = KeyValueUtils.findNextFreeListCounter(propList, testPrefix);
+ Assert.assertEquals("Next free element not fount", 5, result);
+
+ }
+
+ @Test
+ public void findNextFreeListCoutnerTest_2() {
+ final String testPrefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final Set<String> propList = new HashSet<String>();
+ propList.add(testPrefix + ".1");
+ propList.add(testPrefix + ".5");
+ propList.add(testPrefix + ".0");
+ propList.add(testPrefix + ".4");
+ propList.add(testPrefix + ".3");
+
+ final int result = KeyValueUtils.findNextFreeListCounter(propList, testPrefix);
+ Assert.assertEquals("Next free element not fount", 6, result);
+
+ }
+
+ @Test
+ public void findNextFreeListCoutnerTest_3() {
+ final String testPrefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final Set<String> propList = new HashSet<String>();;
+
+ final int result = KeyValueUtils.findNextFreeListCounter(propList, testPrefix);
+ Assert.assertEquals("Next free element not fount", 0, result);
+
+ }
+
+ @Test
+ public void findNextFreeListCoutnerTest_4() {
+ final String testPrefix = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ final java.util.List<String> propList = new ArrayList<String>();
+
+ final int result = KeyValueUtils.findNextFreeListCounter(propList.stream().toArray(String[]::new), testPrefix);
+ Assert.assertEquals("Next free element not fount", 0, result);
+
+ }
+
+ @Test
+ public void normalizeCSVValueStringTest_1() {
+ final String csv1 = RandomStringUtils.randomAlphanumeric(5);
+ final String csv2 = RandomStringUtils.randomAlphanumeric(5);
+ final String csv3 = RandomStringUtils.randomAlphanumeric(5);
+ final String csv4 = RandomStringUtils.randomAlphanumeric(5);
+ final String testValue = " " + csv1 + " ," + csv2 + "," + csv3 + "\n," + csv4 + " ";
+
+ final String result = KeyValueUtils.normalizeCSVValueString(testValue);
+
+ Assert.assertNotNull("Result is null", result);
+ Assert.assertFalse("Result is empty", result.isEmpty());
+ final String[] check = result.split(",");
+ Assert.assertEquals("Result size wrong", 4, check.length);
+ Assert.assertEquals("Result 1 wrong", csv1, check[0]);
+ Assert.assertEquals("Result 2 wrong", csv2, check[1]);
+ Assert.assertEquals("Result 3 wrong", csv3, check[2]);
+ Assert.assertEquals("Result 4 wrong", csv4, check[3]);
+
+ }
+
+ @Test
+ public void isCSVValueStringTest_1() {
+ final String csv1 = RandomStringUtils.randomAlphanumeric(5);
+ final String csv2 = RandomStringUtils.randomAlphanumeric(5);
+ final String csv3 = RandomStringUtils.randomAlphanumeric(5);
+ final String csv4 = RandomStringUtils.randomAlphanumeric(5);
+ final String testValue = " " + csv1 + " ," + csv2 + "," + csv3 + "\n," + csv4 + " ";
+ final boolean result = KeyValueUtils.isCSVValueString(testValue);
+ Assert.assertTrue("CSV value not detected", result);
+
+ }
+
+ @Test
+ public void isCSVValueStringTest_2() {
+ final String csv1 = RandomStringUtils.randomAlphanumeric(5);
+ final String testValue = " " + csv1 + " ,";
+ final boolean result = KeyValueUtils.isCSVValueString(testValue);
+ Assert.assertFalse("CSV value not detected", result);
+
+ }
+
+ @Test
+ public void isCSVValueStringTest_3() {
+ final String csv1 = RandomStringUtils.randomAlphanumeric(5);
+ final String testValue = " " + csv1;
+ final boolean result = KeyValueUtils.isCSVValueString(testValue);
+ Assert.assertFalse("CSV value not detected", result);
+
+ }
+
+ @Test
+ public void getListOfCSVValuesTest_1() {
+ final String csv1 = RandomStringUtils.randomAlphanumeric(5);
+ final String csv2 = RandomStringUtils.randomAlphanumeric(5);
+ final String csv3 = RandomStringUtils.randomAlphanumeric(5);
+ final String csv4 = RandomStringUtils.randomAlphanumeric(5);
+ final String testValue = " " + csv1 + " ," + csv2 + "," + csv3 + "\n," + csv4 + " ";
+
+ final List<String> result = KeyValueUtils.getListOfCSVValues(testValue);
+
+ Assert.assertNotNull("Result is null", result);
+ Assert.assertFalse("Result is empty", result.isEmpty());
+ Assert.assertEquals("Result size wrong", 4, result.size());
+ Assert.assertEquals("Result 1 wrong", csv1, result.get(0));
+ Assert.assertEquals("Result 2 wrong", csv2, result.get(1));
+ Assert.assertEquals("Result 3 wrong", csv3, result.get(2));
+ Assert.assertEquals("Result 4 wrong", csv4, result.get(3));
+
+ }
+
+ @Test
+ public void convertListToMapTest_1() {
+ final java.util.List<String> propList = new ArrayList<String>();
+ final String prefix = RandomStringUtils.randomAlphabetic(4) + ".";
+ final String key1 = RandomStringUtils.randomAlphabetic(5);
+ final String value1 = RandomStringUtils.randomAlphanumeric(10);
+ final String key2 = RandomStringUtils.randomAlphabetic(5);
+ final String value2 = RandomStringUtils.randomAlphanumeric(10);
+ final String key3 = RandomStringUtils.randomAlphabetic(5);
+ final String value3 = RandomStringUtils.randomAlphanumeric(10);
+ final String key4 = RandomStringUtils.randomAlphabetic(5);
+ final String value4 = RandomStringUtils.randomAlphanumeric(10);
+ final String key5 = RandomStringUtils.randomAlphabetic(5);
+ final String value5 = RandomStringUtils.randomAlphanumeric(10);
+ final String key6 = RandomStringUtils.randomAlphabetic(5);
+ final String value6 = "="+RandomStringUtils.randomAlphanumeric(10);
+
+ propList.add(prefix + key1 + "=" + value1);
+ propList.add(prefix + key2 + "=" + value2);
+ propList.add(prefix + key3 + "=" + value3);
+ propList.add(prefix + key4 + "=" + value4);
+ propList.add(prefix + key5 + "+" + value5);
+ propList.add(prefix + key6 + "=" + value6);
+
+ final Map<String, String> result = KeyValueUtils.convertListToMap(propList);
+ Assert.assertNotNull("Result is null", result);
+ Assert.assertFalse("Result is empty", result.isEmpty());
+ Assert.assertEquals("Result size not match", 5, result.size());
+
+ Assert.assertTrue("Key1 not found", result.containsKey(prefix+key1));
+ Assert.assertEquals("Value1 not found", value1, result.get(prefix+key1));
+ Assert.assertTrue("Key2 not found", result.containsKey(prefix+key2));
+ Assert.assertEquals("Value2 not found", value2, result.get(prefix+key2));
+ Assert.assertTrue("Key3 not found", result.containsKey(prefix+key3));
+ Assert.assertEquals("Value3 not found", value3, result.get(prefix+key3));
+ Assert.assertTrue("Key4 not found", result.containsKey(prefix+key4));
+ Assert.assertEquals("Value4 not found", value4, result.get(prefix+key4));
+
+ }
+
+ @Test
+ public void convertListToMapTest_2() {
+ final java.util.List<String> propList = new ArrayList<String>();
+
+ final Map<String, String> result = KeyValueUtils.convertListToMap(propList);
+ Assert.assertNotNull("Result is null", result);
+ Assert.assertTrue("Result is not empty", result.isEmpty());
+
+ }
+
+ private Map<String, String> generateTestMap(String testPrefix, int entriesWithPrefix, int entriesWithoutPrefix) {
+ final Map<String, String> result = new HashMap<String, String>();
+ for (int i=0; i<entriesWithPrefix; i++)
+ result.put(
+ testPrefix + KeyValueUtils.KEY_DELIMITER + RandomStringUtils.randomAlphabetic(5),
+ RandomStringUtils.randomAlphabetic(5));
+
+ final String key = RandomStringUtils.randomAlphabetic(4) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(6) + KeyValueUtils.KEY_DELIMITER
+ + RandomStringUtils.randomAlphabetic(5);
+ for (int i=0; i<entriesWithoutPrefix; i++)
+ result.put(
+ key + KeyValueUtils.KEY_DELIMITER + RandomStringUtils.randomAlphabetic(5),
+ RandomStringUtils.randomAlphabetic(5));
+
+ return result;
+
+ }
+
+
+}