From d0b35deb0ff026649f2598b37e19dbd916c6fcd3 Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Wed, 26 Nov 2025 16:52:41 +0100 Subject: [PATCH] detailView methods findAll and findId with a test for findAll --- client/src/main/java/client/DetailView.java | 58 +++++++++++++++++++ .../src/test/java/client/DetailViewTest.java | 30 ++++++++++ 2 files changed, 88 insertions(+) create mode 100644 client/src/main/java/client/DetailView.java create mode 100644 client/src/test/java/client/DetailViewTest.java diff --git a/client/src/main/java/client/DetailView.java b/client/src/main/java/client/DetailView.java new file mode 100644 index 0000000..b67de78 --- /dev/null +++ b/client/src/main/java/client/DetailView.java @@ -0,0 +1,58 @@ +package client; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import commons.Recipe; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.List; + +public class DetailView { + private final String BASE_URL = "http://localhost:8080/api"; + private final HttpClient client; + private final ObjectMapper objectMapper = new ObjectMapper(); + + public DetailView() { + client = HttpClient.newHttpClient(); + } + + + /** + * Gets all the recipes from the backend + * @return a JSON string with all the recipes + + */ + public List findAll() throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(BASE_URL+ "/recipes")) + .GET() + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + List recipes = objectMapper.readValue(response.body(), new TypeReference>() { + }); // JSON string-> List (Jackson) + return recipes; + } + + + /** + * Gets a single recipe based on its id + * @param id every recipe has it's unique id + * @return a singe recipe with the given id + */ + public Recipe findId(int id) throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(BASE_URL+"/" + id)) + .GET() + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + Recipe recipe = objectMapper.readValue(response.body(),Recipe.class); + return recipe; + } +} diff --git a/client/src/test/java/client/DetailViewTest.java b/client/src/test/java/client/DetailViewTest.java new file mode 100644 index 0000000..dc6b417 --- /dev/null +++ b/client/src/test/java/client/DetailViewTest.java @@ -0,0 +1,30 @@ +package client; + +import commons.Recipe; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class DetailViewTest { + static DetailView dv; + + @BeforeAll + static void setup() { + dv = new DetailView(); + } + + @Test + void findAll() throws IOException, InterruptedException { + List recipes = dv.findAll(); + + assertNotNull(recipes, "The list should not be null"); + assertTrue(recipes.size() >= 0, "The list should be 0 (when no recipes), or more"); + } + + + +} \ No newline at end of file