From 160880d4f525db470c4b0ab89cbc177fb17d93b6 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Mon, 31 Oct 2022 09:42:39 +0100 Subject: [PATCH 1/6] removed useless quotes, corrected typo --- src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json index 927835d..cf08ac5 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -30,7 +30,7 @@ "name": "Germinate", "relativeStartDate": -14, "relativeEndDate": null, - "description": "\"Take an egg carton and fill it with soil. Put the seedling deep enaugh so its half covered with soil. Keep it in 10-15 * Celsius with lots of light.\"", + "description": "Take an egg carton and fill it with soil. Put the seedling deep enough so its half covered with soil. Keep it in 10-15 * Celsius with lots of light.", "interval": null, "isOptional": false } @@ -52,7 +52,7 @@ "name": "hilling", "relativeStartDate": 0, "relativeEndDate": null, - "description": "\"When the plants are 20 cm tall, begin hilling the potatoes by gently mounding the soil from the center of your rows around the stems of the plant. Mound up the soil around the plant until just the top few leaves show above the soil. Two weeks later, hill up the soil again when the plants grow another 20 cm.\"", + "description": "When the plants are 20 cm tall, begin hilling the potatoes by gently mounding the soil from the center of your rows around the stems of the plant. Mound up the soil around the plant until just the top few leaves show above the soil. Two weeks later, hill up the soil again when the plants grow another 20 cm.", "interval": 21, "isOptional": false } From 146c43d5d9cf792a3aa916348bd5a5fac477ff04 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Mon, 31 Oct 2022 10:07:06 +0100 Subject: [PATCH 2/6] extended tests for Zones --- .../io/JsonPlantDatabase.java | 7 +++ .../io/JsonPlantDatabaseTest.java | 45 ++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java index aa06e65..f4872ae 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java @@ -1,5 +1,6 @@ package ch.zhaw.gartenverwaltung.io; +import ch.zhaw.gartenverwaltung.types.GrowthPhase; import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant; import com.fasterxml.jackson.databind.ObjectMapper; @@ -82,6 +83,12 @@ public class JsonPlantDatabase implements PlantDatabase { result = mapper.readerForListOf(Plant.class).readValue(dataSource); for (Plant plant : result) { + // for discussion because of failing tests: + // for(GrowthPhase growthPhase: plant.lifecycle()) { + // if(growthPhase.zone()==zone) { + // keep in result, remove if no match + // } + // } plant.inZone(currentZone); } diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java index b554502..24b4b53 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java @@ -26,7 +26,7 @@ public class JsonPlantDatabaseTest { @Test @DisplayName("Check if results are retrieved completely") - void getPlantList() { + void getPlantListNotEmpty() { List testList; try { testList = testDatabase.getPlantList(HardinessZone.ZONE_8A); @@ -42,13 +42,47 @@ public class JsonPlantDatabaseTest { Assertions.assertEquals(expected,names); } + @Test + @DisplayName("Check if results are retrieved correctly when empty") + void getPlantListEmpty() { + List testList; + try { + testList = testDatabase.getPlantList(HardinessZone.ZONE_1A); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (HardinessZoneNotSetException e) { + throw new RuntimeException(e); + } + Assertions.assertEquals(0, testList.size()); + + + } + @Test @DisplayName("Check whether single access works.") - void getPlantById() { + void getPlantByIdAndZone() { Optional testPlant; try { - testDatabase.getPlantList(HardinessZone.ZONE_8A); - testPlant = testDatabase.getPlantById(1); + testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (HardinessZoneNotSetException e) { + throw new RuntimeException(e); + } + Assertions.assertTrue(testPlant.isPresent()); + Assertions.assertEquals("Early Carrot", testPlant.get().name()); + } + + @Test + @DisplayName("Check whether single access respects zone correctly.") + void getPlantByIdAndWrongZone() { + Optional testPlant; + try { + testPlant = testDatabase.getPlantById(HardinessZone.ZONE_1A, 1); + Assertions.assertFalse(testPlant.isPresent()); + testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1); + Assertions.assertTrue(testPlant.isPresent()); + } catch (IOException e) { throw new RuntimeException(e); } catch (HardinessZoneNotSetException e) { @@ -63,8 +97,7 @@ public class JsonPlantDatabaseTest { void getPlantByIdMustFail() { Optional testPlant; try { - testDatabase.getPlantList(HardinessZone.ZONE_8A); - testPlant = testDatabase.getPlantById(99); + testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 99); } catch (IOException e) { throw new RuntimeException(e); } catch (HardinessZoneNotSetException e) { From 5411ac69aeb18308357c62f16de698a8764e1f77 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Mon, 31 Oct 2022 11:15:27 +0100 Subject: [PATCH 3/6] initial GardenPlan tests --- .../io/JsonGardenPlanTest.java | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java new file mode 100644 index 0000000..159cccf --- /dev/null +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java @@ -0,0 +1,101 @@ +package ch.zhaw.gartenverwaltung.io; + +import ch.zhaw.gartenverwaltung.types.Crop; +import ch.zhaw.gartenverwaltung.types.HardinessZone; +import ch.zhaw.gartenverwaltung.types.Plant; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class JsonGardenPlanTest { + GardenPlan testDatabase; + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + + @BeforeEach + void connectToDb() { + testDatabase = new JsonGardenPlan(); + } + + + @Test + @DisplayName("Check if results are retrieved completely.") + void getCropsNotEmpty() { + List testList; + try { + testList = testDatabase.getCrops(); + } catch (IOException e) { + throw new RuntimeException(e); + } + Assertions.assertEquals(3, testList.size()); + + List plantIds = testList.stream().map(Crop::getPlantId).collect(Collectors.toList()); + List expected = Arrays.asList(1l, 1l, 0l); + Assertions.assertEquals(expected, plantIds); + } + + @Test + @DisplayName("Check whether single access works.") + void getCropById() { + Optional testCrop; + try { + testCrop = testDatabase.getCropById(1); + } catch (IOException e) { + throw new RuntimeException(e); + } + assertTrue(testCrop.isPresent()); + Assertions.assertEquals(1, testCrop.get().getPlantId()); + } + + + @Test + @DisplayName("Check for a nonexisting crop.") + void getCropByIdMustFail() { + Optional testCrop; + try { + testCrop = testDatabase.getCropById(99); + } catch (IOException e) { + throw new RuntimeException(e); + } + Assertions.assertFalse(testCrop.isPresent()); + } + + @Test + @DisplayName("Add new Crop.") + void addNewCrop() { + + + Crop crop = new Crop(2l, LocalDate.parse("22.02.2023", formatter)); + try { + testDatabase.saveCrop(crop); + assertTrue(crop.getCropId().isPresent()); + Optional testCrop; + try { + testCrop = testDatabase.getCropById(crop.getCropId().get()); + } catch (IOException e) { + throw new RuntimeException(e); + } + assertTrue(testCrop.isPresent()); + Assertions.assertEquals(2l, testCrop.get().getPlantId()); + + } catch (IOException e) { + throw new RuntimeException(e); + } + + + + } +} + From 82eab6d5cd6ff451bc5eb367544d966d04538729 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Mon, 31 Oct 2022 11:21:45 +0100 Subject: [PATCH 4/6] continue GardenPlan tests --- .../io/JsonGardenPlanTest.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java index 159cccf..d6e165b 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java @@ -75,8 +75,6 @@ public class JsonGardenPlanTest { @Test @DisplayName("Add new Crop.") void addNewCrop() { - - Crop crop = new Crop(2l, LocalDate.parse("22.02.2023", formatter)); try { testDatabase.saveCrop(crop); @@ -89,13 +87,23 @@ public class JsonGardenPlanTest { } assertTrue(testCrop.isPresent()); Assertions.assertEquals(2l, testCrop.get().getPlantId()); - } catch (IOException e) { throw new RuntimeException(e); } + } - - + @Test + @DisplayName("Remove crop") + void removeCrop(){ + try { + Optional crop = testDatabase.getCropById(2l); + Assertions.assertTrue(crop.isPresent()); + testDatabase.removeCrop(crop.get()); + crop = testDatabase.getCropById(2l); + Assertions.assertFalse(crop.isPresent()); + } catch (IOException e) { + throw new RuntimeException(e); + } } } From ce93531ab85f45c2e762d706321d678fe07224df Mon Sep 17 00:00:00 2001 From: David Guler Date: Thu, 3 Nov 2022 14:25:25 +0100 Subject: [PATCH 5/6] Made tests pass for plants not in zone --- .../io/JsonPlantDatabase.java | 21 +++---- .../io/JsonGardenPlanTest.java | 62 +++++++++---------- 2 files changed, 37 insertions(+), 46 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java index f4872ae..3f84f23 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java @@ -1,6 +1,5 @@ package ch.zhaw.gartenverwaltung.io; -import ch.zhaw.gartenverwaltung.types.GrowthPhase; import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant; import com.fasterxml.jackson.databind.ObjectMapper; @@ -32,6 +31,7 @@ public class JsonPlantDatabase implements PlantDatabase { * Creating constant objects required to deserialize the {@link MonthDay} classes */ private final static JavaTimeModule timeModule = new JavaTimeModule(); + static { DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd"); MonthDayDeserializer dateDeserializer = new MonthDayDeserializer(dateFormat); @@ -82,21 +82,16 @@ public class JsonPlantDatabase implements PlantDatabase { List result; result = mapper.readerForListOf(Plant.class).readValue(dataSource); - for (Plant plant : result) { - // for discussion because of failing tests: - // for(GrowthPhase growthPhase: plant.lifecycle()) { - // if(growthPhase.zone()==zone) { - // keep in result, remove if no match - // } - // } - plant.inZone(currentZone); - } - - // Turn list into a HashMap with structure id => Plant plantMap = result.stream() + // Remove plants not in the current zone + .filter(plant -> { + plant.inZone(currentZone); + return !plant.lifecycle().isEmpty(); + }) + // Create Hashmap from results .collect(HashMap::new, (res, plant) -> res.put(plant.id(), plant), - (existing, replacement) -> { }); + (existing, replacement) -> {}); } } } diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java index d6e165b..ea22b3b 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java @@ -1,16 +1,17 @@ package ch.zhaw.gartenverwaltung.io; import ch.zhaw.gartenverwaltung.types.Crop; -import ch.zhaw.gartenverwaltung.types.HardinessZone; -import ch.zhaw.gartenverwaltung.types.Plant; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.io.IOException; -import java.text.ParseException; -import java.text.SimpleDateFormat; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Arrays; @@ -18,15 +19,24 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; public class JsonGardenPlanTest { - GardenPlan testDatabase; - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + private GardenPlan testDatabase; + private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + /** + * Files to isolate the test-units + */ + private final URL dbDataSource = this.getClass().getResource("user-crops.json"); + private final URL testFile = this.getClass().getResource("test-user-crops.json"); @BeforeEach - void connectToDb() { - testDatabase = new JsonGardenPlan(); + void connectToDb() throws URISyntaxException, IOException { + assertNotNull(testFile); + assertNotNull(dbDataSource); + Files.copy(Path.of(testFile.toURI()), Path.of(dbDataSource.toURI()), StandardCopyOption.REPLACE_EXISTING); + testDatabase = new JsonGardenPlan(dbDataSource); } @@ -42,19 +52,14 @@ public class JsonGardenPlanTest { Assertions.assertEquals(3, testList.size()); List plantIds = testList.stream().map(Crop::getPlantId).collect(Collectors.toList()); - List expected = Arrays.asList(1l, 1l, 0l); + List expected = Arrays.asList(1L, 1L, 0L); Assertions.assertEquals(expected, plantIds); } @Test @DisplayName("Check whether single access works.") - void getCropById() { - Optional testCrop; - try { - testCrop = testDatabase.getCropById(1); - } catch (IOException e) { - throw new RuntimeException(e); - } + void getCropById() throws IOException { + Optional testCrop = testDatabase.getCropById(1); assertTrue(testCrop.isPresent()); Assertions.assertEquals(1, testCrop.get().getPlantId()); } @@ -62,31 +67,22 @@ public class JsonGardenPlanTest { @Test @DisplayName("Check for a nonexisting crop.") - void getCropByIdMustFail() { - Optional testCrop; - try { - testCrop = testDatabase.getCropById(99); - } catch (IOException e) { - throw new RuntimeException(e); - } + void getCropByIdMustFail() throws IOException { + Optional testCrop = testDatabase.getCropById(99); Assertions.assertFalse(testCrop.isPresent()); } @Test @DisplayName("Add new Crop.") void addNewCrop() { - Crop crop = new Crop(2l, LocalDate.parse("22.02.2023", formatter)); + Crop crop = new Crop(3L, LocalDate.parse("2023-02-22", formatter)); try { testDatabase.saveCrop(crop); assertTrue(crop.getCropId().isPresent()); - Optional testCrop; - try { - testCrop = testDatabase.getCropById(crop.getCropId().get()); - } catch (IOException e) { - throw new RuntimeException(e); - } + Optional testCrop = testDatabase.getCropById(crop.getCropId().get()); + assertTrue(testCrop.isPresent()); - Assertions.assertEquals(2l, testCrop.get().getPlantId()); + Assertions.assertEquals(crop, testCrop.get()); } catch (IOException e) { throw new RuntimeException(e); } @@ -96,10 +92,10 @@ public class JsonGardenPlanTest { @DisplayName("Remove crop") void removeCrop(){ try { - Optional crop = testDatabase.getCropById(2l); + Optional crop = testDatabase.getCropById(2L); Assertions.assertTrue(crop.isPresent()); testDatabase.removeCrop(crop.get()); - crop = testDatabase.getCropById(2l); + crop = testDatabase.getCropById(2L); Assertions.assertFalse(crop.isPresent()); } catch (IOException e) { throw new RuntimeException(e); From 83bc0118704315923b458ce37db82f4d35c2cbaa Mon Sep 17 00:00:00 2001 From: David Guler Date: Thu, 3 Nov 2022 14:28:26 +0100 Subject: [PATCH 6/6] #26 added fixed test files, made Crop class testable --- .../gartenverwaltung/io/JsonGardenPlan.java | 24 ++++++++++--- .../ch/zhaw/gartenverwaltung/types/Crop.java | 19 ++++++++++ .../io/JsonPlantDatabaseTest.java | 36 +++++-------------- .../gartenverwaltung/io/test-user-crops.json | 20 +++++++++++ .../zhaw/gartenverwaltung/io/user-crops.json | 2 ++ 5 files changed, 69 insertions(+), 32 deletions(-) create mode 100644 src/test/resources/ch/zhaw/gartenverwaltung/io/test-user-crops.json create mode 100644 src/test/resources/ch/zhaw/gartenverwaltung/io/user-crops.json diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlan.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlan.java index 5a25a0a..3100301 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlan.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlan.java @@ -20,7 +20,7 @@ import java.util.Map; import java.util.Optional; public class JsonGardenPlan implements GardenPlan { - private final URL dataSource = getClass().getResource("user-crops.json"); + private final URL dataSource; private IdProvider idProvider; private Map cropMap = Collections.emptyMap(); @@ -40,7 +40,21 @@ public class JsonGardenPlan implements GardenPlan { } /** - * @see GardenPlan#getCrops() + * Default constructor + */ + public JsonGardenPlan() { + this.dataSource = getClass().getResource("user-crops.json"); + } + + /** + * Constructor to use a specified {@link URL} as a {@link #dataSource} + * @param dataSource A {@link URL} to the file to be used as a data source + */ + public JsonGardenPlan(URL dataSource) { + this.dataSource = dataSource; + } + /** + * {@inheritDoc} */ @Override public List getCrops() throws IOException { @@ -51,7 +65,7 @@ public class JsonGardenPlan implements GardenPlan { } /** - * @see GardenPlan#getCropById(long) + * {@inheritDoc} */ @Override public Optional getCropById(long id) throws IOException { @@ -62,7 +76,7 @@ public class JsonGardenPlan implements GardenPlan { } /** - * @see GardenPlan#saveCrop(Crop) + * {@inheritDoc} * * Saves a crop to the database. * If no {@link Crop#cropId} is set, one will be generated and @@ -79,7 +93,7 @@ public class JsonGardenPlan implements GardenPlan { } /** - * @see GardenPlan#removeCrop(Crop) + * {@inheritDoc} */ @Override public void removeCrop(Crop crop) throws IOException { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java index fbd6286..e8f539f 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java @@ -1,6 +1,7 @@ package ch.zhaw.gartenverwaltung.types; import java.time.LocalDate; +import java.util.Objects; import java.util.Optional; public class Crop { @@ -41,6 +42,24 @@ public class Crop { public LocalDate getStartDate() { return startDate; } public double getArea() { return area; } + @Override + public boolean equals(Object other) { + if (this == other) return true; + if (other instanceof Crop otherCrop) { + return Objects.equals(this.cropId, otherCrop.cropId) && + plantId == otherCrop.plantId && + startDate != null && startDate.equals(otherCrop.startDate) && + area == otherCrop.area; + } + return false; + } + + @Override + public int hashCode() { + int startCode = startDate != null ? startDate.hashCode() : 0; + return (int) plantId ^ (startCode << 16); + } + @Override public String toString() { return String.format("Crop [ cropId: %d, plantId: %d, startDate: %s, area: %f ]", diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java index 24b4b53..b8bbeb3 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java @@ -8,7 +8,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.io.IOException; -import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -16,7 +15,6 @@ import java.util.stream.Collectors; public class JsonPlantDatabaseTest { PlantDatabase testDatabase; - SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy"); @BeforeEach void connectToDb() { @@ -30,9 +28,7 @@ public class JsonPlantDatabaseTest { List testList; try { testList = testDatabase.getPlantList(HardinessZone.ZONE_8A); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (HardinessZoneNotSetException e) { + } catch (IOException | HardinessZoneNotSetException e) { throw new RuntimeException(e); } Assertions.assertEquals(3, testList.size()); @@ -48,9 +44,7 @@ public class JsonPlantDatabaseTest { List testList; try { testList = testDatabase.getPlantList(HardinessZone.ZONE_1A); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (HardinessZoneNotSetException e) { + } catch (IOException | HardinessZoneNotSetException e) { throw new RuntimeException(e); } Assertions.assertEquals(0, testList.size()); @@ -64,9 +58,7 @@ public class JsonPlantDatabaseTest { Optional testPlant; try { testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (HardinessZoneNotSetException e) { + } catch (IOException | HardinessZoneNotSetException e) { throw new RuntimeException(e); } Assertions.assertTrue(testPlant.isPresent()); @@ -75,20 +67,12 @@ public class JsonPlantDatabaseTest { @Test @DisplayName("Check whether single access respects zone correctly.") - void getPlantByIdAndWrongZone() { - Optional testPlant; - try { - testPlant = testDatabase.getPlantById(HardinessZone.ZONE_1A, 1); - Assertions.assertFalse(testPlant.isPresent()); - testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1); - Assertions.assertTrue(testPlant.isPresent()); - - } catch (IOException e) { - throw new RuntimeException(e); - } catch (HardinessZoneNotSetException e) { - throw new RuntimeException(e); - } + void getPlantByIdAndWrongZone() throws HardinessZoneNotSetException, IOException { + Optional testPlant = testDatabase.getPlantById(HardinessZone.ZONE_1A, 1); + Assertions.assertFalse(testPlant.isPresent()); + testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1); Assertions.assertTrue(testPlant.isPresent()); + Assertions.assertEquals("Early Carrot", testPlant.get().name()); } @@ -98,9 +82,7 @@ public class JsonPlantDatabaseTest { Optional testPlant; try { testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 99); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (HardinessZoneNotSetException e) { + } catch (IOException | HardinessZoneNotSetException e) { throw new RuntimeException(e); } Assertions.assertFalse(testPlant.isPresent()); diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/test-user-crops.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-user-crops.json new file mode 100644 index 0000000..ebd1d2d --- /dev/null +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-user-crops.json @@ -0,0 +1,20 @@ +[ + { + "cropId": 0, + "plantId": 1, + "startDate": "2023-02-25", + "area": 0.5 + }, + { + "cropId": 1, + "plantId": 1, + "startDate": "2023-03-01", + "area": 0.5 + }, + { + "cropId": 2, + "plantId": 0, + "startDate": "2023-03-25", + "area": 1.5 + } +] \ No newline at end of file diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/user-crops.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/user-crops.json new file mode 100644 index 0000000..32960f8 --- /dev/null +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/user-crops.json @@ -0,0 +1,2 @@ +[ +] \ No newline at end of file