From 2093a4317416a48a3dce35fe1315b228081cb0e5 Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Thu, 27 Nov 2025 14:42:16 +0100 Subject: [PATCH] implemented clone method plus test and fixed some issues --- client/src/main/java/client/DetailView.java | 37 +++++++++++-------- .../src/test/java/client/DetailViewTest.java | 12 +++++- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/client/src/main/java/client/DetailView.java b/client/src/main/java/client/DetailView.java index 5e23927..ca71bce 100644 --- a/client/src/main/java/client/DetailView.java +++ b/client/src/main/java/client/DetailView.java @@ -13,9 +13,10 @@ import java.net.http.HttpResponse; import java.util.List; public class DetailView { - private final String BASE_URL = "http://localhost:8080/api"; + private static final String BASE_URL = "http://localhost:8080/api"; private final HttpClient client; private final ObjectMapper objectMapper = new ObjectMapper(); + private final int statusOK = 200; public DetailView() { client = HttpClient.newHttpClient(); @@ -32,9 +33,8 @@ public class DetailView { .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - List recipes = objectMapper.readValue(response.body(), new TypeReference>() { - }); // JSON string-> List (Jackson) - return recipes; + return objectMapper.readValue(response.body(), new TypeReference>() { + });// JSON string-> List (Jackson) } /** @@ -49,13 +49,12 @@ public class DetailView { .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - if(response.statusCode() != 200){ + if(response.statusCode() != statusOK){ System.out.println("No recipe found with this id"); return null; } - Recipe recipe = objectMapper.readValue(response.body(),Recipe.class); - return recipe; + return objectMapper.readValue(response.body(),Recipe.class); } /** @@ -68,6 +67,11 @@ public class DetailView { .uri(URI.create(BASE_URL + "/recipes/" + id)) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + if(response.statusCode() != statusOK){ + System.out.println("No recipe to delete"); + } + } /** @@ -83,29 +87,32 @@ public class DetailView { .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - Recipe OGRecipe = objectMapper.readValue(response.body(),Recipe.class); + Recipe ogRecipe = objectMapper.readValue(response.body(),Recipe.class); //200 is the status code for success, other codes can mean there is no recipe to clone - if(response.statusCode() != 200){ + if(response.statusCode() != statusOK){ return null; } // recipe exists so you can make a "new" recipe aka the clone Recipe clone = new Recipe(); - String cloneName = OGRecipe.getName() + "(clone)"; // I suppose we want unique names? + String cloneName = ogRecipe.getName() + "(clone)"; // I suppose we want unique names? clone.setName(cloneName); - clone.setIngredients(OGRecipe.getIngredients()); - clone.setPreparationSteps(OGRecipe.getPreparationSteps()); + clone.setIngredients(ogRecipe.getIngredients()); + clone.setPreparationSteps(ogRecipe.getPreparationSteps()); //From String to Json StringWriter s = new StringWriter(); objectMapper.writeValue(s, clone); // Post the clone to backend - HttpRequest request1 = HttpRequest.newBuilder() + HttpRequest request2 = HttpRequest.newBuilder() .POST(HttpRequest.BodyPublishers.ofString(s.toString())) - .uri(URI.create(BASE_URL + "/recipes/" + id)) + .uri(URI.create(BASE_URL + "/recipes/")) .build(); - return clone; + + HttpResponse cloneResponse = client.send(request2, HttpResponse.BodyHandlers.ofString()); + + return objectMapper.readValue(cloneResponse.body(),Recipe.class); } } \ No newline at end of file diff --git a/client/src/test/java/client/DetailViewTest.java b/client/src/test/java/client/DetailViewTest.java index a20f409..d367b74 100644 --- a/client/src/test/java/client/DetailViewTest.java +++ b/client/src/test/java/client/DetailViewTest.java @@ -46,10 +46,18 @@ class DetailViewTest { @Test void deleteTest() throws IOException, InterruptedException { - Recipe r = dv.findId(testRecipe.getId()); - dv.deleteRecipe(r.getId()); + dv.deleteRecipe(testRecipe.getId()); assertNull(dv.findId(testRecipe.getId())); } + @Test + void cloneTest() throws IOException, InterruptedException { + Recipe clone = dv.cloneRecipe(testRecipe.getId()); + + assertNotEquals(clone.getId(), testRecipe.getId()); + assertEquals(clone.getIngredients(), testRecipe.getIngredients()); + assertEquals(clone.getPreparationSteps(), testRecipe.getPreparationSteps()); + } + } \ No newline at end of file