diff --git a/src/main/java/com/ljunggren/storm/context/Context.java b/src/main/java/com/ljunggren/storm/context/Context.java index 8f6f16f..208244c 100644 --- a/src/main/java/com/ljunggren/storm/context/Context.java +++ b/src/main/java/com/ljunggren/storm/context/Context.java @@ -2,6 +2,9 @@ import java.sql.Connection; import java.sql.DriverManager; +import java.sql.SQLException; + +import com.ljunggren.storm.exceptions.ContextException; import lombok.AllArgsConstructor; import lombok.Builder; @@ -20,9 +23,15 @@ private String username; private String password; - public Connection getConnection() throws Exception { - Class.forName(driver); - return DriverManager.getConnection(url, username, password); - } + public Connection getConnection() { + try { + Class.forName(driver); + return DriverManager.getConnection(url, username, password); + } catch (ClassNotFoundException e1) { + throw new ContextException(String.format("Counld not load driver %s", driver)); + } catch (SQLException e) { + throw new ContextException(String.format("Counld not connect to %s", url)); + } + } } diff --git a/src/main/java/com/ljunggren/storm/context/ContextFactory.java b/src/main/java/com/ljunggren/storm/context/ContextFactory.java index b94e702..7638d68 100644 --- a/src/main/java/com/ljunggren/storm/context/ContextFactory.java +++ b/src/main/java/com/ljunggren/storm/context/ContextFactory.java @@ -1,29 +1,55 @@ package com.ljunggren.storm.context; import java.io.IOException; -import java.util.ArrayList; +import java.io.InputStream; import java.util.List; import org.apache.commons.io.IOUtils; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.ljunggren.storm.exceptions.ContextException; import com.ljunggren.storm.utils.JSONUtils; public class ContextFactory { + + private String definition = "context.json"; + + public ContextFactory() {} + + public ContextFactory(String definition) { + this.definition = definition; + } public Context getContext(String name) { List contexts = loadContexts(); - return contexts.stream().filter(context -> context.getName().equals(name)) - .findFirst() - .orElse(new Context()); + return contexts.stream().filter(context -> context.getName().equals(name)).findFirst() + .orElseThrow(() -> new ContextException(String.format("Could not find context for %s", name))); } private List loadContexts() { - try { - String json = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("context.json"), "UTF-8"); - return JSONUtils.jsonToListObject(json, Context.class); - } catch (IOException e) { - e.printStackTrace(); - } - return new ArrayList(); + String json = readContextFile(); + return parseContextFile(json); } + + private String readContextFile() { + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(definition); + if (inputStream == null) { + throw new ContextException(String.format("Unable to read %s", definition)); + } + try { + String context = IOUtils.toString(inputStream, "UTF-8"); + return context; + } catch (IOException e) { + throw new ContextException(String.format("Unable to read %s", definition)); + } + } + + private List parseContextFile(String json) { + try { + return JSONUtils.jsonToListObject(json, Context.class); + } catch (JsonProcessingException e) { + throw new ContextException(String.format("Unable to parse context.json", definition)); + } + } + } diff --git a/src/main/java/com/ljunggren/storm/exceptions/ContextException.java b/src/main/java/com/ljunggren/storm/exceptions/ContextException.java new file mode 100644 index 0000000..761e493 --- /dev/null +++ b/src/main/java/com/ljunggren/storm/exceptions/ContextException.java @@ -0,0 +1,19 @@ +package com.ljunggren.storm.exceptions; + +public class ContextException extends RuntimeException { + + private static final long serialVersionUID = 3884552155337373601L; + + public ContextException(String message) { + super(message); + } + + public ContextException(Throwable cause) { + super(cause); + } + + public ContextException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/com/ljunggren/storm/utils/JSONUtils.java b/src/main/java/com/ljunggren/storm/utils/JSONUtils.java index a881fb0..c3e7823 100644 --- a/src/main/java/com/ljunggren/storm/utils/JSONUtils.java +++ b/src/main/java/com/ljunggren/storm/utils/JSONUtils.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class JSONUtils { @@ -46,15 +47,10 @@ } @SuppressWarnings("rawtypes") - public static T jsonToListObject(String json, Class clazz) { + public static T jsonToListObject(String json, Class clazz) throws JsonMappingException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz); - try { - return mapper.readValue(json, type); - } catch (IOException e) { - e.printStackTrace(); - } - return null; + return mapper.readValue(json, type); } } diff --git a/src/test/java/com/ljunggren/storm/context/ContextFactoryTest.java b/src/test/java/com/ljunggren/storm/context/ContextFactoryTest.java index ea883b3..3e914b6 100644 --- a/src/test/java/com/ljunggren/storm/context/ContextFactoryTest.java +++ b/src/test/java/com/ljunggren/storm/context/ContextFactoryTest.java @@ -2,22 +2,30 @@ import static org.junit.Assert.assertEquals; +import org.junit.Ignore; import org.junit.Test; +import com.ljunggren.storm.exceptions.ContextException; + public class ContextFactoryTest { + @Ignore @Test - public void getTest() { + public void getContextTest() { Context context = new ContextFactory().getContext("H2"); assertEquals("H2", context.getName()); assertEquals("org.h2.Driver", context.getDriver()); } - @Test - public void getNoContextTest() { - Context context = new ContextFactory().getContext("DNE"); - assertEquals(null, context.getName()); - assertEquals(null, context.getDriver()); + @Ignore + @Test(expected = ContextException.class) + public void getNoContextByNameTest() { + new ContextFactory().getContext("DNE"); + } + + @Test(expected = ContextException.class) + public void getContextNoContextFile() { + new ContextFactory("noContext.json").getContext("H2"); } } diff --git a/src/test/java/com/ljunggren/storm/context/ContextTest.java b/src/test/java/com/ljunggren/storm/context/ContextTest.java new file mode 100644 index 0000000..597cb29 --- /dev/null +++ b/src/test/java/com/ljunggren/storm/context/ContextTest.java @@ -0,0 +1,43 @@ +package com.ljunggren.storm.context; + +import static org.junit.Assert.*; + +import java.sql.Connection; +import java.sql.SQLException; + +import org.junit.Test; + +import com.ljunggren.storm.exceptions.ContextException; + +public class ContextTest { + + @Test + public void getConnectionTest() throws SQLException { + Context context = Context.builder() + .driver("org.h2.Driver") + .url("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1") + .build(); + Connection connection = context.getConnection(); + assertTrue(connection != null); + connection.close(); + } + + @Test(expected = ContextException.class) + public void getConnectionInvalidDriver() { + Context context = Context.builder() + .driver("not.real.Driver") + .url("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1") + .build(); + context.getConnection(); + } + + @Test(expected = ContextException.class) + public void getConnectionInvalidURL() { + Context context = Context.builder() + .driver("org.h2.Driver") + .url("cbdj:h2:mem:test;DB_CLOSE_DELAY=-1") + .build(); + context.getConnection(); + } + +} diff --git a/src/test/java/com/ljunggren/storm/utils/JSONUtilsTest.java b/src/test/java/com/ljunggren/storm/utils/JSONUtilsTest.java index 290a3b6..02dc19a 100644 --- a/src/test/java/com/ljunggren/storm/utils/JSONUtilsTest.java +++ b/src/test/java/com/ljunggren/storm/utils/JSONUtilsTest.java @@ -12,6 +12,7 @@ import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.ljunggren.storm.TestUser; @@ -67,7 +68,7 @@ } @Test - public void jsonToListObjectTest() { + public void jsonToListObjectTest() throws JsonMappingException, JsonProcessingException { List expected = Arrays.asList(new String[] {"Alex", "Christie", "Gage"}); String json = JSONUtils.objectToJson(expected); List actual = JSONUtils.jsonToListObject(json, String.class);