#39 Fixed tests and added some more

This commit is contained in:
Marvin Scham
2023-05-24 01:04:13 +02:00
parent da1cad1a59
commit ffc804aba1
7 changed files with 138 additions and 59 deletions

View File

@@ -0,0 +1,3 @@
# Amazon Credentials
AMAZON_API_URL=https://checksch.de/api/amazon
AMAZON_API_KEY=lassMichRein

View File

@@ -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;

View File

@@ -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;
}

View 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());
}
}

View File

@@ -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.");
}
}
@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.");
}
}
@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());
}
}

View File

View File

@@ -0,0 +1 @@
API_KEY=keyIsHere