From 5411ac69aeb18308357c62f16de698a8764e1f77 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Mon, 31 Oct 2022 11:15:27 +0100 Subject: [PATCH] initial GardenPlan tests --- .../io/JsonGardenPlanTest.java | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java new file mode 100644 index 0000000..159cccf --- /dev/null +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlanTest.java @@ -0,0 +1,101 @@ +package ch.zhaw.gartenverwaltung.io; + +import ch.zhaw.gartenverwaltung.types.Crop; +import ch.zhaw.gartenverwaltung.types.HardinessZone; +import ch.zhaw.gartenverwaltung.types.Plant; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class JsonGardenPlanTest { + GardenPlan testDatabase; + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + + @BeforeEach + void connectToDb() { + testDatabase = new JsonGardenPlan(); + } + + + @Test + @DisplayName("Check if results are retrieved completely.") + void getCropsNotEmpty() { + List testList; + try { + testList = testDatabase.getCrops(); + } catch (IOException e) { + throw new RuntimeException(e); + } + Assertions.assertEquals(3, testList.size()); + + List plantIds = testList.stream().map(Crop::getPlantId).collect(Collectors.toList()); + List expected = Arrays.asList(1l, 1l, 0l); + Assertions.assertEquals(expected, plantIds); + } + + @Test + @DisplayName("Check whether single access works.") + void getCropById() { + Optional testCrop; + try { + testCrop = testDatabase.getCropById(1); + } catch (IOException e) { + throw new RuntimeException(e); + } + assertTrue(testCrop.isPresent()); + Assertions.assertEquals(1, testCrop.get().getPlantId()); + } + + + @Test + @DisplayName("Check for a nonexisting crop.") + void getCropByIdMustFail() { + Optional testCrop; + try { + testCrop = testDatabase.getCropById(99); + } catch (IOException e) { + throw new RuntimeException(e); + } + Assertions.assertFalse(testCrop.isPresent()); + } + + @Test + @DisplayName("Add new Crop.") + void addNewCrop() { + + + Crop crop = new Crop(2l, LocalDate.parse("22.02.2023", formatter)); + try { + testDatabase.saveCrop(crop); + assertTrue(crop.getCropId().isPresent()); + Optional testCrop; + try { + testCrop = testDatabase.getCropById(crop.getCropId().get()); + } catch (IOException e) { + throw new RuntimeException(e); + } + assertTrue(testCrop.isPresent()); + Assertions.assertEquals(2l, testCrop.get().getPlantId()); + + } catch (IOException e) { + throw new RuntimeException(e); + } + + + + } +} +