From 1432ee61e09a13f2b0be2b5a3eee90997667c96e Mon Sep 17 00:00:00 2001 From: HSoreide <sofie.scisly@gmail.com> Date: Sat, 15 Apr 2023 21:06:42 +0200 Subject: [PATCH] Tidy up unit tests of detailed and simplified IngredientsAtHand classes --- .../demo/data/recipes/IngredientsAtHand.java | 39 -------- .../IngredientsAtHandDetailedTest.java | 92 +++++++++++++++++++ .../data/recipes/IngredientsAtHandTest.java | 56 ----------- 3 files changed, 92 insertions(+), 95 deletions(-) create mode 100644 src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandDetailedTest.java diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHand.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHand.java index 8c70a319..d3708f3f 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHand.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHand.java @@ -29,45 +29,6 @@ public class IngredientsAtHand { return ingredientsAtHand.stream().anyMatch( (in) -> in.equals(foodItem)); } -/* *//** - * Returns null if no ingredient of the requested type is found in the collection. - * @param ingredientType What type of food the ingredient is. - * @return The ingredient of the specified type found among the ingredients at hand, null otherwise. - *//* - public Ingredient getIngredient(FoodItem ingredientType) { - if(ingredientType == null) return null; - return this.getIngredientsAtHand().stream() - .filter((ingredient) -> ingredient.getFoodType() == ingredientType) - .findFirst().orElse(null); - }*/ - - /** - * The method takes in three parameters. The method first checks if the Ingredient is at hand in the first place. - * If it is, the old amount and unit of this ingredient are replaced by the provided amount and unit if they - * differ. If not, the ingredient is left as is. If the ingredient is not in the collection, - * @param ingredientType What type of food the ingredient is. - * @param amount The amount of the ingredient. - * @return True if Ingredient is successfully altered or added, false if not. - */ -/* public boolean alterIngredient(FoodItem ingredientType, double amount, MeasuringUnit unit) { - //TODO: Consider handling exceptions differently. - if(ingredientsAtHand.stream().anyMatch((ingredient) -> ingredient.getFoodType() == ingredientType)) { - try { - getIngredient(ingredientType).setAmount(amount); - getIngredient(ingredientType).setUnit(unit); - - } catch (IllegalArgumentException e) { - return false; - } - } else { - try { - addIngredient(new Ingredient(ingredientType, amount, unit)); - } catch (IllegalArgumentException e) { - return false; - } - } - return true; - }*/ /** * The method takes in a value of the FoodItem enum as a parameter and removes it from the collection of diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandDetailedTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandDetailedTest.java new file mode 100644 index 00000000..e1e9ad74 --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandDetailedTest.java @@ -0,0 +1,92 @@ +package no.ntnu.idatt1002.demo.data.recipes; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class IngredientsAtHandDetailedTest { + + IngredientsAtHandDetailed ingredientsAtHand = new IngredientsAtHandDetailed(); + + @BeforeEach + void beforeEach() { + ingredientsAtHand.addIngredient(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC)); + ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MILK, 1.5f, MeasuringUnit.L)); + ingredientsAtHand.addIngredient(new Ingredient(FoodItem.MINCED_MEAT, 400, MeasuringUnit.GR)); + } + + @Test + @DisplayName("The getIngredient method returns the correct ingredient.") + void getIngredientReturnsSuccessfully() { + assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC), + ingredientsAtHand.getIngredient(FoodItem.LEMON)); + } + + + + @Test + @DisplayName("The getIngredient method returns null if the ingredient is not in the collection.") + void getIngredientReturnsNullWhenNotFoundOrNull(){ + assertNull(ingredientsAtHand.getIngredient(FoodItem.ONION)); + assertNull(ingredientsAtHand.getIngredient(null)); + } + + + @Test + @DisplayName("Altering ingredient successfully and return true.") + void alterIngredientSuccessfully() { + assertNotEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR), + ingredientsAtHand.getIngredient(FoodItem.ONION)); + + assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ONION, 500, MeasuringUnit.GR)); + + assertEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR), + ingredientsAtHand.getIngredient(FoodItem.ONION)); + + } + + @Test + @DisplayName("Altering ingredient that does not yet exist in collection adds it and returns true.") + void alterNewIngredientAddsIt() { + int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size(); + assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ORANGE, 8, MeasuringUnit.PC)); + + assertEquals(ingredientsAtStart + 1, ingredientsAtHand.getIngredientsAtHand().size()); + } + + @Test + @DisplayName("Attempting to alter ingredient in illegal way does not alter the collection and returns false.") + void alterIngredientUnchangedForInvalidChange() { + assertFalse(ingredientsAtHand.alterIngredient(null, 350, MeasuringUnit.GR)); + assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC), + ingredientsAtHand.getIngredient(FoodItem.LEMON)); + } + + @Test + @DisplayName("Altering an ingredients without changing values, leaves no change on the collection and returns true.") + void alterNothingLeavesCollectionIntact() { + int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size(); + + assertTrue(ingredientsAtHand.alterIngredient(FoodItem.LEMON, 3, MeasuringUnit.PC)); + + assertEquals(ingredientsAtStart, ingredientsAtHand.getIngredientsAtHand().size()); + } + + @Test + @DisplayName("Ingredient is removed successfully and true is returned.") + void removeIngredientSuccessfully() { + int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size(); + assertTrue(ingredientsAtHand.removeIngredient(FoodItem.LEMON)); + assertEquals(ingredientsAtStart-1, ingredientsAtHand.getIngredientsAtHand().size()); + } + + @Test + @DisplayName("Removing ingredient that is not in the collection, leaves it unchanged and returns false.") + void removeIngredientNotInCollection() { + + assertFalse(ingredientsAtHand.removeIngredient(FoodItem.SALSA_SAUCE)); + } + +} \ No newline at end of file diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandTest.java index 162ae41f..39a009ad 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientsAtHandTest.java @@ -17,12 +17,6 @@ class IngredientsAtHandTest { ingredientsAtHand.addIngredient(FoodItem.MINCED_MEAT); } -/* @Test - @DisplayName("The getIngredient method returns the correct ingredient.") - void getIngredientReturnsSuccessfully() { - assertEquals(FoodItem.LEMON), - ingredientsAtHand.getIngredient(FoodItem.LEMON)); - }*/ @Test @DisplayName("The atHand method returns true/false correctly.") @@ -30,56 +24,6 @@ class IngredientsAtHandTest { assertTrue(ingredientsAtHand.atHand(FoodItem.LEMON)); assertFalse(ingredientsAtHand.atHand(FoodItem.YELLOW_CHEESE)); } -/* - - @Test - @DisplayName("The getIngredient method returns null if the ingredient is not in the collection.") - void getIngredientReturnsNullWhenNotFoundOrNull(){ - assertNull(ingredientsAtHand.getIngredient(FoodItem.ONION)); - assertNull(ingredientsAtHand.getIngredient(null)); - } - - - @Test - @DisplayName("Altering ingredient successfully and return true.") - void alterIngredientSuccessfully() { - assertNotEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR), - ingredientsAtHand.getIngredient(FoodItem.ONION)); - - assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ONION, 500, MeasuringUnit.GR)); - - assertEquals(new Ingredient(FoodItem.ONION, 500, MeasuringUnit.GR), - ingredientsAtHand.getIngredient(FoodItem.ONION)); - - } - - @Test - @DisplayName("Altering ingredient that does not yet exist in collection adds it and returns true.") - void alterNewIngredientAddsIt() { - int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size(); - assertTrue(ingredientsAtHand.alterIngredient(FoodItem.ORANGE, 8, MeasuringUnit.PC)); - - assertEquals(ingredientsAtStart + 1, ingredientsAtHand.getIngredientsAtHand().size()); - } - - @Test - @DisplayName("Attempting to alter ingredient in illegal way does not alter the collection and returns false.") - void alterIngredientUnchangedForInvalidChange() { - assertFalse(ingredientsAtHand.alterIngredient(null, 350, MeasuringUnit.GR)); - assertEquals(new Ingredient(FoodItem.LEMON, 3, MeasuringUnit.PC), - ingredientsAtHand.getIngredient(FoodItem.LEMON)); - } - - @Test - @DisplayName("Altering an ingredients without changing values, leaves no change on the collection and returns true.") - void alterNothingLeavesCollectionIntact() { - int ingredientsAtStart = ingredientsAtHand.getIngredientsAtHand().size(); - - assertTrue(ingredientsAtHand.alterIngredient(FoodItem.LEMON, 3, MeasuringUnit.PC)); - - assertEquals(ingredientsAtStart, ingredientsAtHand.getIngredientsAtHand().size()); - } -*/ @Test @DisplayName("Ingredient is removed successfully and true is returned.") -- GitLab