Skip to content
Snippets Groups Projects
RecipeTest.java 4.51 KiB
Newer Older
Karsch Lukas's avatar
Karsch Lukas committed
package mi.hdm.recipes;

import mi.hdm.exceptions.InvalidRecipeException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class RecipeTest {
    private final static List<RecipeComponent> ingredients = ValidObjectsPool.getValidIngredientList();
    private final static Map<RecipeComponent, Integer> recipeMap = new HashMap<>();

    @BeforeAll
    public static void setUpAll() {
        ingredients.forEach(ingredient -> recipeMap.put(ingredient, 100));
    }

    @Test
    @DisplayName("Test invalid name: Recipes with an invalid name (name is null or empty) should not be created")
    void shouldNotCreateRecipeWithInvalidName() {
        assertThrows(InvalidRecipeException.class, () -> new Recipe(null, recipeMap, "Description!", List.of("step 1"), null, 100));
        assertThrows(InvalidRecipeException.class, () -> new Recipe("", recipeMap, "Description!", List.of("step 1"), null, 100));
    }

    @Test
    @DisplayName("Test invalid ingredients: Recipes with an invalid ingredient map (map is null or empty) should not be created")
    void shouldNotCreateRecipeWithInvalidIngredientMap() {
        assertThrows(InvalidRecipeException.class, () -> new Recipe("Valid name", null, "Description!", List.of("step 1"), null, 100));
        assertThrows(InvalidRecipeException.class, () -> new Recipe("Valid name", Map.of(), "Description!", List.of("step 1"), null, 100));
    }

    @Test
    @DisplayName("Test invalid preparation: Recipes with an invalid preparation list (list is null or empty) should not be created")
    void shouldNotCreateRecipeWithInvalidPrep() {
        assertThrows(InvalidRecipeException.class, () -> new Recipe("Valid name", recipeMap, "Description!", null, null, 100));
        assertThrows(InvalidRecipeException.class, () -> new Recipe("Valid name", recipeMap, "Description!", List.of(), null, 100));
    }

    @Test
    @DisplayName("Test invalid prep time: Recipes with an invalid preparation time (number is negative) should not be created")
    void shouldNotCreateRecipeWithInvalidPrepTime() {
        assertThrows(InvalidRecipeException.class, () -> new Recipe("Valid name", recipeMap, "Description!", null, null, -1));
    }

Karsch Lukas's avatar
Karsch Lukas committed
    @Test
    void getNutritionTableWhenSetManually() {
        //given
        NutritionTable nutritionTable = ValidObjectsPool.getValidNutritionTableOne();

        //when
        Recipe underTest = new Recipe("Valid name", recipeMap, "Valid description", List.of("Step 1"), null, 20, nutritionTable);

        //then
        assertEquals(nutritionTable, underTest.getNutritionTable());
    }

    @Test
    void getNutritionTableWhenCalculated() {
        //given
        NutritionTable nutritionTable = ValidObjectsPool.getValidNutritionTableOne();
        Ingredient ingredientOne = new Ingredient(Measurement.GRAM, "Zucker", nutritionTable);

        //when
        Recipe underTest = new Recipe("Valid name", Map.of(ingredientOne, 100), "Valid description", List.of("Step 1"), null, 20);

        //then
        assertEquals(nutritionTable, underTest.getNutritionTable());
    }

    @Test
    void canAddCategory() {
        //given
        Category category = new Category("Category", 0xFF0000);
        NutritionTable nutritionTable = ValidObjectsPool.getValidNutritionTableOne();
        Ingredient ingredientOne = new Ingredient(Measurement.GRAM, "Zucker", nutritionTable);
        Recipe underTest = new Recipe("Valid name", Map.of(ingredientOne, 100), "Valid description", List.of("Step 1"), new ArrayList<>(), 20);

        //when
        underTest.addCategory(category);

        //then
        assertEquals(
                List.of(category),
                underTest.getCategories()
        );
    }

    @Test
    void canAddCategoryWhenNull() {
        //given
        Category category = new Category("Category", 0xFF0000);
        NutritionTable nutritionTable = ValidObjectsPool.getValidNutritionTableOne();
        Ingredient ingredientOne = new Ingredient(Measurement.GRAM, "Zucker", nutritionTable);
        Recipe underTest = new Recipe("Valid name", Map.of(ingredientOne, 100), "Valid description", List.of("Step 1"), null, 20);

        //when
        underTest.addCategory(category);

        //then
        assertEquals(
                List.of(category),
                underTest.getCategories()
        );
    }