diff options
Diffstat (limited to 'eaaf_core_utils/src/test')
2 files changed, 497 insertions, 46 deletions
diff --git a/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/impl/logging/JUnitTestStatusMessenger.java b/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/impl/logging/JUnitTestStatusMessenger.java index 5cdd404c..be5d95b1 100644 --- a/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/impl/logging/JUnitTestStatusMessenger.java +++ b/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/impl/logging/JUnitTestStatusMessenger.java @@ -3,54 +3,59 @@ package at.gv.egiz.eaaf.core.impl.logging; import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; - import at.gv.egiz.eaaf.core.api.IStatusMessenger; public class JUnitTestStatusMessenger implements IStatusMessenger { - private final Map<String, String> msgStore = new HashMap<>(); - - @Override - public String getMessage(String messageId, Object[] parameters) { - final String msg = getMessageWithoutDefault(messageId, parameters); - if (msg != null) { - return msg; - - } else { - return MessageFormat.format(messageId, parameters); - - } - - } - - @Override - public String getMessageWithoutDefault(String messageId, Object[] parameters) { - if (messageId != null) { - if (msgStore.containsKey(messageId)) { - return MessageFormat.format(msgStore.get(messageId), parameters); - - } - } - - return null; - } - - @Override - public String getResponseErrorCode(Throwable throwable) { - return null; - } - - @Override - public String mapInternalErrorToExternalError(String intErrorCode) { - return null; - } - - public void addMsg(String msgCode, String msg) { - if (!msgStore.containsKey(msgCode)) { - msgStore.put(msgCode, msg); - - } - - } - + private final Map<String, String> msgStore = new HashMap<>(); + + @Override + public String getMessage(final String messageId, final Object[] parameters) { + final String msg = getMessageWithoutDefault(messageId, parameters); + if (msg != null) { + return msg; + + } else { + return MessageFormat.format(messageId, parameters); + + } + + } + + @Override + public String getMessageWithoutDefault(final String messageId, final Object[] parameters) { + if (messageId != null) { + if (msgStore.containsKey(messageId)) { + return MessageFormat.format(msgStore.get(messageId), parameters); + + } + } + + return null; + } + + @Override + public String getResponseErrorCode(final Throwable throwable) { + return null; + } + + @Override + public String mapInternalErrorToExternalError(final String intErrorCode) { + return null; + } + + /** + * Add a message into Message-Store. + * + * @param msgCode message-code + * @param msg message + */ + public void addMsg(final String msgCode, final String msg) { + if (!msgStore.containsKey(msgCode)) { + msgStore.put(msgCode, msg); + + } + + } + } diff --git a/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/impl/utils/test/KeyValueUtilsTest.java b/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/impl/utils/test/KeyValueUtilsTest.java new file mode 100644 index 00000000..258c3210 --- /dev/null +++ b/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/impl/utils/test/KeyValueUtilsTest.java @@ -0,0 +1,446 @@ +package at.gv.egiz.eaaf.core.impl.utils.test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import at.gv.egiz.eaaf.core.impl.utils.KeyValueUtils; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.BlockJUnit4ClassRunner; +import com.google.common.collect.Sets; + +@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 java.util.List<String> propList = new ArrayList<>(); + 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(Sets.newHashSet(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 java.util.List<String> propList = new ArrayList<>(); + 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(Sets.newHashSet(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 java.util.List<String> propList = new ArrayList<>(); + + final int result = KeyValueUtils.findNextFreeListCounter(Sets.newHashSet(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<>(); + + 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<>(); + 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<>(); + + 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(final String testPrefix, final int entriesWithPrefix, + final int entriesWithoutPrefix) { + final Map<String, String> result = new HashMap<>(); + 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; + + } + + +} |