From 146c43d5d9cf792a3aa916348bd5a5fac477ff04 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Mon, 31 Oct 2022 10:07:06 +0100 Subject: [PATCH] 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) {