more tests and debugging

This commit is contained in:
Mei Chang van der Werff 2025-11-27 22:46:42 +01:00
commit b4d6b78656
2 changed files with 37 additions and 16 deletions

View file

@ -51,8 +51,7 @@ public class ServerUtils {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if(response.statusCode() != statusOK){
System.out.println("No recipe found with this id");
return null;
throw new IOException("No recipe found with this id");
}
return objectMapper.readValue(response.body(),Recipe.class);
@ -62,21 +61,19 @@ public class ServerUtils {
* 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 unque
//Make sure the name of the newRecipe is unique
List<Recipe> allRecipes = getRecipes();
boolean exists = allRecipes.stream()
.anyMatch(r -> r.getName().equals(newRecipe.getName()));
if(exists){
int version = 1;
String newName;
// Giving the "new" recipe a unique name
// Giving the recipe a unique name
while (true) {
newName = newRecipe.getName() + "(" + version + ")";
String finalNewName = newName;
@ -90,12 +87,9 @@ public class ServerUtils {
version++;
}
}
}
String json = objectMapper.writeValueAsString(newRecipe);
//Recipe to backend
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(SERVER + "/recipe/new"))

View file

@ -2,7 +2,6 @@ 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;
@ -14,15 +13,20 @@ import static org.junit.jupiter.api.Assertions.*;
class ServerUtilsTest {
static ServerUtils dv;
static Recipe testRecipe;
final int fakeId = -1; // If suppose ID's are only positive
@BeforeEach
void setup() throws IOException, InterruptedException {
dv = new ServerUtils();
//Making sure there is no recipe in the backend yet
for (Recipe recipe : dv.getRecipes()) {
dv.deleteRecipe(recipe.getId());
}
Recipe r = new Recipe();
r.setName("Tosti"+ System.currentTimeMillis());
// + System.currentTimeMillis() ai came up with this, now everytime the test runs it's a different name
r.setName("Tosti");
r.setIngredients(List.of("Bread", "Cheese", "Ham"));
r.setPreparationSteps(List.of("Step 1:", "Step 2"));
@ -33,13 +37,20 @@ class ServerUtilsTest {
@Test
void addRecipeTest() throws IOException, InterruptedException {
Recipe r = new Recipe();
r.setName("Eggs on toast" + System.currentTimeMillis());
r.setName("Eggs on toast");
r.setIngredients(List.of("Bread", "egg", "salt"));
r.setPreparationSteps(List.of("Step 1:", "Step 2"));
testRecipe = dv.addRecipe(r);
}
@Test
void noRecipeToAdd(){
Recipe r = new Recipe();
assertThrows(IOException.class, () -> dv.addRecipe(r));
}
@Test
void getAllRecipesTest() throws IOException, InterruptedException {
List<Recipe> recipes = dv.getRecipes();
@ -52,18 +63,28 @@ class ServerUtilsTest {
@Test
void findRecipeWithIDTest() throws IOException, InterruptedException {
Recipe r = dv.findId(testRecipe.getId());
assertEquals(testRecipe.getId(), r.getId());
assertTrue(r.getName().startsWith("Tosti")); // bcs of the + System.currentTimeMillis()
// assertEquals("Tosti", r.getName());
assertEquals("Tosti", r.getName());
assertIterableEquals(List.of("Bread", "Cheese", "Ham"), r.getIngredients());
assertIterableEquals(List.of("Step 1:", "Step 2"), r.getPreparationSteps());
}
@Test
void noRecipeFoundTest() throws IOException, InterruptedException {
assertThrows(IOException.class, () -> dv.findId(fakeId));
}
@Test
void deleteRecipeTest() throws IOException, InterruptedException {
dv.deleteRecipe(testRecipe.getId());
assertNull(dv.findId(testRecipe.getId()), "The recipe shouldn't exists anymore");
assertThrows(IOException.class, () ->dv.findId(testRecipe.getId()), "The recipe shouldn't exists anymore" );
}
@Test
void noRecipeToDelete() throws IOException, InterruptedException {
assertThrows(IOException.class, () -> dv.deleteRecipe(fakeId));
}
@Test
@ -71,8 +92,14 @@ class ServerUtilsTest {
Recipe clone = dv.cloneRecipe(testRecipe.getId());
assertNotEquals(clone.getId(), testRecipe.getId(), "The id's should not be equal to each other");
assertNotEquals(clone.getName(),testRecipe.getName(),"The name's should not be the same");
assertEquals(clone.getIngredients(), testRecipe.getIngredients(), "the ingredients should be the same");
assertEquals(clone.getPreparationSteps(), testRecipe.getPreparationSteps(),"The steps should be the same");
}
@Test
void noRecipeToClone(){
assertThrows(IOException.class, () -> dv.cloneRecipe(fakeId));
}
}