made tests that work in gitlab, without being skipped

This commit is contained in:
Mei Chang van der Werff 2026-01-09 23:44:43 +01:00
commit a95c5ac7d0

View file

@ -2,8 +2,6 @@ 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.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;
@ -12,85 +10,124 @@ import org.junit.jupiter.api.io.TempDir;
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;
import java.util.ArrayList;
import java.util.List; 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(); @TempDir
Path tempDir;
private Path configPath;
private static final long TEST_ID_A = 23412L; private ConfigService configService;
private static final long TEST_ID_B = 25412L;
@BeforeEach @BeforeEach
public void setup(){ void setUp() throws IOException {
Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available"); configPath = tempDir.resolve("configServiceTest.json");
configService = new ConfigService(configPath);
} }
/*
Tests if the config file loads properly with a prewritten json file
*/
@Test @Test
public void configServiceFileLoadTest(@TempDir Path tempDir) throws IOException { public void constructorTest(){
Path configPath = tempDir.resolve("config.json"); assertEquals(configPath, configService.getConfigPath());
assertNotNull(configService.getMapper());
assertNotNull(configService.getConfig());
}
@Test
public void constructorFileDNETest(){
Path nonExistentPath = tempDir.resolve("DNE.json");
ConfigService newService = new ConfigService(nonExistentPath);
assertNotNull(newService.getConfig());
assertEquals("en", newService.getConfig().getLanguage());
assertEquals("http://localhost:8080", newService.getConfig().getServerUrl());
assertTrue(newService.getConfig().getFavourites().isEmpty());
assertTrue(newService.getConfig().getShoppingList().isEmpty());
}
@Test
public void validJsonLoadTest() throws IOException {
String json = """ String json = """
{ {
"language": "de", "language": "de",
"serverUrl": "http://exmple12.com", "serverUrl": "http://example.com:8080",
"favourites": [23412, 25412], "favourites": [123, 456],
"shoppingList": ["milk", "butter"] "shoppingList": ["milk", "butter"]
} }
"""; """;
Files.writeString(configPath, json); //writes into path Files.writeString(configPath, json);
ConfigService configService = new ConfigService(configPath);//initiates configservice
Config config = configService.getConfig(); //checks ConfigService loadedService = new ConfigService(configPath);
Config loadedConfig = loadedService.getConfig();
assertEquals("de", config.getLanguage()); assertEquals("de", loadedConfig.getLanguage());
assertEquals("http://exmple12.com", config.getServerUrl()); assertEquals("http://example.com:8080", loadedConfig.getServerUrl());
assertEquals(2, loadedConfig.getFavourites().size());
assertTrue(loadedConfig.getFavourites().contains(123L));
assertTrue(loadedConfig.getFavourites().contains(456L));
assertEquals(2, loadedConfig.getShoppingList().size());
assertTrue(loadedConfig.getShoppingList().contains("milk"));
assertTrue(loadedConfig.getShoppingList().contains("butter"));
List<Long> x = new ArrayList<>();
x.add(TEST_ID_A);
x.add(TEST_ID_B);
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 @Test
public void configSaveTest(@TempDir Path tempDir) throws IOException { public void invalidJsonLoadTest() throws IOException {
Path configPath = tempDir.resolve("config.json"); Files.writeString(configPath, "{ invalid json text");
ConfigService configService = new ConfigService(configPath);
Config config = configService.getConfig(); assertThrows(RuntimeException.class, () -> {
ConfigService corruptedService = new ConfigService(configPath);
corruptedService.getConfig();
});
}
@Test
public void safeTest(){
Config realConfig = new Config();
realConfig.setLanguage("de");
realConfig.setServerUrl("http://example.com:8080");
realConfig.addFavourite(1L);
realConfig.addFavourite(2L);
realConfig.setShoppingList(List.of("milk", "bread"));
config.setLanguage("fr"); configService.setConfig(realConfig);
config.setServerUrl("www.domain1.com");
configService.save(); configService.save();
String jsonTest = Files.readString(configPath); ConfigService loadedService = new ConfigService(configPath);
Config loadedConfig = loadedService.getConfig();
assertTrue(jsonTest.contains("\"language\":\"fr\""));
assertTrue(jsonTest.contains("\"serverUrl\":\"www.domain1.com\""));
assertEquals("de", loadedConfig.getLanguage());
assertEquals("http://example.com:8080", loadedConfig.getServerUrl());
assertEquals(2, loadedConfig.getFavourites().size());
assertTrue(loadedConfig.getFavourites().contains(1L));
assertTrue(loadedConfig.getFavourites().contains(2L));
assertEquals(2, loadedConfig.getShoppingList().size());
assertTrue(loadedConfig.getShoppingList().contains("milk"));
assertTrue(loadedConfig.getShoppingList().contains("bread"));
} }
@Test
public void multipleTimesSaveTest(){
Config realConfig = new Config();
configService.setConfig(realConfig);
configService.save();
realConfig.setLanguage("de");
configService.save();
realConfig.setServerUrl("http://example.com:8080");
configService.save();
ConfigService loadedService = new ConfigService(configPath);
Config loadedConfig = loadedService.getConfig();
assertEquals("de", loadedConfig.getLanguage());
assertEquals("http://example.com:8080", loadedConfig.getServerUrl());
}
}
}