Compare commits
No commits in common. "a43a23427c6064fe56869006031737d281cceb46" and "7a6a0eb66fa4003ecc52bea0d58498b2dee8413b" have entirely different histories.
a43a23427c
...
7a6a0eb66f
|
@ -20,7 +20,7 @@ import java.util.Map;
|
|||
import java.util.Optional;
|
||||
|
||||
public class JsonGardenPlan implements GardenPlan {
|
||||
private final URL dataSource;
|
||||
private final URL dataSource = getClass().getResource("user-crops.json");
|
||||
|
||||
private IdProvider idProvider;
|
||||
private Map<Long, Crop> cropMap = Collections.emptyMap();
|
||||
|
@ -40,21 +40,7 @@ public class JsonGardenPlan implements GardenPlan {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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}
|
||||
* @see GardenPlan#getCrops()
|
||||
*/
|
||||
@Override
|
||||
public List<Crop> getCrops() throws IOException {
|
||||
|
@ -65,7 +51,7 @@ public class JsonGardenPlan implements GardenPlan {
|
|||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see GardenPlan#getCropById(long)
|
||||
*/
|
||||
@Override
|
||||
public Optional<Crop> getCropById(long id) throws IOException {
|
||||
|
@ -76,7 +62,7 @@ public class JsonGardenPlan implements GardenPlan {
|
|||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see GardenPlan#saveCrop(Crop)
|
||||
*
|
||||
* Saves a crop to the database.
|
||||
* If no {@link Crop#cropId} is set, one will be generated and
|
||||
|
@ -93,7 +79,7 @@ public class JsonGardenPlan implements GardenPlan {
|
|||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see GardenPlan#removeCrop(Crop)
|
||||
*/
|
||||
@Override
|
||||
public void removeCrop(Crop crop) throws IOException {
|
||||
|
|
|
@ -89,16 +89,15 @@ public class JsonPlantDatabase implements PlantDatabase {
|
|||
List<Plant> result;
|
||||
result = mapper.readerForListOf(Plant.class).readValue(dataSource);
|
||||
|
||||
plantMap = result.stream()
|
||||
// Remove plants not in the current zone
|
||||
.filter(plant -> {
|
||||
for (Plant plant : result) {
|
||||
plant.inZone(currentZone);
|
||||
return !plant.lifecycle().isEmpty();
|
||||
})
|
||||
// Create Hashmap from results
|
||||
}
|
||||
|
||||
// Turn list into a HashMap with structure id => Plant
|
||||
plantMap = result.stream()
|
||||
.collect(HashMap::new,
|
||||
(res, plant) -> res.put(plant.id(), plant),
|
||||
(existing, replacement) -> {});
|
||||
(existing, replacement) -> { });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package ch.zhaw.gartenverwaltung.types;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Crop {
|
||||
|
@ -42,24 +41,6 @@ public class Crop {
|
|||
public LocalDate getStartDate() { return startDate; }
|
||||
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
|
||||
public String toString() {
|
||||
return String.format("Crop [ cropId: %d, plantId: %d, startDate: %s, area: %f ]",
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
"name": "Germinate",
|
||||
"relativeStartDate": -14,
|
||||
"relativeEndDate": null,
|
||||
"description": "Take an egg carton and fill it with soil. Put the seedling deep enough so its half covered with soil. Keep it in 10-15 * Celsius with lots of light.",
|
||||
"description": "\"Take an egg carton and fill it with soil. Put the seedling deep enaugh so its half covered with soil. Keep it in 10-15 * Celsius with lots of light.\"",
|
||||
"interval": null,
|
||||
"isOptional": false
|
||||
}
|
||||
|
@ -53,7 +53,7 @@
|
|||
"name": "hilling",
|
||||
"relativeStartDate": 0,
|
||||
"relativeEndDate": null,
|
||||
"description": "When the plants are 20 cm tall, begin hilling the potatoes by gently mounding the soil from the center of your rows around the stems of the plant. Mound up the soil around the plant until just the top few leaves show above the soil. Two weeks later, hill up the soil again when the plants grow another 20 cm.",
|
||||
"description": "\"When the plants are 20 cm tall, begin hilling the potatoes by gently mounding the soil from the center of your rows around the stems of the plant. Mound up the soil around the plant until just the top few leaves show above the soil. Two weeks later, hill up the soil again when the plants grow another 20 cm.\"",
|
||||
"interval": 21,
|
||||
"isOptional": false
|
||||
}
|
||||
|
|
|
@ -1,105 +0,0 @@
|
|||
package ch.zhaw.gartenverwaltung.io;
|
||||
|
||||
import ch.zhaw.gartenverwaltung.types.Crop;
|
||||
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.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.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.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class JsonGardenPlanTest {
|
||||
private GardenPlan testDatabase;
|
||||
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
/**
|
||||
* Files to isolate the test-units
|
||||
*/
|
||||
private final URL dbDataSource = this.getClass().getResource("user-crops.json");
|
||||
private final URL testFile = this.getClass().getResource("test-user-crops.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 JsonGardenPlan(dbDataSource);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Check if results are retrieved completely.")
|
||||
void getCropsNotEmpty() {
|
||||
List<Crop> testList;
|
||||
try {
|
||||
testList = testDatabase.getCrops();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Assertions.assertEquals(3, testList.size());
|
||||
|
||||
List<Long> plantIds = testList.stream().map(Crop::getPlantId).collect(Collectors.toList());
|
||||
List<Long> expected = Arrays.asList(1L, 1L, 0L);
|
||||
Assertions.assertEquals(expected, plantIds);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check whether single access works.")
|
||||
void getCropById() throws IOException {
|
||||
Optional<Crop> testCrop = testDatabase.getCropById(1);
|
||||
assertTrue(testCrop.isPresent());
|
||||
Assertions.assertEquals(1, testCrop.get().getPlantId());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Check for a nonexisting crop.")
|
||||
void getCropByIdMustFail() throws IOException {
|
||||
Optional<Crop> testCrop = testDatabase.getCropById(99);
|
||||
Assertions.assertFalse(testCrop.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Add new Crop.")
|
||||
void addNewCrop() {
|
||||
Crop crop = new Crop(3L, LocalDate.parse("2023-02-22", formatter));
|
||||
try {
|
||||
testDatabase.saveCrop(crop);
|
||||
assertTrue(crop.getCropId().isPresent());
|
||||
Optional<Crop> testCrop = testDatabase.getCropById(crop.getCropId().get());
|
||||
|
||||
assertTrue(testCrop.isPresent());
|
||||
Assertions.assertEquals(crop, testCrop.get());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Remove crop")
|
||||
void removeCrop(){
|
||||
try {
|
||||
Optional<Crop> crop = testDatabase.getCropById(2L);
|
||||
Assertions.assertTrue(crop.isPresent());
|
||||
testDatabase.removeCrop(crop.get());
|
||||
crop = testDatabase.getCropById(2L);
|
||||
Assertions.assertFalse(crop.isPresent());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ 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;
|
||||
|
@ -15,6 +16,7 @@ import java.util.stream.Collectors;
|
|||
|
||||
public class JsonPlantDatabaseTest {
|
||||
PlantDatabase testDatabase;
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
|
||||
|
||||
@BeforeEach
|
||||
void connectToDb() {
|
||||
|
@ -24,7 +26,7 @@ public class JsonPlantDatabaseTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("Check if results are retrieved completely")
|
||||
void getPlantListNotEmpty() {
|
||||
void getPlantList() {
|
||||
List<Plant> testList;
|
||||
try {
|
||||
testList = testDatabase.getPlantList(HardinessZone.ZONE_8A);
|
||||
|
@ -38,26 +40,12 @@ public class JsonPlantDatabaseTest {
|
|||
Assertions.assertEquals(expected,names);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check if results are retrieved correctly when empty")
|
||||
void getPlantListEmpty() {
|
||||
List<Plant> testList;
|
||||
try {
|
||||
testList = testDatabase.getPlantList(HardinessZone.ZONE_1A);
|
||||
} catch (IOException | HardinessZoneNotSetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Assertions.assertEquals(0, testList.size());
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check whether single access works.")
|
||||
void getPlantByIdAndZone() {
|
||||
void getPlantById() {
|
||||
Optional<Plant> testPlant;
|
||||
try {
|
||||
testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1);
|
||||
testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A,1);
|
||||
} catch (IOException | HardinessZoneNotSetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
@ -65,23 +53,12 @@ public class JsonPlantDatabaseTest {
|
|||
Assertions.assertEquals("Early Carrot", testPlant.get().name());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check whether single access respects zone correctly.")
|
||||
void getPlantByIdAndWrongZone() throws HardinessZoneNotSetException, IOException {
|
||||
Optional<Plant> testPlant = testDatabase.getPlantById(HardinessZone.ZONE_1A, 1);
|
||||
Assertions.assertFalse(testPlant.isPresent());
|
||||
testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1);
|
||||
Assertions.assertTrue(testPlant.isPresent());
|
||||
|
||||
Assertions.assertEquals("Early Carrot", testPlant.get().name());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check for a nonexisting plant.")
|
||||
void getPlantByIdMustFail() {
|
||||
Optional<Plant> testPlant;
|
||||
try {
|
||||
testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 99);
|
||||
testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A,99);
|
||||
} catch (IOException | HardinessZoneNotSetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
[
|
||||
{
|
||||
"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
|
||||
}
|
||||
]
|
|
@ -1,2 +0,0 @@
|
|||
[
|
||||
]
|
Loading…
Reference in New Issue