Compare commits

...

16 Commits

Author SHA1 Message Date
schrom01 4e15a706fb code cleanup 2022-10-24 21:34:19 +02:00
schrom01 d686469336 Created Tests for PlantListModel
#15
2022-10-24 21:30:20 +02:00
schrom01 fb237e47c0 added new HardinessZone for testing 2022-10-24 19:08:24 +02:00
schrom01 9fa4762b9c Merge remote-tracking branch 'origin/feature_plantList_M2' into feature_plantList_M2 2022-10-24 19:01:19 +02:00
schrom01 e5b5e1b88a adapted class PlantListModel to PlantDatabase
#12
2022-10-24 19:00:06 +02:00
schrom01 d1c5c58468 adapted class PlantListModel to PlantDatabase 2022-10-24 18:55:26 +02:00
schrom01 f9149c48fd Merge branch 'dev' into feature_plantList_M2 2022-10-24 12:51:52 +02:00
gulerdav 0d99ee219d #11 Merge pull request #32 from schrom01/feature_json-plant-db_M2
#4 Feature json plant db m2
2022-10-24 12:41:18 +02:00
David Guler 0d24bcc2ad Moved null-check to loading method 2022-10-24 12:32:28 +02:00
David Guler 2c61cd3393 Implemented simple caching for JsonPlantDatabase
Also added Javadoc for the PlantDatabase interface
2022-10-23 11:11:52 +02:00
David Guler 7355ce563f Added WateringCycle and Pest types 2022-10-23 09:54:56 +02:00
David Guler 429ac16d98 filtering lifecycle based on hardiness-zone
- Added 2 more plants to test db
- Db filters plant lifecycles based on hardiness-zones
- getPlantById implemented
- Added methods to Plant for calculating sowDates and accounting for multiple lifecycles
2022-10-23 09:49:22 +02:00
gulerdav 99e8f305e1 Merge pull request #31 from schrom01/feature_json-plant-db_M2
Feature json plant db m2
2022-10-21 12:20:06 +02:00
David Guler bf4d56e759 removed test code 2022-10-21 12:16:15 +02:00
David Guler 6d13bede7a Minimum viable deserialization
Added TaskTemplate type
Added dummy data with (probably) usable format
Used the java.time classes instead of the legacy util.Date
2022-10-20 21:46:00 +02:00
David Guler 5f53bb86c6 #11 Very basic implementation with dummy data 2022-10-17 17:01:50 +02:00
18 changed files with 802 additions and 40 deletions

View File

@ -5,7 +5,7 @@ plugins {
id 'org.beryx.jlink' version '2.25.0'
}
group 'ch.zhaw.pm3'
group 'ch.zhaw.gartenverwaltung'
version '1.0-SNAPSHOT'
repositories {
@ -37,6 +37,9 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
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 {

View File

@ -0,0 +1,7 @@
package ch.zhaw.gartenverwaltung.io;
public class HardinessZoneNotSetException extends Exception {
public HardinessZoneNotSetException() {
super("HardinessZone must be set to retrieve plants!");
}
}

View File

@ -0,0 +1,95 @@
package ch.zhaw.gartenverwaltung.io;
import ch.zhaw.gartenverwaltung.types.HardinessZone;
import ch.zhaw.gartenverwaltung.types.Plant;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.MonthDayDeserializer;
import java.io.IOException;
import java.net.URL;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Implements the {@link PlantDatabase} interface for loading {@link Plant} objects
* from a JSON file.
* The reads are cached to minimize file-io operations.
*/
public class JsonPlantDatabase implements PlantDatabase {
private final URL dataSource = getClass().getResource("plantdb.json");
private HardinessZone currentZone;
private Map<Long, Plant> plantMap = Collections.emptyMap();
/**
* 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);
timeModule.addDeserializer(MonthDay.class, dateDeserializer);
}
/**
* If no data is currently loaded, or the specified zone differs
* from the {@link #currentZone}, data is loaded from {@link #dataSource}.
* In any case, the values of {@link #plantMap} are returned.
*
* @see PlantDatabase#getPlantList(HardinessZone)
*/
@Override
public List<Plant> getPlantList(HardinessZone zone) throws IOException, HardinessZoneNotSetException {
if (plantMap.isEmpty() || zone != currentZone) {
loadPlantList(zone);
}
return plantMap.values().stream().toList();
}
/**
* @see PlantDatabase#getPlantById(long)
*/
@Override
public Optional<Plant> getPlantById(HardinessZone zone, long id) throws HardinessZoneNotSetException, IOException {
if (plantMap.isEmpty()) {
loadPlantList(zone);
}
return Optional.ofNullable(plantMap.get(id));
}
/**
* Loads the database from {@link #dataSource} and updates the cached data.
*
* @param zone The {@link HardinessZone} for which data is to be loaded
* @throws IOException If the database cannot be accessed
*/
private void loadPlantList(HardinessZone zone) throws IOException, HardinessZoneNotSetException {
if (zone == null) {
throw new HardinessZoneNotSetException();
}
if (dataSource != null) {
currentZone = zone;
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(timeModule);
List<Plant> result;
result = mapper.readerForListOf(Plant.class).readValue(dataSource);
for (Plant plant : result) {
plant.inZone(currentZone);
}
// Turn list into a HashMap with structure id => Plant
plantMap = result.stream()
.collect(HashMap::new,
(res, plant) -> res.put(plant.id(), plant),
(existing, replacement) -> { });
}
}
}

View File

@ -3,10 +3,32 @@ package ch.zhaw.gartenverwaltung.io;
import ch.zhaw.gartenverwaltung.types.Plant;
import ch.zhaw.gartenverwaltung.types.HardinessZone;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
/**
* A database of {@link Plant}s.
* The interface specifies the minimal required operations.
*/
public interface PlantDatabase {
List<Plant> getPlantList(HardinessZone zone);
Optional<Plant> getPlantById(long id);
/**
* Yields a list of all {@link Plant}s in the database with only data relevant to the specfied {@link HardinessZone}
*
* @param zone The zone for which data should be fetched
* @return A list of {@link Plant}s with data for the specified zone
* @throws IOException If the database cannot be accessed
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
*/
List<Plant> getPlantList(HardinessZone zone) throws IOException, HardinessZoneNotSetException;
/**
* Attempts to retrieve the {@link Plant} with the specified id.
*
* @param id The {@link Plant#id()} to look for
* @return {@link Optional} of the found {@link Plant}, {@link Optional#empty()} if no entry matched the criteria
* @throws IOException If the database cannot be accessed
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
*/
Optional<Plant> getPlantById(HardinessZone zone, long id) throws IOException, HardinessZoneNotSetException;
}

View File

@ -0,0 +1,28 @@
package ch.zhaw.gartenverwaltung.json;
import ch.zhaw.gartenverwaltung.types.GrowthPhaseType;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
public class GrowthPhaseTypeDeserializer extends StdDeserializer<GrowthPhaseType> {
public GrowthPhaseTypeDeserializer(Class<?> vc) {
super(vc);
}
public GrowthPhaseTypeDeserializer() { this(null); }
@Override
public GrowthPhaseType deserialize(JsonParser parser, DeserializationContext context) throws IOException {
GrowthPhaseType result = null;
try {
result = GrowthPhaseType.valueOf(parser.getText().toUpperCase());
} catch (IllegalArgumentException e) {
// TODO: Log
System.err.println("bad growth phase type");
}
return result;
}
}

View File

@ -0,0 +1,29 @@
package ch.zhaw.gartenverwaltung.json;
import ch.zhaw.gartenverwaltung.types.HardinessZone;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
public class HardinessZoneDeserializer extends StdDeserializer<HardinessZone> {
public HardinessZoneDeserializer(Class<?> vc) {
super(vc);
}
public HardinessZoneDeserializer() {
this(null);
}
@Override
public HardinessZone deserialize(JsonParser parser, DeserializationContext context) throws IOException {
HardinessZone result = null;
try {
result = HardinessZone.valueOf(parser.getText().toUpperCase());
} catch (IllegalArgumentException e) {
// TODO: Log
System.err.println("bad growth phase type");
}
return result;
}
}

View File

@ -1,53 +1,100 @@
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<Plant> plantList;
private PlantDatabase plantDatabase;
private HardinessZone currentZone;
/**
* Comparators to create sorted Plant List
*/
public final Comparator<Plant> sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name());
public final Comparator<Plant> getSortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id());
public final Comparator<Plant> sortBySpacing = (Plant o1, Plant o2) -> o1.spacing() - o2.spacing();
static final Comparator<Plant> sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name());
static final Comparator<Plant> SortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id());
public PlantListModel(List<Plant> plantList) {
setPlantList(plantList);
/**
* Constructor to create Database Object.
*/
public PlantListModel() {
plantDatabase = new JsonPlantDatabase();
setDefaultZone();
}
public void setPlantList(List<Plant> plantList) {
this.plantList = plantList;
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) {
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<Plant> getPlantList() {
return getSortedPlantList(sortByName);
public List<Plant> 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<Plant> getSortedPlantList(HardinessZone zone, Comparator<Plant> 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<Plant> getSortedPlantList(Comparator<Plant> comparator) {
return plantList.stream().sorted(comparator).toList();
public List<Plant> getFilteredPlantList(HardinessZone zone, Predicate<Plant> 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<Plant> getFilteredPlantList(Predicate<Plant> predicate) {
return plantList.stream().filter(predicate).toList();
public List<Plant> getFilteredPlantListById(HardinessZone zone, Long id) throws HardinessZoneNotSetException, IOException {
setCurrentZone(zone);
List<Plant> plantList = new ArrayList<>();
plantDatabase.getPlantById(zone, id).ifPresent(plantList::add);
return plantList;
}
}

View File

@ -1,9 +1,19 @@
package ch.zhaw.gartenverwaltung.types;
import java.util.Date;
import ch.zhaw.gartenverwaltung.json.GrowthPhaseTypeDeserializer;
import ch.zhaw.gartenverwaltung.json.HardinessZoneDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
public record GrowthPhase(Date startDate,
Date endDate,
GrowthPhaseType type,
HardinessZone zone) {
import java.time.MonthDay;
import java.util.List;
public record GrowthPhase(
MonthDay startDate,
MonthDay endDate,
int group,
WateringCycle wateringCycle,
@JsonDeserialize(using = GrowthPhaseTypeDeserializer.class) GrowthPhaseType type,
@JsonDeserialize(using = HardinessZoneDeserializer.class) HardinessZone zone,
List<TaskTemplate> taskTemplates) {
}

View File

@ -1,5 +1,5 @@
package ch.zhaw.gartenverwaltung.types;
public enum GrowthPhaseType {
SOW, PLANT, HARVEST
SOW, PLANT, REPLANT, HARVEST
}

View File

@ -5,5 +5,6 @@ package ch.zhaw.gartenverwaltung.types;
* (Subject to later expansion)
*/
public enum HardinessZone {
ZONE_1A,
ZONE_8A
}

View File

@ -0,0 +1,4 @@
package ch.zhaw.gartenverwaltung.types;
public record Pest(String name, String description, String measures) {
}

View File

@ -1,11 +1,46 @@
package ch.zhaw.gartenverwaltung.types;
import java.time.LocalDate;
import java.util.List;
import static java.time.temporal.ChronoUnit.DAYS;
public record Plant(
long id,
String name,
String description,
int spacing,
String spacing,
int light,
String soil,
List<Pest> pests,
List<GrowthPhase> lifecycle) {
public void inZone(HardinessZone zone) {
lifecycle.removeIf(growthPhase -> !growthPhase.zone().equals(zone));
}
public List<GrowthPhase> lifecycleForGroup(int group) {
return lifecycle.stream()
.filter(growthPhase -> growthPhase.group() != group)
.toList();
}
public LocalDate sowDateFromHarvestDate(LocalDate harvestDate, int group) {
return harvestDate.minusDays(timeToHarvest(group));
}
public int timeToHarvest(int group) {
List<GrowthPhase> activeLifecycle = lifecycleForGroup(group);
GrowthPhase sow = activeLifecycle.stream()
.filter(growthPhase -> !growthPhase.type().equals(GrowthPhaseType.SOW))
.findFirst()
.orElseThrow();
GrowthPhase harvest = activeLifecycle.stream()
.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));
}
}

View File

@ -1,6 +1,7 @@
package ch.zhaw.gartenverwaltung.types;
import java.util.Date;
import java.time.LocalDate;
import java.util.Optional;
/**
@ -11,14 +12,21 @@ public class Task {
private long id;
private final String name;
private final String description;
private final Date startDate;
private final LocalDate startDate;
private Integer interval;
private Date endDate;
private LocalDate endDate;
public Task(String name, String description, Date startDate) {
public Task(String name, String description, LocalDate startDate) {
this.name = name;
this.description = description;
this.startDate = endDate;
this.startDate = startDate;
}
public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval) {
this.name = name;
this.description = description;
this.startDate = startDate;
this.endDate = endDate;
this.interval = interval;
}
// Builder-pattern-style setters
@ -30,7 +38,7 @@ public class Task {
this.interval = interval;
return this;
}
public Task withEndDate(Date endDate) {
public Task withEndDate(LocalDate endDate) {
this.endDate = endDate;
return this;
}
@ -39,12 +47,12 @@ public class Task {
public long getId() { return id; }
public String getName() { return name; }
public String getDescription() { return description; }
public Date getStartDate() { return startDate; }
public LocalDate getStartDate() { return startDate; }
public Optional<Integer> getInterval() {
return Optional.ofNullable(interval);
}
public Optional<Date> getEndDate() {
public Optional<LocalDate> getEndDate() {
return Optional.ofNullable(endDate);
}
}

View File

@ -0,0 +1,57 @@
package ch.zhaw.gartenverwaltung.types;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDate;
public class TaskTemplate {
@JsonProperty
private final String name;
@JsonProperty
private final String description;
@JsonProperty
private final int relativeStartDate;
@JsonProperty
private Integer relativeEndDate;
@JsonProperty
private Integer interval;
// TODO: reconsider if we need this
@JsonProperty
private boolean isOptional = false;
/**
* Default constructor
* (Used by deserializer)
*/
public TaskTemplate() {
this.name = "";
this.description = "";
this.relativeStartDate = 0;
}
// Setters
public void setRelativeEndDate(Integer relativeEndDate) {
this.relativeEndDate = relativeEndDate;
}
public void setInterval(Integer interval) {
this.interval = interval;
}
public TaskTemplate(String name, String description, int relativeStartDate) {
this.name = name;
this.description = description;
this.relativeStartDate = relativeStartDate;
}
public Task generateTask(LocalDate realStartDate) {
Task task = new Task(name, description, realStartDate.plusDays(relativeStartDate));
if (relativeEndDate != null) {
task.withEndDate(realStartDate.plusDays(relativeEndDate));
}
if (interval != null) {
task.withInterval(interval);
}
return task;
}
}

View File

@ -0,0 +1,8 @@
package ch.zhaw.gartenverwaltung.types;
public record WateringCycle(
int litersPerSqM,
int interval,
String[] notes
) {
}

View File

@ -1,8 +1,13 @@
module ch.zhaw.gartenverwaltung {
requires javafx.controls;
requires javafx.fxml;
requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.datatype.jsr310;
opens ch.zhaw.gartenverwaltung to javafx.fxml;
opens ch.zhaw.gartenverwaltung.types to com.fasterxml.jackson.databind;
// opens ch.zhaw.gartenverwaltung.types to com.fasterxml.jackson.databind;
exports ch.zhaw.gartenverwaltung;
exports ch.zhaw.gartenverwaltung.types;
exports ch.zhaw.gartenverwaltung.json;
}

View File

@ -0,0 +1,254 @@
[
{
"id": 0,
"name": "Potato",
"description": "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.",
"light": 6,
"spacing": "35",
"soil": "sandy",
"pests": [
{
"name": "Rot",
"description": "Rot, any of several plant diseases, caused by any of hundreds of species of soil-borne bacteria, fungi, and funguslike organisms (Oomycota). Rot diseases are characterized by plant decomposition and putrefaction. The decay may be hard, dry, spongy, watery, mushy, or slimy and may affect any plant part.",
"measures": "Less water."
}
],
"lifecycle": [
{
"startDate": "03-10",
"endDate": "04-10",
"type": "SOW",
"zone": "ZONE_8A",
"group": 0,
"wateringCycle": {
"litersPerSqM": 0,
"interval": null,
"notes": []
},
"taskTemplates": [
{
"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.\"",
"interval": null,
"isOptional": false
}
]
},
{
"startDate": "04-10",
"endDate": "07-10",
"type": "PLANT",
"zone": "ZONE_8A",
"group": 0,
"wateringCycle": {
"litersPerSqM": 25,
"interval": 7,
"notes": []
},
"taskTemplates": [
{
"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.\"",
"interval": 21,
"isOptional": false
}
]
},
{
"startDate": "06-10",
"endDate": "08-10",
"type": "HARVEST",
"zone": "ZONE_8A",
"group": 0,
"wateringCycle": {
"litersPerSqM": 0,
"interval": null,
"notes": []
},
"taskTemplates": [
{
"name": "Harvest",
"relativeStartDate": 0,
"relativeEndDate": null,
"description": "Once the foliage has wilted and dried completely, harvest on a dry day. Store in a dark and cool location.",
"interval": null,
"isOptional": false
}
]
}
]
},
{
"id": 1,
"name": "Early Carrot",
"description": "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.",
"lifecycle": [
{
"startDate": "02-20",
"endDate": "03-10",
"zone": "ZONE_8A",
"type": "SOW",
"wateringCycle": {
"litersPerSqM": 15,
"interval": 3,
"notes": []
},
"taskTemplates": [
{
"name": "hilling",
"relativeStartDate": 0,
"relativeEndDate": 0,
"description": "Mound up the soil around the plant until just the top few leaves show above the soil. ",
"interval": null,
"isOptional": false
}
]
},
{
"startDate": "03-10",
"endDate": "05-10",
"zone": "ZONE_8A",
"type": "PLANT",
"wateringCycle": {
"litersPerSqM": 25,
"interval": 3,
"notes": [
"Be careful not to pour water over the leaves, as this will lead to sunburn."
]
},
"taskTemplates": [
{
"name": "hilling",
"relativeStartDate": 0,
"relativeEndDate": null,
"description": "Mound up the soil around the plant until just the top few leaves show above the soil. ",
"interval": 15,
"isOptional": true
}
]
},
{
"startDate": "05-10",
"endDate": "05-20",
"zone": "ZONE_8A",
"type": "HARVEST",
"wateringCycle": {
"litersPerSqM": 0,
"interval": null,
"notes": []
},
"taskTemplates": [
{
"name": "Harvesting",
"relativeStartDate": 0,
"relativeEndDate": 14,
"description": "When the leaves turn to a yellowish brown. Do not harvest earlier. The plant will show when it's ready.",
"interval": null,
"isOptional": false
}
]
}
],
"soil": "sandy to loamy, loose soil, free of stones",
"spacing": "5,35,2.5",
"pests": [
{
"name": "Rot",
"description": "rot, any of several plant diseases, caused by any of hundreds of species of soil-borne bacteria, fungi, and funguslike organisms (Oomycota). Rot diseases are characterized by plant decomposition and putrefaction. The decay may be hard, dry, spongy, watery, mushy, or slimy and may affect any plant part.",
"measures": "less water"
}
]
},
{
"id": 2,
"name": "summertime onion",
"description": "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.",
"lifecycle": [
{
"startDate": "03-15",
"endDate": "04-10",
"type": "SOW",
"zone": "ZONE_8A",
"group": 0,
"wateringCycle": {
"litersPerSqM": 15,
"interval": 4,
"notes": [
]
},
"taskTemplates": [
{
"name": "hilling",
"relativeStartDate": 0,
"relativeEndDate": 0,
"description": "Mound up the soil around the plant until just the top few leaves show above the soil. ",
"interval": null,
"isOptional": false
}
]
},
{
"startDate": "04-10",
"endDate": "07-10",
"type": "PLANT",
"zone": "ZONE_8A",
"group": 0,
"wateringCycle": {
"litersPerSqM": 25,
"interval": 3,
"notes": [
""
]
},
"taskTemplates": [
{
"name": "hilling",
"relativeStartDate": 0,
"relativeEndDate": null,
"description": "Mound up the soil around the plant until just the top few leaves show above the soil. ",
"interval": 15,
"isOptional": true
}
]
},
{
"startDate": "07-10",
"endDate": "09-20",
"type": "HARVEST",
"zone": "ZONE_8A",
"group": 0,
"wateringCycle": {
"litersPerSqM": 0,
"interval": null,
"notes": [
]
},
"taskTemplates": [
{
"name": "Harvesting",
"relativeStartDate": 0,
"relativeEndDate": 14,
"description": "When ready for harvest, the leaves on your onion plants will start to flop over. This happens at the \"neck\" of the onion and it signals that the plant has stopped growing and is ready for storage. Onions should be harvested soon thereafter",
"interval": null,
"isOptional": false
}
]
}
],
"soil": "sandy to loamy, loose soil, free of stones",
"spacing": "15,30,2",
"pests": [
{
"name": "Rot",
"description": "rot, any of several plant diseases, caused by any of hundreds of species of soil-borne bacteria, fungi, and funguslike organisms (Oomycota). Rot diseases are characterized by plant decomposition and putrefaction. The decay may be hard, dry, spongy, watery, mushy, or slimy and may affect any plant part.",
"measures": "less water"
}
]
}
]

View File

@ -0,0 +1,149 @@
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<Plant> 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<Plant> 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<Plant> 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<Plant> 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<Plant> predicate = plant -> plant.name().toUpperCase(Locale.ROOT).contains("E");
List<Plant> 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<Plant> 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));
}
}