diff --git a/client/src/main/java/client/utils/ServerUtils.java b/client/src/main/java/client/utils/ServerUtils.java index cf14fd7..c87777c 100644 --- a/client/src/main/java/client/utils/ServerUtils.java +++ b/client/src/main/java/client/utils/ServerUtils.java @@ -1,23 +1,17 @@ package client.utils; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import commons.Recipe; -import jakarta.ws.rs.ProcessingException; -import jakarta.ws.rs.client.ClientBuilder; -import org.glassfish.jersey.client.ClientConfig; + import java.io.IOException; -import java.io.StringWriter; -import java.net.ConnectException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.List; -import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; public class ServerUtils { private static final String SERVER = "http://localhost:8080/api"; @@ -35,8 +29,8 @@ public class ServerUtils { */ public List getRecipes() throws IOException, InterruptedException { HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(SERVER + "/recipes")) .GET() - .uri(URI.create(SERVER + "/recipe")) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); @@ -51,8 +45,8 @@ public class ServerUtils { */ public Recipe findId(long id) throws IOException, InterruptedException { HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(SERVER +"/recipe/" + id)) .GET() - .uri(URI.create(SERVER +"/recipes/" + id)) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); @@ -64,30 +58,37 @@ public class ServerUtils { return objectMapper.readValue(response.body(),Recipe.class); } - // todo : make an add button - public Recipe addRecipe(Recipe newRecipe) throws IOException, InterruptedException { - StringWriter s = new StringWriter(); - objectMapper.writeValue(s, newRecipe); + /** + * Adds a recipe to the backend + * @param newRecipe the recipe to be added + * @return a recipe + */ + public Recipe addRecipe(Recipe newRecipe) throws IOException, InterruptedException { + String json = objectMapper.writeValueAsString(newRecipe); + + // TODO : check whether the name is unique, else add a number after it +// while(true){ +// if(){ +// System.out.println("Card name already exists pls enter a new one"); +// } +// } + + + //Recipe to backend HttpRequest request = HttpRequest.newBuilder() - .PUT(HttpRequest.BodyPublishers.ofString(s.toString())) .uri(URI.create(SERVER + "/recipe/new")) + .header("Content-Type", "application/json") + .PUT(HttpRequest.BodyPublishers.ofString(json)) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - if(response.statusCode() != 200){ - System.out.println("No recipe to add"); - return null; + if(response.statusCode() != statusOK){ + throw new IOException("No recipe to add"); } return objectMapper.readValue(response.body(),Recipe.class); } -// public Quote addR(Quote quote) { -// return ClientBuilder.newClient(new ClientConfig()) // -// .target(SERVER).path("api/quotes") // -// .request(APPLICATION_JSON) // -// .post(Entity.entity(quote, APPLICATION_JSON), Quote.class); -// } /** * Deletes a recipe @@ -95,15 +96,14 @@ public class ServerUtils { */ public void deleteRecipe(long id) throws IOException, InterruptedException { HttpRequest request = HttpRequest.newBuilder() - .DELETE() .uri(URI.create(SERVER + "/recipe/" + id)) + .DELETE() .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); if(response.statusCode() != statusOK){ - System.out.println("No recipe to delete"); + throw new IOException("No recipe to delete"); } - } /** @@ -114,53 +114,39 @@ public class ServerUtils { public Recipe cloneRecipe(long id) throws IOException, InterruptedException { //Get the recipe HttpRequest request = HttpRequest.newBuilder() - .GET() .uri(URI.create(SERVER + "/recipe/" + id)) + .GET() .build(); HttpResponse 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){ - return null; + throw new IOException("No recipe to clone"); } // recipe exists so you can make a "new" recipe aka the clone Recipe recipe = objectMapper.readValue(response.body(), Recipe.class); - // TODO : Make the names unique, this isn't unique. Cause when cloning again we get the same name + List allRecipes = getRecipes(); int version = 1; - while(version != 1){ // fix the condition!!!!!!! - version++; - } + String newName; - String newName = recipe.getName() + "("+ version + ")"; // I suppose we want unique names? - recipe.setName(newName); + // Giving the "new" recipe a unique name + while (true) { + newName = recipe.getName() + "(" + version + ")"; + String finalNewName = newName; + boolean exists = allRecipes.stream() + .anyMatch(r -> r.getName().equals(finalNewName)); - //From String to Json - StringWriter s = new StringWriter(); - objectMapper.writeValue(s, recipe); - - // Post the clone to backend - HttpRequest request2 = HttpRequest.newBuilder() - .PUT(HttpRequest.BodyPublishers.ofString(s.toString())) - .uri(URI.create(SERVER + "/recipe/new")) - .build(); - - HttpResponse cloneResponse = client.send(request2, HttpResponse.BodyHandlers.ofString()); - - return objectMapper.readValue(cloneResponse.body(),Recipe.class); - } - - public boolean isServerAvailable() { - try { - ClientBuilder.newClient(new ClientConfig()) // - .target(SERVER) // - .request(APPLICATION_JSON) // - .get(); - } catch (ProcessingException e) { - if (e.getCause() instanceof ConnectException) { - return false; + if (!exists){ + break; + }else { + version++; } } - return true; + + recipe.setId(null); // otherwise the id is the same as the original, and that's wrong + recipe.setName(newName); + + return addRecipe(recipe); } } \ No newline at end of file diff --git a/client/src/test/java/client/ServerUtilsTest.java b/client/src/test/java/client/ServerUtilsTest.java index 13960d4..feb9ed6 100644 --- a/client/src/test/java/client/ServerUtilsTest.java +++ b/client/src/test/java/client/ServerUtilsTest.java @@ -3,6 +3,7 @@ package client; import client.utils.ServerUtils; import commons.Recipe; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; @@ -14,21 +15,33 @@ class ServerUtilsTest { static ServerUtils dv; static Recipe testRecipe; - @BeforeAll - static void setup() throws IOException, InterruptedException { + @BeforeEach + void setup() throws IOException, InterruptedException { dv = new ServerUtils(); Recipe r = new Recipe(); - r.setName("Tosti"); + + r.setName("Tosti"+ System.currentTimeMillis()); + // + System.currentTimeMillis() ai came up with this, now everytime the test runs it's a different name + r.setIngredients(List.of("Bread", "Cheese", "Ham")); r.setPreparationSteps(List.of("Step 1:", "Step 2")); testRecipe = dv.addRecipe(r); } + @Test + void addRecipeTest() throws IOException, InterruptedException { + Recipe r = new Recipe(); + r.setName("Eggs on toast" + System.currentTimeMillis()); + r.setIngredients(List.of("Bread", "egg", "salt")); + r.setPreparationSteps(List.of("Step 1:", "Step 2")); + + testRecipe = dv.addRecipe(r); + } @Test - void findAllTest() throws IOException, InterruptedException { + void getAllRecipesTest() throws IOException, InterruptedException { List recipes = dv.getRecipes(); assertNotNull(recipes, "The list should not be null"); @@ -40,25 +53,26 @@ class ServerUtilsTest { void findRecipeWithIDTest() throws IOException, InterruptedException { Recipe r = dv.findId(testRecipe.getId()); assertEquals(testRecipe.getId(), r.getId()); - assertEquals("Tosti", r.getName()); + assertTrue(r.getName().startsWith("Tosti")); // bcs of the + System.currentTimeMillis() +// assertEquals("Tosti", r.getName()); assertIterableEquals(List.of("Bread", "Cheese", "Ham"), r.getIngredients()); - assertIterableEquals(List.of("Step 1: ", "Step 2: "), r.getPreparationSteps()); + assertIterableEquals(List.of("Step 1:", "Step 2"), r.getPreparationSteps()); } @Test void deleteRecipeTest() throws IOException, InterruptedException { dv.deleteRecipe(testRecipe.getId()); - assertNull(dv.findId(testRecipe.getId())); + assertNull(dv.findId(testRecipe.getId()), "The recipe shouldn't exists anymore"); } @Test void cloneRecipeTest() throws IOException, InterruptedException { Recipe clone = dv.cloneRecipe(testRecipe.getId()); - assertNotEquals(clone.getId(), testRecipe.getId()); - assertEquals(clone.getIngredients(), testRecipe.getIngredients()); - assertEquals(clone.getPreparationSteps(), testRecipe.getPreparationSteps()); + assertNotEquals(clone.getId(), testRecipe.getId(), "The id's should not be equal to each other"); + assertEquals(clone.getIngredients(), testRecipe.getIngredients(), "the ingredients should be the same"); + assertEquals(clone.getPreparationSteps(), testRecipe.getPreparationSteps(),"The steps should be the same"); } } \ No newline at end of file