#23 bugfix testclass WeatherGradenTaskPlannerTest and bugfix Task and WeatherGradenTaskPlanner

This commit is contained in:
Gian-Andrea Hutter 2022-12-10 13:47:54 +01:00
parent b3c7dd67e8
commit 3bda390708
4 changed files with 71 additions and 17 deletions

View File

@ -25,11 +25,12 @@ public class BackgroundTasks extends TimerTask {
private void movePastTasks() throws IOException { private void movePastTasks() throws IOException {
List<Task> taskList = this.taskList.getTaskList(LocalDate.MIN, LocalDate.now().minusDays(1)); List<Task> taskList = this.taskList.getTaskList(LocalDate.MIN, LocalDate.now().minusDays(1));
taskList.forEach(task -> { for (Task task : taskList) {
if (!task.isDone()) { if (!task.isDone()) {
task.setNextExecution(LocalDate.now()); task.setNextExecution(LocalDate.now());
this.taskList.saveTask(task);
} }
}); }
} }
public BackgroundTasks(TaskList taskList, CropList cropList, PlantList plantList) { public BackgroundTasks(TaskList taskList, CropList cropList, PlantList plantList) {

View File

@ -9,8 +9,12 @@ import ch.zhaw.gartenverwaltung.types.*;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import static java.util.stream.Collectors.toList;
/** /**
* The WeatherGardenTaskPlanner creates Tasks based on weather events and the rain amount from the last days * The WeatherGardenTaskPlanner creates Tasks based on weather events and the rain amount from the last days
* *
@ -43,8 +47,7 @@ public class WeatherGradenTaskPlanner {
private void getSevereWeatherEvents() throws IOException { private void getSevereWeatherEvents() throws IOException {
SevereWeather actualWeather = weatherService.causeSevereWeather(1); SevereWeather actualWeather = weatherService.causeSevereWeather(1);
if (SevereWeather.HAIL.equals(actualWeather)) { if (SevereWeather.HAIL.equals(actualWeather)) {
//ToDo creates hail task all 3 seconds, pls fix createPreHailTask();
//createPreHailTask();
} else if (SevereWeather.FROST.equals(actualWeather)) { } else if (SevereWeather.FROST.equals(actualWeather)) {
createPreFrostTask(); createPreFrostTask();
} else if (SevereWeather.SNOW.equals(actualWeather)) { } else if (SevereWeather.SNOW.equals(actualWeather)) {
@ -63,11 +66,27 @@ public class WeatherGradenTaskPlanner {
*/ */
private void createPreHailTask() throws IOException { private void createPreHailTask() throws IOException {
Task preHailTask = new Task("Hail", List<Crop> actualCrops = cropList.getCrops();
"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),1L);
taskList.saveTask(preHailTask);
for (Crop crop : actualCrops) {
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",
dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L));
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
List<Task> hailTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Hail")).toList();
List<Task> hailTaskListAtDate = new ArrayList<>();
for (Task task : hailTasklist) {
if (task.getStartDate() == (preHailTask.getStartDate())) {
hailTaskListAtDate.add(task);
}
}
if(hailTaskListAtDate.isEmpty() && hailTasklist.isEmpty()){
taskList.saveTask(preHailTask);
}
}
} }
/** /**
@ -75,10 +94,26 @@ public class WeatherGradenTaskPlanner {
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private void createPreFrostTask() throws IOException { private void createPreFrostTask() throws IOException {
Task preFrostTask = new Task("Frost", List<Crop> actualCrops = cropList.getCrops();
"The temperatur falls below zero degrees, cover especially the root with wool", for (Crop crop : actualCrops) {
dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L),1L);
taskList.saveTask(preFrostTask); Task preFrostTask = new Task("Frost",
"The temperatur falls below zero degrees, cover especially the root with wool",
dateSevereWeather,dateSevereWeather.plusDays(1L),crop.getCropId().orElse(-1L));
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
List<Task> frostTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Frost")).toList();
List<Task> frostTaskListAtDate = new ArrayList<>();
for (Task task : frostTasklist) {
if (task.getStartDate() == preFrostTask.getStartDate()) {
frostTaskListAtDate.add(task);
}
}
if(frostTaskListAtDate.isEmpty() && frostTasklist.isEmpty()){
taskList.saveTask(preFrostTask);
}
}
} }
/** /**
@ -86,10 +121,27 @@ public class WeatherGradenTaskPlanner {
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
*/ */
private void createPreSnowTask() throws IOException { private void createPreSnowTask() throws IOException {
Task preSnowTask = new Task("Snow", List<Crop> actualCrops = cropList.getCrops();
"The weather brings little snowfall. Cover your crops", for (Crop crop : actualCrops) {
dateSevereWeather.minusDays(1L),dateSevereWeather.plusDays(1L),1L);
taskList.saveTask(preSnowTask); Task preSnowTask = new Task("Snow",
"The weather brings little snowfall. Cover your crops",
dateSevereWeather, dateSevereWeather.plusDays(1L), crop.getCropId().orElse(-1L));
List<Task> actualCropTaskList = taskList.getTaskForCrop(crop.getCropId().orElse(-1L));
List<Task> snowTasklist = actualCropTaskList.stream().filter(task -> task.getName().equals("Snow")).toList();
List<Task> snowTaskListAtDate = new ArrayList<>();
for (Task task : snowTasklist) {
if (task.getStartDate() == preSnowTask.getStartDate()) {
snowTaskListAtDate.add(task);
}
}
if(snowTaskListAtDate .isEmpty() && snowTasklist.isEmpty()){
taskList.saveTask(preSnowTask);
}
}
} }
/** /**

View File

@ -63,6 +63,7 @@ public class Task {
this.startDate = startDate; this.startDate = startDate;
nextExecution = startDate; nextExecution = startDate;
this.endDate = endDate; this.endDate = endDate;
this.cropId = cropId;
} }
/** /**

View File

@ -52,7 +52,7 @@ public class WeatherGardenTaskPlannerTest {
exampleTask.setNextExecution(LocalDate.now().plusDays(1L)); exampleTask.setNextExecution(LocalDate.now().plusDays(1L));
exampleWeatherTask = new Task("Hail", 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", "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); LocalDate.now(),LocalDate.now().plusDays(1L),3L);
taskList = new JsonTaskList(testFile); taskList = new JsonTaskList(testFile);