#39 Fixed tests and added some more
This commit is contained in:
3
config/notconfig.properties
Normal file
3
config/notconfig.properties
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Amazon Credentials
|
||||||
|
AMAZON_API_URL=https://checksch.de/api/amazon
|
||||||
|
AMAZON_API_KEY=lassMichRein
|
||||||
@@ -33,6 +33,7 @@ public final class Main {
|
|||||||
*/
|
*/
|
||||||
public static void main(final String[] args) throws ConfigurationException {
|
public static void main(final String[] args) throws ConfigurationException {
|
||||||
Config config = Config.getInstance();
|
Config config = Config.getInstance();
|
||||||
|
config.loadConfig();
|
||||||
String amznBaseUrl = config.getProperty("AMAZON_API_URL");
|
String amznBaseUrl = config.getProperty("AMAZON_API_URL");
|
||||||
String amznApiKey = config.getProperty("AMAZON_API_KEY");
|
String amznApiKey = config.getProperty("AMAZON_API_KEY");
|
||||||
String testProduct = null;
|
String testProduct = null;
|
||||||
|
|||||||
@@ -16,7 +16,22 @@ public final class Config {
|
|||||||
/**
|
/**
|
||||||
* Config file location.
|
* 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.
|
* Holds the config values.
|
||||||
*/
|
*/
|
||||||
@@ -27,10 +42,10 @@ public final class Config {
|
|||||||
private static Config instance = null;
|
private static Config instance = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private constructor to prevent unwanted instantiation.
|
* Private constructor to prevent external instantiation.
|
||||||
*/
|
*/
|
||||||
private Config() throws ConfigurationException {
|
private Config() {
|
||||||
loadConfig();
|
// Do Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,7 +54,7 @@ public final class Config {
|
|||||||
* @return Config instance
|
* @return Config instance
|
||||||
* @throws ConfigurationException
|
* @throws ConfigurationException
|
||||||
*/
|
*/
|
||||||
public static Config getInstance() throws ConfigurationException {
|
public static Config getInstance() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
return new Config();
|
return new Config();
|
||||||
}
|
}
|
||||||
@@ -54,7 +69,7 @@ public final class Config {
|
|||||||
*/
|
*/
|
||||||
public void loadConfig() throws ConfigurationException {
|
public void loadConfig() throws ConfigurationException {
|
||||||
Properties newProps = new Properties();
|
Properties newProps = new Properties();
|
||||||
try (FileInputStream input = new FileInputStream(CONFIG_LOCATION)) {
|
try (FileInputStream input = new FileInputStream(configLocation)) {
|
||||||
newProps.load(input);
|
newProps.load(input);
|
||||||
properties = newProps;
|
properties = newProps;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -70,10 +85,18 @@ public final class Config {
|
|||||||
* @throws NoSuchElementException Required key missing
|
* @throws NoSuchElementException Required key missing
|
||||||
*/
|
*/
|
||||||
public String getProperty(final String key) throws NoSuchElementException {
|
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) {
|
if (value == null) {
|
||||||
throw new NoSuchElementException("Requested config value does not exist");
|
throw new NoSuchElementException("Requested config value does not exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
95
src/test/java/de/rwu/easydrop/util/ConfigImplTest.java
Normal file
95
src/test/java/de/rwu/easydrop/util/ConfigImplTest.java
Normal file
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,24 +2,15 @@ package de.rwu.easydrop.util;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
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 static org.mockito.Mockito.spy;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
|
|
||||||
import javax.naming.ConfigurationException;
|
import javax.naming.ConfigurationException;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
class ConfigTest {
|
class ConfigTest {
|
||||||
@Mock
|
|
||||||
private FileInputStream mockFileInputStream;
|
|
||||||
|
|
||||||
private Config config;
|
private Config config;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
@@ -28,59 +19,24 @@ class ConfigTest {
|
|||||||
config = spy(Config.getInstance());
|
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
|
@Test
|
||||||
void testGetInstanceNull() {
|
void testGetInstanceNull() {
|
||||||
config = null;
|
config = null;
|
||||||
|
|
||||||
try {
|
Config newConfig = Config.getInstance();
|
||||||
Config newConfig = Config.getInstance();
|
assertNotNull(newConfig);
|
||||||
// Check if the returned instance is not null
|
|
||||||
assertNotNull(newConfig);
|
|
||||||
} catch (ConfigurationException e) {
|
|
||||||
fail("ConfigurationException should not be thrown.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGetInstanceNotNull() {
|
void testGetInstanceNotNull() {
|
||||||
try {
|
Config newConfig = Config.getInstance();
|
||||||
Config newConfig = Config.getInstance();
|
assertNotNull(newConfig);
|
||||||
// Check if the returned instance is not null
|
|
||||||
assertNotNull(newConfig);
|
|
||||||
} catch (ConfigurationException e) {
|
|
||||||
fail("ConfigurationException should not be thrown.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testLoadConfigSuccessfully() {
|
void testSetConfigLocation() {
|
||||||
try {
|
String newPath = "new/location/config.properties";
|
||||||
config.loadConfig();
|
config.setConfigLocation(newPath);
|
||||||
config.setProperty("WHATEVER", "SUCCESS");
|
assertEquals(newPath, config.getConfigLocation());
|
||||||
assertNotNull(config.getProperty("WHATEVER"));
|
|
||||||
} catch (ConfigurationException e) {
|
|
||||||
fail("ConfigurationException should not be thrown");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
0
testResources/empty.properties
Normal file
0
testResources/empty.properties
Normal file
1
testResources/testdata.properties
Normal file
1
testResources/testdata.properties
Normal file
@@ -0,0 +1 @@
|
|||||||
|
API_KEY=keyIsHere
|
||||||
Reference in New Issue
Block a user