Initial Commit

This commit is contained in:
Isaac Parenteau
2019-07-20 12:39:03 -05:00
commit 79529ecc40
66 changed files with 6549 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
package net.locusworks.test;
import org.junit.Assert;
import org.junit.Test;
import net.locusworks.common.crypto.AES;
import net.locusworks.common.utils.Utils;
public class AESEncryptionTest {
@Test
public void testEncryption() {
try {
String encrypted = AES.createInstance().encrypt("hello world");
Assert.assertTrue(String.format("Encrypted String is not blank? :%s", encrypted), !Utils.isEmptyString(encrypted));
} catch (Exception ex) {
ex.printStackTrace(System.err);
Assert.fail();
}
}
@Test
public void testDecryption() {
String testString ="hello world";
try {
AES aes = AES.createInstance();
String encrypted = aes.encrypt(testString);
Assert.assertTrue(String.format("Encrypted String is not blank? :%s", encrypted), !Utils.isEmptyString(encrypted));
String decrypted = aes.decrypt(encrypted);
Assert.assertTrue(String.format("Decrypted String is not blank? :%s", decrypted), !Utils.isEmptyString(encrypted));
Assert.assertTrue("Test String and Original String the same? :%s", testString.equals(decrypted));
} catch (Exception ex) {
ex.printStackTrace(System.err);
Assert.fail();
}
}
}

View File

@@ -0,0 +1,11 @@
package net.locusworks.test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ AESEncryptionTest.class, FileReaderTest.class, HashSaltTest.class, RandomStringTest.class })
public class AllTests {
}

View File

@@ -0,0 +1,92 @@
package net.locusworks.test;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import net.locusworks.common.interfaces.AutoCloseableIterator;
import net.locusworks.common.utils.FileReader;
import net.locusworks.common.utils.FileReader.LineInfo;
import net.locusworks.common.utils.RandomString;
public class FileReaderTest {
private static final String TEST_FILE = "test_file.txt";
private static Map<Integer, Integer> numLines = new LinkedHashMap<>();
@BeforeClass
public static void setUpBeforeClass() throws Exception {
File testFile = new File(TEST_FILE);
FileOutputStream fos = new FileOutputStream(testFile);
Integer count = ThreadLocalRandom.current().nextInt(100);
for (int i = 1; i <= count; i++) {
String randomString = RandomString.getString(ThreadLocalRandom.current().nextInt(5, 100)) + "\n";
numLines.put(i, randomString.length());
fos.write(randomString.getBytes());
}
fos.close();
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
File file = new File(TEST_FILE);
file.delete();
}
@Test
public void testForLoop() {
Integer lineCount = 0;
for(LineInfo s : new FileReader(new File(TEST_FILE))) {
lineCount++;
Integer lineNumber = s.getLineNumber();
Integer lineLength = s.getLine().length();
Integer mapLineLength = numLines.get(lineNumber);
assertTrue(lineLength == (mapLineLength-1));
}
assertTrue(lineCount == numLines.size());
}
@Test
public void testIterator() {
Integer lineCount = 0;
try(AutoCloseableIterator<LineInfo> iter = new FileReader(new File(TEST_FILE))) {
while(iter.hasNext()) {
lineCount++;
LineInfo s = iter.next();
Integer lineNumber = s.getLineNumber();
Integer lineLength = s.getLine().length();
Integer mapLineLength = numLines.get(lineNumber);
assertTrue(lineLength == (mapLineLength-1));
}
}
assertTrue(lineCount == numLines.size());
}
@Test
public void testForIterator() {
Integer lineCount = 0;
for(Iterator<LineInfo> iter = new FileReader(new File(TEST_FILE)); iter.hasNext();) {
lineCount++;
LineInfo s = iter.next();
Integer lineNumber = s.getLineNumber();
Integer lineLength = s.getLine().length();
Integer mapLineLength = numLines.get(lineNumber);
assertTrue(lineLength == (mapLineLength-1));
}
assertTrue(lineCount == numLines.size());
}
}

View File

@@ -0,0 +1,34 @@
package net.locusworks.test;
import org.junit.Assert;
import org.junit.Test;
import net.locusworks.common.crypto.HashSalt;
import net.locusworks.common.utils.Utils;
public class HashSaltTest {
private static String samplePassword="Hello World";
@Test
public void testEncryption() {
try {
String hashSalt = HashSalt.createHash(samplePassword);
Assert.assertTrue(String.format("Encrypted String is not blank? :%s", hashSalt), !Utils.isEmptyString(hashSalt));
} catch(Exception ex) {
Assert.fail();
}
}
@Test
public void testDecryption() {
try {
String hashSalt = HashSalt.createHash(samplePassword);
boolean decrypted = HashSalt.validatePassword(samplePassword, hashSalt);
Assert.assertTrue("Test String and Original String the same? :%s", decrypted);
} catch(Exception ex) {
Assert.fail();
}
}
}

View File

@@ -0,0 +1,38 @@
package net.locusworks.test;
import static org.junit.Assert.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;
import net.locusworks.common.utils.HashUtils;
public class HashUtilsTest {
private static final String TEST_STRING = "Hello World";
@Test
public void testMD5() throws Exception {
String digestUtilsMD5 = DigestUtils.md5Hex(TEST_STRING.getBytes());
String hashUtilsMD5 = HashUtils.hash("MD5", TEST_STRING);
assertTrue(digestUtilsMD5.equals(hashUtilsMD5));
}
@Test
public void testSHA1() throws Exception {
String digestUtilsMD5 = DigestUtils.sha1Hex(TEST_STRING.getBytes());
String hashUtilsMD5 = HashUtils.hash("SHA-1", TEST_STRING);
assertTrue(digestUtilsMD5.equals(hashUtilsMD5));
}
@Test
public void testSHA512() throws Exception {
String digestUtilsMD5 = DigestUtils.sha512Hex(TEST_STRING.getBytes());
String hashUtilsMD5 = HashUtils.hash("SHA-512", TEST_STRING);
assertTrue(digestUtilsMD5.equals(hashUtilsMD5));
}
}

View File

@@ -0,0 +1,45 @@
package net.locusworks.test;
import static org.junit.Assert.*;
import org.junit.Test;
import net.locusworks.common.immutables.Pair;
import net.locusworks.common.immutables.Triplet;
import net.locusworks.common.immutables.Unit;
public class ImmutablesTest {
@Test
public void testUnit() {
Unit<String> unit = new Unit<>("Hello World");
assertTrue(unit.getValue1().equals("Hello World"));
Unit<Integer> unit2 = new Unit<>(2);
assertTrue(unit2.getValue1().equals(2));
}
@Test
public void testPair() {
Pair<String, String> pair1 = new Pair<>("Hello", "World");
assertTrue(pair1.getValue1().equals("Hello"));
assertTrue(pair1.getValue2().equals("World"));
Pair<String, Integer> pair2 = new Pair<>("Foo", 25);
assertTrue(pair2.getValue1().equals("Foo"));
assertTrue(pair2.getValue2().equals(25));
Pair<Integer, Integer> pair3 = new Pair<>(1, 23);
assertTrue(pair3.getValue1().equals(1));
assertTrue(pair3.getValue2().equals(23));
}
@Test
public void testTriplet() {
Triplet<String, Integer, String> triplet1 = new Triplet<>("Hello", 24, "World");
assertTrue(triplet1.getValue1().equals("Hello"));
assertTrue(triplet1.getValue2().equals(24));
assertTrue(triplet1.getValue3().equals("World"));
}
}

View File

@@ -0,0 +1,73 @@
/**
*
*/
package net.locusworks.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import net.locusworks.common.immutables.Triplet;
import net.locusworks.common.objectmapper.ObjectMapperHelper;
import net.locusworks.common.utils.Utils;
import static net.locusworks.common.utils.Constants.JUNIT_TEST_CHECK;
import static net.locusworks.common.utils.Constants.LOG4J_CONFIG_PROPERTY;
/**
* Test cases to test ObjectMapperHelper.class
* @author Isaac Parenteau
* @since 1.0.0-RELEASE
*
*/
public class ObjectMapperHelperTest {
private static Triplet<String, Integer, String> test;
/**
* @throws java.lang.Exception exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.setProperty(LOG4J_CONFIG_PROPERTY, "log4j2-test.xml");
System.setProperty(JUNIT_TEST_CHECK, "true");
test = new Triplet<String, Integer, String>("Hello", 24, "World");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.clearProperty(LOG4J_CONFIG_PROPERTY);
System.clearProperty(JUNIT_TEST_CHECK);
}
@Test
public void testWrite() {
String value = ObjectMapperHelper.writeValue(test).getResults();
assertTrue(value != null && !value.trim().isEmpty());
}
@Test
public void testRead() {
String value = ObjectMapperHelper.writeValue(test).getResults();
assertTrue(value != null && !value.trim().isEmpty());
Triplet<?, ?, ?> tmp = ObjectMapperHelper.readValue(value, Triplet.class).getResults();
assertTrue(tmp != null);
assertTrue(tmp.equals(test));
}
@SuppressWarnings("rawtypes")
@Test
public void testListWriteRead() {
List<Triplet<String, Integer, String>> htrList = Utils.toList(test);
String value = ObjectMapperHelper.writeValue(htrList).getResults();
assertTrue(value != null && !value.trim().isEmpty());
List<Triplet> tmpList = ObjectMapperHelper.readListValue(value, Triplet.class).getResults();
assertTrue(tmpList != null && tmpList.size() > 0);
}
}

View File

@@ -0,0 +1,122 @@
package net.locusworks.test;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import org.junit.AfterClass;
import org.junit.Test;
import net.locusworks.common.configuration.PropertiesManager;
/**
* Test cases for the properties manager class
* @author Isaac Parenteau
* @since 1.0.0-RELEASE
*
*/
public class PropertiesManagerTest {
private static final String PROPERTIES_FILE = "test.properties";
private static final String TMP_PROPS = "temp.properties";
private static final int ENTRY_SIZE = 4;
public static enum Configuration {
DB_HOST("dbHost"),
DB_PORT("dbPort"),
USER_EXPIRATION_DAYS("userExpirationDays"),
LOG_LEVEL("logLevel");
private String value;
private Configuration(String value) {
this.value = value;
}
/**
* Get the current value of the enumeration
* @return value
*/
public String getValue() {
return this.value;
}
@Override
public String toString() {
return this.value;
}
}
@AfterClass
public static void removeSavedProps() {
File tmp = new File(TMP_PROPS);
if (tmp.exists()) {
tmp.delete();
}
}
@Test
public void testPropertiesLoad() {
try {
Properties props = PropertiesManager.loadConfiguration(this.getClass(), PROPERTIES_FILE);
assertTrue(props != null);
assertTrue(props.containsKey(Configuration.USER_EXPIRATION_DAYS.toString()));
assertTrue(props.containsKey(Configuration.DB_HOST.toString()));
assertTrue(props.containsKey(Configuration.DB_PORT.toString()));
assertTrue(props.containsKey(Configuration.LOG_LEVEL.toString()));
} catch (IOException e) {
fail(e.getMessage());
}
}
@Test
public void testAddConfiguration() {
try {
Properties props = PropertiesManager.loadConfiguration(this.getClass(), PROPERTIES_FILE);
Properties tmp = new Properties();
assertTrue(tmp.keySet().size() == 0);
PropertiesManager.addConfiguration(tmp, props);
assertTrue(tmp.keySet().size() == ENTRY_SIZE);
assertTrue(tmp.containsKey(Configuration.USER_EXPIRATION_DAYS.toString()));
assertTrue(tmp.containsKey(Configuration.DB_HOST.toString()));
assertTrue(tmp.containsKey(Configuration.DB_PORT.toString()));
assertTrue(tmp.containsKey(Configuration.LOG_LEVEL.toString()));
} catch (IOException e) {
fail(e.getMessage());
}
}
@Test
public void testRemoveConfiguration() {
try {
Properties props = PropertiesManager.loadConfiguration(this.getClass(), PROPERTIES_FILE);
Properties tmp = new Properties();
assertTrue(props.keySet().size() == ENTRY_SIZE);
assertTrue(tmp.keySet().size() == 0);
PropertiesManager.removeConfiguration(props, tmp);
assertTrue(props.keySet().size() == 0);
assertTrue(tmp.keySet().size() == 0);
} catch (IOException e) {
fail(e.getMessage());
}
}
@Test
public void testSaveConfiguration() {
try {
Properties props = PropertiesManager.loadConfiguration(this.getClass(), PROPERTIES_FILE);
File tmpFile = new File(TMP_PROPS);
PropertiesManager.saveConfiguration(props, tmpFile, "test propertis");
Properties tmp = PropertiesManager.loadConfiguration(tmpFile);
assertTrue(tmp.keySet().size() == ENTRY_SIZE);
assertTrue(tmp.containsKey(Configuration.USER_EXPIRATION_DAYS.toString()));
assertTrue(tmp.containsKey(Configuration.DB_HOST.toString()));
assertTrue(tmp.containsKey(Configuration.DB_PORT.toString()));
assertTrue(tmp.containsKey(Configuration.LOG_LEVEL.toString()));
} catch (IOException e) {
fail(e.getMessage());
}
}
}

View File

@@ -0,0 +1,26 @@
package net.locusworks.test;
import static org.junit.Assert.*;
import org.junit.Test;
import net.locusworks.common.utils.RandomString;
public class RandomStringTest {
@Test
public void testStaticBytes() {
for (Integer length = 3; length < 50; length++) {
assertTrue(RandomString.getBytes(length).length == length);
}
}
@Test
public void testStaticString() {
for (Integer length = 3; length < 50; length++) {
String random = RandomString.getString(length);
assertTrue(random.length() == length);
}
}
}

View File

@@ -0,0 +1,39 @@
package net.locusworks.test;
import static org.junit.Assert.*;
import org.junit.Test;
import net.locusworks.common.utils.Utils;
/**
* Test cases for the Utils class
* @author Isaac Parenteau
* @since 1.0.0-RELEASE
*
*/
public class UtilsTest {
@Test
public void testSafeString() {
assertNotNull(Utils.safeString(null));
assertTrue(Utils.safeString(null).isEmpty());
assertFalse(Utils.safeString("hello world").isEmpty());
}
@Test
public void testEmptyString() {
assertTrue(Utils.isEmptyString(null));
assertTrue(Utils.isEmptyString(""));
assertTrue(Utils.isEmptyString(" "));
assertFalse(Utils.isEmptyString("foo"));
assertFalse(Utils.isEmptyString(" bar "));
}
@Test
public void testToInteger() {
assertTrue(Utils.toInteger("Hello word", 2) == 2);
assertTrue(Utils.toInteger("23", 5023) == 23);
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5p] %m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="TRACE">
<AppenderRef ref="ConsoleAppender" level="INFO"/>
</Root>
</Loggers>
</Configuration>

View File

@@ -0,0 +1,4 @@
userExpirationDays=3650
dbHost=localhost
dbPort=3306
logLevel=INFO