completed Tests for GardenScheduleTest
This commit is contained in:
parent
86a9eeaf2e
commit
384dc2d853
|
@ -1,35 +1,45 @@
|
||||||
package ch.zhaw.gartenverwaltung.taskList;
|
package ch.zhaw.gartenverwaltung.models;
|
||||||
|
|
||||||
import ch.zhaw.gartenverwaltung.io.*;
|
import ch.zhaw.gartenverwaltung.io.*;
|
||||||
import ch.zhaw.gartenverwaltung.models.GardenSchedule;
|
import ch.zhaw.gartenverwaltung.models.GardenSchedule;
|
||||||
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
import ch.zhaw.gartenverwaltung.types.*;
|
||||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
|
||||||
import ch.zhaw.gartenverwaltung.types.Task;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.time.MonthDay;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
class GardenScheduleTest {
|
class GardenScheduleTest {
|
||||||
|
LocalDate exampleStartDate = LocalDate.of(2020, 02, 02);
|
||||||
TaskList taskList;
|
TaskList taskList;
|
||||||
PlantList plantList;
|
PlantList plantList;
|
||||||
|
List<Plant> examplePlantList;
|
||||||
List<Task> exampleTaskList;
|
List<Task> exampleTaskList;
|
||||||
Map<Long, Plant> examplePlantMap;
|
List<TaskTemplate> exampleTaskTemplateList;
|
||||||
GardenSchedule model;
|
GardenSchedule model;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() throws IOException {
|
void setUp() throws IOException, HardinessZoneNotSetException {
|
||||||
createExampleTaskList();
|
createExampleTaskList();
|
||||||
|
createExampleTaskTemplateList();
|
||||||
|
createExamplePlantList();
|
||||||
taskList = mockTaskDatabase(exampleTaskList);
|
taskList = mockTaskDatabase(exampleTaskList);
|
||||||
plantList = mockPlantDatabase(examplePlantMap);
|
plantList = mockPlantDatabase(examplePlantList);
|
||||||
model = new GardenSchedule(taskList, plantList);
|
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 {
|
private TaskList mockTaskDatabase(List<Task> exampleTaskList) throws IOException {
|
||||||
TaskList taskList = mock(JsonTaskList.class);
|
TaskList taskList = mock(JsonTaskList.class);
|
||||||
when(taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)).thenReturn(exampleTaskList);
|
when(taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)).thenReturn(exampleTaskList);
|
||||||
|
@ -54,18 +64,39 @@ class GardenScheduleTest {
|
||||||
return taskList;
|
return taskList;
|
||||||
}
|
}
|
||||||
|
|
||||||
private PlantList mockPlantDatabase(Map<Long, Plant> examplePlantMap) {
|
void createExampleTaskTemplateList(){
|
||||||
return new PlantList() {
|
exampleTaskTemplateList = new ArrayList<>();
|
||||||
@Override
|
exampleTaskTemplateList.add(mock(TaskTemplate.class));
|
||||||
public List<Plant> getPlantList(HardinessZone zone) {
|
exampleTaskTemplateList.add(mock(TaskTemplate.class));
|
||||||
return null;
|
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
|
void createExamplePlantList(){
|
||||||
public Optional<Plant> getPlantById(HardinessZone zone, long id) {
|
examplePlantList = new ArrayList<>();
|
||||||
return Optional.ofNullable(examplePlantMap.get(id));
|
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() {
|
void createExampleTaskList() {
|
||||||
|
@ -77,12 +108,6 @@ class GardenScheduleTest {
|
||||||
exampleTaskList.add(new Task("name", "description", LocalDate.of(2019, 5, 5), 2L));
|
exampleTaskList.add(new Task("name", "description", LocalDate.of(2019, 5, 5), 2L));
|
||||||
}
|
}
|
||||||
|
|
||||||
void createExamplePlantMap() {
|
|
||||||
examplePlantMap = new HashMap<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void addTask() throws IOException {
|
void addTask() throws IOException {
|
||||||
Task taskToAdd = new Task("name", "description", LocalDate.now(), 1L);
|
Task taskToAdd = new Task("name", "description", LocalDate.now(), 1L);
|
||||||
|
@ -156,4 +181,17 @@ class GardenScheduleTest {
|
||||||
model.removeTasksForCrop(1L);
|
model.removeTasksForCrop(1L);
|
||||||
verify(taskList, times(1)).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));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue