Cloned recipes have unique name and some debugging
This commit is contained in:
parent
f8efed44c1
commit
3f3c761356
2 changed files with 69 additions and 69 deletions
|
|
@ -1,23 +1,17 @@
|
||||||
package client.utils;
|
package client.utils;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import commons.Recipe;
|
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.IOException;
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.net.ConnectException;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
|
|
||||||
|
|
||||||
public class ServerUtils {
|
public class ServerUtils {
|
||||||
private static final String SERVER = "http://localhost:8080/api";
|
private static final String SERVER = "http://localhost:8080/api";
|
||||||
|
|
@ -35,8 +29,8 @@ public class ServerUtils {
|
||||||
*/
|
*/
|
||||||
public List<Recipe> getRecipes() throws IOException, InterruptedException {
|
public List<Recipe> getRecipes() throws IOException, InterruptedException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(SERVER + "/recipes"))
|
||||||
.GET()
|
.GET()
|
||||||
.uri(URI.create(SERVER + "/recipe"))
|
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
|
@ -51,8 +45,8 @@ public class ServerUtils {
|
||||||
*/
|
*/
|
||||||
public Recipe findId(long id) throws IOException, InterruptedException {
|
public Recipe findId(long id) throws IOException, InterruptedException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(SERVER +"/recipe/" + id))
|
||||||
.GET()
|
.GET()
|
||||||
.uri(URI.create(SERVER +"/recipes/" + id))
|
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
|
@ -64,30 +58,37 @@ public class ServerUtils {
|
||||||
return objectMapper.readValue(response.body(),Recipe.class);
|
return objectMapper.readValue(response.body(),Recipe.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo : make an add button
|
/**
|
||||||
public Recipe addRecipe(Recipe newRecipe) throws IOException, InterruptedException {
|
* Adds a recipe to the backend
|
||||||
StringWriter s = new StringWriter();
|
* @param newRecipe the recipe to be added
|
||||||
objectMapper.writeValue(s, newRecipe);
|
* @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()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.PUT(HttpRequest.BodyPublishers.ofString(s.toString()))
|
|
||||||
.uri(URI.create(SERVER + "/recipe/new"))
|
.uri(URI.create(SERVER + "/recipe/new"))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.PUT(HttpRequest.BodyPublishers.ofString(json))
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
if(response.statusCode() != 200){
|
if(response.statusCode() != statusOK){
|
||||||
System.out.println("No recipe to add");
|
throw new IOException("No recipe to add");
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return objectMapper.readValue(response.body(),Recipe.class);
|
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
|
* Deletes a recipe
|
||||||
|
|
@ -95,15 +96,14 @@ public class ServerUtils {
|
||||||
*/
|
*/
|
||||||
public void deleteRecipe(long id) throws IOException, InterruptedException {
|
public void deleteRecipe(long id) throws IOException, InterruptedException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.DELETE()
|
|
||||||
.uri(URI.create(SERVER + "/recipe/" + id))
|
.uri(URI.create(SERVER + "/recipe/" + id))
|
||||||
|
.DELETE()
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
if(response.statusCode() != statusOK){
|
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 {
|
public Recipe cloneRecipe(long id) throws IOException, InterruptedException {
|
||||||
//Get the recipe
|
//Get the recipe
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.GET()
|
|
||||||
.uri(URI.create(SERVER + "/recipe/" + id))
|
.uri(URI.create(SERVER + "/recipe/" + id))
|
||||||
|
.GET()
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
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
|
//200 is the status code for success, other codes can mean there is no recipe to clone
|
||||||
if(response.statusCode() != statusOK){
|
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 exists so you can make a "new" recipe aka the clone
|
||||||
Recipe recipe = objectMapper.readValue(response.body(), Recipe.class);
|
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<Recipe> allRecipes = getRecipes();
|
||||||
int version = 1;
|
int version = 1;
|
||||||
while(version != 1){ // fix the condition!!!!!!!
|
String newName;
|
||||||
version++;
|
|
||||||
}
|
|
||||||
|
|
||||||
String newName = recipe.getName() + "("+ version + ")"; // I suppose we want unique names?
|
// Giving the "new" recipe a unique name
|
||||||
recipe.setName(newName);
|
while (true) {
|
||||||
|
newName = recipe.getName() + "(" + version + ")";
|
||||||
|
String finalNewName = newName;
|
||||||
|
boolean exists = allRecipes.stream()
|
||||||
|
.anyMatch(r -> r.getName().equals(finalNewName));
|
||||||
|
|
||||||
//From String to Json
|
if (!exists){
|
||||||
StringWriter s = new StringWriter();
|
break;
|
||||||
objectMapper.writeValue(s, recipe);
|
}else {
|
||||||
|
version++;
|
||||||
// Post the clone to backend
|
|
||||||
HttpRequest request2 = HttpRequest.newBuilder()
|
|
||||||
.PUT(HttpRequest.BodyPublishers.ofString(s.toString()))
|
|
||||||
.uri(URI.create(SERVER + "/recipe/new"))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
HttpResponse<String> 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
recipe.setId(null); // otherwise the id is the same as the original, and that's wrong
|
||||||
|
recipe.setName(newName);
|
||||||
|
|
||||||
|
return addRecipe(recipe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ package client;
|
||||||
import client.utils.ServerUtils;
|
import client.utils.ServerUtils;
|
||||||
import commons.Recipe;
|
import commons.Recipe;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
@ -14,21 +15,33 @@ class ServerUtilsTest {
|
||||||
static ServerUtils dv;
|
static ServerUtils dv;
|
||||||
static Recipe testRecipe;
|
static Recipe testRecipe;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeEach
|
||||||
static void setup() throws IOException, InterruptedException {
|
void setup() throws IOException, InterruptedException {
|
||||||
dv = new ServerUtils();
|
dv = new ServerUtils();
|
||||||
|
|
||||||
Recipe r = new Recipe();
|
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.setIngredients(List.of("Bread", "Cheese", "Ham"));
|
||||||
r.setPreparationSteps(List.of("Step 1:", "Step 2"));
|
r.setPreparationSteps(List.of("Step 1:", "Step 2"));
|
||||||
|
|
||||||
testRecipe = dv.addRecipe(r);
|
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
|
@Test
|
||||||
void findAllTest() throws IOException, InterruptedException {
|
void getAllRecipesTest() throws IOException, InterruptedException {
|
||||||
List<Recipe> recipes = dv.getRecipes();
|
List<Recipe> recipes = dv.getRecipes();
|
||||||
|
|
||||||
assertNotNull(recipes, "The list should not be null");
|
assertNotNull(recipes, "The list should not be null");
|
||||||
|
|
@ -40,25 +53,26 @@ class ServerUtilsTest {
|
||||||
void findRecipeWithIDTest() throws IOException, InterruptedException {
|
void findRecipeWithIDTest() throws IOException, InterruptedException {
|
||||||
Recipe r = dv.findId(testRecipe.getId());
|
Recipe r = dv.findId(testRecipe.getId());
|
||||||
assertEquals(testRecipe.getId(), r.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("Bread", "Cheese", "Ham"), r.getIngredients());
|
||||||
assertIterableEquals(List.of("Step 1: ", "Step 2: "), r.getPreparationSteps());
|
assertIterableEquals(List.of("Step 1:", "Step 2"), r.getPreparationSteps());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteRecipeTest() throws IOException, InterruptedException {
|
void deleteRecipeTest() throws IOException, InterruptedException {
|
||||||
dv.deleteRecipe(testRecipe.getId());
|
dv.deleteRecipe(testRecipe.getId());
|
||||||
|
|
||||||
assertNull(dv.findId(testRecipe.getId()));
|
assertNull(dv.findId(testRecipe.getId()), "The recipe shouldn't exists anymore");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cloneRecipeTest() throws IOException, InterruptedException {
|
void cloneRecipeTest() throws IOException, InterruptedException {
|
||||||
Recipe clone = dv.cloneRecipe(testRecipe.getId());
|
Recipe clone = dv.cloneRecipe(testRecipe.getId());
|
||||||
|
|
||||||
assertNotEquals(clone.getId(), testRecipe.getId());
|
assertNotEquals(clone.getId(), testRecipe.getId(), "The id's should not be equal to each other");
|
||||||
assertEquals(clone.getIngredients(), testRecipe.getIngredients());
|
assertEquals(clone.getIngredients(), testRecipe.getIngredients(), "the ingredients should be the same");
|
||||||
assertEquals(clone.getPreparationSteps(), testRecipe.getPreparationSteps());
|
assertEquals(clone.getPreparationSteps(), testRecipe.getPreparationSteps(),"The steps should be the same");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue