Implemented ConfigService load/save methods and tests
This commit is contained in:
parent
11c0d7b489
commit
fc03b6998a
5 changed files with 259 additions and 0 deletions
|
|
@ -23,6 +23,12 @@
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>2.20.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.glassfish.jersey.core</groupId>
|
<groupId>org.glassfish.jersey.core</groupId>
|
||||||
<artifactId>jersey-client</artifactId>
|
<artifactId>jersey-client</artifactId>
|
||||||
|
|
|
||||||
51
client/src/main/java/client/utils/Config.java
Normal file
51
client/src/main/java/client/utils/Config.java
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
package client.utils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Config {
|
||||||
|
|
||||||
|
/*
|
||||||
|
Sets parameters for what the config file needs with a config
|
||||||
|
Object that will be used in the ConfigService object
|
||||||
|
*/
|
||||||
|
private String language = "en";
|
||||||
|
private String serverUrl = "http://localhost:8080";
|
||||||
|
|
||||||
|
private List<String> favourites = new ArrayList<>();
|
||||||
|
private List<String> shoppingList = new ArrayList<>();
|
||||||
|
|
||||||
|
public Config(){}
|
||||||
|
|
||||||
|
public String getLanguage() {
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getShoppingList() {
|
||||||
|
return shoppingList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> 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<String> favourites) {
|
||||||
|
this.favourites = favourites;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setShoppingList(List<String> shoppingList) {
|
||||||
|
this.shoppingList = shoppingList;
|
||||||
|
}
|
||||||
|
}
|
||||||
84
client/src/main/java/client/utils/ConfigService.java
Normal file
84
client/src/main/java/client/utils/ConfigService.java
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
package client.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.exc.StreamReadException;
|
||||||
|
import com.fasterxml.jackson.databind.DatabindException;
|
||||||
|
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()){
|
||||||
|
try{
|
||||||
|
config = mapper.readValue(file, Config.class); //uses Jackson to map the file to the config attribute
|
||||||
|
|
||||||
|
} catch (StreamReadException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (DatabindException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
config = new Config(); // if file doesn't exist, it creates one
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
81
client/src/test/java/client/scenes/ConfigServiceTest.java
Normal file
81
client/src/test/java/client/scenes/ConfigServiceTest.java
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
package client.scenes;
|
||||||
|
|
||||||
|
import client.utils.Config;
|
||||||
|
import client.utils.ConfigService;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
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 {
|
||||||
|
|
||||||
|
/*
|
||||||
|
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<String> x = new ArrayList<>();
|
||||||
|
x.add("banana bread");
|
||||||
|
x.add("pineapple pie");
|
||||||
|
|
||||||
|
List<String> 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\""));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
37
client/src/test/java/client/scenes/ConfigTest.java
Normal file
37
client/src/test/java/client/scenes/ConfigTest.java
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
package client.scenes;
|
||||||
|
|
||||||
|
import client.utils.Config;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class ConfigTest {
|
||||||
|
|
||||||
|
@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<String> x = new ArrayList<>();
|
||||||
|
x.add("Lava Cake");
|
||||||
|
x.add("Brownie");
|
||||||
|
config.setFavourites(x);
|
||||||
|
assertEquals(config.getFavourites(), x);
|
||||||
|
config.setShoppingList(x);
|
||||||
|
assertEquals(config.getShoppingList(), x);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue