From 77541c282c866ef5407bf752bc65f45c7b3bfcd1 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Sat, 26 Nov 2022 11:22:27 +0100 Subject: [PATCH 1/5] fixed RemoveTasksForCrop --- .../ch/zhaw/gartenverwaltung/io/JsonTaskList.java | 3 ++- .../zhaw/gartenverwaltung/io/JsonTaskListTest.java | 12 +++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java index 7f6d36b..caecb1a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskList.java @@ -88,7 +88,8 @@ public class JsonTaskList implements TaskList { if(taskMap.isEmpty()) { loadTaskListFromFile(); } - taskMap.values().removeIf(task -> task.getCropId() == cropId); + taskMap.entrySet().removeIf(entry -> entry.getValue().getCropId() == cropId); + writeTaskListToFile(); notifySubscribers(); } diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java index bc11d99..359ea9a 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java @@ -47,17 +47,16 @@ public class JsonTaskListTest { } - @Disabled("disabled until adding works.") @Test @DisplayName("Add task.") void addTask() { - Task task = new Task("Testtask", "This is a test Task.", LocalDate.parse("01.05.2022", formatter), 1); + Task task = new Task("Testtask", "This is a test Task.", LocalDate.parse("2022-05-01", formatter), 1); try { testDatabase.saveTask(task); List taskList; try { - taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022", formatter), - LocalDate.parse("31.05.2022", formatter)); + taskList = testDatabase.getTaskList(LocalDate.parse("2022-04-30", formatter), + LocalDate.parse("2022-05-31", formatter)); } catch (IOException e) { throw new RuntimeException(e); } @@ -72,8 +71,7 @@ public class JsonTaskListTest { @Test @DisplayName("Remove task.") void removeTask() { - Task task = new Task("Dummy", "Dummy", LocalDate.parse("2022-05-31", formatter), 1) - .withId(2); + Task task = new Task("Dummy", "Dummy", LocalDate.parse("2022-05-31", formatter), 1).withId(2); try { testDatabase.removeTask(task); List taskList; @@ -102,7 +100,7 @@ public class JsonTaskListTest { } - @Disabled("Disabled until removing works") + @Test void removeTasksForCrop() { List taskList; From 4e720c2ddc620ed89d114d52fd41890ee300e22c Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Sat, 26 Nov 2022 11:54:47 +0100 Subject: [PATCH 2/5] finished tests for Plant, added fix for Plant --- .../ch/zhaw/gartenverwaltung/types/Plant.java | 6 +- .../gartenverwaltung/types/PlantTest.java | 97 +++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 src/test/java/ch/zhaw/gartenverwaltung/types/PlantTest.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java index b77540c..30773df 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Plant.java @@ -66,16 +66,16 @@ public record Plant( public int timeToHarvest(int group) { List activeLifecycle = lifecycleForGroup(group); GrowthPhase sow = activeLifecycle.stream() - .filter(growthPhase -> !growthPhase.type().equals(GrowthPhaseType.SOW)) + .filter(growthPhase -> growthPhase.type().equals(GrowthPhaseType.SOW)) .findFirst() .orElseThrow(); GrowthPhase harvest = activeLifecycle.stream() - .filter(growthPhase -> !growthPhase.type().equals(GrowthPhaseType.HARVEST)) + .filter(growthPhase -> growthPhase.type().equals(GrowthPhaseType.HARVEST)) .findFirst() .orElseThrow(); int currentYear = LocalDate.now().getYear(); - return (int) DAYS.between(harvest.startDate().atYear(currentYear), sow.startDate().atYear(currentYear)); + return (int) DAYS.between(sow.startDate().atYear(currentYear),harvest.startDate().atYear(currentYear)); } public int lifecycleGroupFromHarvestDate(LocalDate harvestDate) { diff --git a/src/test/java/ch/zhaw/gartenverwaltung/types/PlantTest.java b/src/test/java/ch/zhaw/gartenverwaltung/types/PlantTest.java new file mode 100644 index 0000000..88cdf54 --- /dev/null +++ b/src/test/java/ch/zhaw/gartenverwaltung/types/PlantTest.java @@ -0,0 +1,97 @@ +package ch.zhaw.gartenverwaltung.types; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.time.MonthDay; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class PlantTest { + + Plant testPlant; + + @BeforeEach + void setUp() { + List growthPhases = new ArrayList<>(); + growthPhases.add(new GrowthPhase(MonthDay.of(2, 1), MonthDay.of(4, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.SOW, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(4, 2), MonthDay.of(6, 5), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(6, 3), MonthDay.of(8, 6), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(3, 1), MonthDay.of(5, 4), 1, new WateringCycle(0, 0, null), GrowthPhaseType.SOW, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(5, 2), MonthDay.of(7, 5), 1, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(7, 3), MonthDay.of(9, 6), 1, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(4, 1), MonthDay.of(6, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.SOW, HardinessZone.ZONE_1A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(6, 2), MonthDay.of(8, 5), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_1A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(7, 2), MonthDay.of(9, 5), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_1A, new ArrayList<>())); + growthPhases.add(new GrowthPhase(MonthDay.of(8, 3), MonthDay.of(10, 6), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_1A, new ArrayList<>())); + + testPlant = 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<>(), + growthPhases); + } + + @AfterEach + void tearDown() { + } + + @Test + void inZone() { + assertEquals(7,testPlant.lifecycleForGroup(0).size()); + testPlant.inZone(HardinessZone.ZONE_8A); + assertEquals(3,testPlant.lifecycleForGroup(0).size()); + assertEquals(Arrays.asList(3,5,7),testPlant.lifecycleForGroup(1).stream().map(gp ->gp.startDate().getMonthValue()).toList() ); + testPlant.inZone(HardinessZone.ZONE_1A); + assertEquals(0,testPlant.lifecycleForGroup(0).size()); + } + + @Test + void lifecycleForGroup() { + assertEquals(Arrays.asList(2,4,6,4,6,7,8),testPlant.lifecycleForGroup(0).stream().map(gp ->gp.startDate().getMonthValue()).toList() ); + assertEquals(Arrays.asList(3,5,7),testPlant.lifecycleForGroup(1).stream().map(gp ->gp.startDate().getMonthValue()).toList() ); + + } + + @Test + void sowDateFromHarvestDate() { + assertEquals(LocalDate.of(2022,8,1), + testPlant.sowDateFromHarvestDate(LocalDate.of(2022,12,1)) + ); + } + + @Test + void timeToHarvest() { + testPlant.inZone(HardinessZone.ZONE_8A); + assertEquals(122,testPlant.timeToHarvest(0)); + } + + @Test + void lifecycleGroupFromHarvestDate() { + testPlant.inZone(HardinessZone.ZONE_8A); + assertEquals(0,testPlant.lifecycleGroupFromHarvestDate(LocalDate.of(2022,6,30))); + assertEquals(1,testPlant.lifecycleGroupFromHarvestDate(LocalDate.of(2022,8,30))); + } + + @Test + void isDateInPhase() { + assertTrue(testPlant.isDateInPhase(LocalDate.of(2022,6,30),GrowthPhaseType.HARVEST)); + assertTrue(testPlant.isDateInPhase(LocalDate.of(2022,8,30),GrowthPhaseType.HARVEST)); + assertTrue(testPlant.isDateInPhase(LocalDate.of(2022,2,1),GrowthPhaseType.SOW)); + assertTrue(testPlant.isDateInPhase(LocalDate.of(2022,4,2),GrowthPhaseType.PLANT)); + assertFalse(testPlant.isDateInPhase(LocalDate.of(2022,6,30),GrowthPhaseType.SOW)); + + + } +} \ No newline at end of file From 560cea2ff9a715026c8d602cde5c807b45420d0b Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Sat, 26 Nov 2022 12:49:42 +0100 Subject: [PATCH 3/5] Extended coverage for tests --- .../io/JsonPlantListTest.java | 11 ++++++++ .../gartenverwaltung/io/JsonTaskListTest.java | 27 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java index b28c486..4ceeb25 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java @@ -95,5 +95,16 @@ public class JsonPlantListTest { } + @Test + void testDefaultConstructor(){ + JsonPlantList db = new JsonPlantList(); + try { + assertNotNull(db.getPlantList(HardinessZone.ZONE_8A)); + } catch (IOException | HardinessZoneNotSetException e) { + throw new RuntimeException(e); + } + + } + } diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java index 359ea9a..00e21e3 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskListTest.java @@ -2,6 +2,8 @@ package ch.zhaw.gartenverwaltung.io; import ch.zhaw.gartenverwaltung.types.Task; import org.junit.jupiter.api.*; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; import java.io.IOException; import java.net.URISyntaxException; @@ -14,6 +16,8 @@ import java.time.format.DateTimeFormatter; import java.util.List; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; public class JsonTaskListTest { @@ -113,4 +117,27 @@ public class JsonTaskListTest { Assertions.assertEquals(0, taskList.size()); } + + @Test + void testDefaultConstructor(){ + JsonTaskList db = new JsonTaskList(); + try { + assertNotNull(db.getTaskForCrop(0)); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + @Test + void testSubscription() { + TaskList.TaskListObserver mockObs = Mockito.mock(TaskList.TaskListObserver.class); + testDatabase.subscribe(mockObs); + try { + testDatabase.removeTasksForCrop(0); + } catch (IOException e) { + throw new RuntimeException(e); + } + verify(mockObs, times(1)).onChange(ArgumentMatchers.anyList()); + } } \ No newline at end of file From e75ececedb26447a112a052c56166bdfe05698c8 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Sat, 26 Nov 2022 14:29:49 +0100 Subject: [PATCH 4/5] Refactored AllSEASONS to ALLSEASONS and added tests --- .../gartenverwaltung/PlantsController.java | 4 +- .../zhaw/gartenverwaltung/types/Seasons.java | 2 +- .../gartenverwaltung/types/SeasonsTest.java | 38 +++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/test/java/ch/zhaw/gartenverwaltung/types/SeasonsTest.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java index 5961c5f..2acee0d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/PlantsController.java @@ -233,11 +233,11 @@ public class PlantsController { RadioButton radioButton = new RadioButton(season.getName()); radioButton.setToggleGroup(seasonGroup); radioButton.setPadding(new Insets(0, 0, 10, 0)); - if (season.equals(Seasons.AllSEASONS)) { + if (season.equals(Seasons.ALLSEASONS)) { radioButton.setSelected(true); } radioButton.selectedProperty().addListener((observable, oldValue, newValue) -> { - if (season.equals(Seasons.AllSEASONS)) { + if (season.equals(Seasons.ALLSEASONS)) { fillPlantListWithHardinessZone(); } else { try { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Seasons.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Seasons.java index 2a2d2d0..fd2601c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Seasons.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Seasons.java @@ -3,7 +3,7 @@ package ch.zhaw.gartenverwaltung.types; import java.time.MonthDay; public enum Seasons { - AllSEASONS("--01-01", "--12-31", "All Seasons"), + ALLSEASONS("--01-01", "--12-31", "All Seasons"), SPRING("--03-01", "--05-30", "Spring"), SUMMER("--06-01", "--08-30", "Summer"), AUTUMN("--09-01", "--11-30", "Autumn"), diff --git a/src/test/java/ch/zhaw/gartenverwaltung/types/SeasonsTest.java b/src/test/java/ch/zhaw/gartenverwaltung/types/SeasonsTest.java new file mode 100644 index 0000000..520a937 --- /dev/null +++ b/src/test/java/ch/zhaw/gartenverwaltung/types/SeasonsTest.java @@ -0,0 +1,38 @@ +package ch.zhaw.gartenverwaltung.types; + +import org.junit.jupiter.api.Test; + +import java.time.MonthDay; + +import static org.junit.jupiter.api.Assertions.*; + +class SeasonsTest { + + @Test + void getStartDate() { + assertEquals(MonthDay.of(1,1), Seasons.ALLSEASONS.getStartDate()); + assertEquals(MonthDay.of(3,1), Seasons.SPRING.getStartDate()); + assertEquals(MonthDay.of(6,1), Seasons.SUMMER.getStartDate()); + assertEquals(MonthDay.of(9,1), Seasons.AUTUMN.getStartDate()); + assertEquals(MonthDay.of(12,1), Seasons.WINTER.getStartDate()); + } + + @Test + void getEndDate() { + assertEquals(MonthDay.of(12,31), Seasons.ALLSEASONS.getEndDate()); + assertEquals(MonthDay.of(5,30), Seasons.SPRING.getEndDate()); + assertEquals(MonthDay.of(8,30), Seasons.SUMMER.getEndDate()); + assertEquals(MonthDay.of(11,30), Seasons.AUTUMN.getEndDate()); + assertEquals(MonthDay.of(2,28), Seasons.WINTER.getEndDate()); + } + + @Test + void getName() { + assertEquals("All Seasons", Seasons.ALLSEASONS.getName()); + assertEquals("Spring", Seasons.SPRING.getName()); + assertEquals("Summer", Seasons.SUMMER.getName()); + assertEquals("Autumn", Seasons.AUTUMN.getName()); + assertEquals("Winter", Seasons.WINTER.getName()); + + } +} \ No newline at end of file From 1dc2ad17749b38745f5e6d48e7bd92e18bc9da62 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Sat, 26 Nov 2022 14:30:16 +0100 Subject: [PATCH 5/5] Extended testcoverage for classes --- .../ch/zhaw/gartenverwaltung/io/JsonCropListTest.java | 9 +++++++++ .../ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java | 3 +++ .../zhaw/gartenverwaltung/models/GardenScheduleTest.java | 1 + .../gartenverwaltung/io/corrupt-template-user-crops.json | 7 +++++++ 4 files changed, 20 insertions(+) create mode 100644 src/test/resources/ch/zhaw/gartenverwaltung/io/corrupt-template-user-crops.json diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonCropListTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonCropListTest.java index 87763a8..2913ea0 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonCropListTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonCropListTest.java @@ -30,6 +30,7 @@ public class JsonCropListTest { */ private final URL dbDataSource = this.getClass().getResource("test-user-crops.json"); private final URL testFile = this.getClass().getResource("template-user-crops.json"); + private final URL corruptTestFile = this.getClass().getResource("corrupt-template-user-crops.json"); @BeforeEach void connectToDb() throws URISyntaxException, IOException { @@ -101,5 +102,13 @@ public class JsonCropListTest { throw new RuntimeException(e); } } + + @Test + void detectCorruptJsonResource(){ + JsonCropList tL = new JsonCropList(corruptTestFile); + Assertions.assertThrows(InvalidJsonException.class, () -> { + tL.getCrops(); + }); + } } diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java index 4ceeb25..161ca47 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java @@ -15,6 +15,7 @@ import java.util.Optional; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; public class JsonPlantListTest { private final URL testFile = this.getClass().getResource("test-plantdb.json"); @@ -62,6 +63,8 @@ public class JsonPlantListTest { void getPlantByIdAndZone() { Optional testPlant; try { + assertThrows(HardinessZoneNotSetException.class, () -> testDatabase.getPlantById(null,1).get()); + testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1); } catch (IOException | HardinessZoneNotSetException e) { throw new RuntimeException(e); diff --git a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java index 5e32687..0c05e36 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java @@ -188,6 +188,7 @@ class GardenScheduleTest { @Test void planTasksForCrop() throws HardinessZoneNotSetException, PlantNotFoundException, IOException { + assertThrows(PlantNotFoundException.class,()-> model.planTasksForCrop(new Crop())); 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); diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/corrupt-template-user-crops.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/corrupt-template-user-crops.json new file mode 100644 index 0000000..73bf592 --- /dev/null +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/corrupt-template-user-crops.json @@ -0,0 +1,7 @@ +[ + { + "plantId": 1, + "startDate": "2023-02-25", + "area": 0.5 + } +] \ No newline at end of file