Skip to content
Snippets Groups Projects
Commit f0ec12f9 authored by HSoreide's avatar HSoreide
Browse files

Add JavaDock and restructure unit tests

parent d918f6dd
No related branches found
No related tags found
2 merge requests!42Hs frontend recipes,!41Hs frontend recipes
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;
}
......
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);
}*/
}
......@@ -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);
}
}
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;
}
......
......@@ -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
*/
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment