completed Tests for GardenScheduleTest

This commit is contained in:
schrom01 2022-11-18 11:34:53 +01:00
parent 86a9eeaf2e
commit 384dc2d853
1 changed files with 62 additions and 24 deletions

View File

@ -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<Plant> examplePlantList;
List<Task> exampleTaskList;
Map<Long, Plant> examplePlantMap;
List<TaskTemplate> 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<Plant> 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<Task> 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<Long, Plant> examplePlantMap) {
return new PlantList() {
@Override
public List<Plant> 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<Plant> 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));
}
}