From ffc804aba10ea757a754f9d8399b3f98c0a1056b Mon Sep 17 00:00:00 2001 From: Marvin Scham Date: Wed, 24 May 2023 01:04:13 +0200 Subject: [PATCH] #39 Fixed tests and added some more --- config/notconfig.properties | 3 + src/main/java/de/rwu/easydrop/Main.java | 1 + .../java/de/rwu/easydrop/util/Config.java | 37 ++++++-- .../de/rwu/easydrop/util/ConfigImplTest.java | 95 +++++++++++++++++++ .../java/de/rwu/easydrop/util/ConfigTest.java | 60 ++---------- testResources/empty.properties | 0 testResources/testdata.properties | 1 + 7 files changed, 138 insertions(+), 59 deletions(-) create mode 100644 config/notconfig.properties create mode 100644 src/test/java/de/rwu/easydrop/util/ConfigImplTest.java create mode 100644 testResources/empty.properties create mode 100644 testResources/testdata.properties diff --git a/config/notconfig.properties b/config/notconfig.properties new file mode 100644 index 0000000..1f5ee7e --- /dev/null +++ b/config/notconfig.properties @@ -0,0 +1,3 @@ +# Amazon Credentials +AMAZON_API_URL=https://checksch.de/api/amazon +AMAZON_API_KEY=lassMichRein \ No newline at end of file diff --git a/src/main/java/de/rwu/easydrop/Main.java b/src/main/java/de/rwu/easydrop/Main.java index 45299f2..af1aeea 100644 --- a/src/main/java/de/rwu/easydrop/Main.java +++ b/src/main/java/de/rwu/easydrop/Main.java @@ -33,6 +33,7 @@ public final class Main { */ public static void main(final String[] args) throws ConfigurationException { Config config = Config.getInstance(); + config.loadConfig(); String amznBaseUrl = config.getProperty("AMAZON_API_URL"); String amznApiKey = config.getProperty("AMAZON_API_KEY"); String testProduct = null; diff --git a/src/main/java/de/rwu/easydrop/util/Config.java b/src/main/java/de/rwu/easydrop/util/Config.java index 6b0e89d..7a7f1e3 100644 --- a/src/main/java/de/rwu/easydrop/util/Config.java +++ b/src/main/java/de/rwu/easydrop/util/Config.java @@ -16,7 +16,22 @@ public final class Config { /** * Config file location. */ - private static final String CONFIG_LOCATION = "config/config.properties"; + private String configLocation = "config/config.properties"; + + /** + * @return the configLocation + */ + public String getConfigLocation() { + return configLocation; + } + + /** + * @param newConfigLocation the configLocation to set + */ + public void setConfigLocation(final String newConfigLocation) { + configLocation = newConfigLocation; + } + /** * Holds the config values. */ @@ -27,10 +42,10 @@ public final class Config { private static Config instance = null; /** - * Private constructor to prevent unwanted instantiation. + * Private constructor to prevent external instantiation. */ - private Config() throws ConfigurationException { - loadConfig(); + private Config() { + // Do Nothing } /** @@ -39,7 +54,7 @@ public final class Config { * @return Config instance * @throws ConfigurationException */ - public static Config getInstance() throws ConfigurationException { + public static Config getInstance() { if (instance == null) { return new Config(); } @@ -54,7 +69,7 @@ public final class Config { */ public void loadConfig() throws ConfigurationException { Properties newProps = new Properties(); - try (FileInputStream input = new FileInputStream(CONFIG_LOCATION)) { + try (FileInputStream input = new FileInputStream(configLocation)) { newProps.load(input); properties = newProps; } catch (IOException e) { @@ -70,10 +85,18 @@ public final class Config { * @throws NoSuchElementException Required key missing */ public String getProperty(final String key) throws NoSuchElementException { - String value = properties.getProperty(key); + String value = null; + + try { + value = properties.getProperty(key); + } catch (NullPointerException e) { + throw new NoSuchElementException("Config has not been loaded"); + } + if (value == null) { throw new NoSuchElementException("Requested config value does not exist"); } + return value; } diff --git a/src/test/java/de/rwu/easydrop/util/ConfigImplTest.java b/src/test/java/de/rwu/easydrop/util/ConfigImplTest.java new file mode 100644 index 0000000..9dbc58d --- /dev/null +++ b/src/test/java/de/rwu/easydrop/util/ConfigImplTest.java @@ -0,0 +1,95 @@ +package de.rwu.easydrop.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; + +import java.util.NoSuchElementException; + +import javax.naming.ConfigurationException; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ConfigImplTest { + private Config config; + private final static String TESTDATA_PATH = "testResources/testdata.properties"; + private final static String TESTDATA_KEY = "API_KEY"; + private final static String TESTDATA_VAL = "keyIsHere"; + + @BeforeEach + void setUp() { + config = Config.getInstance(); + } + + @Test + void testGetProperty_ExistingKey() throws ConfigurationException { + config.setConfigLocation(TESTDATA_PATH); + config.loadConfig(); + config.setProperty(TESTDATA_KEY, TESTDATA_VAL); + + String value = config.getProperty(TESTDATA_KEY); + + assertEquals(TESTDATA_VAL, value); + } + + @Test + void testGetProperty_ConfigNotLoaded() { + NoSuchElementException exception = assertThrows(NoSuchElementException.class, () -> { + config.getProperty(TESTDATA_KEY); + }); + + assertEquals("Config has not been loaded", exception.getMessage()); + } + + @Test + void testGetProperty_NonExistingKey() { + try { + config.setConfigLocation(TESTDATA_PATH); + config.loadConfig(); + + NoSuchElementException exception = assertThrows(NoSuchElementException.class, () -> { + config.getProperty("I_DONT_EXIST"); + }); + + assertEquals("Requested config value does not exist", exception.getMessage()); + } catch (ConfigurationException e) { + fail("ConfigurationException should not be thrown"); + } + } + + @Test + void testSetProperty() { + try { + config.setConfigLocation(TESTDATA_PATH); + config.loadConfig(); + config.setProperty(TESTDATA_KEY, "12345"); + + assertEquals("12345", config.getProperty(TESTDATA_KEY)); + } catch (ConfigurationException e) { + fail("ConfigurationException should not be thrown"); + } + } + + @Test + void testLoadConfigSuccessfully() { + try { + config.setConfigLocation("testResources/testdata.properties"); + config.loadConfig(); + assertEquals(TESTDATA_VAL, config.getProperty(TESTDATA_KEY)); + } catch (ConfigurationException e) { + fail("ConfigurationException should not be thrown"); + } + } + + @Test + void testLoadConfigMissingFile() { + config.setConfigLocation("path/that/doesnt/exist/config.properties"); + + ConfigurationException exception = assertThrows(ConfigurationException.class, () -> { + config.loadConfig(); + }); + + assertEquals("Couldn't load required config file", exception.getMessage()); + } +} diff --git a/src/test/java/de/rwu/easydrop/util/ConfigTest.java b/src/test/java/de/rwu/easydrop/util/ConfigTest.java index 9aa2d09..8cf0c2a 100644 --- a/src/test/java/de/rwu/easydrop/util/ConfigTest.java +++ b/src/test/java/de/rwu/easydrop/util/ConfigTest.java @@ -2,24 +2,15 @@ package de.rwu.easydrop.util; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.spy; -import java.io.FileInputStream; -import java.util.NoSuchElementException; - import javax.naming.ConfigurationException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mock; import org.mockito.MockitoAnnotations; class ConfigTest { - @Mock - private FileInputStream mockFileInputStream; - private Config config; @BeforeEach @@ -28,59 +19,24 @@ class ConfigTest { config = spy(Config.getInstance()); } - @Test - void testGetProperty_ExistingKey() throws NoSuchElementException { - config.setProperty("API_KEY", "12345"); - - String value = config.getProperty("API_KEY"); - - assertEquals("12345", value); - } - - @Test - void testGetProperty_NonExistingKey() { - assertThrows(NoSuchElementException.class, () -> config.getProperty("NON_EXISTING_KEY")); - } - - @Test - void testSetProperty() { - config.setProperty("API_KEY", "12345"); - - assertEquals("12345", config.getProperty("API_KEY")); - } - @Test void testGetInstanceNull() { config = null; - try { - Config newConfig = Config.getInstance(); - // Check if the returned instance is not null - assertNotNull(newConfig); - } catch (ConfigurationException e) { - fail("ConfigurationException should not be thrown."); - } + Config newConfig = Config.getInstance(); + assertNotNull(newConfig); } @Test void testGetInstanceNotNull() { - try { - Config newConfig = Config.getInstance(); - // Check if the returned instance is not null - assertNotNull(newConfig); - } catch (ConfigurationException e) { - fail("ConfigurationException should not be thrown."); - } + Config newConfig = Config.getInstance(); + assertNotNull(newConfig); } @Test - void testLoadConfigSuccessfully() { - try { - config.loadConfig(); - config.setProperty("WHATEVER", "SUCCESS"); - assertNotNull(config.getProperty("WHATEVER")); - } catch (ConfigurationException e) { - fail("ConfigurationException should not be thrown"); - } + void testSetConfigLocation() { + String newPath = "new/location/config.properties"; + config.setConfigLocation(newPath); + assertEquals(newPath, config.getConfigLocation()); } } diff --git a/testResources/empty.properties b/testResources/empty.properties new file mode 100644 index 0000000..e69de29 diff --git a/testResources/testdata.properties b/testResources/testdata.properties new file mode 100644 index 0000000..cbf5875 --- /dev/null +++ b/testResources/testdata.properties @@ -0,0 +1 @@ +API_KEY=keyIsHere \ No newline at end of file