diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/AllRecipesController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/AllRecipesController.java index 65b924531e6091f052ecd30969fe80eee632d5ff..97254a17e3541d226155125b6aedeecd8bfb7a0b 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/AllRecipesController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/AllRecipesController.java @@ -66,10 +66,10 @@ public class AllRecipesController implements Initializable { Scene scene = new Scene(root); stage.setScene(scene); stage.show(); - } + @Override public void initialize(URL url, ResourceBundle resourceBundle) { @@ -94,7 +94,8 @@ public class AllRecipesController implements Initializable { .getSelectedItem().split("-|#")[1].strip(); try { - showRecipe(selectedRecipeName); + showRecipe(selectedRecipeName); + } catch (IOException e) { throw new RuntimeException(e); } 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 8d0cd9e668d99d575673554f657000f9c347fa11..3c6c3f171b58eda89b2b4fb99cf055c757263e5d 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 @@ -41,4 +41,19 @@ public class RecipeIngredient extends Ingredient{ this.atHand = atHand; } + /** + * The method returns a String representation of an Recipe Ingredient object, listing its type, amount, unit + * and whether it is at hand or not. + * @return A String representation of the recipe ingredient object. + */ + @Override + public String toString() { + return "Ingredient{" + + "foodType=" + this.getFoodType().label + + ", amount=" + this.getAmount() + + ", unit=" + this.getUnit().label + + ", at hand=" + this.isAtHand() + + '}'; + } + } diff --git a/src/main/resources/view/AllRecipes.fxml b/src/main/resources/view/AllRecipes.fxml index 397c2ec41853ada9b9da66e0b010ad7f530a7373..b58fa895c0942d96f5b2bac6b055fc551245034f 100644 --- a/src/main/resources/view/AllRecipes.fxml +++ b/src/main/resources/view/AllRecipes.fxml @@ -16,14 +16,14 @@ <top> <HBox prefHeight="125.0" prefWidth="1130.0" BorderPane.alignment="CENTER"> <children> - <Pane prefHeight="200.0" prefWidth="200.0"> + <Pane prefHeight="125.0" prefWidth="284.0"> <children> - <Button layoutX="60.0" layoutY="38.0" mnemonicParsing="false" onAction="#goBack" styleClass="button-style" stylesheets="@../style.css" text="GoBack" /> + <Button fx:id="goBackBtn" layoutX="60.0" layoutY="38.0" mnemonicParsing="false" onAction="#goBack" styleClass="button-style" stylesheets="@../style.css" text="Back to Suggestions" /> </children> </Pane> <Pane prefHeight="100.0" prefWidth="623.0"> <children> - <Label layoutX="195.0" layoutY="21.0" text="All recipes"> + <Label layoutX="115.0" layoutY="24.0" text="All recipes"> <font> <Font size="48.0" /> </font> diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientTest.java index 79dd0439298817a773fc8ac9c6c70f19575c0957..322b967efbf7da712f1a9d3c62579af4e073f415 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/IngredientTest.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.*; @@ -15,62 +16,86 @@ class IngredientTest { testIngredient = new Ingredient(FoodItem.ONION, 0.5f, MeasuringUnit.KG); } - @Test - @DisplayName("The constructor creates an ingredient object successfully") - void constructValidIngredient() { - Ingredient validIngredient = new Ingredient(FoodItem.ONION, 1, MeasuringUnit.KG); - assertEquals(validIngredient, new Ingredient(FoodItem.ONION, 1, MeasuringUnit.KG)); - } + @Nested + @DisplayName("Test creation of an Ingredient") + class testCreation{ - @Test - @DisplayName("The constructor throws exceptions for illegal input.") - void constructorThrowsExceptionsWhenItShould() { - assertThrows(IllegalArgumentException.class, () -> new Ingredient(null, 2, MeasuringUnit.DL)); - assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, -5, MeasuringUnit.DL)); - assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, 0, MeasuringUnit.DL)); - assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, 2, null)); - } + // Indirectly tests equals method. + @Test + @DisplayName("The constructor creates an ingredient object successfully") + void constructValidIngredient() { + Ingredient validIngredient = new Ingredient(FoodItem.ONION, 1, MeasuringUnit.KG); + assertEquals(validIngredient, new Ingredient(FoodItem.ONION, 1, MeasuringUnit.KG)); + } - @Test - @DisplayName("Change of food type works for valid food type.") - void setFoodTypeWorksForValidType() { - testIngredient.setFoodType(FoodItem.LEMON); - assertEquals(new Ingredient(FoodItem.LEMON, 0.5f, MeasuringUnit.KG), testIngredient); + @Test + @DisplayName("The constructor throws exceptions for illegal input.") + void constructorThrowsExceptionsWhenItShould() { + assertThrows(IllegalArgumentException.class, () -> new Ingredient(null, 2, MeasuringUnit.DL)); + assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, -5, MeasuringUnit.DL)); + assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, 0, MeasuringUnit.DL)); + assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, 2, null)); + } } - @Test - @DisplayName("Change of food type to invalid type throws exception.") - void setFoodTypeThrowsException() { - assertThrows(IllegalArgumentException.class, () -> testIngredient.setFoodType(null)); - } + @Nested + @DisplayName("Test alternation of Ingredient object") + class testAlternations{ - @Test - @DisplayName("Change of food amount works for valid amount.") - void setAmountWorksForValidAmount() { - testIngredient.setAmount(2.5); - assertEquals(new Ingredient(FoodItem.ONION, 2.5f, MeasuringUnit.KG), testIngredient); - } + @Nested + @DisplayName("Valid alternations") + class testValidAlternations { + @Test + @DisplayName("Change of food type works for valid food type.") + void setFoodTypeWorksForValidType() { + testIngredient.setFoodType(FoodItem.LEMON); + assertEquals(new Ingredient(FoodItem.LEMON, 0.5f, MeasuringUnit.KG), testIngredient); + } - @Test - @DisplayName("Change of food amount to invalid amount throws exception.") - void setAmountThrowsException() { - assertThrows(IllegalArgumentException.class, () -> testIngredient.setAmount(0)); - assertThrows(IllegalArgumentException.class, () -> testIngredient.setAmount(-1)); - } + @Test + @DisplayName("Change of food amount works for valid amount.") + void setAmountWorksForValidAmount() { + testIngredient.setAmount(2.5); + assertEquals(new Ingredient(FoodItem.ONION, 2.5f, MeasuringUnit.KG), testIngredient); + } + + @Test + @DisplayName("Change of measuring unit works for valid unit.") + void setUnitWorksForValidUnit() { + testIngredient.setUnit(MeasuringUnit.TBS); + assertEquals(new Ingredient(FoodItem.ONION, 0.5f, MeasuringUnit.TBS), testIngredient); + } - @Test - @DisplayName("Change of measuring unit works for valid unit.") - void setUnitWorksForValidUnit() { - testIngredient.setUnit(MeasuringUnit.TBS); - assertEquals(new Ingredient(FoodItem.ONION, 0.5f, MeasuringUnit.TBS), testIngredient); - } - @Test - @DisplayName("Change of measuring to invalid unit throws exception.") - void setUnitThrowsException() { - assertThrows(IllegalArgumentException.class, () -> testIngredient.setUnit(null)); + } + + @Nested + @DisplayName("Invalid alternations") + class testInvalidAlternations { + @Test + @DisplayName("Change of food type to invalid type throws exception.") + void setFoodTypeThrowsException() { + assertThrows(IllegalArgumentException.class, () -> testIngredient.setFoodType(null)); + } + + @Test + @DisplayName("Change of food amount to invalid amount throws exception.") + void setAmountThrowsException() { + assertThrows(IllegalArgumentException.class, () -> testIngredient.setAmount(0)); + assertThrows(IllegalArgumentException.class, () -> testIngredient.setAmount(-1)); + } + + @Test + @DisplayName("Change of measuring to invalid unit throws exception.") + void setUnitThrowsException() { + assertThrows(IllegalArgumentException.class, () -> testIngredient.setUnit(null)); + } + } + } + + //TODO: Test for equals method? } \ No newline at end of file diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/recipes/RecipeIngredientTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/RecipeIngredientTest.java new file mode 100644 index 0000000000000000000000000000000000000000..31c7400cf3fa20be2233c0ae5e171800dc252ec4 --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/recipes/RecipeIngredientTest.java @@ -0,0 +1,41 @@ +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 RecipeIngredientTest { + + RecipeIngredient testIngredient; + + @BeforeEach + void beforeEach() { + testIngredient = new RecipeIngredient(FoodItem.ONION, 0.5f, MeasuringUnit.KG); + } + + @Test + @DisplayName("Check that recipe ingredient is initiated not at hand") + void initiateAsNotAtHand() { + assertFalse(testIngredient.isAtHand()); + } + + @Test + @DisplayName("AtHand correctly set") + void setAtHandCorrectly() { + assertFalse(testIngredient.isAtHand()); + testIngredient.setAtHand(false); + assertFalse(testIngredient.isAtHand()); + } + + @Test + @DisplayName("Validate that two ingredients are equal despite at hand status.") + void equalIngredientsDespiteStatus() { + RecipeIngredient secondTestIngredient = new RecipeIngredient(FoodItem.ONION, 0.5f, MeasuringUnit.KG); + testIngredient.setAtHand(true); + + assertEquals(testIngredient, secondTestIngredient); + } + +} \ No newline at end of file