Merge branch 'feature/client_config' into feature/client_navigation

pulling in checkstyle to unblock CI
This commit is contained in:
Rithvik Sriram 2025-12-04 18:55:11 +01:00
commit 79b3c0a967
4 changed files with 60 additions and 37 deletions

View file

@ -5,14 +5,11 @@ import java.util.List;
public class Config { 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 language = "en";
private String serverUrl = "http://localhost:8080"; private String serverUrl = "http://localhost:8080";
private List<String> favourites = new ArrayList<>(); private List<Long> favourites = new ArrayList<>();
private List<String> shoppingList = new ArrayList<>(); private List<String> shoppingList = new ArrayList<>();
public Config(){} public Config(){}
@ -25,7 +22,7 @@ public class Config {
return shoppingList; return shoppingList;
} }
public List<String> getFavourites() { public List<Long> getFavourites() {
return favourites; return favourites;
} }
@ -41,7 +38,7 @@ public class Config {
this.serverUrl = serverUrl; this.serverUrl = serverUrl;
} }
public void setFavourites(List<String> favourites) { public void setFavourites(List<Long> favourites) {
this.favourites = favourites; this.favourites = favourites;
} }

View file

@ -1,8 +1,7 @@
package client.utils; package client.utils;
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import java.nio.file.Path; import java.nio.file.Path;
@ -31,21 +30,18 @@ public class ConfigService {
*/ */
private void load(){ private void load(){
File file = configPath.toFile(); //reads the config file as file File file = configPath.toFile(); //reads the config file as file
if(file.exists()){ if (!file.exists()) { //if file doesn't exist then it creates a config object
try{ config = new Config();
config = mapper.readValue(file, Config.class); //uses Jackson to map the file to the config attribute return;
}
} catch (StreamReadException e) { try{
throw new RuntimeException(e); config = mapper.readValue(file, Config.class); //uses Jackson to map the file to the config attribute
} catch (DatabindException e) {
throw new RuntimeException(e); } catch (IOException e) {
} catch (IOException e) { throw new RuntimeException(e);
throw new RuntimeException(e);
}
}
else{
config = new Config(); // if file doesn't exist, it creates one
} }
} }
public Path getConfigPath() { public Path getConfigPath() {

View file

@ -2,10 +2,13 @@ package client.scenes;
import client.utils.Config; import client.utils.Config;
import client.utils.ConfigService; 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.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -15,12 +18,23 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
public class ConfigServiceTest { 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 Tests if the config file loads properly with a prewritten json file
*/ */
@Test @Test
public void ConfigServiceFileLoadTest(@TempDir Path tempDir) throws IOException { public void configServiceFileLoadTest(@TempDir Path tempDir) throws IOException {
Path configPath = tempDir.resolve("config.json"); Path configPath = tempDir.resolve("config.json");
String json = """ String json = """
@ -39,9 +53,10 @@ public class ConfigServiceTest {
assertEquals("de", config.getLanguage()); assertEquals("de", config.getLanguage());
assertEquals("http://exmple12.com", config.getServerUrl()); assertEquals("http://exmple12.com", config.getServerUrl());
List<String> x = new ArrayList<>(); List<Long> x = new ArrayList<>();
x.add("banana bread");
x.add("pineapple pie"); x.add(TEST_ID_A);
x.add(TEST_ID_B);
List<String> y = new ArrayList<>(); List<String> y = new ArrayList<>();
y.add("milk"); y.add("milk");
@ -56,7 +71,7 @@ public class ConfigServiceTest {
Tests if the save method saves changes to the config file. Tests if the save method saves changes to the config file.
*/ */
@Test @Test
public void ConfigSaveTest(@TempDir Path tempDir) throws IOException { public void configSaveTest(@TempDir Path tempDir) throws IOException {
Path configPath = tempDir.resolve("config.json"); Path configPath = tempDir.resolve("config.json");
ConfigService configService = new ConfigService(configPath); ConfigService configService = new ConfigService(configPath);

View file

@ -1,6 +1,9 @@
package client.scenes; package client.scenes;
import client.utils.Config; 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 org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
@ -8,9 +11,21 @@ import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
public class ConfigTest { 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 @Test
public void ConfigDefaultValueTest(){ public void configDefaultValueTest(){
Config config = new Config(); Config config = new Config();
assertEquals("en", config.getLanguage()); assertEquals("en", config.getLanguage());
assertEquals("http://localhost:8080", config.getServerUrl()); assertEquals("http://localhost:8080", config.getServerUrl());
@ -19,19 +34,19 @@ public class ConfigTest {
} }
@Test @Test
public void ConfigGetterTest(){ public void configGetterTest(){
Config config = new Config(); Config config = new Config();
config.setLanguage("nl"); config.setLanguage("nl");
config.setServerUrl("http://localhost:8081"); config.setServerUrl("http://localhost:8081");
ArrayList<String> x = new ArrayList<>(); ArrayList<String> x = new ArrayList<>();
x.add("Lava Cake"); x.add("Lava Cake");
x.add("Brownie"); x.add("Brownie");
config.setFavourites(x); ArrayList<Long> y = new ArrayList<>();
assertEquals(config.getFavourites(), x); y.add(FAV_LIST_ID_1);
y.add(FAV_LIST_ID_2);
config.setFavourites(y);
assertEquals(config.getFavourites(), y);
config.setShoppingList(x); config.setShoppingList(x);
assertEquals(config.getShoppingList(), x); assertEquals(config.getShoppingList(), x);
} }
} }