From 64630f097b2a842190164fa83385e367b0493663 Mon Sep 17 00:00:00 2001
From: Lukas Karsch <lk224@hdm-stuttgart.de>
Date: Tue, 6 Jun 2023 15:44:02 +0200
Subject: [PATCH] final modifications to make the file manager work

---
 src/main/java/mi/hdm/GuiController.java       | 11 +++++++---
 .../mi/hdm/controllers/BaseController.java    |  8 ++++---
 src/main/java/mi/hdm/controllers/View.java    |  5 ++---
 src/main/java/mi/hdm/recipes/Recipe.java      |  6 ++----
 .../java/mi/hdm/recipes/RecipeSearch.java     |  1 -
 .../java/mi/hdm/recipes/RecipeSearchTest.java |  2 +-
 .../mi/hdm/shoppingList/ShoppingListTest.java | 21 +++++++++++++++----
 7 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/src/main/java/mi/hdm/GuiController.java b/src/main/java/mi/hdm/GuiController.java
index 28651a9..45144fa 100644
--- a/src/main/java/mi/hdm/GuiController.java
+++ b/src/main/java/mi/hdm/GuiController.java
@@ -12,6 +12,8 @@ import org.apache.logging.log4j.Logger;
 public class GuiController extends Application {
     private final static Logger log = LogManager.getLogger(GuiController.class);
 
+    private final static View STARTUP_VIEW = View.MAIN;
+
     private static Stage stage;
     private static TastyPages model;
 
@@ -33,12 +35,11 @@ public class GuiController extends Application {
     @Override
     public void start(Stage stage) throws Exception {
         GuiController.stage = stage;
-        final View startupView = View.MAIN;
 
-        Parent parent = startupView.getScene();
+        Parent parent = STARTUP_VIEW.getScene();
         final Scene scene = new Scene(parent, 1400, 800);
 
-        stage.setTitle(startupView.getWindowTitle());
+        stage.setTitle(STARTUP_VIEW.getWindowTitle());
         stage.setMaximized(true); //open scene in full screen
         stage.setScene(scene);
         stage.show();
@@ -51,4 +52,8 @@ public class GuiController extends Application {
     public static Stage getApplicationStage() {
         return stage;
     }
+
+    public static View getStartupView() {
+        return STARTUP_VIEW;
+    }
 }
diff --git a/src/main/java/mi/hdm/controllers/BaseController.java b/src/main/java/mi/hdm/controllers/BaseController.java
index 2d6abfa..ca66cf0 100644
--- a/src/main/java/mi/hdm/controllers/BaseController.java
+++ b/src/main/java/mi/hdm/controllers/BaseController.java
@@ -13,11 +13,13 @@ import java.io.IOException;
 public abstract class BaseController {
     private final static Logger log = LogManager.getLogger(BaseController.class);
 
-    protected Scene currentScene;
-    private static View currentView = TastyPages.startUpView;
+    protected Scene currentScene; //TODO this should be static?
+    private static View currentView = GuiController.getStartupView();
 
     protected void changeScene(final View newScene) {
-        if (newScene == currentView) { return; }
+        if (newScene == currentView) {
+            return;
+        }
         final Stage stage = GuiController.getApplicationStage();
         try {
             boolean wasMaximized = stage.isMaximized();
diff --git a/src/main/java/mi/hdm/controllers/View.java b/src/main/java/mi/hdm/controllers/View.java
index 22fecfa..20e12dd 100644
--- a/src/main/java/mi/hdm/controllers/View.java
+++ b/src/main/java/mi/hdm/controllers/View.java
@@ -17,8 +17,6 @@ public enum View {
 
     private static final Logger log = LogManager.getLogger(View.class);
 
-    private final static TastyPages model = GuiController.getModel();
-
     private final String path;
     private final String windowTitle;
 
@@ -31,8 +29,9 @@ public enum View {
         log.info("Loading FXML for '{}' view from: {}", windowTitle, path);
         final FXMLLoader loader = new FXMLLoader(getClass().getResource(path));
         log.debug("Successfully retrieved resource from {}", path);
+        TastyPages model = GuiController.getModel();
 
-        //set the correct controller factory for views (necessary to enable dependency injection)
+        //set the correct controller factory for views (used for dependency injection)
         switch (this) {
             case MAIN -> loader.setControllerFactory((callback) ->
                     new MainPageController(model.getRecipeManager(), model.getIngredientManager(), model.getCategoryManager())
diff --git a/src/main/java/mi/hdm/recipes/Recipe.java b/src/main/java/mi/hdm/recipes/Recipe.java
index d5c9e82..ad77653 100644
--- a/src/main/java/mi/hdm/recipes/Recipe.java
+++ b/src/main/java/mi/hdm/recipes/Recipe.java
@@ -74,7 +74,6 @@ public class Recipe implements RecipeComponent {
         setCategoriesFromObjects(categories);
 
         this.creationTime = LocalDateTime.now();
-<<<<<<< src/main/java/mi/hdm/recipes/Recipe.java
         this.code = calculateUniqueCode();
     }
 
@@ -98,6 +97,7 @@ public class Recipe implements RecipeComponent {
         setCategories(categories);
         this.creationTime = creationTime;
         this.code = code;
+        //TODO: fix filepath
         filepathForImage = "C:\\Users\\Lara Blersch\\Documents\\Studium\\SE2\\tasty-pages\\src\\main\\resources\\images\\dish-fork-and-knife.png"; //path to default image which can be changed by the user later
     }
 
@@ -202,8 +202,6 @@ public class Recipe implements RecipeComponent {
         this.ingredients = ingredients;
     }
 
-<<<<<<< src/main/java/mi/hdm/recipes/Recipe.java
-
     private void setCategoriesFromObjects(List<Category> categories) {
         if (categories != null) {
             this.categories = categories.stream()
@@ -222,11 +220,11 @@ public class Recipe implements RecipeComponent {
             result.put(c.getUniqueCode(), map.get(c));
         }
         return result;
+    }
     
     public void setImage(String path) {
         filepathForImage = path;
     }
-=======
 
     @Override
     public boolean equals(Object o) {
diff --git a/src/main/java/mi/hdm/recipes/RecipeSearch.java b/src/main/java/mi/hdm/recipes/RecipeSearch.java
index 2e9cb34..45f3c5e 100644
--- a/src/main/java/mi/hdm/recipes/RecipeSearch.java
+++ b/src/main/java/mi/hdm/recipes/RecipeSearch.java
@@ -7,7 +7,6 @@ import java.util.ArrayList;
 import java.util.List;
 
 public class RecipeSearch {
-<<<<<<< src/main/java/mi/hdm/recipes/RecipeSearch.java
     //TODO: lets make these methods static, no need to instantiate a new object every time
     private static final Logger log = LogManager.getLogger(RecipeSearch.class);
 
diff --git a/src/test/java/mi/hdm/recipes/RecipeSearchTest.java b/src/test/java/mi/hdm/recipes/RecipeSearchTest.java
index 9fa5d9a..fcb059a 100644
--- a/src/test/java/mi/hdm/recipes/RecipeSearchTest.java
+++ b/src/test/java/mi/hdm/recipes/RecipeSearchTest.java
@@ -22,7 +22,7 @@ class RecipeSearchTest {
     private static final IngredientManager ingredientManager = new IngredientManager();
 
     private List<Recipe> recipes;
-    //private RecipeSearch underTest; //not needed when methods are static
+    private RecipeSearch underTest; //not needed when methods are static
 
     @BeforeAll
     public static void setupAll() {
diff --git a/src/test/java/mi/hdm/shoppingList/ShoppingListTest.java b/src/test/java/mi/hdm/shoppingList/ShoppingListTest.java
index 808a42b..6d02fc4 100644
--- a/src/test/java/mi/hdm/shoppingList/ShoppingListTest.java
+++ b/src/test/java/mi/hdm/shoppingList/ShoppingListTest.java
@@ -3,17 +3,20 @@ package mi.hdm.shoppingList;
 import mi.hdm.exceptions.InvalidIngredientException;
 import mi.hdm.recipes.Ingredient;
 import mi.hdm.recipes.Recipe;
+import mi.hdm.recipes.RecipeManager;
 import mi.hdm.recipes.ValidObjectsPool;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
+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 ShoppingListTest {
-    private final ShoppingList underTest = new ShoppingList(ValidObjectsPool.getRecipeManager());
+    private final static RecipeManager recipeManager = ValidObjectsPool.getRecipeManager();
+    private final static ShoppingList underTest = new ShoppingList(recipeManager);
     private final static Ingredient ing1 = ValidObjectsPool.getValidIngredientOne();
     private final static Ingredient ing2 = ValidObjectsPool.getValidIngredientTwo();
 
@@ -21,7 +24,7 @@ class ShoppingListTest {
 
     @BeforeEach
     public void setup() {
-        ValidObjectsPool.getRecipeManager().clear();
+        recipeManager.clear();
         underTest.clear();
     }
 
@@ -51,8 +54,18 @@ class ShoppingListTest {
 
     @Test
     public void shouldAllAllIngredientsRecursively() {
-        //TODO: write test for adding ingredients to shopping list recursively
-        throw new RuntimeException("not implemented");
+        //given
+        Recipe r1 = new Recipe("parent", Map.of(ValidObjectsPool.getValidIngredientOne(), 100), "Desc", List.of("prep"), List.of(), 10);
+        Recipe r2 = new Recipe("child", Map.of(r1, 100), "Desc", List.of("prep"), List.of(), 10);
+        Map<String, Boolean> expected = Map.of(ValidObjectsPool.getValidIngredientOne().getUniqueCode(), false);
+        recipeManager.addRecipe(r1);
+        recipeManager.addRecipe(r2);
+
+        //when
+        underTest.addAllToShoppingList(r2);
+
+        //expect
+        assertEquals(expected, underTest.getList());
     }
 
     @Test
-- 
GitLab