From aceb6aa1e6c120f0ca0334857a57b3388a768e02 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Mon, 24 Oct 2022 14:54:49 +0200 Subject: [PATCH] first tests and a typo corrected --- .../ch/zhaw/gartenverwaltung/io/plantdb.json | 2 +- .../io/JsonPlantDatabaseTest.java | 79 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json index f513570..723b450 100644 --- a/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/plantdb.json @@ -165,7 +165,7 @@ }, { "id": 2, - "name": "summertime onion", + "name": "Summertime Onion", "description": "Onion, (Allium cepa), herbaceous biennial plant in the amaryllis family (Amaryllidaceae) grown for its edible bulb. The onion is likely native to southwestern Asia but is now grown throughout the world, chiefly in the temperate zones. Onions are low in nutrients but are valued for their flavour and are used widely in cooking. They add flavour to such dishes as stews, roasts, soups, and salads and are also served as a cooked vegetable.", "lifecycle": [ { diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java new file mode 100644 index 0000000..b554502 --- /dev/null +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantDatabaseTest.java @@ -0,0 +1,79 @@ +package ch.zhaw.gartenverwaltung.io; + +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.SimpleDateFormat; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +public class JsonPlantDatabaseTest { + PlantDatabase testDatabase; + SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy"); + + @BeforeEach + void connectToDb() { + testDatabase = new JsonPlantDatabase(); + } + + + @Test + @DisplayName("Check if results are retrieved completely") + void getPlantList() { + List testList; + try { + testList = testDatabase.getPlantList(HardinessZone.ZONE_8A); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (HardinessZoneNotSetException e) { + throw new RuntimeException(e); + } + Assertions.assertEquals(3, testList.size()); + + List names = testList.stream().map(Plant::name).collect(Collectors.toList()); + List expected = Arrays.asList("Potato","Early Carrot","Summertime Onion"); + Assertions.assertEquals(expected,names); + } + + @Test + @DisplayName("Check whether single access works.") + void getPlantById() { + Optional testPlant; + try { + testDatabase.getPlantList(HardinessZone.ZONE_8A); + testPlant = testDatabase.getPlantById(1); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (HardinessZoneNotSetException e) { + throw new RuntimeException(e); + } + Assertions.assertTrue(testPlant.isPresent()); + Assertions.assertEquals("Early Carrot", testPlant.get().name()); + } + + @Test + @DisplayName("Check for a nonexisting plant.") + void getPlantByIdMustFail() { + Optional testPlant; + try { + testDatabase.getPlantList(HardinessZone.ZONE_8A); + testPlant = testDatabase.getPlantById(99); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (HardinessZoneNotSetException e) { + throw new RuntimeException(e); + } + Assertions.assertFalse(testPlant.isPresent()); + + + } + +} +