#23 implemented test class for WeatherGradenTaskPlanner
This commit is contained in:
parent
d119fd1331
commit
86386d8a91
|
@ -20,7 +20,7 @@ public class WeatherGradenTaskPlanner {
|
||||||
private final PlantList plantList;
|
private final PlantList plantList;
|
||||||
private final CropList cropList;
|
private final CropList cropList;
|
||||||
WeatherService weatherService;
|
WeatherService weatherService;
|
||||||
private final LocalDate dateSevereWeather = LocalDate.of(2022,12,15);
|
private final LocalDate dateSevereWeather = LocalDate.now();
|
||||||
|
|
||||||
public WeatherGradenTaskPlanner(TaskList taskList, PlantList plantList, CropList cropList) {
|
public WeatherGradenTaskPlanner(TaskList taskList, PlantList plantList, CropList cropList) {
|
||||||
this.taskList = taskList;
|
this.taskList = taskList;
|
||||||
|
@ -64,7 +64,7 @@ public class WeatherGradenTaskPlanner {
|
||||||
|
|
||||||
Task preHailTask = new Task("Hail",
|
Task preHailTask = new Task("Hail",
|
||||||
"During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin",
|
"During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin",
|
||||||
dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L));
|
dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L),1L);
|
||||||
taskList.saveTask(preHailTask);
|
taskList.saveTask(preHailTask);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ public class WeatherGradenTaskPlanner {
|
||||||
private void createPreFrostTask() throws IOException {
|
private void createPreFrostTask() throws IOException {
|
||||||
Task preFrostTask = new Task("Frost",
|
Task preFrostTask = new Task("Frost",
|
||||||
"The temperatur falls below zero degrees, cover especially the root with wool",
|
"The temperatur falls below zero degrees, cover especially the root with wool",
|
||||||
dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L));
|
dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L),1L);
|
||||||
taskList.saveTask(preFrostTask);
|
taskList.saveTask(preFrostTask);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ public class WeatherGradenTaskPlanner {
|
||||||
private void createPreSnowTask() throws IOException {
|
private void createPreSnowTask() throws IOException {
|
||||||
Task preSnowTask = new Task("Snow",
|
Task preSnowTask = new Task("Snow",
|
||||||
"The weather brings little snowfall. Cover your crops",
|
"The weather brings little snowfall. Cover your crops",
|
||||||
dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L));
|
dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L),1L);
|
||||||
taskList.saveTask(preSnowTask);
|
taskList.saveTask(preSnowTask);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,9 +113,6 @@ public class WeatherGradenTaskPlanner {
|
||||||
/**
|
/**
|
||||||
* Method to set next execution date of the water plant tasks
|
* Method to set next execution date of the water plant tasks
|
||||||
* @param cropTaskList List with tasks from crops
|
* @param cropTaskList List with tasks from crops
|
||||||
* @throws IOException If the database cannot be accessed
|
|
||||||
* @throws HardinessZoneNotSetException If the hardiness zone is not available
|
|
||||||
* @throws PlantNotFoundException if the plant is not available for the watering task
|
|
||||||
*/
|
*/
|
||||||
private void adjustNextExecutionOfWateringTasks(List<Task> cropTaskList){
|
private void adjustNextExecutionOfWateringTasks(List<Task> cropTaskList){
|
||||||
for(Task task : cropTaskList){
|
for(Task task : cropTaskList){
|
||||||
|
|
|
@ -44,10 +44,11 @@ public class Task {
|
||||||
/**
|
/**
|
||||||
* Constructor for weather events
|
* Constructor for weather events
|
||||||
*/
|
*/
|
||||||
public Task(String name, String description, LocalDate startDate, LocalDate endDate) {
|
public Task(String name, String description, LocalDate startDate, LocalDate endDate, long cropId) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.startDate = startDate;
|
this.startDate = startDate;
|
||||||
|
nextExecution = startDate;
|
||||||
this.endDate = endDate;
|
this.endDate = endDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
package ch.zhaw.gartenverwaltung.backgroundtasks.weather;
|
||||||
|
|
||||||
|
import ch.zhaw.gartenverwaltung.io.*;
|
||||||
|
import ch.zhaw.gartenverwaltung.models.PlantNotFoundException;
|
||||||
|
import ch.zhaw.gartenverwaltung.types.*;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.MonthDay;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
public class WeatherGardenTaskPlannerTest {
|
||||||
|
private final URL dbDataSource = JsonCropListTest.class.getResource("test-crops.json");
|
||||||
|
private final URL testFile = JsonCropListTest.class.getResource("test-taskdb.json");
|
||||||
|
Plant examplePlantOnion;
|
||||||
|
Crop exampleCropOnion;
|
||||||
|
Task exampleTask;
|
||||||
|
Task exampleWeatherTask;
|
||||||
|
List<Crop> cropList;
|
||||||
|
List<Plant> plantList;
|
||||||
|
TaskList taskList;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
examplePlantOnion = 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<>(),
|
||||||
|
new WateringCycle(15, 3, null),
|
||||||
|
List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()),
|
||||||
|
new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()),
|
||||||
|
new GrowthPhase(MonthDay.of(8, 5), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()),
|
||||||
|
new GrowthPhase(MonthDay.of(2, 8), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()),
|
||||||
|
new GrowthPhase(MonthDay.of(10, 2), MonthDay.of(12, 4), 0, GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())));
|
||||||
|
|
||||||
|
exampleCropOnion = new Crop(examplePlantOnion.id(), LocalDate.of(2022, 12, 5))
|
||||||
|
.withId(3L);
|
||||||
|
exampleTask = new Task("water plant","water the plant until the ground is wet enough",LocalDate.now(), LocalDate.now().plusDays(2L),3L);
|
||||||
|
exampleTask.setNextExecution(LocalDate.now().plusDays(1L));
|
||||||
|
exampleWeatherTask = new Task("Hail",
|
||||||
|
"During a summer Thunderstorm it could hail heavily. THe Hail could damage the crops. To prevent damage cover the plants with a strong tarpaulin",
|
||||||
|
LocalDate.now().minusDays(1L),LocalDate.now().plusDays(1L),3L);
|
||||||
|
|
||||||
|
taskList = new JsonTaskList(testFile);
|
||||||
|
|
||||||
|
plantList = new ArrayList<>();
|
||||||
|
plantList.add(examplePlantOnion);
|
||||||
|
|
||||||
|
cropList = new ArrayList<>();
|
||||||
|
cropList.add(exampleCropOnion);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
CropList mockCropList(List<Crop> cropList) throws IOException {
|
||||||
|
CropList croplist = mock(CropList.class);
|
||||||
|
when(croplist.getCrops()).thenReturn(cropList);
|
||||||
|
when(croplist.getCropById(3)).thenReturn(java.util.Optional.ofNullable(exampleCropOnion));
|
||||||
|
return croplist;
|
||||||
|
}
|
||||||
|
|
||||||
|
PlantList mockPlantDatabase(List<Plant> plantList) throws HardinessZoneNotSetException, IOException {
|
||||||
|
PlantList plantDatabase = mock(JsonPlantList.class);
|
||||||
|
when(plantDatabase.getPlantList(HardinessZone.ZONE_8A)).thenReturn(plantList);
|
||||||
|
when(plantDatabase.getPlantById(HardinessZone.ZONE_8A,20)).thenReturn(Optional.of(plantList.get(0)));
|
||||||
|
return plantDatabase;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void refreshtasks() throws HardinessZoneNotSetException, IOException, PlantNotFoundException {
|
||||||
|
WeatherGradenTaskPlanner weatherGradenTaskPlanner = new WeatherGradenTaskPlanner(taskList, mockPlantDatabase(plantList), mockCropList(cropList));
|
||||||
|
taskList.saveTask(exampleTask);
|
||||||
|
weatherGradenTaskPlanner.refreshTasks();
|
||||||
|
assertEquals(exampleTask.getName(),taskList.getTaskList(LocalDate.now(),LocalDate.now().plusDays(7)).get(0).getName());
|
||||||
|
assertEquals(LocalDate.of(2022,12,10),taskList.getTaskList(LocalDate.now(),LocalDate.now().plusDays(7)).get(0).getNextExecution());
|
||||||
|
assertEquals(exampleWeatherTask.getName(),taskList.getTaskList(LocalDate.now(),LocalDate.now().plusDays(7)).get(1).getName());
|
||||||
|
assertEquals(exampleWeatherTask.getStartDate(),taskList.getTaskList(LocalDate.now(),LocalDate.now().plusDays(7)).get(1).getStartDate());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,215 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"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",
|
||||||
|
"image": "potato.jpg",
|
||||||
|
"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."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"wateringCycle": {
|
||||||
|
"litersPerSqM": 25,
|
||||||
|
"interval": null,
|
||||||
|
"notes": []
|
||||||
|
},
|
||||||
|
"lifecycle": [
|
||||||
|
{
|
||||||
|
"startDate": "03-10",
|
||||||
|
"endDate": "04-10",
|
||||||
|
"type": "SOW",
|
||||||
|
"zone": "ZONE_8A",
|
||||||
|
"group": 0,
|
||||||
|
"taskTemplates": [
|
||||||
|
{
|
||||||
|
"name": "Germinate",
|
||||||
|
"relativeStartDate": -14,
|
||||||
|
"relativeEndDate": null,
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"startDate": "04-10",
|
||||||
|
"endDate": "07-10",
|
||||||
|
"type": "PLANT",
|
||||||
|
"zone": "ZONE_8A",
|
||||||
|
"group": 0,
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"startDate": "06-10",
|
||||||
|
"endDate": "08-10",
|
||||||
|
"type": "HARVEST",
|
||||||
|
"zone": "ZONE_8A",
|
||||||
|
"group": 0,
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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.",
|
||||||
|
"image": "carrot.jpg",
|
||||||
|
"wateringCycle": {
|
||||||
|
"litersPerSqM": 15,
|
||||||
|
"interval": 3,
|
||||||
|
"notes": []
|
||||||
|
},
|
||||||
|
"lifecycle": [
|
||||||
|
{
|
||||||
|
"startDate": "02-20",
|
||||||
|
"endDate": "03-10",
|
||||||
|
"zone": "ZONE_8A",
|
||||||
|
"type": "SOW",
|
||||||
|
"group": 0,
|
||||||
|
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"startDate": "03-10",
|
||||||
|
"endDate": "05-10",
|
||||||
|
"zone": "ZONE_8A",
|
||||||
|
"type": "PLANT",
|
||||||
|
"group": 0,
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"startDate": "05-10",
|
||||||
|
"endDate": "05-20",
|
||||||
|
"zone": "ZONE_8A",
|
||||||
|
"type": "HARVEST",
|
||||||
|
"group": 0,
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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.",
|
||||||
|
"image": "onion.jpg",
|
||||||
|
"wateringCycle": {
|
||||||
|
"litersPerSqM": 15,
|
||||||
|
"interval": 4,
|
||||||
|
"notes": [
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"lifecycle": [
|
||||||
|
{
|
||||||
|
"startDate": "03-15",
|
||||||
|
"endDate": "04-10",
|
||||||
|
"type": "SOW",
|
||||||
|
"zone": "ZONE_8A",
|
||||||
|
"group": 0,
|
||||||
|
"taskTemplates": [
|
||||||
|
{
|
||||||
|
"name": "Plant Sets",
|
||||||
|
"relativeStartDate": 0,
|
||||||
|
"relativeEndDate": 0,
|
||||||
|
"description": "Plant the sets about 5cm deep into the soil.",
|
||||||
|
"interval": null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"startDate": "04-10",
|
||||||
|
"endDate": "07-10",
|
||||||
|
"type": "PLANT",
|
||||||
|
"zone": "ZONE_8A",
|
||||||
|
"group": 0,
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"startDate": "07-10",
|
||||||
|
"endDate": "09-20",
|
||||||
|
"type": "HARVEST",
|
||||||
|
"zone": "ZONE_8A",
|
||||||
|
"group": 0,
|
||||||
|
"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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
|
@ -0,0 +1,2 @@
|
||||||
|
[
|
||||||
|
]
|
|
@ -0,0 +1,2 @@
|
||||||
|
[
|
||||||
|
]
|
Loading…
Reference in New Issue