extending tests and adding integration test

and better precision when comparing results
This commit is contained in:
Elias Csomor 2022-12-10 12:48:06 +01:00
parent 56020a7529
commit 76197a19df
3 changed files with 115 additions and 9 deletions

View File

@ -1,7 +1,12 @@
package ch.zhaw.gartenverwaltung.io;
import ch.zhaw.gartenverwaltung.models.GardenSchedule;
import ch.zhaw.gartenverwaltung.models.PlantNotFoundException;
import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Task;
import javafx.application.Platform;
import org.junit.jupiter.api.*;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
@ -13,6 +18,7 @@ import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -27,6 +33,22 @@ public class JsonTaskListTest {
private final URL dbDataSource = this.getClass().getResource("test-taskdb.json");
private final URL testFile = this.getClass().getResource("template-taskdb.json");
@BeforeAll
static void setUpAll() {
try{
Platform.startup(()->{});
}catch (IllegalStateException ise){
//ignore double launches
}
}
@AfterAll
static void tearDownAll() {
//Dont do: Platform.exit();
}
@BeforeEach
void connectToDb() throws URISyntaxException, IOException {
assertNotNull(testFile);
@ -35,6 +57,10 @@ public class JsonTaskListTest {
testDatabase = new JsonTaskList(dbDataSource);
}
private void reloadDb() {
testDatabase = new JsonTaskList(dbDataSource);
}
@Test
@DisplayName("Check if results are retrieved completely")
void getTasks() {
@ -47,7 +73,8 @@ public class JsonTaskListTest {
throw new RuntimeException(e);
}
Assertions.assertEquals(3, taskList.size());
List<Long> ids = taskList.stream().map(t -> t.getId().orElse(0L)).toList();
Assertions.assertEquals(Arrays.asList(1L, 2L, 5L), ids);
}
@ -65,7 +92,8 @@ public class JsonTaskListTest {
throw new RuntimeException(e);
}
Assertions.assertEquals(4, taskList.size());
List<Long> ids = taskList.stream().map(t -> t.getId().orElse(0L)).toList();
Assertions.assertEquals(Arrays.asList(1L, 2L, 5L, 9L), ids);
} catch (IOException e) {
throw new RuntimeException(e);
@ -83,7 +111,8 @@ public class JsonTaskListTest {
taskList = testDatabase.getTaskList(LocalDate.parse("2022-04-30", formatter),
LocalDate.parse("2022-05-31", formatter));
Assertions.assertEquals(2, taskList.size());
List<Long> ids = taskList.stream().map(t -> t.getId().orElse(0L)).toList();
Assertions.assertEquals(Arrays.asList(1L, 5L), ids);
} catch (IOException e) {
throw new RuntimeException(e);
}
@ -100,8 +129,8 @@ public class JsonTaskListTest {
throw new RuntimeException(e);
}
Assertions.assertEquals(6, taskList.size());
List<Long> ids = taskList.stream().map(t -> t.getId().orElse(0L)).toList();
Assertions.assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L), ids);
}
@ -116,10 +145,20 @@ public class JsonTaskListTest {
}
Assertions.assertEquals(0, taskList.size());
try {
taskList = testDatabase.getTaskForCrop(1);
} catch (IOException e) {
throw new RuntimeException(e);
}
List<Long> ids = (taskList.stream().map( task -> task.getId().orElse(0L)).toList());
Assertions.assertEquals(Arrays.asList(7L, 8L), ids);
}
@Test
void testDefaultConstructor(){
void testDefaultConstructor() {
JsonTaskList db = new JsonTaskList();
try {
assertNotNull(db.getTaskForCrop(0));
@ -138,6 +177,43 @@ public class JsonTaskListTest {
} catch (IOException e) {
throw new RuntimeException(e);
}
verify(mockObs, times(1)).onChange(ArgumentMatchers.anyList());
ArgumentCaptor<List<Task>> captor = ArgumentCaptor.forClass(List.class);
verify(mockObs, times(1)).onChange(captor.capture());
List<Long> ids = captor.getValue().stream().map(t -> t.getId().orElse(0L)).toList();
Assertions.assertEquals(Arrays.asList(7L, 8L), ids);
}
@Test
@Tag("IntegrationTest")
void testComplete() {
JsonPlantList plantList = new JsonPlantList();
try {
testDatabase.removeTasksForCrop(0);
testDatabase.removeTasksForCrop(1);
} catch (IOException e) {
throw new RuntimeException(e);
}
GardenSchedule gardenSchedule = null;
try {
gardenSchedule = new GardenSchedule(testDatabase, plantList);
} catch (IOException e) {
throw new RuntimeException(e);
}
Crop crop = new Crop(3L, LocalDate.parse("2022-12-01", formatter));
try {
gardenSchedule.planTasksForCrop(crop);
reloadDb();
List<Task> tasks = gardenSchedule.getTaskList();
List<String> tasknames = (tasks.stream().map( task -> task.getName().substring(0,3).toLowerCase()).toList());
Assertions.assertEquals(Arrays.asList("ger","wat","hil","har"), tasknames);
} catch (IOException | PlantNotFoundException | HardinessZoneNotSetException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -34,12 +34,17 @@ class GardenScheduleTest {
@BeforeAll
static void setUpAll() {
try{
Platform.startup(()->{});
}catch (IllegalStateException ise){
//ignore double launches
}
}
@AfterAll
static void tearDownAll() {
Platform.exit();
//Dont do: Platform.exit();
}
@BeforeEach
@ -234,5 +239,8 @@ class GardenScheduleTest {
testTaskList.getTaskList(LocalDate.MIN,LocalDate.MAX).get(0);
}
@Test
void testPlantNotFoundException() throws HardinessZoneNotSetException, IOException {
assertThrowsExactly(PlantNotFoundException.class, () -> { model.planTasksForCrop(new Crop(0, exampleStartDate)); });
}
}

View File

@ -64,5 +64,27 @@
"endDate" : "2022-09-01",
"interval" : 0,
"cropId" : 0
},
{
"id" : 7,
"name" : "sow plant",
"description": "Plant the seeds, crops in de bed.",
"startDate" : "2022-11-01",
"nextExecution": "2022-11-01",
"nextNotification": "2022-11-01",
"endDate" : "2022-11-01",
"interval" : 0,
"cropId" : 1
},
{
"id" : 8,
"name" : "harvest plant",
"description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ",
"startDate" : "2022-12-01",
"nextExecution": "2022-12-01",
"nextNotification": "2022-12-01",
"endDate" : "2022-12-01",
"interval" : 0,
"cropId" : 1
}
]