diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java index d3710ab..3ffa0fc 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java @@ -4,44 +4,117 @@ import ch.zhaw.gartenverwaltung.types.Task; import org.junit.jupiter.api.*; 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.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + public class JsonTaskDatabaseTest { TaskDatabase testDatabase; - SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy"); - @BeforeEach - void connectToDb() { - // testDatabase = new JsonTaskDatabase(); - } + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + private final URL dbDataSource = this.getClass().getResource("taskdb.json"); + private final URL testFile = this.getClass().getResource("test-taskdb.json"); + + @BeforeEach + void connectToDb() throws URISyntaxException, IOException { + assertNotNull(testFile); + assertNotNull(dbDataSource); + Files.copy(Path.of(testFile.toURI()), Path.of(dbDataSource.toURI()), StandardCopyOption.REPLACE_EXISTING); + testDatabase = new JsonTaskDatabase(); + } @Test @DisplayName("Check if results are retrieved completely") - void getTasks(){ - /* - List taskList=null; + void getTasks() { + + List taskList = null; try { - taskList = testDatabase.getTaskList(formatter.parse("01.05.2022"), formatter.parse("01.08.2022")); + taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022", formatter), + LocalDate.parse("31.05.2022", formatter)); } catch (IOException e) { throw new RuntimeException(e); - } catch (ParseException e) { - throw new RuntimeException(e); } - Assertions.assertTrue(taskList.size()>0); - */ + Assertions.assertEquals(3, taskList.size()); + + } + + //@Disabled("disabled until idProvider works") + @Test + @DisplayName("Add task.") + void addTask() { + Task task = new Task("Testtask", "This is a test Task.", LocalDate.parse("01.05.2022", formatter), 1); + try { + testDatabase.saveTask(task); + List taskList = null; + try { + taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022", formatter), + LocalDate.parse("31.05.2022", formatter)); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Assertions.assertEquals(4, taskList.size()); + + } catch (IOException e) { + throw new RuntimeException(e); + } } + @Test + @DisplayName("Remove task.") + void removeTask() { + Task task = new Task("Dummy", "Dummy", LocalDate.parse("31.05.2022", formatter), 1).withId(2); + try { + testDatabase.removeTask(task); + List taskList = null; + + taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022", formatter), + LocalDate.parse("31.05.2022", formatter)); + + Assertions.assertEquals(2, taskList.size()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + @Test void getTaskForCrop() { - // TODO implement Test + List taskList = null; + try { + taskList = testDatabase.getTaskForCrop(0); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Assertions.assertEquals(6, taskList.size()); + } @Test void removeTasksForCrop() { - // TODO implement Test + List taskList = null; + try { + testDatabase.removeTasksForCrop(0); + taskList = testDatabase.getTaskForCrop(0); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Assertions.assertEquals(0, taskList.size()); } } diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/test-taskdb.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-taskdb.json new file mode 100644 index 0000000..ff69563 --- /dev/null +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/test-taskdb.json @@ -0,0 +1,56 @@ +[ + { + "id" : 1, + "name" : "sow plant", + "description": "Plant the seeds, crops in de bed.", + "startDate" : "2022-05-01", + "endDate" : "2022-05-01", + "interval" : 0, + "cropId" : 0 + }, + { + "id" : 2, + "name" : "water plant", + "description": "water the plant, so that the soil is wet around the plant.", + "startDate" : "2022-05-01", + "endDate" : "2022-09-01", + "interval" : 2, + "cropId" : 0 + }, + { + "id" : 3, + "name" : "fertilize plant", + "description": "The fertilizer has to be mixed with water. Then fertilize the plants soil with the mixture", + "startDate" : "2022-06-01", + "endDate" : "2022-08-01", + "interval" : 28, + "cropId" : 0 + }, + { + "id" : 4, + "name" : "covering plant", + "description": "Take a big enough coverage for the plants. Cover the whole plant with a bit space between the plant and the coverage", + "startDate" : "2022-07-01", + "endDate" : "2022-07-01", + "interval" : 0, + "cropId" : 0 + }, + { + "id" : 5, + "name" : "look after plant", + "description": "Look for pest or illness at the leaves of the plant. Check the soil around the plant, if the roots are enough covered with soil", + "startDate" : "2022-05-01", + "endDate" : "2022-09-01", + "interval" : 5, + "cropId" : 0 + }, + { + "id" : 6, + "name" : "harvest plant", + "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", + "startDate" : "2022-09-01", + "endDate" : "2022-09-01", + "interval" : 0, + "cropId" : 0 + } +] \ No newline at end of file