#26 added fixed test files, made Crop class testable

This commit is contained in:
David Guler 2022-11-03 14:28:26 +01:00
parent ce93531ab8
commit 83bc011870
5 changed files with 69 additions and 32 deletions

View File

@ -20,7 +20,7 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
public class JsonGardenPlan implements GardenPlan { public class JsonGardenPlan implements GardenPlan {
private final URL dataSource = getClass().getResource("user-crops.json"); private final URL dataSource;
private IdProvider idProvider; private IdProvider idProvider;
private Map<Long, Crop> cropMap = Collections.emptyMap(); private Map<Long, Crop> cropMap = Collections.emptyMap();
@ -40,7 +40,21 @@ public class JsonGardenPlan implements GardenPlan {
} }
/** /**
* @see GardenPlan#getCrops() * Default constructor
*/
public JsonGardenPlan() {
this.dataSource = getClass().getResource("user-crops.json");
}
/**
* Constructor to use a specified {@link URL} as a {@link #dataSource}
* @param dataSource A {@link URL} to the file to be used as a data source
*/
public JsonGardenPlan(URL dataSource) {
this.dataSource = dataSource;
}
/**
* {@inheritDoc}
*/ */
@Override @Override
public List<Crop> getCrops() throws IOException { public List<Crop> getCrops() throws IOException {
@ -51,7 +65,7 @@ public class JsonGardenPlan implements GardenPlan {
} }
/** /**
* @see GardenPlan#getCropById(long) * {@inheritDoc}
*/ */
@Override @Override
public Optional<Crop> getCropById(long id) throws IOException { public Optional<Crop> getCropById(long id) throws IOException {
@ -62,7 +76,7 @@ public class JsonGardenPlan implements GardenPlan {
} }
/** /**
* @see GardenPlan#saveCrop(Crop) * {@inheritDoc}
* *
* Saves a crop to the database. * Saves a crop to the database.
* If no {@link Crop#cropId} is set, one will be generated and * If no {@link Crop#cropId} is set, one will be generated and
@ -79,7 +93,7 @@ public class JsonGardenPlan implements GardenPlan {
} }
/** /**
* @see GardenPlan#removeCrop(Crop) * {@inheritDoc}
*/ */
@Override @Override
public void removeCrop(Crop crop) throws IOException { public void removeCrop(Crop crop) throws IOException {

View File

@ -1,6 +1,7 @@
package ch.zhaw.gartenverwaltung.types; package ch.zhaw.gartenverwaltung.types;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
public class Crop { public class Crop {
@ -41,6 +42,24 @@ public class Crop {
public LocalDate getStartDate() { return startDate; } public LocalDate getStartDate() { return startDate; }
public double getArea() { return area; } public double getArea() { return area; }
@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other instanceof Crop otherCrop) {
return Objects.equals(this.cropId, otherCrop.cropId) &&
plantId == otherCrop.plantId &&
startDate != null && startDate.equals(otherCrop.startDate) &&
area == otherCrop.area;
}
return false;
}
@Override
public int hashCode() {
int startCode = startDate != null ? startDate.hashCode() : 0;
return (int) plantId ^ (startCode << 16);
}
@Override @Override
public String toString() { public String toString() {
return String.format("Crop [ cropId: %d, plantId: %d, startDate: %s, area: %f ]", return String.format("Crop [ cropId: %d, plantId: %d, startDate: %s, area: %f ]",

View File

@ -8,7 +8,6 @@ import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -16,7 +15,6 @@ import java.util.stream.Collectors;
public class JsonPlantDatabaseTest { public class JsonPlantDatabaseTest {
PlantDatabase testDatabase; PlantDatabase testDatabase;
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
@BeforeEach @BeforeEach
void connectToDb() { void connectToDb() {
@ -30,9 +28,7 @@ public class JsonPlantDatabaseTest {
List<Plant> testList; List<Plant> testList;
try { try {
testList = testDatabase.getPlantList(HardinessZone.ZONE_8A); testList = testDatabase.getPlantList(HardinessZone.ZONE_8A);
} catch (IOException e) { } catch (IOException | HardinessZoneNotSetException e) {
throw new RuntimeException(e);
} catch (HardinessZoneNotSetException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
Assertions.assertEquals(3, testList.size()); Assertions.assertEquals(3, testList.size());
@ -48,9 +44,7 @@ public class JsonPlantDatabaseTest {
List<Plant> testList; List<Plant> testList;
try { try {
testList = testDatabase.getPlantList(HardinessZone.ZONE_1A); testList = testDatabase.getPlantList(HardinessZone.ZONE_1A);
} catch (IOException e) { } catch (IOException | HardinessZoneNotSetException e) {
throw new RuntimeException(e);
} catch (HardinessZoneNotSetException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
Assertions.assertEquals(0, testList.size()); Assertions.assertEquals(0, testList.size());
@ -64,9 +58,7 @@ public class JsonPlantDatabaseTest {
Optional<Plant> testPlant; Optional<Plant> testPlant;
try { try {
testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1); testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1);
} catch (IOException e) { } catch (IOException | HardinessZoneNotSetException e) {
throw new RuntimeException(e);
} catch (HardinessZoneNotSetException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
Assertions.assertTrue(testPlant.isPresent()); Assertions.assertTrue(testPlant.isPresent());
@ -75,20 +67,12 @@ public class JsonPlantDatabaseTest {
@Test @Test
@DisplayName("Check whether single access respects zone correctly.") @DisplayName("Check whether single access respects zone correctly.")
void getPlantByIdAndWrongZone() { void getPlantByIdAndWrongZone() throws HardinessZoneNotSetException, IOException {
Optional<Plant> testPlant; Optional<Plant> testPlant = testDatabase.getPlantById(HardinessZone.ZONE_1A, 1);
try { Assertions.assertFalse(testPlant.isPresent());
testPlant = testDatabase.getPlantById(HardinessZone.ZONE_1A, 1); testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1);
Assertions.assertFalse(testPlant.isPresent());
testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1);
Assertions.assertTrue(testPlant.isPresent());
} catch (IOException e) {
throw new RuntimeException(e);
} catch (HardinessZoneNotSetException e) {
throw new RuntimeException(e);
}
Assertions.assertTrue(testPlant.isPresent()); Assertions.assertTrue(testPlant.isPresent());
Assertions.assertEquals("Early Carrot", testPlant.get().name()); Assertions.assertEquals("Early Carrot", testPlant.get().name());
} }
@ -98,9 +82,7 @@ public class JsonPlantDatabaseTest {
Optional<Plant> testPlant; Optional<Plant> testPlant;
try { try {
testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 99); testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 99);
} catch (IOException e) { } catch (IOException | HardinessZoneNotSetException e) {
throw new RuntimeException(e);
} catch (HardinessZoneNotSetException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
Assertions.assertFalse(testPlant.isPresent()); Assertions.assertFalse(testPlant.isPresent());

View File

@ -0,0 +1,20 @@
[
{
"cropId": 0,
"plantId": 1,
"startDate": "2023-02-25",
"area": 0.5
},
{
"cropId": 1,
"plantId": 1,
"startDate": "2023-03-01",
"area": 0.5
},
{
"cropId": 2,
"plantId": 0,
"startDate": "2023-03-25",
"area": 1.5
}
]

View File

@ -0,0 +1,2 @@
[
]