From b4d6b7865678648b448039059456825931aacc3c Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Thu, 27 Nov 2025 22:46:42 +0100 Subject: [PATCH] more tests and debugging --- .../main/java/client/utils/ServerUtils.java | 12 ++---- .../src/test/java/client/ServerUtilsTest.java | 41 +++++++++++++++---- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/client/src/main/java/client/utils/ServerUtils.java b/client/src/main/java/client/utils/ServerUtils.java index bb7e6a6..42ccc46 100644 --- a/client/src/main/java/client/utils/ServerUtils.java +++ b/client/src/main/java/client/utils/ServerUtils.java @@ -51,8 +51,7 @@ public class ServerUtils { HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); if(response.statusCode() != statusOK){ - System.out.println("No recipe found with this id"); - return null; + throw new IOException("No recipe found with this id"); } return objectMapper.readValue(response.body(),Recipe.class); @@ -62,21 +61,19 @@ public class ServerUtils { * Adds a recipe to the backend * @param newRecipe the recipe to be added * @return a recipe - */ public Recipe addRecipe(Recipe newRecipe) throws IOException, InterruptedException { - //Make sure the name of the newRecipe is unque + //Make sure the name of the newRecipe is unique List allRecipes = getRecipes(); boolean exists = allRecipes.stream() .anyMatch(r -> r.getName().equals(newRecipe.getName())); - if(exists){ int version = 1; String newName; - // Giving the "new" recipe a unique name + // Giving the recipe a unique name while (true) { newName = newRecipe.getName() + "(" + version + ")"; String finalNewName = newName; @@ -90,12 +87,9 @@ public class ServerUtils { version++; } } - } - String json = objectMapper.writeValueAsString(newRecipe); - //Recipe to backend HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(SERVER + "/recipe/new")) diff --git a/client/src/test/java/client/ServerUtilsTest.java b/client/src/test/java/client/ServerUtilsTest.java index feb9ed6..b6f8dee 100644 --- a/client/src/test/java/client/ServerUtilsTest.java +++ b/client/src/test/java/client/ServerUtilsTest.java @@ -2,7 +2,6 @@ package client; import client.utils.ServerUtils; import commons.Recipe; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -14,15 +13,20 @@ import static org.junit.jupiter.api.Assertions.*; class ServerUtilsTest { static ServerUtils dv; static Recipe testRecipe; + final int fakeId = -1; // If suppose ID's are only positive @BeforeEach void setup() throws IOException, InterruptedException { dv = new ServerUtils(); + //Making sure there is no recipe in the backend yet + for (Recipe recipe : dv.getRecipes()) { + dv.deleteRecipe(recipe.getId()); + } + Recipe r = new Recipe(); - r.setName("Tosti"+ System.currentTimeMillis()); - // + System.currentTimeMillis() ai came up with this, now everytime the test runs it's a different name + r.setName("Tosti"); r.setIngredients(List.of("Bread", "Cheese", "Ham")); r.setPreparationSteps(List.of("Step 1:", "Step 2")); @@ -33,13 +37,20 @@ class ServerUtilsTest { @Test void addRecipeTest() throws IOException, InterruptedException { Recipe r = new Recipe(); - r.setName("Eggs on toast" + System.currentTimeMillis()); + r.setName("Eggs on toast"); r.setIngredients(List.of("Bread", "egg", "salt")); r.setPreparationSteps(List.of("Step 1:", "Step 2")); testRecipe = dv.addRecipe(r); } + @Test + void noRecipeToAdd(){ + Recipe r = new Recipe(); + assertThrows(IOException.class, () -> dv.addRecipe(r)); + + } + @Test void getAllRecipesTest() throws IOException, InterruptedException { List recipes = dv.getRecipes(); @@ -52,18 +63,28 @@ class ServerUtilsTest { @Test void findRecipeWithIDTest() throws IOException, InterruptedException { Recipe r = dv.findId(testRecipe.getId()); + assertEquals(testRecipe.getId(), r.getId()); - assertTrue(r.getName().startsWith("Tosti")); // bcs of the + System.currentTimeMillis() -// assertEquals("Tosti", r.getName()); + assertEquals("Tosti", r.getName()); assertIterableEquals(List.of("Bread", "Cheese", "Ham"), r.getIngredients()); assertIterableEquals(List.of("Step 1:", "Step 2"), r.getPreparationSteps()); } + @Test + void noRecipeFoundTest() throws IOException, InterruptedException { + assertThrows(IOException.class, () -> dv.findId(fakeId)); + } + @Test void deleteRecipeTest() throws IOException, InterruptedException { dv.deleteRecipe(testRecipe.getId()); - assertNull(dv.findId(testRecipe.getId()), "The recipe shouldn't exists anymore"); + assertThrows(IOException.class, () ->dv.findId(testRecipe.getId()), "The recipe shouldn't exists anymore" ); + } + + @Test + void noRecipeToDelete() throws IOException, InterruptedException { + assertThrows(IOException.class, () -> dv.deleteRecipe(fakeId)); } @Test @@ -71,8 +92,14 @@ class ServerUtilsTest { Recipe clone = dv.cloneRecipe(testRecipe.getId()); assertNotEquals(clone.getId(), testRecipe.getId(), "The id's should not be equal to each other"); + assertNotEquals(clone.getName(),testRecipe.getName(),"The name's should not be the same"); assertEquals(clone.getIngredients(), testRecipe.getIngredients(), "the ingredients should be the same"); assertEquals(clone.getPreparationSteps(), testRecipe.getPreparationSteps(),"The steps should be the same"); } + @Test + void noRecipeToClone(){ + assertThrows(IOException.class, () -> dv.cloneRecipe(fakeId)); + } + } \ No newline at end of file