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;
|
||||
|
||||
public class Config {
|
||||
|
||||
|
||||
private String language = "en";
|
||||
private List<String> recipeLanguages = new ArrayList<>();
|
||||
private String serverUrl = "http://localhost:8080";
|
||||
|
|
@ -14,6 +12,10 @@ public class Config {
|
|||
private List<String> shoppingList = new ArrayList<>();
|
||||
|
||||
public Config() {
|
||||
this.language = "en";
|
||||
this.serverUrl = "http://localhost:8080";
|
||||
this.favourites = new ArrayList<>();
|
||||
this.shoppingList = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getLanguage() {
|
||||
|
|
|
|||
|
|
@ -1,96 +1,120 @@
|
|||
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;
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
@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
|
||||
public void configServiceFileLoadTest(@TempDir Path tempDir) throws IOException {
|
||||
Path configPath = tempDir.resolve("config.json");
|
||||
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
|
||||
|
||||
Config config = configService.getConfig(); //checks
|
||||
Files.writeString(configPath, json);
|
||||
ConfigService loadedService = new ConfigService(configPath);
|
||||
Config loadedConfig = loadedService.getConfig();
|
||||
|
||||
assertEquals("de", config.getLanguage());
|
||||
assertEquals("http://exmple12.com", config.getServerUrl());
|
||||
|
||||
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());
|
||||
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"));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,6 @@
|
|||
package client.scenes;
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -11,42 +9,64 @@ import java.util.ArrayList;
|
|||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
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_2 = 1235L;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void setup(){
|
||||
Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available");
|
||||
void setUp(){
|
||||
config = new Config();
|
||||
config.setLanguage("nl");
|
||||
config.setServerUrl("http://localhost:8081");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void configDefaultValueTest(){
|
||||
|
||||
Config config = new Config();
|
||||
assertEquals("en", config.getLanguage());
|
||||
assertEquals("http://localhost:8080", config.getServerUrl());
|
||||
assertEquals("nl", config.getLanguage());
|
||||
assertEquals("http://localhost:8081", 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");
|
||||
|
||||
ArrayList<Long> y = new ArrayList<>();
|
||||
y.add(FAV_LIST_ID_1);
|
||||
y.add(FAV_LIST_ID_2);
|
||||
config.setFavourites(y);
|
||||
|
||||
assertEquals(config.getFavourites(), y);
|
||||
config.setShoppingList(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.PrintExportService;
|
||||
import client.utils.ServerUtils;
|
||||
import commons.Recipe;
|
||||
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.io.TempDir;
|
||||
|
||||
|
|
@ -19,17 +17,15 @@ import java.util.List;
|
|||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class PrintExportTest {
|
||||
static ServerUtils dv = new ServerUtils();
|
||||
@BeforeEach
|
||||
public void setup(){
|
||||
Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available");
|
||||
}
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
public void buildRecipeTextTest(){
|
||||
List<RecipeIngredient> ingredients = new ArrayList<>();
|
||||
ingredients.add(DefaultValueFactory.getDefaultVagueIngredient("Banana"));
|
||||
ingredients.add(DefaultValueFactory.getDefaultVagueIngredient("Bread"));
|
||||
|
||||
final long testRecipeId = 1234L;
|
||||
List<String> preparationSteps = new ArrayList<>();
|
||||
preparationSteps.add("Mix Ingredients");
|
||||
|
|
@ -44,11 +40,8 @@ public class PrintExportTest {
|
|||
1: Mix Ingredients
|
||||
2: Heat in Oven
|
||||
""", PrintExportService.buildRecipeText(recipe1));
|
||||
|
||||
}
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
@Test
|
||||
public void validateFolderWithValidFolderTest(){
|
||||
assertDoesNotThrow(() -> PrintExportService.validateFolder(tempDir));
|
||||
|
|
@ -69,4 +62,27 @@ public class PrintExportTest {
|
|||
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