From eea530931eaedb69313b83ed4822c7caa6a4df7a Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 21:01:25 +0100 Subject: [PATCH] created Some TaskListModelTests --- .../taskList/TaskListModelTest.java | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java diff --git a/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java new file mode 100644 index 0000000..76099a2 --- /dev/null +++ b/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java @@ -0,0 +1,134 @@ +package ch.zhaw.gartenverwaltung.taskList; + +import ch.zhaw.gartenverwaltung.io.*; +import ch.zhaw.gartenverwaltung.types.HardinessZone; +import ch.zhaw.gartenverwaltung.types.Plant; +import ch.zhaw.gartenverwaltung.types.Task; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.time.LocalDate; +import java.util.*; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class TaskListModelTest { + TaskDatabase taskDatabase; + PlantDatabase plantDatabase; + List exampleTaskList; + Map examplePlantMap; + TaskListModel model; + + @BeforeEach + void setUp() throws IOException { + createExampleTaskList(); + taskDatabase = mockTaskDatabase(exampleTaskList); + plantDatabase = mockPlantDatabase(examplePlantMap); + model = new TaskListModel(taskDatabase, plantDatabase); + } + + private TaskDatabase mockTaskDatabase(List exampleTaskList) throws IOException { + TaskDatabase taskDatabase = mock(JsonTaskDatabase.class); + when(taskDatabase.getTaskList(LocalDate.MIN, LocalDate.MAX)).thenReturn(exampleTaskList); + + when(taskDatabase.getTaskList(LocalDate.now(), LocalDate.MAX)).thenReturn((exampleTaskList.subList(1, 4))); + + List pastTasks = new ArrayList<>(); + pastTasks.add(exampleTaskList.get(0)); + pastTasks.add(exampleTaskList.get(2)); + pastTasks.add(exampleTaskList.get(4)); + when(taskDatabase.getTaskList(LocalDate.MIN, LocalDate.now())).thenReturn(pastTasks); + + return taskDatabase; + } + + private PlantDatabase mockPlantDatabase(Map examplePlantMap) { + return new PlantDatabase() { + @Override + public List getPlantList(HardinessZone zone) throws IOException, HardinessZoneNotSetException { + return null; + } + + @Override + public Optional getPlantById(HardinessZone zone, long id) throws IOException, HardinessZoneNotSetException { + return Optional.ofNullable(examplePlantMap.get(id)); + } + }; + } + + void createExampleTaskList() { + exampleTaskList = new ArrayList<>(); + exampleTaskList.add(new Task("name", "description", LocalDate.now().minusDays(1), 1L)); + exampleTaskList.add(new Task("name", "description", LocalDate.now().plusDays(1), 1L)); + exampleTaskList.add(new Task("name", "description", LocalDate.now(), 1L)); + exampleTaskList.add(new Task("name", "description", LocalDate.of(9019, 5, 5), 1L)); + exampleTaskList.add(new Task("name", "description", LocalDate.of(2019, 5, 5), 1L)); + } + + void createExamplePlantMap() { + examplePlantMap = new HashMap<>(); + } + + + + @Test + void addTask() throws IOException { + Task taskToAdd = new Task("name", "description", LocalDate.now(), 1L); + model.addTask(taskToAdd); + verify(taskDatabase, times(1)).saveTask(taskToAdd); + } + + @Test + void removeTask() throws IOException { + Task taskToRemove = new Task("name", "description", LocalDate.now(), 1L); + model.removeTask(taskToRemove); + verify(taskDatabase, times(1)).removeTask(taskToRemove); + } + + @Test + void getTaskList() throws IOException { + List listToCheck = model.getTaskList(); + assertEquals(5, listToCheck.size()); + assertEquals(exampleTaskList.get(4), listToCheck.get(0)); + assertEquals(exampleTaskList.get(0), listToCheck.get(1)); + assertEquals(exampleTaskList.get(2), listToCheck.get(2)); + assertEquals(exampleTaskList.get(1), listToCheck.get(3)); + assertEquals(exampleTaskList.get(3), listToCheck.get(4)); + } + + @Test + void getFutureTasks() throws IOException { + List listToCheck = model.getFutureTasks(); + assertEquals(3, listToCheck.size()); + assertEquals(exampleTaskList.get(2), listToCheck.get(0)); + assertEquals(exampleTaskList.get(1), listToCheck.get(1)); + assertEquals(exampleTaskList.get(3), listToCheck.get(2)); + } + + @Test + void getPastTasks() throws IOException { + List listToCheck = model.getPastTasks(); + assertEquals(3, listToCheck.size()); + assertEquals(exampleTaskList.get(4), listToCheck.get(0)); + assertEquals(exampleTaskList.get(0), listToCheck.get(1)); + assertEquals(exampleTaskList.get(2), listToCheck.get(2)); + } + + @Test + void getTasksUpcomingWeek() { + } + + @Test + void getFilteredTaskList() { + } + + @Test + void planTasksForCrop() { + } + + @Test + void removeTasksForCrop() { + } +} \ No newline at end of file