From 384dc2d85313df09d4c470b2a272d8ab2d91a891 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 18 Nov 2022 11:34:53 +0100 Subject: [PATCH] completed Tests for GardenScheduleTest --- .../GardenScheduleTest.java | 86 +++++++++++++------ 1 file changed, 62 insertions(+), 24 deletions(-) rename src/test/java/ch/zhaw/gartenverwaltung/{taskList => models}/GardenScheduleTest.java (57%) diff --git a/src/test/java/ch/zhaw/gartenverwaltung/taskList/GardenScheduleTest.java b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java similarity index 57% rename from src/test/java/ch/zhaw/gartenverwaltung/taskList/GardenScheduleTest.java rename to src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java index cf354de..0cadffb 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/taskList/GardenScheduleTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java @@ -1,35 +1,45 @@ -package ch.zhaw.gartenverwaltung.taskList; +package ch.zhaw.gartenverwaltung.models; import ch.zhaw.gartenverwaltung.io.*; import ch.zhaw.gartenverwaltung.models.GardenSchedule; -import ch.zhaw.gartenverwaltung.types.HardinessZone; -import ch.zhaw.gartenverwaltung.types.Plant; -import ch.zhaw.gartenverwaltung.types.Task; +import ch.zhaw.gartenverwaltung.types.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; import java.time.LocalDate; +import java.time.MonthDay; import java.util.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; class GardenScheduleTest { + LocalDate exampleStartDate = LocalDate.of(2020, 02, 02); TaskList taskList; PlantList plantList; + List examplePlantList; List exampleTaskList; - Map examplePlantMap; + List exampleTaskTemplateList; GardenSchedule model; @BeforeEach - void setUp() throws IOException { + void setUp() throws IOException, HardinessZoneNotSetException { createExampleTaskList(); + createExampleTaskTemplateList(); + createExamplePlantList(); taskList = mockTaskDatabase(exampleTaskList); - plantList = mockPlantDatabase(examplePlantMap); + plantList = mockPlantDatabase(examplePlantList); model = new GardenSchedule(taskList, plantList); } + PlantList mockPlantDatabase(List plantList) throws HardinessZoneNotSetException, IOException { + PlantList plantDatabase = mock(JsonPlantList.class); + when(plantDatabase.getPlantList(HardinessZone.ZONE_8A)).thenReturn(plantList); + when(plantDatabase.getPlantById(HardinessZone.ZONE_8A,20)).thenReturn(Optional.of(examplePlantList.get(0))); + return plantDatabase; + } + private TaskList mockTaskDatabase(List exampleTaskList) throws IOException { TaskList taskList = mock(JsonTaskList.class); when(taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)).thenReturn(exampleTaskList); @@ -54,18 +64,39 @@ class GardenScheduleTest { return taskList; } - private PlantList mockPlantDatabase(Map examplePlantMap) { - return new PlantList() { - @Override - public List getPlantList(HardinessZone zone) { - return null; - } + void createExampleTaskTemplateList(){ + exampleTaskTemplateList = new ArrayList<>(); + exampleTaskTemplateList.add(mock(TaskTemplate.class)); + exampleTaskTemplateList.add(mock(TaskTemplate.class)); + exampleTaskTemplateList.add(mock(TaskTemplate.class)); + exampleTaskTemplateList.add(mock(TaskTemplate.class)); + when(exampleTaskTemplateList.get(0).generateTask(exampleStartDate, 30)).thenReturn(exampleTaskList.get(0)); + when(exampleTaskTemplateList.get(1).generateTask(exampleStartDate, 30)).thenReturn(exampleTaskList.get(1)); + when(exampleTaskTemplateList.get(2).generateTask(exampleStartDate, 30)).thenReturn(exampleTaskList.get(2)); + when(exampleTaskTemplateList.get(3).generateTask(exampleStartDate, 30)).thenReturn(exampleTaskList.get(3)); + } - @Override - public Optional getPlantById(HardinessZone zone, long id) { - return Optional.ofNullable(examplePlantMap.get(id)); - } - }; + void createExamplePlantList(){ + examplePlantList = new ArrayList<>(); + examplePlantList.add(new Plant( + 20, + "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, List.of( + exampleTaskTemplateList.get(0), + exampleTaskTemplateList.get(1) + )), + new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, List.of( + exampleTaskTemplateList.get(2), + exampleTaskTemplateList.get(3) + )) + ))); } void createExampleTaskList() { @@ -77,12 +108,6 @@ class GardenScheduleTest { exampleTaskList.add(new Task("name", "description", LocalDate.of(2019, 5, 5), 2L)); } - void createExamplePlantMap() { - examplePlantMap = new HashMap<>(); - } - - - @Test void addTask() throws IOException { Task taskToAdd = new Task("name", "description", LocalDate.now(), 1L); @@ -156,4 +181,17 @@ class GardenScheduleTest { model.removeTasksForCrop(1L); verify(taskList, times(1)).removeTasksForCrop(1L); } + + @Test + void planTasksForCrop() throws HardinessZoneNotSetException, PlantNotFoundException, IOException { + model.planTasksForCrop(new Crop(20, exampleStartDate).withId(30)); + verify(exampleTaskTemplateList.get(0), times(1)).generateTask(exampleStartDate, 30); + verify(exampleTaskTemplateList.get(1), times(1)).generateTask(exampleStartDate, 30); + verify(exampleTaskTemplateList.get(2), times(1)).generateTask(exampleStartDate, 30); + verify(exampleTaskTemplateList.get(3), times(1)).generateTask(exampleStartDate, 30); + verify(taskList, times(1)).saveTask(exampleTaskList.get(0)); + verify(taskList, times(1)).saveTask(exampleTaskList.get(1)); + verify(taskList, times(1)).saveTask(exampleTaskList.get(2)); + verify(taskList, times(1)).saveTask(exampleTaskList.get(3)); + } } \ No newline at end of file