From 7dd157b9d50bdf48ac8ec3e8b61d880c48846183 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 20 Oct 2022 21:26:30 +0200 Subject: [PATCH 1/8] implemented Methods to sort and filter PlantList --- .../plantList/PlantListModel.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java new file mode 100644 index 0000000..0327e59 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -0,0 +1,45 @@ +package ch.zhaw.gartenverwaltung.plantList; + +import ch.zhaw.gartenverwaltung.types.Plant; + +import java.util.Comparator; +import java.util.List; +import java.util.function.Function; +import java.util.function.Predicate; + +public class PlantListModel { + private List plantList; + + /** + * Comparators to create sorted Plant List + */ + public final Comparator sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name()); + public final Comparator getSortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id()); + public final Comparator sortBySpacing = (Plant o1, Plant o2) -> o1.spacing() - o2.spacing(); + + /** + * Functions to get Plant Attribute as String + */ + public final Function filterByName = Plant::name; + public final Function filterById = plant -> Long.toString(plant.id()); + + public PlantListModel(List plantList) { + setPlantList(plantList); + } + + public void setPlantList(List plantList) { + this.plantList = plantList; + } + + public List getPlantList() { + return getSortedPlantList(sortByName); + } + + public List getSortedPlantList(Comparator comparator) { + return plantList.stream().sorted(comparator).toList(); + } + + public List getFilteredList(String filterString, Function filterFunction) { + return plantList.stream().filter(plant -> filterFunction.apply(plant).contains(filterString)).toList(); + } +} From 5b0e472ec73978fa50e23682d2fb31561889c990 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 20 Oct 2022 21:33:42 +0200 Subject: [PATCH 2/8] implemented Methods to sort and filter PlantList --- .../plantList/PlantListModel.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java index 0327e59..913e2a5 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -23,6 +23,7 @@ public class PlantListModel { public final Function filterByName = Plant::name; public final Function filterById = plant -> Long.toString(plant.id()); + public PlantListModel(List plantList) { setPlantList(plantList); } @@ -31,15 +32,30 @@ public class PlantListModel { this.plantList = plantList; } + /** + * Method to get actual Plant List in alphabetic Order + * @return actual Plant List in alphabetic Order + */ public List getPlantList() { return getSortedPlantList(sortByName); } + /** + * Method to get actual Plant List sorted in custom order + * @param comparator comparator Object which is used to sort list + * @return actual Plant List in custom order + */ public List getSortedPlantList(Comparator comparator) { return plantList.stream().sorted(comparator).toList(); } - public List getFilteredList(String filterString, Function filterFunction) { + /** + * Method to get filtered Plant List with custom filter + * @param filterString String to search for plants + * @param filterFunction Function Object to get Plant attribute as String which must contain filter String + * @return filtered Plant List + */ + public List getFilteredPlantListbyString(String filterString, Function filterFunction) { return plantList.stream().filter(plant -> filterFunction.apply(plant).contains(filterString)).toList(); } } From e51da071bc4a0ff471e5c140016c01cb23b2c809 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 24 Oct 2022 12:44:11 +0200 Subject: [PATCH 3/8] updated method getFilteredPlantList --- .../gartenverwaltung/plantList/PlantListModel.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java index 913e2a5..22b7de2 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -4,7 +4,6 @@ import ch.zhaw.gartenverwaltung.types.Plant; import java.util.Comparator; import java.util.List; -import java.util.function.Function; import java.util.function.Predicate; public class PlantListModel { @@ -17,12 +16,6 @@ public class PlantListModel { public final Comparator getSortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id()); public final Comparator sortBySpacing = (Plant o1, Plant o2) -> o1.spacing() - o2.spacing(); - /** - * Functions to get Plant Attribute as String - */ - public final Function filterByName = Plant::name; - public final Function filterById = plant -> Long.toString(plant.id()); - public PlantListModel(List plantList) { setPlantList(plantList); @@ -51,11 +44,10 @@ public class PlantListModel { /** * Method to get filtered Plant List with custom filter - * @param filterString String to search for plants - * @param filterFunction Function Object to get Plant attribute as String which must contain filter String + * @param predicate predicate for filter * @return filtered Plant List */ - public List getFilteredPlantListbyString(String filterString, Function filterFunction) { - return plantList.stream().filter(plant -> filterFunction.apply(plant).contains(filterString)).toList(); + public List getFilteredPlantList(Predicate predicate) { + return plantList.stream().filter(predicate).toList(); } } From d1c5c584686492457ae6fe116415d6d1e5938176 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 24 Oct 2022 18:55:26 +0200 Subject: [PATCH 4/8] adapted class PlantListModel to PlantDatabase --- .../io/JsonPlantDatabase.java | 4 +- .../gartenverwaltung/io/PlantDatabase.java | 2 +- .../plantList/PlantListModel.java | 72 ++++++++++++++----- .../ch/zhaw/gartenverwaltung/io/plantdb.json | 2 +- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java index 5591acd..aa06e65 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java @@ -56,9 +56,9 @@ public class JsonPlantDatabase implements PlantDatabase { * @see PlantDatabase#getPlantById(long) */ @Override - public Optional getPlantById(long id) throws HardinessZoneNotSetException, IOException { + public Optional getPlantById(HardinessZone zone, long id) throws HardinessZoneNotSetException, IOException { if (plantMap.isEmpty()) { - loadPlantList(currentZone); + loadPlantList(zone); } return Optional.ofNullable(plantMap.get(id)); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java index 0b6908d..455791e 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java @@ -30,5 +30,5 @@ public interface PlantDatabase { * @throws IOException If the database cannot be accessed * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified */ - Optional getPlantById(long id) throws IOException, HardinessZoneNotSetException; + Optional getPlantById(HardinessZone zone, long id) throws IOException, HardinessZoneNotSetException; } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java index 22b7de2..86e21f2 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -1,53 +1,87 @@ package ch.zhaw.gartenverwaltung.plantList; +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; +import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase; +import ch.zhaw.gartenverwaltung.io.PlantDatabase; +import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant; +import java.io.IOException; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.function.Predicate; public class PlantListModel { - private List plantList; + private final PlantDatabase plantDatabase; + private HardinessZone currentZone; /** * Comparators to create sorted Plant List */ public final Comparator sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name()); public final Comparator getSortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id()); - public final Comparator sortBySpacing = (Plant o1, Plant o2) -> o1.spacing() - o2.spacing(); - - public PlantListModel(List plantList) { - setPlantList(plantList); + public PlantListModel() { + plantDatabase = new JsonPlantDatabase(); } - public void setPlantList(List plantList) { - this.plantList = plantList; + public void setCurrentZone(HardinessZone currentZone) { + this.currentZone = currentZone; + } + + public HardinessZone getCurrentZone() { + return currentZone; } /** * Method to get actual Plant List in alphabetic Order * @return actual Plant List in alphabetic Order */ - public List getPlantList() { - return getSortedPlantList(sortByName); + public List getPlantList(HardinessZone zone) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + return getSortedPlantList(zone, sortByName); + } + + + /** + * Method to get the actual Plant list in custom Order + * @param zone selected hardiness zone + * @param comparator comparator to sort the list + * @return sorted list with plants in the given hardiness zone + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified + */ + public List getSortedPlantList(HardinessZone zone, Comparator comparator) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + return plantDatabase.getPlantList(zone).stream().sorted(comparator).toList(); } /** - * Method to get actual Plant List sorted in custom order - * @param comparator comparator Object which is used to sort list - * @return actual Plant List in custom order + * Method to get Filtered plant list + * @param predicate predicate to filter the list + * @param zone selected hardiness zone + * @return filterd list with plants in the hardinness zone + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified */ - public List getSortedPlantList(Comparator comparator) { - return plantList.stream().sorted(comparator).toList(); + public List getFilteredPlantList(HardinessZone zone, Predicate predicate) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + return getPlantList(zone).stream().filter(predicate).toList(); } /** - * Method to get filtered Plant List with custom filter - * @param predicate predicate for filter - * @return filtered Plant List + * Method to get Filtered plant list by id by exact match + * @param zone selected hardiness zone + * @param id id of plant + * @return if id doesn't exist: empty List, else list with 1 plant entry. + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified */ - public List getFilteredPlantList(Predicate predicate) { - return plantList.stream().filter(predicate).toList(); + public List getFilteredPlantListById(HardinessZone zone, Long id) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + List plantList = new ArrayList<>(); + plantDatabase.getPlantById(zone, id).ifPresent(plantList::add); + return plantList; } } diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json index f513570..aae89bc 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -251,4 +251,4 @@ } ] } -] \ No newline at end of file +] From e5b5e1b88a35c205da02a4b49b06d1df8172f60f Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 24 Oct 2022 18:55:26 +0200 Subject: [PATCH 5/8] adapted class PlantListModel to PlantDatabase #12 --- .../io/JsonPlantDatabase.java | 4 +- .../gartenverwaltung/io/PlantDatabase.java | 2 +- .../plantList/PlantListModel.java | 72 ++++++++++++++----- .../ch/zhaw/gartenverwaltung/io/plantdb.json | 2 +- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java index 5591acd..aa06e65 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabase.java @@ -56,9 +56,9 @@ public class JsonPlantDatabase implements PlantDatabase { * @see PlantDatabase#getPlantById(long) */ @Override - public Optional getPlantById(long id) throws HardinessZoneNotSetException, IOException { + public Optional getPlantById(HardinessZone zone, long id) throws HardinessZoneNotSetException, IOException { if (plantMap.isEmpty()) { - loadPlantList(currentZone); + loadPlantList(zone); } return Optional.ofNullable(plantMap.get(id)); } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java index 0b6908d..455791e 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/PlantDatabase.java @@ -30,5 +30,5 @@ public interface PlantDatabase { * @throws IOException If the database cannot be accessed * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified */ - Optional getPlantById(long id) throws IOException, HardinessZoneNotSetException; + Optional getPlantById(HardinessZone zone, long id) throws IOException, HardinessZoneNotSetException; } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java index 22b7de2..86e21f2 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -1,53 +1,87 @@ package ch.zhaw.gartenverwaltung.plantList; +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; +import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase; +import ch.zhaw.gartenverwaltung.io.PlantDatabase; +import ch.zhaw.gartenverwaltung.types.HardinessZone; import ch.zhaw.gartenverwaltung.types.Plant; +import java.io.IOException; +import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.function.Predicate; public class PlantListModel { - private List plantList; + private final PlantDatabase plantDatabase; + private HardinessZone currentZone; /** * Comparators to create sorted Plant List */ public final Comparator sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name()); public final Comparator getSortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id()); - public final Comparator sortBySpacing = (Plant o1, Plant o2) -> o1.spacing() - o2.spacing(); - - public PlantListModel(List plantList) { - setPlantList(plantList); + public PlantListModel() { + plantDatabase = new JsonPlantDatabase(); } - public void setPlantList(List plantList) { - this.plantList = plantList; + public void setCurrentZone(HardinessZone currentZone) { + this.currentZone = currentZone; + } + + public HardinessZone getCurrentZone() { + return currentZone; } /** * Method to get actual Plant List in alphabetic Order * @return actual Plant List in alphabetic Order */ - public List getPlantList() { - return getSortedPlantList(sortByName); + public List getPlantList(HardinessZone zone) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + return getSortedPlantList(zone, sortByName); + } + + + /** + * Method to get the actual Plant list in custom Order + * @param zone selected hardiness zone + * @param comparator comparator to sort the list + * @return sorted list with plants in the given hardiness zone + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified + */ + public List getSortedPlantList(HardinessZone zone, Comparator comparator) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + return plantDatabase.getPlantList(zone).stream().sorted(comparator).toList(); } /** - * Method to get actual Plant List sorted in custom order - * @param comparator comparator Object which is used to sort list - * @return actual Plant List in custom order + * Method to get Filtered plant list + * @param predicate predicate to filter the list + * @param zone selected hardiness zone + * @return filterd list with plants in the hardinness zone + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified */ - public List getSortedPlantList(Comparator comparator) { - return plantList.stream().sorted(comparator).toList(); + public List getFilteredPlantList(HardinessZone zone, Predicate predicate) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + return getPlantList(zone).stream().filter(predicate).toList(); } /** - * Method to get filtered Plant List with custom filter - * @param predicate predicate for filter - * @return filtered Plant List + * Method to get Filtered plant list by id by exact match + * @param zone selected hardiness zone + * @param id id of plant + * @return if id doesn't exist: empty List, else list with 1 plant entry. + * @throws IOException If the database cannot be accessed + * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified */ - public List getFilteredPlantList(Predicate predicate) { - return plantList.stream().filter(predicate).toList(); + public List getFilteredPlantListById(HardinessZone zone, Long id) throws HardinessZoneNotSetException, IOException { + setCurrentZone(zone); + List plantList = new ArrayList<>(); + plantDatabase.getPlantById(zone, id).ifPresent(plantList::add); + return plantList; } } diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json index f513570..aae89bc 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -251,4 +251,4 @@ } ] } -] \ No newline at end of file +] From fb237e47c084bf4fa76f2268ba2ff9533f993ca1 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 24 Oct 2022 19:08:24 +0200 Subject: [PATCH 6/8] added new HardinessZone for testing --- src/main/java/ch/zhaw/gartenverwaltung/types/HardinessZone.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/HardinessZone.java b/src/main/java/ch/zhaw/gartenverwaltung/types/HardinessZone.java index 5d0bc91..da3c3c8 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/HardinessZone.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/HardinessZone.java @@ -5,5 +5,6 @@ package ch.zhaw.gartenverwaltung.types; * (Subject to later expansion) */ public enum HardinessZone { + ZONE_1A, ZONE_8A } From d686469336286539de9559983266184254b841f4 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 24 Oct 2022 21:30:20 +0200 Subject: [PATCH 7/8] Created Tests for PlantListModel #15 --- build.gradle | 1 + .../plantList/PlantListModel.java | 19 ++- .../plantList/PlantListModelTest.java | 155 ++++++++++++++++++ 3 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java diff --git a/build.gradle b/build.gradle index 122bba0..74e4660 100644 --- a/build.gradle +++ b/build.gradle @@ -39,6 +39,7 @@ dependencies { testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4' implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4' + testImplementation 'org.mockito:mockito-core:4.3.+' } test { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java index 86e21f2..bd12a28 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/plantList/PlantListModel.java @@ -13,17 +13,30 @@ import java.util.List; import java.util.function.Predicate; public class PlantListModel { - private final PlantDatabase plantDatabase; + private PlantDatabase plantDatabase; private HardinessZone currentZone; /** * Comparators to create sorted Plant List */ - public final Comparator sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name()); - public final Comparator getSortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id()); + static final Comparator sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name()); + static final Comparator SortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id()); + /** + * Constructor to create Database Object. + */ public PlantListModel() { plantDatabase = new JsonPlantDatabase(); + setDefaultZone(); + } + + public PlantListModel(PlantDatabase plantDatabase){ + this.plantDatabase = plantDatabase; + setDefaultZone(); + } + + private void setDefaultZone(){ + currentZone = HardinessZone.ZONE_8A; // TODO: get Default Zone from Config } public void setCurrentZone(HardinessZone currentZone) { diff --git a/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java new file mode 100644 index 0000000..fbf6417 --- /dev/null +++ b/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java @@ -0,0 +1,155 @@ +package ch.zhaw.gartenverwaltung.plantList; + +import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; +import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase; +import ch.zhaw.gartenverwaltung.io.PlantDatabase; +import ch.zhaw.gartenverwaltung.types.HardinessZone; +import ch.zhaw.gartenverwaltung.types.Plant; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Optional; +import java.util.function.Predicate; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +class PlantListModelTest { + PlantDatabase plantDatabase; + List examplePlantList; + PlantListModel model; + + @BeforeEach + void setUp() throws HardinessZoneNotSetException, IOException { + createExamplePlantList(); + plantDatabase = mockPlantDatabase(examplePlantList); + model = new PlantListModel(plantDatabase); + } + + @AfterEach + void tearDown() { + } + + void createExamplePlantList(){ + examplePlantList = new ArrayList<>(); + examplePlantList.add(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.", + "15,30,2", + 0, + "sandy to loamy, loose soil, free of stones", + new ArrayList<>(), + new ArrayList<>()) + ); + examplePlantList.add(new Plant( + 0, + "Potato", + "The potato is a tuber, round or oval, with small white roots called 'eyes', that are growth buds. The size varies depending on the variety; the colour of the skin can be white, yellow or even purple.", + "35", + 6, + "sandy", + new ArrayList<>(), + new ArrayList<>()) + ); + examplePlantList.add(new Plant( + 1, + "Early Carrot", + "Carrot, (Daucus carota), herbaceous, generally biennial plant of the Apiaceae family that produces an edible taproot. Among common varieties root shapes range from globular to long, with lower ends blunt to pointed. Besides the orange-coloured roots, white-, yellow-, and purple-fleshed varieties are known.", + "5,35,2.5", + 0, + "sandy to loamy, loose soil, free of stones", + new ArrayList<>(), + new ArrayList<>()) + ); + } + + PlantDatabase mockPlantDatabase(List plantList) throws HardinessZoneNotSetException, IOException { + PlantDatabase plantDatabase = mock(JsonPlantDatabase.class); + when(plantDatabase.getPlantList(HardinessZone.ZONE_8A)).thenReturn(plantList); + when(plantDatabase.getPlantList(HardinessZone.ZONE_1A)).thenReturn(new ArrayList<>()); + when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 0)).thenReturn(Optional.of(plantList.get(1))); + when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 1)).thenReturn(Optional.of(plantList.get(2))); + when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 20)).thenReturn(Optional.of(plantList.get(0))); + when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 2)).thenReturn(Optional.empty()); + + return plantDatabase; + } + + void checkCurrentZone(HardinessZone expectedZone) { + assertEquals(expectedZone, model.getCurrentZone()); + } + + @Test + void setCurrentZone() { + checkCurrentZone(HardinessZone.ZONE_8A); // TODO change to get default zone from config + model.setCurrentZone(HardinessZone.ZONE_1A); + checkCurrentZone(HardinessZone.ZONE_1A); + model.setCurrentZone(HardinessZone.ZONE_8A); + checkCurrentZone(HardinessZone.ZONE_8A); + } + + @Test + void getPlantList() throws HardinessZoneNotSetException, IOException { + model.setCurrentZone(HardinessZone.ZONE_1A); + List plantListResult = model.getPlantList(HardinessZone.ZONE_8A); + checkCurrentZone(HardinessZone.ZONE_8A); + assertEquals(examplePlantList.size(), plantListResult.size()); + assertEquals(examplePlantList.get(2), plantListResult.get(0)); + assertEquals(examplePlantList.get(1), plantListResult.get(1)); + assertEquals(examplePlantList.get(0), plantListResult.get(2)); + + assertEquals(0, model.getPlantList(HardinessZone.ZONE_1A).size()); + } + + @Test + void getSortedPlantList() throws HardinessZoneNotSetException, IOException { + model.setCurrentZone(HardinessZone.ZONE_1A); + List plantListResult = model.getSortedPlantList(HardinessZone.ZONE_8A, PlantListModel.sortByName); + checkCurrentZone(HardinessZone.ZONE_8A); + assertEquals(examplePlantList.size(), plantListResult.size()); + assertEquals(examplePlantList.get(2), plantListResult.get(0)); + assertEquals(examplePlantList.get(1), plantListResult.get(1)); + assertEquals(examplePlantList.get(0), plantListResult.get(2)); + + plantListResult = model.getSortedPlantList(HardinessZone.ZONE_8A, PlantListModel.SortById); + assertEquals(examplePlantList.size(), plantListResult.size()); + assertEquals(examplePlantList.get(1), plantListResult.get(0)); + assertEquals(examplePlantList.get(2), plantListResult.get(1)); + assertEquals(examplePlantList.get(0), plantListResult.get(2)); + } + + @Test + void getFilteredPlantList() throws HardinessZoneNotSetException, IOException { + model.setCurrentZone(HardinessZone.ZONE_1A); + Predicate predicate = new Predicate() { + @Override + public boolean test(Plant plant) { + return plant.name().toUpperCase(Locale.ROOT).contains("E"); + } + }; + List plantListResult = model.getFilteredPlantList(HardinessZone.ZONE_8A, predicate); + checkCurrentZone(HardinessZone.ZONE_8A); + assertEquals(2, plantListResult.size()); + assertEquals(examplePlantList.get(2), plantListResult.get(0)); + assertEquals(examplePlantList.get(0), plantListResult.get(1)); + } + + @Test + void getFilteredPlantListById() throws HardinessZoneNotSetException, IOException { + model.setCurrentZone(HardinessZone.ZONE_1A); + List plantListResult = model.getFilteredPlantListById(HardinessZone.ZONE_8A, 2L); + assertEquals(0, plantListResult.size()); + plantListResult = model.getFilteredPlantListById(HardinessZone.ZONE_8A, 20L); + assertEquals(1, plantListResult.size()); + assertEquals(examplePlantList.get(0), plantListResult.get(0)); + } +} \ No newline at end of file From 4e15a706fb035f43c41453099b1676ed8cc1e7ce Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 24 Oct 2022 21:34:19 +0200 Subject: [PATCH 8/8] code cleanup --- .../gartenverwaltung/plantList/PlantListModelTest.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java index fbf6417..0aecc35 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/plantList/PlantListModelTest.java @@ -21,7 +21,6 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - class PlantListModelTest { PlantDatabase plantDatabase; List examplePlantList; @@ -130,12 +129,7 @@ class PlantListModelTest { @Test void getFilteredPlantList() throws HardinessZoneNotSetException, IOException { model.setCurrentZone(HardinessZone.ZONE_1A); - Predicate predicate = new Predicate() { - @Override - public boolean test(Plant plant) { - return plant.name().toUpperCase(Locale.ROOT).contains("E"); - } - }; + Predicate predicate = plant -> plant.name().toUpperCase(Locale.ROOT).contains("E"); List plantListResult = model.getFilteredPlantList(HardinessZone.ZONE_8A, predicate); checkCurrentZone(HardinessZone.ZONE_8A); assertEquals(2, plantListResult.size()); @@ -152,4 +146,4 @@ class PlantListModelTest { assertEquals(1, plantListResult.size()); assertEquals(examplePlantList.get(0), plantListResult.get(0)); } -} \ No newline at end of file +}