From f0ec12f982f5466558fd2488a11c22acbb3f3200 Mon Sep 17 00:00:00 2001
From: HSoreide <sofie.scisly@gmail.com>
Date: Wed, 19 Apr 2023 17:31:48 +0200
Subject: [PATCH] Add JavaDock and restructure unit tests

---
 .../idatt1002/demo/data/recipes/FoodItem.java | 15 +++-
 .../demo/data/recipes/Ingredient.java         | 12 +--
 .../demo/data/recipes/IngredientsAtHand.java  | 26 +++++--
 .../demo/data/recipes/MeasuringUnit.java      | 13 ++++
 .../demo/data/recipes/RecipeIngredient.java   |  2 +-
 .../data/recipes/IngredientsAtHandTest.java   | 77 +++++++++++++++----
 6 files changed, 112 insertions(+), 33 deletions(-)

diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FoodItem.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FoodItem.java
index 8221af52..8dfa3710 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FoodItem.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/FoodItem.java
@@ -1,5 +1,13 @@
 package no.ntnu.idatt1002.demo.data.recipes;
 
+/**
+ * The FoodItem enum class defines a set of food types that may be serving as ingredients in recipes and that the
+ * user may, or may not, have available in the cupboard, fridge or freezer. The label is a strict lower case
+ * version of the constant value where the underscore is replaced by a space. This value is used at the front-end
+ * to represent each enum value.
+ *
+ * @author hannesofie
+ */
 public enum FoodItem {
 
     ONION("onion"),
@@ -66,10 +74,13 @@ public enum FoodItem {
     SAUSAGE("sausage"),
     DRY_OREGANO("dry oregano");
 
-
-
     public final String label;
 
+    /**
+     * The constructor of the enum constants takes in a string label and assigns it to its respective constant.
+     * The label is used for representation in texts and lists at the frontend of the application.
+     * @param label A lower-case and readable string representation of the enum constant.
+     */
     FoodItem(String label) {
         this.label = label;
     }
diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Ingredient.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Ingredient.java
index 50c85057..76a5b9e7 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Ingredient.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/Ingredient.java
@@ -1,7 +1,5 @@
 package no.ntnu.idatt1002.demo.data.recipes;
 
-import java.util.Objects;
-
 /**
  * The Ingredient class represents an ingredient that can be part of a recipe in real life and/or
  * be available to the user in real life. When the ingredient is part of a recipe, the subclass called
@@ -42,6 +40,12 @@ public class Ingredient {
         return foodType;
     }
 
+    /**
+     * The method takes in a value of the FoodItem enum and sets the ingredient's foodType field equal to this constant.
+     * If null is given an IllegalArgumentException is thrown.
+     *
+     * @param foodType The type of food to assign the ingredient to.
+     */
     public void setFoodType(FoodItem foodType) {
         if(foodType == null) {
             throw new IllegalArgumentException("The food type must be set to a valid value of FoodItem.");
@@ -116,8 +120,4 @@ public class Ingredient {
         return Double.compare(that.amount, amount) == 0 && foodType == that.foodType && unit == that.unit;
     }
 
-/*    @Override
-    public int hashCode() {
-        return Objects.hash(foodType, amount, unit);
-    }*/
 }
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 041c059c..b09e355e 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
@@ -3,15 +3,20 @@ package no.ntnu.idatt1002.demo.data.recipes;
 import java.util.ArrayList;
 import java.util.List;
 
+/**
+ * The class holds a collection of FoodItem constants in an ArrayList that represents the groceries that the
+ * user has available in real life. The class provides methods to add and remove food types from the collection
+ * as well as checking if a given food type is available, i.e. is part of the collection.
+ *
+ * @author hannesofie
+ */
 public class IngredientsAtHand {
-//TODO: go through JavaDoc after refactoring.
-    //TODO: Make into a set
 
     private final ArrayList<FoodItem> ingredientsAtHand = new ArrayList<>();
 
     /**
-     * The method returns the collection of ingredients at hand as an arraylist of ingredient objects.
-     * @return The collection of ingredients at hand to the user.
+     * The method returns the collection of ingredients at hand as an arraylist of FoodItem enum constants.
+     * @return The collection of food types at hand to the user.
      */
     public List<FoodItem> getIngredientsAtHand() {
         return ingredientsAtHand;
@@ -19,7 +24,7 @@ public class IngredientsAtHand {
 
     /**
      * The method takes in an ingredient object and adds it to the collection of ingredients at hand.
-     * @param ingredient The ingredient object to add to the collection of ingredients at hand.
+     * @param ingredient The food type to add to the collection of ingredients at hand.
      */
     public void addIngredient(FoodItem ingredient) {
         if(!this.atHand(ingredient)) {
@@ -27,7 +32,13 @@ public class IngredientsAtHand {
         }
     }
 
-    //TODO: Unit tests and javadoc
+    /**
+     * The method takes in a constant of the FoodType enum class and checks if it is present in the collection.
+     * If it is present, true is returned, otherwise false.
+     * @param foodItem A constant value of the FoodItem enum class to check for in the collection
+     *                 of ingredients at hand.
+     * @return True if the food type is at hand, otherwise false.
+     */
     public boolean atHand(FoodItem foodItem) {
         return ingredientsAtHand.stream().anyMatch( (in) -> in.equals(foodItem));
     }
@@ -37,12 +48,11 @@ public class IngredientsAtHand {
      * The method takes in a value of the FoodItem enum as a parameter and removes it from the collection of
      * ingredients at hand if it exists and returns true. If no ingredient of the given type was found in the
      * collection, false is returned.
-     * @param ingredientType What type of food the ingredient is.
+     * @param ingredientType What type of food to remove from the list of ingredients at hand.
      * @return True if the ingredient was found among the ingredients at hand and removed, false otherwise.
      */
     public boolean removeIngredient(FoodItem ingredientType) {
         return ingredientsAtHand.remove(ingredientType);
     }
 
-
 }
diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/MeasuringUnit.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/MeasuringUnit.java
index a23bb77c..5f0165ad 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/MeasuringUnit.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/MeasuringUnit.java
@@ -1,5 +1,13 @@
 package no.ntnu.idatt1002.demo.data.recipes;
 
+/**
+ * The enum class defines a set of valid units of measurements related to the ingredients and recipes.
+ * The label property of each constant provides a lower-case representation of each unit that can be used in
+ * presentation to the user.
+ *
+ * @author hannesofie
+ */
+
 public enum MeasuringUnit {
 
     DL("dl."),
@@ -16,6 +24,11 @@ public enum MeasuringUnit {
 
     public final String label;
 
+    /**
+     * The constructor of each enum constant takes in a string representation of the constant that is more suited for
+     * presentation at the frontend.
+     * @param label Lower case representation of the unit of measure.
+     */
     MeasuringUnit(String label) {
         this.label = label;
     }
diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeIngredient.java b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeIngredient.java
index 3c6c3f17..eef4686f 100644
--- a/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeIngredient.java
+++ b/src/main/java/no/ntnu/idatt1002/demo/data/recipes/RecipeIngredient.java
@@ -4,7 +4,7 @@ package no.ntnu.idatt1002.demo.data.recipes;
  * The RecipeIngredient class is an extension of the Ingredient class  used as part of recipes
  * that also stored the boolean value 'atHand'. This value can be set to true whenever the given ingredient,
  * being part of a recipe, is also at hand to the user. Ingredients that the user has available are stored in
- * the IngredientsAtHandDetailed class.
+ * the IngredientsAtHand class.
  *
  * @author hannesofie
  */
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 39a009ad..d2cee2ad 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
@@ -2,6 +2,7 @@ package no.ntnu.idatt1002.demo.data.recipes;
 
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.*;
@@ -18,26 +19,70 @@ class IngredientsAtHandTest {
     }
 
 
-    @Test
-    @DisplayName("The atHand method returns true/false correctly.")
-    void atHandReturnsSuccessfully() {
-        assertTrue(ingredientsAtHand.atHand(FoodItem.LEMON));
-        assertFalse(ingredientsAtHand.atHand(FoodItem.YELLOW_CHEESE));
-    }
+    @Nested
+    @DisplayName("Test addition of food types to the collection.")
+    class testAddIngredient {
+        int numberOfIngredientsBefore;
+
+        @BeforeEach
+        void getNumberOfIngredientsAtHand() {
+            numberOfIngredientsBefore = ingredientsAtHand.getIngredientsAtHand().size();
+        }
+
+        @Test
+        @DisplayName("A valid food type is added successfully.")
+        void addValidFoodType() {
+            ingredientsAtHand.addIngredient(FoodItem.POTATO);
+            assertEquals(numberOfIngredientsBefore+1, ingredientsAtHand.getIngredientsAtHand().size());
+        }
+
+        @Test
+        @DisplayName("An invalid(null) food type is not added to the collection.")
+        void notAddNullFoodType() {
+            ingredientsAtHand.addIngredient(null);
+            assertEquals(numberOfIngredientsBefore, 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("An duplicate food type is not added to the collection.")
+        void notAddDuplicateFoodType() {
+            ingredientsAtHand.addIngredient(FoodItem.LEMON);
+            assertEquals(numberOfIngredientsBefore, 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));
+    @Nested
+    @DisplayName("Test the atHand method of the ingredients at hand class. ")
+    class testAtHandMethod {
+        @Test
+        @DisplayName("The atHand method returns true correctly when the food type is at hand.")
+        void atHandReturnsTrueSuccessfully() {
+            assertTrue(ingredientsAtHand.atHand(FoodItem.LEMON));
+        }
+
+        @Test
+        @DisplayName("The atHand method returns false correctly when the food type is not at hand.")
+        void atHandReturnsFalseSuccessfully() {
+            assertFalse(ingredientsAtHand.atHand(FoodItem.YELLOW_CHEESE));
+        }
     }
 
+    @Nested
+    @DisplayName("Test the removeIngredient method of the ingredients at hand class. ")
+    class testRemoveIngredientMethod {
+        @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
-- 
GitLab