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

Write unit tests for the IngredientsAtHand class

parent 59d9ae29
No related branches found
No related tags found
1 merge request!7Create food and recipe classes with tests
package no.ntnu.idatt1002.demo.data.recipes;
import java.util.ArrayList;
import java.util.List;
/**
......@@ -11,11 +12,11 @@ import java.util.List;
*/
public class IngredientsAtHand {
private List<Ingredient> ingredientsAtHand;
private final List<Ingredient> ingredientsAtHand = new ArrayList<>();
/**
* The method returns the collection of ingredients at hand as an arraylist of ingredient objects.
* @return
* @return The collection of ingredients at hand to the user.
*/
public List<Ingredient> getIngredientsAtHand() {
return ingredientsAtHand;
......@@ -35,6 +36,7 @@ public class IngredientsAtHand {
* @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);
......@@ -46,18 +48,26 @@ public class IngredientsAtHand {
* 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 void alterIngredient(FoodItem ingredientType, double amount, MeasuringUnit unit) {
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) {
e.printStackTrace();
return false;
}
} else {
addIngredient(new Ingredient(ingredientType, amount, unit));
try {
addIngredient(new Ingredient(ingredientType, amount, unit));
} catch (IllegalArgumentException e) {
return false;
}
}
return true;
}
/**
......
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 IngredientsAtHandTest {
IngredientsAtHand ingredientsAtHand = new IngredientsAtHand();
@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
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