test: endpoints

This commit is contained in:
Natalia Cholewa 2026-01-16 23:57:06 +01:00
commit 6f2400cc50
2 changed files with 71 additions and 10 deletions

View file

@ -25,62 +25,69 @@ public class Endpoints {
return this.configService.getConfig().getServerUrl() + "/api"; return this.configService.getConfig().getServerUrl() + "/api";
} }
public String createApiUrl(String route) {
return this.baseUrl() + route;
}
public HttpRequest.Builder fetchAllRecipes(List<String> locales) { public HttpRequest.Builder fetchAllRecipes(List<String> locales) {
String url = this.baseUrl() + "/recipes" + "?locales=" + String.join(",", locales); String url = this.createApiUrl(
"/recipe" +
"?locales=" + String.join(",", locales)
);
return this.http(url).GET(); return this.http(url).GET();
} }
public HttpRequest.Builder fetchRecipe(long recipeId) { public HttpRequest.Builder fetchRecipe(long recipeId) {
String url = this.baseUrl() + "/recipe/" + recipeId; String url = this.createApiUrl("/recipe/" + recipeId);
return this.http(url).GET(); return this.http(url).GET();
} }
public HttpRequest.Builder createNewRecipe(HttpRequest.BodyPublisher body) { 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); return this.http(url).PUT(body);
} }
public HttpRequest.Builder updateRecipe(long recipeId, HttpRequest.BodyPublisher 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); return this.http(url).POST(body);
} }
public HttpRequest.Builder deleteRecipe(long recipeId) { public HttpRequest.Builder deleteRecipe(long recipeId) {
String url = this.baseUrl() + "/recipe/" + recipeId; String url = this.createApiUrl("/recipe/" + recipeId);
return this.http(url).DELETE(); return this.http(url).DELETE();
} }
public HttpRequest.Builder fetchIngredientUsage(long ingredientId) { 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(); return this.http(url).GET();
} }
public HttpRequest.Builder deleteIngredient(long ingredientId) { public HttpRequest.Builder deleteIngredient(long ingredientId) {
String url = this.baseUrl() + "/ingredients/" + ingredientId; String url = this.createApiUrl("/ingredients/" + ingredientId);
return this.http(url).DELETE(); return this.http(url).DELETE();
} }
public HttpRequest.Builder getIngredients() { public HttpRequest.Builder getIngredients() {
String url = this.baseUrl() + "/ingredients"; String url = this.createApiUrl("/ingredients");
return this.http(url).GET(); return this.http(url).GET();
} }
public HttpRequest.Builder createIngredient(HttpRequest.BodyPublisher body) { public HttpRequest.Builder createIngredient(HttpRequest.BodyPublisher body) {
String url = this.baseUrl() + "/ingredients"; String url = this.createApiUrl("/ingredients");
return this.http(url).POST(body); return this.http(url).POST(body);
} }
public HttpRequest.Builder updateIngredient(long ingredientId, HttpRequest.BodyPublisher 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); return this.http(url).method("PATCH", body);
} }

View file

@ -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());
}
}