diff --git a/client/src/main/java/client/utils/Config.java b/client/src/main/java/client/utils/Config.java index 3933e6f..a70d55c 100644 --- a/client/src/main/java/client/utils/Config.java +++ b/client/src/main/java/client/utils/Config.java @@ -4,8 +4,6 @@ import java.util.ArrayList; import java.util.List; public class Config { - - private String language = "en"; private List recipeLanguages = new ArrayList<>(); private String serverUrl = "http://localhost:8080"; @@ -14,6 +12,10 @@ public class Config { private List shoppingList = new ArrayList<>(); public Config() { + this.language = "en"; + this.serverUrl = "http://localhost:8080"; + this.favourites = new ArrayList<>(); + this.shoppingList = new ArrayList<>(); } public String getLanguage() { diff --git a/client/src/test/java/client/scenes/ConfigServiceTest.java b/client/src/test/java/client/scenes/ConfigServiceTest.java index 1f2e493..b743339 100644 --- a/client/src/test/java/client/scenes/ConfigServiceTest.java +++ b/client/src/test/java/client/scenes/ConfigServiceTest.java @@ -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 x = new ArrayList<>(); - - x.add(TEST_ID_A); - x.add(TEST_ID_B); - - List 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()); + } } \ No newline at end of file diff --git a/client/src/test/java/client/scenes/ConfigTest.java b/client/src/test/java/client/scenes/ConfigTest.java index 9c82d34..812dd19 100644 --- a/client/src/test/java/client/scenes/ConfigTest.java +++ b/client/src/test/java/client/scenes/ConfigTest.java @@ -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 x = new ArrayList<>(); x.add("Lava Cake"); x.add("Brownie"); + ArrayList 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 y = new ArrayList<>(); + y.add(FAV_LIST_ID_1); + config.setFavourites(y); + + assertTrue(config.isFavourite(FAV_LIST_ID_1)); + } + + @Test + public void removeFavTest(){ + ArrayList 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)); + } } \ No newline at end of file diff --git a/client/src/test/java/client/scenes/PrintExportTest.java b/client/src/test/java/client/scenes/PrintExportTest.java index 958a994..d961d64 100644 --- a/client/src/test/java/client/scenes/PrintExportTest.java +++ b/client/src/test/java/client/scenes/PrintExportTest.java @@ -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 ingredients = new ArrayList<>(); ingredients.add(DefaultValueFactory.getDefaultVagueIngredient("Banana")); ingredients.add(DefaultValueFactory.getDefaultVagueIngredient("Bread")); + final long testRecipeId = 1234L; List 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()); + } + }