Made tests pass for plants not in zone
This commit is contained in:
parent
82eab6d5cd
commit
ce93531ab8
|
@ -1,6 +1,5 @@
|
||||||
package ch.zhaw.gartenverwaltung.io;
|
package ch.zhaw.gartenverwaltung.io;
|
||||||
|
|
||||||
import ch.zhaw.gartenverwaltung.types.GrowthPhase;
|
|
||||||
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
||||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
@ -32,6 +31,7 @@ public class JsonPlantDatabase implements PlantDatabase {
|
||||||
* Creating constant objects required to deserialize the {@link MonthDay} classes
|
* Creating constant objects required to deserialize the {@link MonthDay} classes
|
||||||
*/
|
*/
|
||||||
private final static JavaTimeModule timeModule = new JavaTimeModule();
|
private final static JavaTimeModule timeModule = new JavaTimeModule();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd");
|
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd");
|
||||||
MonthDayDeserializer dateDeserializer = new MonthDayDeserializer(dateFormat);
|
MonthDayDeserializer dateDeserializer = new MonthDayDeserializer(dateFormat);
|
||||||
|
@ -82,21 +82,16 @@ public class JsonPlantDatabase implements PlantDatabase {
|
||||||
List<Plant> result;
|
List<Plant> result;
|
||||||
result = mapper.readerForListOf(Plant.class).readValue(dataSource);
|
result = mapper.readerForListOf(Plant.class).readValue(dataSource);
|
||||||
|
|
||||||
for (Plant plant : result) {
|
|
||||||
// for discussion because of failing tests:
|
|
||||||
// for(GrowthPhase growthPhase: plant.lifecycle()) {
|
|
||||||
// if(growthPhase.zone()==zone) {
|
|
||||||
// keep in result, remove if no match
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
plant.inZone(currentZone);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Turn list into a HashMap with structure id => Plant
|
|
||||||
plantMap = result.stream()
|
plantMap = result.stream()
|
||||||
|
// Remove plants not in the current zone
|
||||||
|
.filter(plant -> {
|
||||||
|
plant.inZone(currentZone);
|
||||||
|
return !plant.lifecycle().isEmpty();
|
||||||
|
})
|
||||||
|
// Create Hashmap from results
|
||||||
.collect(HashMap::new,
|
.collect(HashMap::new,
|
||||||
(res, plant) -> res.put(plant.id(), plant),
|
(res, plant) -> res.put(plant.id(), plant),
|
||||||
(existing, replacement) -> { });
|
(existing, replacement) -> {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
package ch.zhaw.gartenverwaltung.io;
|
package ch.zhaw.gartenverwaltung.io;
|
||||||
|
|
||||||
import ch.zhaw.gartenverwaltung.types.Crop;
|
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.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
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.ParseException;
|
import java.net.URISyntaxException;
|
||||||
import java.text.SimpleDateFormat;
|
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.LocalDate;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@ -18,15 +19,24 @@ import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
public class JsonGardenPlanTest {
|
public class JsonGardenPlanTest {
|
||||||
GardenPlan testDatabase;
|
private GardenPlan testDatabase;
|
||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
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
|
@BeforeEach
|
||||||
void connectToDb() {
|
void connectToDb() throws URISyntaxException, IOException {
|
||||||
testDatabase = new JsonGardenPlan();
|
assertNotNull(testFile);
|
||||||
|
assertNotNull(dbDataSource);
|
||||||
|
Files.copy(Path.of(testFile.toURI()), Path.of(dbDataSource.toURI()), StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
testDatabase = new JsonGardenPlan(dbDataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,19 +52,14 @@ public class JsonGardenPlanTest {
|
||||||
Assertions.assertEquals(3, testList.size());
|
Assertions.assertEquals(3, testList.size());
|
||||||
|
|
||||||
List<Long> plantIds = testList.stream().map(Crop::getPlantId).collect(Collectors.toList());
|
List<Long> plantIds = testList.stream().map(Crop::getPlantId).collect(Collectors.toList());
|
||||||
List<Long> expected = Arrays.asList(1l, 1l, 0l);
|
List<Long> expected = Arrays.asList(1L, 1L, 0L);
|
||||||
Assertions.assertEquals(expected, plantIds);
|
Assertions.assertEquals(expected, plantIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Check whether single access works.")
|
@DisplayName("Check whether single access works.")
|
||||||
void getCropById() {
|
void getCropById() throws IOException {
|
||||||
Optional<Crop> testCrop;
|
Optional<Crop> testCrop = testDatabase.getCropById(1);
|
||||||
try {
|
|
||||||
testCrop = testDatabase.getCropById(1);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
assertTrue(testCrop.isPresent());
|
assertTrue(testCrop.isPresent());
|
||||||
Assertions.assertEquals(1, testCrop.get().getPlantId());
|
Assertions.assertEquals(1, testCrop.get().getPlantId());
|
||||||
}
|
}
|
||||||
|
@ -62,31 +67,22 @@ public class JsonGardenPlanTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Check for a nonexisting crop.")
|
@DisplayName("Check for a nonexisting crop.")
|
||||||
void getCropByIdMustFail() {
|
void getCropByIdMustFail() throws IOException {
|
||||||
Optional<Crop> testCrop;
|
Optional<Crop> testCrop = testDatabase.getCropById(99);
|
||||||
try {
|
|
||||||
testCrop = testDatabase.getCropById(99);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
Assertions.assertFalse(testCrop.isPresent());
|
Assertions.assertFalse(testCrop.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Add new Crop.")
|
@DisplayName("Add new Crop.")
|
||||||
void addNewCrop() {
|
void addNewCrop() {
|
||||||
Crop crop = new Crop(2l, LocalDate.parse("22.02.2023", formatter));
|
Crop crop = new Crop(3L, LocalDate.parse("2023-02-22", formatter));
|
||||||
try {
|
try {
|
||||||
testDatabase.saveCrop(crop);
|
testDatabase.saveCrop(crop);
|
||||||
assertTrue(crop.getCropId().isPresent());
|
assertTrue(crop.getCropId().isPresent());
|
||||||
Optional<Crop> testCrop;
|
Optional<Crop> testCrop = testDatabase.getCropById(crop.getCropId().get());
|
||||||
try {
|
|
||||||
testCrop = testDatabase.getCropById(crop.getCropId().get());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
assertTrue(testCrop.isPresent());
|
assertTrue(testCrop.isPresent());
|
||||||
Assertions.assertEquals(2l, testCrop.get().getPlantId());
|
Assertions.assertEquals(crop, testCrop.get());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -96,10 +92,10 @@ public class JsonGardenPlanTest {
|
||||||
@DisplayName("Remove crop")
|
@DisplayName("Remove crop")
|
||||||
void removeCrop(){
|
void removeCrop(){
|
||||||
try {
|
try {
|
||||||
Optional<Crop> crop = testDatabase.getCropById(2l);
|
Optional<Crop> crop = testDatabase.getCropById(2L);
|
||||||
Assertions.assertTrue(crop.isPresent());
|
Assertions.assertTrue(crop.isPresent());
|
||||||
testDatabase.removeCrop(crop.get());
|
testDatabase.removeCrop(crop.get());
|
||||||
crop = testDatabase.getCropById(2l);
|
crop = testDatabase.getCropById(2L);
|
||||||
Assertions.assertFalse(crop.isPresent());
|
Assertions.assertFalse(crop.isPresent());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
|
Loading…
Reference in New Issue