diff --git a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenPlanModelTest.java b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenPlanModelTest.java index 10cd252..e70026b 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenPlanModelTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenPlanModelTest.java @@ -6,17 +6,25 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.time.LocalDate; import java.time.MonthDay; import java.util.ArrayList; import java.util.List; import java.util.Optional; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.*; public class GardenPlanModelTest { + private final URL dbDataSource = JsonCropList.class.getResource("user-crops.json"); + private final URL testFile = JsonCropList.class.getResource("test-user-crops.json"); + CropList cropList; List exampleCrops; Crop exampleCropOnion; @@ -29,7 +37,7 @@ public class GardenPlanModelTest { Garden model; @BeforeEach - void setUp() throws IOException { + void setUp() throws IOException, URISyntaxException { examplePlantOnion = new Plant( @@ -76,18 +84,24 @@ public class GardenPlanModelTest { exampleCrops.add(exampleCrop1); exampleCrops.add(exampleCrop2); exampleCrops.add(exampleCrop3); - cropList = mockGardenPlan(exampleCrops); + cropList = mockCropList(exampleCrops); - GardenSchedule gardenSchedule = new GardenSchedule(new JsonTaskList(), new JsonPlantList()); - model = new Garden(gardenSchedule, cropList); + // Reset Crop "database" before test + assertNotNull(testFile); + assertNotNull(dbDataSource); + Files.copy(Path.of(testFile.toURI()), Path.of(dbDataSource.toURI()), StandardCopyOption.REPLACE_EXISTING); + CropList testDatabase = new JsonCropList(dbDataSource); + + GardenSchedule gardenSchedule = mock(GardenSchedule.class); + model = new Garden(gardenSchedule, testDatabase); } - CropList mockGardenPlan(List cropList) throws IOException { - CropList gardenPlan = mock(CropList.class); - when(gardenPlan.getCrops()).thenReturn(cropList); - when(gardenPlan.getCropById(5)).thenReturn(java.util.Optional.ofNullable(exampleCropCarrot)); - when(gardenPlan.getCropById(3)).thenReturn(java.util.Optional.ofNullable(exampleCropOnion)); - return gardenPlan; + CropList mockCropList(List cropList) throws IOException { + CropList croplist = mock(CropList.class); + when(croplist.getCrops()).thenReturn(cropList); + when(croplist.getCropById(5)).thenReturn(java.util.Optional.ofNullable(exampleCropCarrot)); + when(croplist.getCropById(3)).thenReturn(java.util.Optional.ofNullable(exampleCropOnion)); + return croplist; } @Test