From eea530931eaedb69313b83ed4822c7caa6a4df7a Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 31 Oct 2022 21:01:25 +0100 Subject: [PATCH 1/3] 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 From 38288f8561bbd51f835737c4e78c349428966b25 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 4 Nov 2022 14:20:58 +0100 Subject: [PATCH 2/3] finished Tests for TaskListModel --- .../taskList/TaskListModel.java | 13 ++--- .../taskList/TaskListModelTest.java | 52 ++++++++++++++----- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java index 4f9384d..430d14c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -9,6 +9,7 @@ import ch.zhaw.gartenverwaltung.types.*; import java.io.IOException; import java.time.LocalDate; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -105,17 +106,17 @@ public class TaskListModel { } /** - * Method to get an Array of 7 Tasklists for the next 7 days. Index 0 is Tasklist for Today. - * @return Array with length 7 (List[]) + * Method to get an List of 7 Tasklists for the next 7 days. Index 0 is Tasklist for Today. + * @return List with length 7 (List>) * @throws IOException If the database cannot be accessed */ - public List[] getTasksUpcomingWeek() throws IOException { - List[] listArray = new List[7]; + public List> getTasksUpcomingWeek() throws IOException { + List> dayTaskList = new ArrayList<>(); for(int i = 0; i < 7; i++) { LocalDate date = LocalDate.now().plusDays(i); - listArray[i] = taskDatabase.getTaskList(date, date); + dayTaskList.add(taskDatabase.getTaskList(date, date)); } - return listArray; + return dayTaskList; } /** diff --git a/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java index 76099a2..9caf65a 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/taskList/TaskListModelTest.java @@ -41,18 +41,27 @@ class TaskListModelTest { pastTasks.add(exampleTaskList.get(4)); when(taskDatabase.getTaskList(LocalDate.MIN, LocalDate.now())).thenReturn(pastTasks); + + when(taskDatabase.getTaskList(LocalDate.now(), LocalDate.now())).thenReturn(List.of(exampleTaskList.get(2))); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(1L), LocalDate.now().plusDays(1L))).thenReturn(List.of(exampleTaskList.get(1))); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(2L), LocalDate.now().plusDays(2L))).thenReturn(List.of()); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(3L), LocalDate.now().plusDays(3L))).thenReturn(List.of()); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(4L), LocalDate.now().plusDays(4L))).thenReturn(List.of()); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(5L), LocalDate.now().plusDays(5L))).thenReturn(List.of()); + when(taskDatabase.getTaskList(LocalDate.now().plusDays(6L), LocalDate.now().plusDays(6L))).thenReturn(List.of()); + return taskDatabase; } private PlantDatabase mockPlantDatabase(Map examplePlantMap) { return new PlantDatabase() { @Override - public List getPlantList(HardinessZone zone) throws IOException, HardinessZoneNotSetException { + public List getPlantList(HardinessZone zone) { return null; } @Override - public Optional getPlantById(HardinessZone zone, long id) throws IOException, HardinessZoneNotSetException { + public Optional getPlantById(HardinessZone zone, long id) { return Optional.ofNullable(examplePlantMap.get(id)); } }; @@ -61,10 +70,10 @@ class TaskListModelTest { 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().plusDays(1), 2L)); 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)); + exampleTaskList.add(new Task("name", "description", LocalDate.of(2019, 5, 5), 2L)); } void createExamplePlantMap() { @@ -117,18 +126,33 @@ class TaskListModelTest { } @Test - void getTasksUpcomingWeek() { + void getTasksUpcomingWeek() throws IOException { + List> dayList = model.getTasksUpcomingWeek(); + assertEquals(7, dayList.size()); + + //Check day 0 + assertEquals(1, dayList.get(0).size()); + assertEquals(exampleTaskList.get(2), dayList.get(0).get(0)); + + //Check day 1 + assertEquals(1, dayList.get(1).size()); + assertEquals(exampleTaskList.get(1), dayList.get(1).get(0)); + + //Check day 2 + assertEquals(0, dayList.get(2).size()); + //Check day 3 + assertEquals(0, dayList.get(3).size()); + //Check day 4 + assertEquals(0, dayList.get(4).size()); + //Check day 5 + assertEquals(0, dayList.get(5).size()); + //Check day 6 + assertEquals(0, dayList.get(6).size()); } @Test - void getFilteredTaskList() { - } - - @Test - void planTasksForCrop() { - } - - @Test - void removeTasksForCrop() { + void removeTasksForCrop() throws IOException { + model.removeTasksForCrop(1L); + verify(taskDatabase, times(1)).removeTasksForCrop(1L); } } \ No newline at end of file From b7d08944a6d6128391d6d8dba90cd26e8e5ad702 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sun, 6 Nov 2022 17:29:21 +0100 Subject: [PATCH 3/3] added new Methods to filter List by Crop ID --- .../taskList/TaskListModel.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java index 430d14c..cb766be 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -12,6 +12,7 @@ import java.time.LocalDate; import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.function.Predicate; import java.util.stream.Collectors; public class TaskListModel { @@ -78,6 +79,10 @@ public class TaskListModel { taskDatabase.removeTask(task); } + private List filterListByCrop(List taskList, Long cropId) { + return taskList.stream().filter(task -> task.getCropId() == cropId).collect(Collectors.toList()); + } + /** * Method to get all Tasks * @return List of all Tasks @@ -87,6 +92,15 @@ public class TaskListModel { return getFilteredTaskList(LocalDate.MIN, LocalDate.MAX); } + /** + * Method to get all Tasks for specific Crop + * @return List of all Tasks for with given CropID + * @throws IOException If the database cannot be accessed + */ + public List getTaskListForCrop(Long cropId) throws IOException { + return filterListByCrop(getTaskList(), cropId); + } + /** * Method to get all Tasks which are today or in future * @return List of Tasks @@ -96,6 +110,15 @@ public class TaskListModel { return getFilteredTaskList(LocalDate.now(), LocalDate.MAX); } + /** + * Method to get all Tasks which are today or in future for specific Crop + * @return List of Tasks with given crop ID + * @throws IOException If the database cannot be accessed + */ + public List getFutureTasksForCrop(Long cropId) throws IOException { + return filterListByCrop(getFutureTasks(), cropId); + } + /** * Method to get all Tasks which are today or in past * @return List of Tasks @@ -105,6 +128,15 @@ public class TaskListModel { return getFilteredTaskList(LocalDate.MIN, LocalDate.now()); } + /** + * Method to get all Tasks which are today or in past for specifc crop + * @return List of Tasks with given grop id + * @throws IOException If the database cannot be accessed + */ + public List getPastTasksForCrop(Long cropId) throws IOException { + return filterListByCrop(getPastTasks(), cropId); + } + /** * Method to get an List of 7 Tasklists for the next 7 days. Index 0 is Tasklist for Today. * @return List with length 7 (List>) @@ -119,6 +151,20 @@ public class TaskListModel { return dayTaskList; } + /** + * Method to get an List of 7 Tasklists for the next 7 days. (Filtered Index 0 is Tasklist for Today. + * @return List with length 7 (List>) + * @throws IOException If the database cannot be accessed + */ + public List> getTasksUpcomingWeekForCrop(Long cropId) throws IOException { + List> dayTaskList = new ArrayList<>(); + for(int i = 0; i < 7; i++) { + LocalDate date = LocalDate.now().plusDays(i); + dayTaskList.add(filterListByCrop(taskDatabase.getTaskList(date, date), cropId)); + } + return dayTaskList; + } + /** * Method to get Tasklist filtered by date. * @param start the start date for the filter