diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java index 2a515d7..2f8959e 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java @@ -1,7 +1,12 @@ package ch.zhaw.gartenverwaltung.io; +import ch.zhaw.gartenverwaltung.models.GardenSchedule; +import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; +import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Task; +import javafx.application.Platform; import org.junit.jupiter.api.*; +import org.mockito.ArgumentCaptor; import org.mockito.ArgumentMatchers; import org.mockito.Mockito; @@ -13,6 +18,7 @@ import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.time.LocalDate; import java.time.format.DateTimeFormatter; +import java.util.Arrays; import java.util.List; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -27,6 +33,22 @@ public class JsonTaskListTest { private final URL dbDataSource = this.getClass().getResource("test-taskdb.json"); private final URL testFile = this.getClass().getResource("template-taskdb.json"); + + @BeforeAll + static void setUpAll() { + try{ + Platform.startup(()->{}); + }catch (IllegalStateException ise){ + //ignore double launches + + } + } + + @AfterAll + static void tearDownAll() { + //Dont do: Platform.exit(); + } + @BeforeEach void connectToDb() throws URISyntaxException, IOException { assertNotNull(testFile); @@ -35,6 +57,10 @@ public class JsonTaskListTest { testDatabase = new JsonTaskList(dbDataSource); } + private void reloadDb() { + testDatabase = new JsonTaskList(dbDataSource); + } + @Test @DisplayName("Check if results are retrieved completely") void getTasks() { @@ -47,7 +73,8 @@ public class JsonTaskListTest { throw new RuntimeException(e); } - Assertions.assertEquals(3, taskList.size()); + List ids = taskList.stream().map(t -> t.getId().orElse(0L)).toList(); + Assertions.assertEquals(Arrays.asList(1L, 2L, 5L), ids); } @@ -65,7 +92,8 @@ public class JsonTaskListTest { throw new RuntimeException(e); } - Assertions.assertEquals(4, taskList.size()); + List ids = taskList.stream().map(t -> t.getId().orElse(0L)).toList(); + Assertions.assertEquals(Arrays.asList(1L, 2L, 5L, 9L), ids); } catch (IOException e) { throw new RuntimeException(e); @@ -83,7 +111,8 @@ public class JsonTaskListTest { taskList = testDatabase.getTaskList(LocalDate.parse("2022-04-30", formatter), LocalDate.parse("2022-05-31", formatter)); - Assertions.assertEquals(2, taskList.size()); + List ids = taskList.stream().map(t -> t.getId().orElse(0L)).toList(); + Assertions.assertEquals(Arrays.asList(1L, 5L), ids); } catch (IOException e) { throw new RuntimeException(e); } @@ -100,8 +129,8 @@ public class JsonTaskListTest { throw new RuntimeException(e); } - Assertions.assertEquals(6, taskList.size()); - + List ids = taskList.stream().map(t -> t.getId().orElse(0L)).toList(); + Assertions.assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L), ids); } @@ -116,10 +145,20 @@ public class JsonTaskListTest { } Assertions.assertEquals(0, taskList.size()); + + try { + taskList = testDatabase.getTaskForCrop(1); + } catch (IOException e) { + throw new RuntimeException(e); + } + + List ids = (taskList.stream().map( task -> task.getId().orElse(0L)).toList()); + Assertions.assertEquals(Arrays.asList(7L, 8L), ids); + } @Test - void testDefaultConstructor(){ + void testDefaultConstructor() { JsonTaskList db = new JsonTaskList(); try { assertNotNull(db.getTaskForCrop(0)); @@ -138,6 +177,43 @@ public class JsonTaskListTest { } catch (IOException e) { throw new RuntimeException(e); } - verify(mockObs, times(1)).onChange(ArgumentMatchers.anyList()); + + ArgumentCaptor> captor = ArgumentCaptor.forClass(List.class); + + verify(mockObs, times(1)).onChange(captor.capture()); + List ids = captor.getValue().stream().map(t -> t.getId().orElse(0L)).toList(); + Assertions.assertEquals(Arrays.asList(7L, 8L), ids); + } + + @Test + @Tag("IntegrationTest") + void testComplete() { + JsonPlantList plantList = new JsonPlantList(); + try { + testDatabase.removeTasksForCrop(0); + testDatabase.removeTasksForCrop(1); + } catch (IOException e) { + throw new RuntimeException(e); + } + GardenSchedule gardenSchedule = null; + try { + gardenSchedule = new GardenSchedule(testDatabase, plantList); + } catch (IOException e) { + throw new RuntimeException(e); + } + Crop crop = new Crop(3L, LocalDate.parse("2022-12-01", formatter)); + + try { + gardenSchedule.planTasksForCrop(crop); + + reloadDb(); + + List tasks = gardenSchedule.getTaskList(); + List tasknames = (tasks.stream().map( task -> task.getName().substring(0,3).toLowerCase()).toList()); + Assertions.assertEquals(Arrays.asList("ger","wat","hil","har"), tasknames); + } catch (IOException | PlantNotFoundException | HardinessZoneNotSetException e) { + throw new RuntimeException(e); + } + } } \ No newline at end of file diff --git a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java index 4f45242..67928a9 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java @@ -34,12 +34,17 @@ class GardenScheduleTest { @BeforeAll static void setUpAll() { + try{ Platform.startup(()->{}); + }catch (IllegalStateException ise){ + //ignore double launches + + } } @AfterAll static void tearDownAll() { - Platform.exit(); + //Dont do: Platform.exit(); } @BeforeEach @@ -234,5 +239,8 @@ class GardenScheduleTest { testTaskList.getTaskList(LocalDate.MIN,LocalDate.MAX).get(0); } - + @Test + void testPlantNotFoundException() throws HardinessZoneNotSetException, IOException { + assertThrowsExactly(PlantNotFoundException.class, () -> { model.planTasksForCrop(new Crop(0, exampleStartDate)); }); + } } \ No newline at end of file diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/template-taskdb.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/template-taskdb.json index 53ea6c3..cec82ae 100644 --- a/src/test/resources/ch/zhaw/gartenverwaltung/io/template-taskdb.json +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/template-taskdb.json @@ -64,5 +64,27 @@ "endDate" : "2022-09-01", "interval" : 0, "cropId" : 0 + }, + { + "id" : 7, + "name" : "sow plant", + "description": "Plant the seeds, crops in de bed.", + "startDate" : "2022-11-01", + "nextExecution": "2022-11-01", + "nextNotification": "2022-11-01", + "endDate" : "2022-11-01", + "interval" : 0, + "cropId" : 1 + }, + { + "id" : 8, + "name" : "harvest plant", + "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", + "startDate" : "2022-12-01", + "nextExecution": "2022-12-01", + "nextNotification": "2022-12-01", + "endDate" : "2022-12-01", + "interval" : 0, + "cropId" : 1 } ] \ No newline at end of file