more tests and debugging
This commit is contained in:
parent
0df654df3c
commit
b4d6b78656
2 changed files with 37 additions and 16 deletions
|
|
@ -51,8 +51,7 @@ public class ServerUtils {
|
||||||
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 found with this id");
|
throw new IOException("No recipe found with this id");
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return objectMapper.readValue(response.body(),Recipe.class);
|
return objectMapper.readValue(response.body(),Recipe.class);
|
||||||
|
|
@ -62,21 +61,19 @@ public class ServerUtils {
|
||||||
* Adds a recipe to the backend
|
* Adds a recipe to the backend
|
||||||
* @param newRecipe the recipe to be added
|
* @param newRecipe the recipe to be added
|
||||||
* @return a recipe
|
* @return a recipe
|
||||||
|
|
||||||
*/
|
*/
|
||||||
public Recipe addRecipe(Recipe newRecipe) throws IOException, InterruptedException {
|
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();
|
List<Recipe> allRecipes = getRecipes();
|
||||||
|
|
||||||
boolean exists = allRecipes.stream()
|
boolean exists = allRecipes.stream()
|
||||||
.anyMatch(r -> r.getName().equals(newRecipe.getName()));
|
.anyMatch(r -> r.getName().equals(newRecipe.getName()));
|
||||||
|
|
||||||
|
|
||||||
if(exists){
|
if(exists){
|
||||||
int version = 1;
|
int version = 1;
|
||||||
String newName;
|
String newName;
|
||||||
|
|
||||||
// Giving the "new" recipe a unique name
|
// Giving the recipe a unique name
|
||||||
while (true) {
|
while (true) {
|
||||||
newName = newRecipe.getName() + "(" + version + ")";
|
newName = newRecipe.getName() + "(" + version + ")";
|
||||||
String finalNewName = newName;
|
String finalNewName = newName;
|
||||||
|
|
@ -90,12 +87,9 @@ public class ServerUtils {
|
||||||
version++;
|
version++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String json = objectMapper.writeValueAsString(newRecipe);
|
String json = objectMapper.writeValueAsString(newRecipe);
|
||||||
|
|
||||||
|
|
||||||
//Recipe to backend
|
//Recipe to backend
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(URI.create(SERVER + "/recipe/new"))
|
.uri(URI.create(SERVER + "/recipe/new"))
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ 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.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
@ -14,15 +13,20 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||||
class ServerUtilsTest {
|
class ServerUtilsTest {
|
||||||
static ServerUtils dv;
|
static ServerUtils dv;
|
||||||
static Recipe testRecipe;
|
static Recipe testRecipe;
|
||||||
|
final int fakeId = -1; // If suppose ID's are only positive
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() throws IOException, InterruptedException {
|
void setup() throws IOException, InterruptedException {
|
||||||
dv = new ServerUtils();
|
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();
|
Recipe r = new Recipe();
|
||||||
|
|
||||||
r.setName("Tosti"+ System.currentTimeMillis());
|
r.setName("Tosti");
|
||||||
// + 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"));
|
||||||
|
|
@ -33,13 +37,20 @@ class ServerUtilsTest {
|
||||||
@Test
|
@Test
|
||||||
void addRecipeTest() throws IOException, InterruptedException {
|
void addRecipeTest() throws IOException, InterruptedException {
|
||||||
Recipe r = new Recipe();
|
Recipe r = new Recipe();
|
||||||
r.setName("Eggs on toast" + System.currentTimeMillis());
|
r.setName("Eggs on toast");
|
||||||
r.setIngredients(List.of("Bread", "egg", "salt"));
|
r.setIngredients(List.of("Bread", "egg", "salt"));
|
||||||
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 noRecipeToAdd(){
|
||||||
|
Recipe r = new Recipe();
|
||||||
|
assertThrows(IOException.class, () -> dv.addRecipe(r));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getAllRecipesTest() throws IOException, InterruptedException {
|
void getAllRecipesTest() throws IOException, InterruptedException {
|
||||||
List<Recipe> recipes = dv.getRecipes();
|
List<Recipe> recipes = dv.getRecipes();
|
||||||
|
|
@ -52,18 +63,28 @@ class ServerUtilsTest {
|
||||||
@Test
|
@Test
|
||||||
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());
|
||||||
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("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 noRecipeFoundTest() throws IOException, InterruptedException {
|
||||||
|
assertThrows(IOException.class, () -> dv.findId(fakeId));
|
||||||
|
}
|
||||||
|
|
||||||
@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()), "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
|
@Test
|
||||||
|
|
@ -71,8 +92,14 @@ class ServerUtilsTest {
|
||||||
Recipe clone = dv.cloneRecipe(testRecipe.getId());
|
Recipe clone = dv.cloneRecipe(testRecipe.getId());
|
||||||
|
|
||||||
assertNotEquals(clone.getId(), testRecipe.getId(), "The id's should not be equal to each other");
|
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.getIngredients(), testRecipe.getIngredients(), "the ingredients should be the same");
|
||||||
assertEquals(clone.getPreparationSteps(), testRecipe.getPreparationSteps(),"The steps should be the same");
|
assertEquals(clone.getPreparationSteps(), testRecipe.getPreparationSteps(),"The steps should be the same");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void noRecipeToClone(){
|
||||||
|
assertThrows(IOException.class, () -> dv.cloneRecipe(fakeId));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue