From b1e4e51d68d5abf72773e41693e74af679687f30 Mon Sep 17 00:00:00 2001 From: Gian-Andrea Hutter Date: Wed, 2 Nov 2022 13:08:23 +0100 Subject: [PATCH] #27 gardenplanmodelTest first version implemented --- .../gardenplan/Gardenplanmodel.java | 14 ++- .../zhaw/gartenverwaltung/io/GardenPlan.java | 4 +- .../gardenplan/GardenPlanModelTest.java | 109 ++++++++++++++++++ 3 files changed, 119 insertions(+), 8 deletions(-) create mode 100644 src/test/java/ch/zhaw/gartenverwaltung/gardenplan/GardenPlanModelTest.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java b/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java index 18e072b..5dfe5cd 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java @@ -3,7 +3,6 @@ package ch.zhaw.gartenverwaltung.gardenplan; import ch.zhaw.gartenverwaltung.io.GardenPlan; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.JsonGardenPlan; -import ch.zhaw.gartenverwaltung.io.TaskDatabase; import ch.zhaw.gartenverwaltung.taskList.PlantNotFoundException; import ch.zhaw.gartenverwaltung.taskList.TaskListModel; import ch.zhaw.gartenverwaltung.types.Crop; @@ -12,9 +11,12 @@ import ch.zhaw.gartenverwaltung.types.Task; import java.io.IOException; import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Optional; + /** * The Gardenplan model manages the crops in the gardenplan. */ @@ -31,6 +33,7 @@ public class Gardenplanmodel { public Gardenplanmodel(TaskListModel taskListModel) throws IOException { this.taskListModel = taskListModel; gardenPlan = new JsonGardenPlan(); + cropList = new ArrayList<>(); cropList = gardenPlan.getCrops(); } @@ -47,11 +50,10 @@ public class Gardenplanmodel { public void plantAsCrop(Plant plant, LocalDate plantingDate) throws IOException, HardinessZoneNotSetException, PlantNotFoundException { Crop crop = new Crop(plant.id(),plantingDate); //TODO Add Area to Plant - crop.withArea(0); - - cropList.add(crop); + //crop.withArea(0); gardenPlan.saveCrop(crop); taskListModel.planTasksForCrop(crop); + cropList = gardenPlan.getCrops(); } /** @@ -62,9 +64,9 @@ public class Gardenplanmodel { */ public void removeCrop(Crop crop) throws IOException { - cropList.remove(crop); gardenPlan.removeCrop(crop); taskListModel.removeTasksForCrop(crop); + cropList = gardenPlan.getCrops(); } /** * Returns a list of {@link Crop}s which are currently in the gardenplan. @@ -72,7 +74,7 @@ public class Gardenplanmodel { * @throws IOException If the database cannot be accessed */ public List getCrops() throws IOException { - if(cropList.isEmpty()){ + if(!cropList.isEmpty()){ cropList = gardenPlan.getCrops(); } return cropList; diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/GardenPlan.java b/src/main/java/ch/zhaw/gartenverwaltung/io/GardenPlan.java index 0e04f9a..38d0598 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/GardenPlan.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/GardenPlan.java @@ -18,11 +18,11 @@ public interface GardenPlan { /** * Attempts to retrieve the {@link Crop} with the specified cropId. * - * @param id The {@link Crop#cropId} to look for + * @param cropId The {@link Crop} to look for * @return {@link Optional} of the found {@link Crop}, {@link Optional#empty()} if no entry matched the criteria * @throws IOException If there is a problem reading from or writing to the database */ - Optional getCropById(long id) throws IOException; + Optional getCropById(long cropId) throws IOException; /** * Saves a Crop to the Database. diff --git a/src/test/java/ch/zhaw/gartenverwaltung/gardenplan/GardenPlanModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/gardenplan/GardenPlanModelTest.java new file mode 100644 index 0000000..1f951ea --- /dev/null +++ b/src/test/java/ch/zhaw/gartenverwaltung/gardenplan/GardenPlanModelTest.java @@ -0,0 +1,109 @@ +package ch.zhaw.gartenverwaltung.gardenplan; + +import ch.zhaw.gartenverwaltung.io.*; +import ch.zhaw.gartenverwaltung.taskList.PlantNotFoundException; +import ch.zhaw.gartenverwaltung.taskList.TaskListModel; +import ch.zhaw.gartenverwaltung.types.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.internal.stubbing.answers.DoesNothing; + +import java.io.IOException; +import java.time.LocalDate; +import java.time.MonthDay; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.*; + +public class GardenPlanModelTest { + GardenPlan gardenPlan; + List cropList; + Crop exampleCropOnion; + Crop exampleCropCarrot; + Crop exampleCrop1; + Crop exampleCrop2; + Crop exampleCrop3; + Plant examplePlantOnion; + Plant examplePlantCarrot; + Gardenplanmodel model; + + @BeforeEach + void setUp() throws IOException { + examplePlantOnion = new Plant( + 0, + "summertime onion", + "Onion, (Allium cepa), herbaceous biennial plant in the amaryllis family (Amaryllidaceae) grown for its edible bulb. The onion is likely native to southwestern Asia but is now grown throughout the world, chiefly in the temperate zones. Onions are low in nutrients but are valued for their flavour and are used widely in cooking. They add flavour to such dishes as stews, roasts, soups, and salads and are also served as a cooked vegetable.", + null, + "15,30,2", + 0, + "sandy to loamy, loose soil, free of stones", + new ArrayList<>(), + List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(8, 5), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(2, 8), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()), + new GrowthPhase(MonthDay.of(10, 2), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))); + + exampleCropOnion = new Crop(examplePlantOnion.id(), LocalDate.now()); + exampleCropOnion.withId(13); + examplePlantCarrot = new Plant( + 1, + "Early Carrot", + "Carrot, (Daucus carota), herbaceous, generally biennial plant of the Apiaceae family that produces an edible taproot. Among common varieties root shapes range from globular to long, with lower ends blunt to pointed. Besides the orange-coloured roots, white-, yellow-, and purple-fleshed varieties are known.", + null, + "5,35,2.5", + 0, + "sandy to loamy, loose soil, free of stones", + new ArrayList<>(), + List.of(new GrowthPhase(MonthDay.of(4, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()))); + exampleCropCarrot = new Crop(examplePlantCarrot.id(), LocalDate.now()); + exampleCropCarrot.withId(5); + + exampleCrop1 = new Crop(1, LocalDate.of(2023,2,25)); + exampleCrop1.withId(0); + exampleCrop1.withArea(0.5); + exampleCrop2 = new Crop(1,LocalDate.of(2023,3,1)); + exampleCrop2.withId(1); + exampleCrop2.withArea(0.5); + exampleCrop3 = new Crop(0,LocalDate.of(2023,3,25)); + exampleCrop3.withId(2); + exampleCrop3.withArea(1.5); + + cropList = new ArrayList<>(); + cropList.add(exampleCrop1); + cropList.add(exampleCrop2); + cropList.add(exampleCrop3); + gardenPlan = mockGardenPlan(cropList); + + TaskListModel taskListModel = new TaskListModel(new JsonTaskDatabase(), new JsonPlantDatabase()); + model = new Gardenplanmodel(taskListModel); + } + + GardenPlan mockGardenPlan(List cropList) throws IOException { + GardenPlan gardenPlan = mock(GardenPlan.class); + when(gardenPlan.getCrops()).thenReturn(cropList); + when(gardenPlan.getCropById(5)).thenReturn(java.util.Optional.ofNullable(exampleCropCarrot)); + when(gardenPlan.getCropById(13)).thenReturn(java.util.Optional.ofNullable(exampleCropOnion)); + return gardenPlan; + } + + @Test + void plantAsCrop() throws HardinessZoneNotSetException, IOException, PlantNotFoundException { + + model.plantAsCrop(examplePlantOnion, LocalDate.of(2023,3,1)); + assertTrue(model.getCrops().contains(exampleCropOnion)); + } + + @Test + void removeCrop() throws IOException { + exampleCrop1.withId(2); + exampleCrop1.withArea(1.500000); + model.removeCrop(exampleCrop1); + assertEquals(2,model.getCrops().size()); + } +}