diff --git a/client/pom.xml b/client/pom.xml index 45f2677..d48d069 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -23,6 +23,12 @@ 0.0.1-SNAPSHOT + + com.fasterxml.jackson.core + jackson-databind + 2.20.1 + + org.glassfish.jersey.core jersey-client diff --git a/client/src/main/java/client/utils/Config.java b/client/src/main/java/client/utils/Config.java new file mode 100644 index 0000000..c94dd4c --- /dev/null +++ b/client/src/main/java/client/utils/Config.java @@ -0,0 +1,48 @@ +package client.utils; + +import java.util.ArrayList; +import java.util.List; + +public class Config { + + + private String language = "en"; + private String serverUrl = "http://localhost:8080"; + + private List favourites = new ArrayList<>(); + private List shoppingList = new ArrayList<>(); + + public Config(){} + + public String getLanguage() { + return language; + } + + public List getShoppingList() { + return shoppingList; + } + + public List getFavourites() { + return favourites; + } + + public String getServerUrl() { + return serverUrl; + } + + public void setLanguage(String language) { + this.language = language; + } + + public void setServerUrl(String serverUrl) { + this.serverUrl = serverUrl; + } + + public void setFavourites(List favourites) { + this.favourites = favourites; + } + + public void setShoppingList(List shoppingList) { + this.shoppingList = shoppingList; + } +} diff --git a/client/src/main/java/client/utils/ConfigService.java b/client/src/main/java/client/utils/ConfigService.java new file mode 100644 index 0000000..d1d70c6 --- /dev/null +++ b/client/src/main/java/client/utils/ConfigService.java @@ -0,0 +1,80 @@ +package client.utils; + + + +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.nio.file.Path; +import java.io.File; +import java.io.IOException; + +public class ConfigService { + private final Path configPath; + private final ObjectMapper mapper = new ObjectMapper(); + private Config config; + + + /* + Constructor that takes the path to the config file as it's parameter + + */ + public ConfigService(Path configPath){ + this.configPath = configPath; + load(); + } + + /* + Load needs to be called to load any changes from the config file + to the config attribute of the configService class. + Takes no parameters + */ + private void load(){ + File file = configPath.toFile(); //reads the config file as file + if (!file.exists()) { //if file doesn't exist then it creates a config object + config = new Config(); + return; + } + + try{ + config = mapper.readValue(file, Config.class); //uses Jackson to map the file to the config attribute + + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + public Path getConfigPath() { + return configPath; + } + + public ObjectMapper getMapper() { + return mapper; + } + + public Config getConfig() { + return config; + } + + public void setConfig(Config config) { + this.config = config; + } + + + /* + The save method saves any changes done to the file + from the config object + */ + public void save(){ + + try { + File file = configPath.toFile(); // file is the config file here + mapper.writeValue(file, config); // here we edit the value of the file using config + + } + catch (Exception e){ + throw new RuntimeException(e); + } + } + +} diff --git a/client/src/test/java/client/scenes/ConfigServiceTest.java b/client/src/test/java/client/scenes/ConfigServiceTest.java new file mode 100644 index 0000000..8af3d78 --- /dev/null +++ b/client/src/test/java/client/scenes/ConfigServiceTest.java @@ -0,0 +1,96 @@ +package client.scenes; + +import client.utils.Config; +import client.utils.ConfigService; +import client.utils.ServerUtils; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class ConfigServiceTest { + static ServerUtils dv = new ServerUtils(); + + + private static final long TEST_ID_A = 23412L; + private static final long TEST_ID_B = 25412L; + + @BeforeEach + public void setup(){ + Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available"); + } + + + /* + Tests if the config file loads properly with a prewritten json file + */ + @Test + public void configServiceFileLoadTest(@TempDir Path tempDir) throws IOException { + Path configPath = tempDir.resolve("config.json"); + + String json = """ + { + "language": "de", + "serverUrl": "http://exmple12.com", + "favourites": ["banana bread", "pineapple pie"], + "shoppingList": ["milk", "butter"] + } + """; + Files.writeString(configPath, json); //writes into path + ConfigService configService = new ConfigService(configPath);//initiates configservice + + Config config = configService.getConfig(); //checks + + assertEquals("de", config.getLanguage()); + assertEquals("http://exmple12.com", config.getServerUrl()); + + List x = new ArrayList<>(); + + x.add(TEST_ID_A); + x.add(TEST_ID_B); + + List y = new ArrayList<>(); + y.add("milk"); + y.add("butter"); + + assertEquals(x , config.getFavourites()); + assertEquals(y , config.getShoppingList()); + } + + + /* + Tests if the save method saves changes to the config file. + */ + @Test + public void configSaveTest(@TempDir Path tempDir) throws IOException { + Path configPath = tempDir.resolve("config.json"); + ConfigService configService = new ConfigService(configPath); + + Config config = configService.getConfig(); + + + config.setLanguage("fr"); + config.setServerUrl("www.domain1.com"); + + configService.save(); + + String jsonTest = Files.readString(configPath); + + assertTrue(jsonTest.contains("\"language\":\"fr\"")); + assertTrue(jsonTest.contains("\"serverUrl\":\"www.domain1.com\"")); + + + } + + + +} \ No newline at end of file diff --git a/client/src/test/java/client/scenes/ConfigTest.java b/client/src/test/java/client/scenes/ConfigTest.java new file mode 100644 index 0000000..9c82d34 --- /dev/null +++ b/client/src/test/java/client/scenes/ConfigTest.java @@ -0,0 +1,52 @@ +package client.scenes; + +import client.utils.Config; +import client.utils.ServerUtils; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.*; + +public class ConfigTest { + static ServerUtils dv = new ServerUtils(); + + private static final long FAV_LIST_ID_1 = 1234L; + private static final long FAV_LIST_ID_2 = 1235L; + + + @BeforeEach + public void setup(){ + Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available"); + } + + + @Test + public void configDefaultValueTest(){ + + Config config = new Config(); + assertEquals("en", config.getLanguage()); + assertEquals("http://localhost:8080", config.getServerUrl()); + assertTrue(config.getFavourites().isEmpty()); + assertTrue(config.getShoppingList().isEmpty()); + } + + @Test + public void configGetterTest(){ + Config config = new Config(); + config.setLanguage("nl"); + config.setServerUrl("http://localhost:8081"); + ArrayList x = new ArrayList<>(); + x.add("Lava Cake"); + x.add("Brownie"); + ArrayList y = new ArrayList<>(); + y.add(FAV_LIST_ID_1); + y.add(FAV_LIST_ID_2); + config.setFavourites(y); + assertEquals(config.getFavourites(), y); + config.setShoppingList(x); + assertEquals(config.getShoppingList(), x); + } +} \ No newline at end of file