Merge branch 'bugfix-configServiceTest' into 'main'
Skipped tests aren't skipped anymore Closes #46 See merge request cse1105/2025-2026/teams/csep-team-76!50
This commit is contained in:
commit
624a0cbe86
4 changed files with 141 additions and 79 deletions
|
|
@ -4,8 +4,6 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
|
|
||||||
private String language = "en";
|
private String language = "en";
|
||||||
private List<String> recipeLanguages = new ArrayList<>();
|
private List<String> recipeLanguages = new ArrayList<>();
|
||||||
private String serverUrl = "http://localhost:8080";
|
private String serverUrl = "http://localhost:8080";
|
||||||
|
|
@ -14,6 +12,10 @@ public class Config {
|
||||||
private List<String> shoppingList = new ArrayList<>();
|
private List<String> shoppingList = new ArrayList<>();
|
||||||
|
|
||||||
public Config() {
|
public Config() {
|
||||||
|
this.language = "en";
|
||||||
|
this.serverUrl = "http://localhost:8080";
|
||||||
|
this.favourites = new ArrayList<>();
|
||||||
|
this.shoppingList = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLanguage() {
|
public String getLanguage() {
|
||||||
|
|
|
||||||
|
|
@ -1,96 +1,120 @@
|
||||||
package client.scenes;
|
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;
|
||||||
|
|
||||||
|
|
||||||
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 static final long TEST_ID_A = 23412L;
|
private Path configPath;
|
||||||
private static final long TEST_ID_B = 25412L;
|
private ConfigService configService;
|
||||||
|
|
||||||
@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);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void constructorTest(){
|
||||||
|
assertEquals(configPath, configService.getConfigPath());
|
||||||
|
assertNotNull(configService.getMapper());
|
||||||
|
assertNotNull(configService.getConfig());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
Tests if the config file loads properly with a prewritten json file
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void configServiceFileLoadTest(@TempDir Path tempDir) throws IOException {
|
public void constructorFileDNETest(){
|
||||||
Path configPath = tempDir.resolve("config.json");
|
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
|
|
||||||
ConfigService configService = new ConfigService(configPath);//initiates configservice
|
|
||||||
|
|
||||||
Config config = configService.getConfig(); //checks
|
Files.writeString(configPath, json);
|
||||||
|
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());
|
||||||
List<Long> x = new ArrayList<>();
|
assertTrue(loadedConfig.getFavourites().contains(123L));
|
||||||
|
assertTrue(loadedConfig.getFavourites().contains(456L));
|
||||||
x.add(TEST_ID_A);
|
assertEquals(2, loadedConfig.getShoppingList().size());
|
||||||
x.add(TEST_ID_B);
|
assertTrue(loadedConfig.getShoppingList().contains("milk"));
|
||||||
|
assertTrue(loadedConfig.getShoppingList().contains("butter"));
|
||||||
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
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.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -11,42 +9,64 @@ 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 Config config;
|
||||||
|
|
||||||
private static final long FAV_LIST_ID_1 = 1234L;
|
private static final long FAV_LIST_ID_1 = 1234L;
|
||||||
private static final long FAV_LIST_ID_2 = 1235L;
|
private static final long FAV_LIST_ID_2 = 1235L;
|
||||||
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setup(){
|
void setUp(){
|
||||||
Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available");
|
config = new Config();
|
||||||
|
config.setLanguage("nl");
|
||||||
|
config.setServerUrl("http://localhost:8081");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void configDefaultValueTest(){
|
public void configDefaultValueTest(){
|
||||||
|
|
||||||
Config config = new Config();
|
assertEquals("nl", config.getLanguage());
|
||||||
assertEquals("en", config.getLanguage());
|
assertEquals("http://localhost:8081", config.getServerUrl());
|
||||||
assertEquals("http://localhost:8080", config.getServerUrl());
|
|
||||||
assertTrue(config.getFavourites().isEmpty());
|
assertTrue(config.getFavourites().isEmpty());
|
||||||
assertTrue(config.getShoppingList().isEmpty());
|
assertTrue(config.getShoppingList().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void configGetterTest(){
|
public void configGetterTest(){
|
||||||
Config config = new Config();
|
|
||||||
config.setLanguage("nl");
|
|
||||||
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");
|
||||||
|
|
||||||
ArrayList<Long> y = new ArrayList<>();
|
ArrayList<Long> y = new ArrayList<>();
|
||||||
y.add(FAV_LIST_ID_1);
|
y.add(FAV_LIST_ID_1);
|
||||||
y.add(FAV_LIST_ID_2);
|
y.add(FAV_LIST_ID_2);
|
||||||
config.setFavourites(y);
|
config.setFavourites(y);
|
||||||
|
|
||||||
assertEquals(config.getFavourites(), y);
|
assertEquals(config.getFavourites(), y);
|
||||||
config.setShoppingList(x);
|
config.setShoppingList(x);
|
||||||
assertEquals(config.getShoppingList(), x);
|
assertEquals(config.getShoppingList(), x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isFavTest(){
|
||||||
|
assertFalse(config.isFavourite(FAV_LIST_ID_1)); //not yet fav
|
||||||
|
|
||||||
|
ArrayList<Long> y = new ArrayList<>();
|
||||||
|
y.add(FAV_LIST_ID_1);
|
||||||
|
config.setFavourites(y);
|
||||||
|
|
||||||
|
assertTrue(config.isFavourite(FAV_LIST_ID_1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void removeFavTest(){
|
||||||
|
ArrayList<Long> y = new ArrayList<>();
|
||||||
|
y.add(FAV_LIST_ID_1);
|
||||||
|
config.setFavourites(y);
|
||||||
|
|
||||||
|
assertTrue(config.isFavourite(FAV_LIST_ID_1));
|
||||||
|
|
||||||
|
config.removeFavourite(FAV_LIST_ID_1);
|
||||||
|
assertFalse(config.isFavourite(FAV_LIST_ID_1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,11 +2,9 @@ package client.scenes;
|
||||||
|
|
||||||
import client.utils.DefaultValueFactory;
|
import client.utils.DefaultValueFactory;
|
||||||
import client.utils.PrintExportService;
|
import client.utils.PrintExportService;
|
||||||
import client.utils.ServerUtils;
|
|
||||||
import commons.Recipe;
|
import commons.Recipe;
|
||||||
import commons.RecipeIngredient;
|
import commons.RecipeIngredient;
|
||||||
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;
|
||||||
|
|
||||||
|
|
@ -19,17 +17,15 @@ import java.util.List;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class PrintExportTest {
|
public class PrintExportTest {
|
||||||
static ServerUtils dv = new ServerUtils();
|
@TempDir
|
||||||
@BeforeEach
|
Path tempDir;
|
||||||
public void setup(){
|
|
||||||
Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void buildRecipeTextTest(){
|
public void buildRecipeTextTest(){
|
||||||
List<RecipeIngredient> ingredients = new ArrayList<>();
|
List<RecipeIngredient> ingredients = new ArrayList<>();
|
||||||
ingredients.add(DefaultValueFactory.getDefaultVagueIngredient("Banana"));
|
ingredients.add(DefaultValueFactory.getDefaultVagueIngredient("Banana"));
|
||||||
ingredients.add(DefaultValueFactory.getDefaultVagueIngredient("Bread"));
|
ingredients.add(DefaultValueFactory.getDefaultVagueIngredient("Bread"));
|
||||||
|
|
||||||
final long testRecipeId = 1234L;
|
final long testRecipeId = 1234L;
|
||||||
List<String> preparationSteps = new ArrayList<>();
|
List<String> preparationSteps = new ArrayList<>();
|
||||||
preparationSteps.add("Mix Ingredients");
|
preparationSteps.add("Mix Ingredients");
|
||||||
|
|
@ -44,11 +40,8 @@ public class PrintExportTest {
|
||||||
1: Mix Ingredients
|
1: Mix Ingredients
|
||||||
2: Heat in Oven
|
2: Heat in Oven
|
||||||
""", PrintExportService.buildRecipeText(recipe1));
|
""", PrintExportService.buildRecipeText(recipe1));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@TempDir
|
|
||||||
Path tempDir;
|
|
||||||
@Test
|
@Test
|
||||||
public void validateFolderWithValidFolderTest(){
|
public void validateFolderWithValidFolderTest(){
|
||||||
assertDoesNotThrow(() -> PrintExportService.validateFolder(tempDir));
|
assertDoesNotThrow(() -> PrintExportService.validateFolder(tempDir));
|
||||||
|
|
@ -69,4 +62,27 @@ public class PrintExportTest {
|
||||||
assertEquals("Given path is not a folder", i.getMessage());
|
assertEquals("Given path is not a folder", i.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void succesExportTest() throws IOException {
|
||||||
|
String data = "recipe data";
|
||||||
|
String fileName = "succes.txt";
|
||||||
|
Path filePath = tempDir.resolve(fileName);
|
||||||
|
|
||||||
|
PrintExportService.exportToFile(data,tempDir,fileName);
|
||||||
|
|
||||||
|
assertTrue(Files.exists(filePath));
|
||||||
|
assertEquals(data, Files.readString(filePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void failExportTest(){
|
||||||
|
String data = "recipe data";
|
||||||
|
String fileName = "succes.txt";
|
||||||
|
Path filePath = tempDir.resolve("fail/failDir");
|
||||||
|
|
||||||
|
IllegalArgumentException i = assertThrows(IllegalArgumentException.class,
|
||||||
|
()->PrintExportService.exportToFile(data,filePath,fileName));
|
||||||
|
assertEquals("Folder does not exist", i.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue