fix: resolve merge conflicts
This commit is contained in:
parent
5388c52aaa
commit
61ad8a1ea9
10 changed files with 151 additions and 77 deletions
|
|
@ -23,7 +23,7 @@ import client.scenes.recipe.IngredientListCtrl;
|
||||||
import client.scenes.recipe.RecipeStepListCtrl;
|
import client.scenes.recipe.RecipeStepListCtrl;
|
||||||
import client.utils.ConfigService;
|
import client.utils.ConfigService;
|
||||||
import client.utils.LocaleManager;
|
import client.utils.LocaleManager;
|
||||||
import client.utils.ServerUtils;
|
import client.utils.server.ServerUtils;
|
||||||
import client.utils.WebSocketDataService;
|
import client.utils.WebSocketDataService;
|
||||||
import client.utils.WebSocketUtils;
|
import client.utils.WebSocketUtils;
|
||||||
import com.google.inject.Binder;
|
import com.google.inject.Binder;
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ package client;
|
||||||
|
|
||||||
import client.scenes.FoodpalApplicationCtrl;
|
import client.scenes.FoodpalApplicationCtrl;
|
||||||
import client.scenes.MainCtrl;
|
import client.scenes.MainCtrl;
|
||||||
import client.utils.ServerUtils;
|
import client.utils.server.ServerUtils;
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import client.utils.ConfigService;
|
||||||
import client.utils.DefaultValueFactory;
|
import client.utils.DefaultValueFactory;
|
||||||
import client.utils.LocaleAware;
|
import client.utils.LocaleAware;
|
||||||
import client.utils.LocaleManager;
|
import client.utils.LocaleManager;
|
||||||
import client.utils.ServerUtils;
|
import client.utils.server.ServerUtils;
|
||||||
import client.utils.WebSocketDataService;
|
import client.utils.WebSocketDataService;
|
||||||
import client.utils.WebSocketUtils;
|
import client.utils.WebSocketUtils;
|
||||||
import commons.Ingredient;
|
import commons.Ingredient;
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package client.scenes;
|
||||||
import client.utils.ConfigService;
|
import client.utils.ConfigService;
|
||||||
import client.utils.LocaleAware;
|
import client.utils.LocaleAware;
|
||||||
import client.utils.LocaleManager;
|
import client.utils.LocaleManager;
|
||||||
import client.utils.ServerUtils;
|
import client.utils.server.ServerUtils;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import commons.Recipe;
|
import commons.Recipe;
|
||||||
import javafx.animation.PauseTransition;
|
import javafx.animation.PauseTransition;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package client.scenes.recipe;
|
package client.scenes.recipe;
|
||||||
|
|
||||||
import client.utils.ServerUtils;
|
import client.utils.server.ServerUtils;
|
||||||
import commons.Ingredient;
|
import commons.Ingredient;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import client.utils.ConfigService;
|
||||||
import client.utils.LocaleAware;
|
import client.utils.LocaleAware;
|
||||||
import client.utils.LocaleManager;
|
import client.utils.LocaleManager;
|
||||||
import client.utils.PrintExportService;
|
import client.utils.PrintExportService;
|
||||||
import client.utils.ServerUtils;
|
import client.utils.server.ServerUtils;
|
||||||
import client.utils.WebSocketDataService;
|
import client.utils.WebSocketDataService;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import commons.Recipe;
|
import commons.Recipe;
|
||||||
|
|
|
||||||
87
client/src/main/java/client/utils/server/Endpoints.java
Normal file
87
client/src/main/java/client/utils/server/Endpoints.java
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
package client.utils.server;
|
||||||
|
|
||||||
|
import client.utils.ConfigService;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Endpoints {
|
||||||
|
private final ConfigService configService;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public Endpoints(ConfigService configService) {
|
||||||
|
this.configService = configService;
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpRequest.Builder http(String url) {
|
||||||
|
return HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(url))
|
||||||
|
.header("Content-Type", "application/json");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String baseUrl() {
|
||||||
|
return this.configService.getConfig().getServerUrl();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequest.Builder fetchAllRecipes(List<String> locales) {
|
||||||
|
String url = this.baseUrl() + "/recipes" + "?locales=" + String.join(",", locales);
|
||||||
|
|
||||||
|
return this.http(url).GET();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequest.Builder fetchRecipe(long recipeId) {
|
||||||
|
String url = this.baseUrl() + "/recipe/" + recipeId;
|
||||||
|
|
||||||
|
return this.http(url).GET();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequest.Builder createNewRecipe(HttpRequest.BodyPublisher body) {
|
||||||
|
String url = this.baseUrl() + "/recipe/new";
|
||||||
|
|
||||||
|
return this.http(url).PUT(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequest.Builder updateRecipe(long recipeId, HttpRequest.BodyPublisher body) {
|
||||||
|
String url = this.baseUrl() + "/recipe/" + recipeId;
|
||||||
|
|
||||||
|
return this.http(url).POST(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequest.Builder deleteRecipe(long recipeId) {
|
||||||
|
String url = this.baseUrl() + "/recipe/" + recipeId;
|
||||||
|
|
||||||
|
return this.http(url).DELETE();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequest.Builder fetchIngredientUsage(long ingredientId) {
|
||||||
|
String url = this.baseUrl() + "/ingredients/" + ingredientId + "/usage";
|
||||||
|
|
||||||
|
return this.http(url).GET();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequest.Builder deleteIngredient(long ingredientId) {
|
||||||
|
String url = this.baseUrl() + "/ingredients/" + ingredientId;
|
||||||
|
|
||||||
|
return this.http(url).DELETE();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequest.Builder getIngredients() {
|
||||||
|
String url = this.baseUrl() + "/ingredients";
|
||||||
|
|
||||||
|
return this.http(url).GET();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequest.Builder createIngredient(HttpRequest.BodyPublisher body) {
|
||||||
|
String url = this.baseUrl() + "/ingredients";
|
||||||
|
|
||||||
|
return this.http(url).POST(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequest.Builder updateIngredient(long ingredientId, HttpRequest.BodyPublisher body) {
|
||||||
|
String url = this.baseUrl() + "/ingredients/" + ingredientId;
|
||||||
|
|
||||||
|
return this.http(url).method("PATCH", body);
|
||||||
|
}
|
||||||
|
}
|
||||||
282
client/src/main/java/client/utils/server/ServerUtils.java
Normal file
282
client/src/main/java/client/utils/server/ServerUtils.java
Normal file
|
|
@ -0,0 +1,282 @@
|
||||||
|
package client.utils.server;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import commons.Ingredient;
|
||||||
|
import commons.Recipe;
|
||||||
|
import commons.RecipeIngredient;
|
||||||
|
import jakarta.ws.rs.ProcessingException;
|
||||||
|
import jakarta.ws.rs.client.ClientBuilder;
|
||||||
|
import org.glassfish.jersey.client.ClientConfig;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ConnectException;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
|
||||||
|
|
||||||
|
|
||||||
|
public class ServerUtils {
|
||||||
|
private Logger logger = Logger.getLogger(ServerUtils.class.getName());
|
||||||
|
private final HttpClient client;
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
private final int statusOK = 200;
|
||||||
|
|
||||||
|
private final Endpoints endpoints;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ServerUtils(HttpClient client, Endpoints endpoints) {
|
||||||
|
this.client = client;
|
||||||
|
this.endpoints = endpoints;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all the recipes from the backend.
|
||||||
|
* @return a JSON string with all the recipes
|
||||||
|
*/
|
||||||
|
public List<Recipe> getRecipes(List<String> locales) throws IOException, InterruptedException {
|
||||||
|
HttpRequest request = this.endpoints.fetchAllRecipes(locales).build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
if (response.statusCode() != statusOK) {
|
||||||
|
throw new IOException("No recipe to get. Server responds with " + response.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Recipe> list = objectMapper.readValue(response.body(), new TypeReference<List<Recipe>>() {});
|
||||||
|
|
||||||
|
logger.info("Received response from server: " + list);
|
||||||
|
return list; // JSON string-> List<Recipe> (Jackson)
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Recipe> getRecipesFiltered(String filter, List<String> locales) throws IOException, InterruptedException {
|
||||||
|
// TODO: implement filtering on server side
|
||||||
|
return this.getRecipes(locales);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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(long id) throws IOException, InterruptedException {
|
||||||
|
HttpRequest request = this.endpoints.fetchRecipe(id).build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
if(response.statusCode() != statusOK){
|
||||||
|
throw new IOException("failed finding recipe with id: "+ id + " body: " + response.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
return objectMapper.readValue(response.body(),Recipe.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 unique
|
||||||
|
List<Recipe> allRecipes = getRecipes(List.of());
|
||||||
|
newRecipe.setId(null); // otherwise the id is the same as the original, and that's wrong
|
||||||
|
// now that each recipeIngredient has its own ID in the database,
|
||||||
|
// we set that to null too to force a new persist value on the server
|
||||||
|
newRecipe.getIngredients().forEach(ingredient -> ingredient.setId(null));
|
||||||
|
|
||||||
|
int version = 1;
|
||||||
|
String originalName = newRecipe.getName();
|
||||||
|
while (allRecipes.stream().anyMatch(r -> r.getName().equals(newRecipe.getName()))) {
|
||||||
|
String newName = originalName + "(" + version++ + ")";
|
||||||
|
newRecipe.setName(newName);
|
||||||
|
}
|
||||||
|
|
||||||
|
String json = objectMapper.writeValueAsString(newRecipe);
|
||||||
|
|
||||||
|
//Recipe to backend
|
||||||
|
HttpRequest request =
|
||||||
|
this.endpoints.createNewRecipe(HttpRequest.BodyPublishers.ofString(json)).build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
if(response.statusCode() != statusOK){
|
||||||
|
throw new IOException("Failed to add Recipe: " + newRecipe.toDetailedString() + "body: " + response.body());
|
||||||
|
}
|
||||||
|
return objectMapper.readValue(response.body(),Recipe.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a recipe.
|
||||||
|
* @param id the recipe that get deleted
|
||||||
|
*/
|
||||||
|
public void deleteRecipe(long id) throws IOException, InterruptedException {
|
||||||
|
HttpRequest request = this.endpoints.deleteRecipe(id).build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
if(response.statusCode() != statusOK){
|
||||||
|
throw new IOException("Failed removing recipe with id: " + id + "body: " + response.body());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clones a recipe.
|
||||||
|
* @param id the id of the recipe to be cloned
|
||||||
|
* @return a duplicated recipe of the given recipe
|
||||||
|
*/
|
||||||
|
public Recipe cloneRecipe(long id) throws IOException, InterruptedException {
|
||||||
|
//Get the recipe
|
||||||
|
HttpRequest request = this.endpoints.fetchRecipe(id).build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
//200 is the status code for success, other codes can mean there is no recipe to clone
|
||||||
|
if(response.statusCode() != statusOK){
|
||||||
|
throw new IOException("Failed to get recipe to clone with id: " + id + "body: " + response.body());
|
||||||
|
}
|
||||||
|
// recipe exists so you can make a "new" recipe aka the clone
|
||||||
|
Recipe recipe = objectMapper.readValue(response.body(), Recipe.class);
|
||||||
|
return addRecipe(recipe);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isServerAvailable() {
|
||||||
|
try {
|
||||||
|
ClientBuilder.newClient(new ClientConfig()) //
|
||||||
|
.target(this.endpoints.baseUrl()) //
|
||||||
|
.request(APPLICATION_JSON) //
|
||||||
|
.get();
|
||||||
|
} catch (ProcessingException e) {
|
||||||
|
if (e.getCause() instanceof ConnectException) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRecipeName(String name) throws IOException, InterruptedException {
|
||||||
|
Recipe newRecipe = new Recipe();
|
||||||
|
newRecipe.setName(name);
|
||||||
|
addRecipe(newRecipe);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRecipeIngredient(Recipe recipe, RecipeIngredient ingredient) throws IOException, InterruptedException {
|
||||||
|
List<RecipeIngredient> ingredients = new ArrayList<>(recipe.getIngredients());
|
||||||
|
ingredients.add(ingredient);
|
||||||
|
recipe.setIngredients(ingredients);
|
||||||
|
|
||||||
|
updateRecipe(recipe);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateRecipe(Recipe recipe) throws IOException, InterruptedException {
|
||||||
|
String json = objectMapper.writeValueAsString(recipe);
|
||||||
|
|
||||||
|
HttpRequest request = this.endpoints.updateRecipe(recipe.getId(), HttpRequest.BodyPublishers.ofString(json))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
if(response.statusCode() != statusOK){
|
||||||
|
throw new IOException("Failed to update recipe: " + recipe.toDetailedString() + "body: " + response.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
objectMapper.readValue(response.body(), Recipe.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRecipeStep(Recipe recipe, String preparationStep) throws IOException, InterruptedException {
|
||||||
|
List<String> preparationSteps = new ArrayList<>(recipe.getPreparationSteps());
|
||||||
|
preparationSteps.add(preparationStep);
|
||||||
|
recipe.setPreparationSteps(preparationSteps);
|
||||||
|
|
||||||
|
updateRecipe(recipe);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the amount of recipes this ingredient is being used in.
|
||||||
|
* @param ingredientId The queried ingredient's ID.
|
||||||
|
* @return The amount of recipes the ingredient is used in.
|
||||||
|
* @throws IOException if server query failed.
|
||||||
|
* @throws InterruptedException if operation is interrupted.
|
||||||
|
*/
|
||||||
|
public long getIngredientUsage(long ingredientId) throws IOException, InterruptedException {
|
||||||
|
HttpRequest request = this.endpoints.fetchIngredientUsage(ingredientId).build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
if (response.statusCode() != statusOK) {
|
||||||
|
throw new IOException("Failed to get usage for ingredient with id: " + ingredientId
|
||||||
|
+ " body: " + response.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
record IngredientUsageResponse(long ingredientId, long usedInRecipes) {}
|
||||||
|
IngredientUsageResponse usage =
|
||||||
|
objectMapper.readValue(response.body(), IngredientUsageResponse.class);
|
||||||
|
|
||||||
|
return usage.usedInRecipes();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void deleteIngredient(long ingredientId) throws IOException, InterruptedException {
|
||||||
|
// Send delete request to remove the ingredient
|
||||||
|
HttpRequest request = this.endpoints.deleteIngredient(ingredientId).build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
if (response.statusCode() != statusOK) {
|
||||||
|
throw new IOException("Failed to delete ingredient with id: " + ingredientId + " body: " + response.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Successfully deleted ingredient with id: " + ingredientId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//retrieves the list of ingredients saved to backend
|
||||||
|
|
||||||
|
public List<Ingredient> getIngredients() throws IOException, InterruptedException {
|
||||||
|
HttpRequest request = this.endpoints.getIngredients().build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
if (response.statusCode() != statusOK) {
|
||||||
|
throw new IOException("Failed to fetch ingredients. Server responds with: " + response.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
return objectMapper.readValue(response.body(), new com.fasterxml.jackson.core.type.TypeReference<List<Ingredient>>() {});
|
||||||
|
}
|
||||||
|
|
||||||
|
//creates new ingredients in the ingredient list
|
||||||
|
|
||||||
|
public Ingredient createIngredient(String name) throws IOException, InterruptedException {
|
||||||
|
Ingredient ingredient = new Ingredient(name, 0.0, 0.0, 0.0);
|
||||||
|
String json = objectMapper.writeValueAsString(ingredient);
|
||||||
|
|
||||||
|
HttpRequest request = this.endpoints.createIngredient(HttpRequest.BodyPublishers.ofString(json))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
if (response.statusCode() != statusOK) {
|
||||||
|
throw new IOException("Failed to create ingredient. Server responds with: " + response.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
return objectMapper.readValue(response.body(), Ingredient.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Ingredient updateIngredient(Ingredient newIngredient) throws IOException, InterruptedException {
|
||||||
|
logger.info("PATCH ingredient with id: " + newIngredient.getId());
|
||||||
|
HttpRequest request = this.endpoints.updateIngredient(
|
||||||
|
newIngredient.getId(),
|
||||||
|
HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(newIngredient))
|
||||||
|
).build();
|
||||||
|
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
if (response.statusCode() != statusOK) {
|
||||||
|
throw new IOException("Failed to update ingredient with id: " + newIngredient.getId() + " body: " + response.body());
|
||||||
|
}
|
||||||
|
return objectMapper.readValue(response.body(), Ingredient.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package client;
|
package client;
|
||||||
|
|
||||||
import client.utils.ServerUtils;
|
import client.utils.ConfigService;
|
||||||
|
import client.utils.server.Endpoints;
|
||||||
|
import client.utils.server.ServerUtils;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
|
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
|
||||||
import commons.FormalIngredient;
|
import commons.FormalIngredient;
|
||||||
|
|
@ -10,9 +12,11 @@ import commons.RecipeIngredient;
|
||||||
import commons.VagueIngredient;
|
import commons.VagueIngredient;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -38,10 +42,18 @@ class ServerUtilsMockTest {
|
||||||
|
|
||||||
static final List<String> testPrepSteps = List.of("1. do smth", "2. do smth else");
|
static final List<String> testPrepSteps = List.of("1. do smth", "2. do smth else");
|
||||||
|
|
||||||
|
@TempDir
|
||||||
|
Path tempDir;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
|
var configPath = tempDir.resolve("configServiceTest.json");
|
||||||
|
var configService = new ConfigService(configPath);
|
||||||
|
|
||||||
|
var endpoints = new Endpoints(configService);
|
||||||
|
|
||||||
objectMapper = new ObjectMapper();
|
objectMapper = new ObjectMapper();
|
||||||
serverUtils = new ServerUtils(HttpClient.newHttpClient());
|
serverUtils = new ServerUtils(HttpClient.newHttpClient(), endpoints);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package client;
|
package client;
|
||||||
|
|
||||||
import client.utils.ServerUtils;
|
import client.utils.ConfigService;
|
||||||
|
import client.utils.server.Endpoints;
|
||||||
|
import client.utils.server.ServerUtils;
|
||||||
import commons.Ingredient;
|
import commons.Ingredient;
|
||||||
import commons.Recipe;
|
import commons.Recipe;
|
||||||
import commons.RecipeIngredient;
|
import commons.RecipeIngredient;
|
||||||
|
|
@ -9,9 +11,11 @@ import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Assumptions;
|
import org.junit.jupiter.api.Assumptions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
@ -31,10 +35,17 @@ class ServerUtilsTest {
|
||||||
);
|
);
|
||||||
final int fakeId = -1; // If suppose ID's are only positive
|
final int fakeId = -1; // If suppose ID's are only positive
|
||||||
|
|
||||||
|
@TempDir
|
||||||
|
Path tempDir;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() throws IOException, InterruptedException {
|
void setup() throws IOException, InterruptedException {
|
||||||
// FIXME: Prefer Guice-provided instance via a unit test Module.
|
var configPath = tempDir.resolve("configServiceTest.json");
|
||||||
dv = new ServerUtils(HttpClient.newHttpClient());
|
var configService = new ConfigService(configPath);
|
||||||
|
configService.getConfig().setServerUrl("http://localhost:8080");
|
||||||
|
|
||||||
|
var endpoints = new Endpoints(configService);
|
||||||
|
dv = new ServerUtils(HttpClient.newHttpClient(), endpoints);
|
||||||
|
|
||||||
Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available");
|
Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue