made tests that work in gitlab, without being skipped
This commit is contained in:
parent
07adedde3e
commit
a95c5ac7d0
1 changed files with 85 additions and 48 deletions
|
|
@ -2,8 +2,6 @@ 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;
|
||||
|
|
@ -12,85 +10,124 @@ 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();
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
|
||||
private static final long TEST_ID_A = 23412L;
|
||||
private static final long TEST_ID_B = 25412L;
|
||||
private Path configPath;
|
||||
private ConfigService configService;
|
||||
|
||||
@BeforeEach
|
||||
public void setup(){
|
||||
Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available");
|
||||
void setUp() throws IOException {
|
||||
configPath = tempDir.resolve("configServiceTest.json");
|
||||
configService = new ConfigService(configPath);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
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");
|
||||
public void constructorTest(){
|
||||
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 = """
|
||||
{
|
||||
"language": "de",
|
||||
"serverUrl": "http://exmple12.com",
|
||||
"favourites": [23412, 25412],
|
||||
"serverUrl": "http://example.com:8080",
|
||||
"favourites": [123, 456],
|
||||
"shoppingList": ["milk", "butter"]
|
||||
}
|
||||
""";
|
||||
Files.writeString(configPath, json); //writes into path
|
||||
ConfigService configService = new ConfigService(configPath);//initiates configservice
|
||||
Files.writeString(configPath, json);
|
||||
|
||||
Config config = configService.getConfig(); //checks
|
||||
ConfigService loadedService = new ConfigService(configPath);
|
||||
Config loadedConfig = loadedService.getConfig();
|
||||
|
||||
assertEquals("de", config.getLanguage());
|
||||
assertEquals("http://exmple12.com", config.getServerUrl());
|
||||
assertEquals("de", loadedConfig.getLanguage());
|
||||
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
|
||||
public void configSaveTest(@TempDir Path tempDir) throws IOException {
|
||||
Path configPath = tempDir.resolve("config.json");
|
||||
ConfigService configService = new ConfigService(configPath);
|
||||
public void invalidJsonLoadTest() throws IOException {
|
||||
Files.writeString(configPath, "{ invalid json text");
|
||||
|
||||
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");
|
||||
config.setServerUrl("www.domain1.com");
|
||||
configService.setConfig(realConfig);
|
||||
|
||||
configService.save();
|
||||
|
||||
String jsonTest = Files.readString(configPath);
|
||||
|
||||
assertTrue(jsonTest.contains("\"language\":\"fr\""));
|
||||
assertTrue(jsonTest.contains("\"serverUrl\":\"www.domain1.com\""));
|
||||
ConfigService loadedService = new ConfigService(configPath);
|
||||
Config loadedConfig = loadedService.getConfig();
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue