From 6f2400cc50989ef7d05211e8ceaa76c9fccc5eeb Mon Sep 17 00:00:00 2001 From: Natalia Cholewa Date: Fri, 16 Jan 2026 23:57:06 +0100 Subject: [PATCH] test: endpoints --- .../java/client/utils/server/Endpoints.java | 27 ++++++---- .../src/test/java/client/EndpointsTest.java | 54 +++++++++++++++++++ 2 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 client/src/test/java/client/EndpointsTest.java diff --git a/client/src/main/java/client/utils/server/Endpoints.java b/client/src/main/java/client/utils/server/Endpoints.java index 7a9d4e7..d0f1e7d 100644 --- a/client/src/main/java/client/utils/server/Endpoints.java +++ b/client/src/main/java/client/utils/server/Endpoints.java @@ -25,62 +25,69 @@ public class Endpoints { return this.configService.getConfig().getServerUrl() + "/api"; } + public String createApiUrl(String route) { + return this.baseUrl() + route; + } + public HttpRequest.Builder fetchAllRecipes(List locales) { - String url = this.baseUrl() + "/recipes" + "?locales=" + String.join(",", locales); + String url = this.createApiUrl( + "/recipe" + + "?locales=" + String.join(",", locales) + ); return this.http(url).GET(); } public HttpRequest.Builder fetchRecipe(long recipeId) { - String url = this.baseUrl() + "/recipe/" + recipeId; + String url = this.createApiUrl("/recipe/" + recipeId); return this.http(url).GET(); } public HttpRequest.Builder createNewRecipe(HttpRequest.BodyPublisher body) { - String url = this.baseUrl() + "/recipe/new"; + String url = this.createApiUrl("/recipe/new"); return this.http(url).PUT(body); } public HttpRequest.Builder updateRecipe(long recipeId, HttpRequest.BodyPublisher body) { - String url = this.baseUrl() + "/recipe/" + recipeId; + String url = this.createApiUrl("/recipe/" + recipeId); return this.http(url).POST(body); } public HttpRequest.Builder deleteRecipe(long recipeId) { - String url = this.baseUrl() + "/recipe/" + recipeId; + String url = this.createApiUrl("/recipe/" + recipeId); return this.http(url).DELETE(); } public HttpRequest.Builder fetchIngredientUsage(long ingredientId) { - String url = this.baseUrl() + "/ingredients/" + ingredientId + "/usage"; + String url = this.createApiUrl("/ingredients/" + ingredientId + "/usage"); return this.http(url).GET(); } public HttpRequest.Builder deleteIngredient(long ingredientId) { - String url = this.baseUrl() + "/ingredients/" + ingredientId; + String url = this.createApiUrl("/ingredients/" + ingredientId); return this.http(url).DELETE(); } public HttpRequest.Builder getIngredients() { - String url = this.baseUrl() + "/ingredients"; + String url = this.createApiUrl("/ingredients"); return this.http(url).GET(); } public HttpRequest.Builder createIngredient(HttpRequest.BodyPublisher body) { - String url = this.baseUrl() + "/ingredients"; + String url = this.createApiUrl("/ingredients"); return this.http(url).POST(body); } public HttpRequest.Builder updateIngredient(long ingredientId, HttpRequest.BodyPublisher body) { - String url = this.baseUrl() + "/ingredients/" + ingredientId; + String url = this.createApiUrl("/ingredients/" + ingredientId); return this.http(url).method("PATCH", body); } diff --git a/client/src/test/java/client/EndpointsTest.java b/client/src/test/java/client/EndpointsTest.java new file mode 100644 index 0000000..5bf8c20 --- /dev/null +++ b/client/src/test/java/client/EndpointsTest.java @@ -0,0 +1,54 @@ +package client; + +import client.utils.ConfigService; +import client.utils.server.Endpoints; +import client.utils.server.ServerUtils; +import commons.Ingredient; +import commons.Recipe; +import commons.RecipeIngredient; +import commons.VagueIngredient; +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.Path; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class EndpointsTest { + static Endpoints endpoints; + + @TempDir + Path tempDir; + + @BeforeEach + void setup() throws IOException, InterruptedException { + var configPath = tempDir.resolve("configServiceTest.json"); + var configService = new ConfigService(configPath); + configService.getConfig().setServerUrl("http://localhost:8080"); + + endpoints = new Endpoints(configService); + } + + @Test + public void testEndpointForRecipe() { + var endpoint = endpoints.fetchRecipe(20); + var request = endpoint.build(); + + assertEquals("GET", request.method()); + assertEquals("http://localhost:8080/api/recipe/20", request.uri().toString()); + } + + + @Test + public void testEndpointForIngredientUsage() { + var endpoint = endpoints.fetchIngredientUsage(20); + var request = endpoint.build(); + + assertEquals("GET", request.method()); + assertEquals("http://localhost:8080/api/ingredients/20/usage", request.uri().toString()); + } +}