Compare commits

..

No commits in common. "63f0501397c5812f55750a68aec03f3e5688325e" and "8fd57d91f249cce56244fdd679b56a46be17e958" have entirely different histories.

11 changed files with 51 additions and 365 deletions

View File

@ -39,7 +39,6 @@ dependencies {
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4' implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4' implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4'
testImplementation 'org.mockito:mockito-core:4.3.+'
} }
test { test {

View File

@ -56,9 +56,9 @@ public class JsonPlantDatabase implements PlantDatabase {
* @see PlantDatabase#getPlantById(long) * @see PlantDatabase#getPlantById(long)
*/ */
@Override @Override
public Optional<Plant> getPlantById(HardinessZone zone, long id) throws HardinessZoneNotSetException, IOException { public Optional<Plant> getPlantById(long id) throws HardinessZoneNotSetException, IOException {
if (plantMap.isEmpty()) { if (plantMap.isEmpty()) {
loadPlantList(zone); loadPlantList(currentZone);
} }
return Optional.ofNullable(plantMap.get(id)); return Optional.ofNullable(plantMap.get(id));
} }

View File

@ -1,112 +1,67 @@
package ch.zhaw.gartenverwaltung.io; package ch.zhaw.gartenverwaltung.io;
import ch.zhaw.gartenverwaltung.types.HardinessZone;
import ch.zhaw.gartenverwaltung.types.Task; import ch.zhaw.gartenverwaltung.types.Task;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.YearMonthDeserializer;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/**
* Implements the {@link TaskDatabase} interface for loading and writing {@link Task} objects
* from and to a JSON file.
* The reads are cached to minimize file-io operations.
*/
public class JsonTaskDatabase implements TaskDatabase{ public class JsonTaskDatabase implements TaskDatabase{
IdProvider idProvider;
private final URL dataSource = getClass().getResource("taskdb.json"); private final URL dataSource = getClass().getResource("taskdb.json");
private Map<Long, Task> taskMap = Collections.emptyMap(); private Map<Long, Task> taskMap = Collections.emptyMap();
/**
* Creating constant objects required to deserialize the {@link LocalDate} classes
*/
private final static JavaTimeModule timeModule = new JavaTimeModule(); private final static JavaTimeModule timeModule = new JavaTimeModule();
static { static {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd"); DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yy-MM");
LocalDateDeserializer dateDeserializer = new LocalDateDeserializer(dateFormat); YearMonthDeserializer dateDeserializer = new YearMonthDeserializer(dateFormat);
timeModule.addDeserializer(LocalDate.class, dateDeserializer); timeModule.addDeserializer(YearMonth.class, dateDeserializer);
} }
/**
* If no data is currently loaded, data is loaded from {@link #dataSource}.
* In any case, the values of {@link #taskMap} are returned.
*
* @see TaskDatabase#getTaskList(LocalDate, LocalDate)
*/
@Override @Override
public List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException{ public List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException{
if(taskMap.isEmpty()) { if(taskMap.isEmpty()) {
loadTaskListFromFile(); loadTaskListFromFile();
} }
return taskMap.values().stream().filter(task -> task.isInTimePeriode(start, end)).toList(); return taskMap.values().stream().filter(task -> task.isInTimePeriode(start, end)).toList();
} }
/**
* If no data is currently loaded, data is loaded from {@link #dataSource}.
* If the {@link Task} has an id than the task is added to the {@link #taskMap}
* otherwise the id is generated with the {@link IdProvider} before adding
* it to the {@link #taskMap}. In any case, the {@link #taskMap} is written
* to the {@link #dataSource}.
*
* @see TaskDatabase#saveTask(Task)
*/
@Override @Override
public void saveTask(Task task) throws IOException { public void saveTask(Task task) throws IOException {
if(taskMap.isEmpty()) { ObjectMapper mapper = new ObjectMapper();
loadTaskListFromFile(); if(dataSource != null) {
mapper.writeValue(new File(dataSource.getFile()), task);
} }
if(task.getId() == 0) {
task.withId(idProvider.incrementAndGet());
}
writeTaskListToFile();
} }
/**
* If no data is currently loaded, data is loaded from {@link #dataSource}.
* If the {@link Task}s id is found in the {@link #taskMap}, the Task is removed
* from the {@link #taskMap}. Then the Task are written to the {@link #dataSource}.
*
* @see TaskDatabase#removeTask(Task)
*/
@Override @Override
public void removeTask(Task task) throws IOException { public void removeTask(Task task) throws IOException {
if(taskMap.isEmpty()) { if(!taskMap.isEmpty() && taskMap.containsKey(task.getId())){
loadTaskListFromFile();
}
if(taskMap.containsKey(task.getId())){
taskMap.remove(task.getId()); taskMap.remove(task.getId());
writeTaskListToFile();
} }
} }
/**
* Writes cached data to the {@link #dataSource}.
*
* @throws IOException If the database cannot be accessed
*/
private void writeTaskListToFile() throws IOException { private void writeTaskListToFile() throws IOException {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(timeModule); mapper.registerModule(timeModule);
if(dataSource != null) {
mapper.writeValue(new File(dataSource.getFile()), taskMap); mapper.writeValue(new File(dataSource.getFile()), taskMap);
} }
}
/**
* Loads the database from {@link #dataSource} and updates the cached data.
*
* @throws IOException If the database cannot be accessed
*/
private void loadTaskListFromFile() throws IOException { private void loadTaskListFromFile() throws IOException {
if (dataSource != null) { if (dataSource != null) {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
@ -119,6 +74,26 @@ public class JsonTaskDatabase implements TaskDatabase{
.collect(HashMap::new, .collect(HashMap::new,
(res, task) -> res.put(task.getId(), task), (res, task) -> res.put(task.getId(), task),
(existing, replacement) -> {}); (existing, replacement) -> {});
System.out.println(taskMap);
}
}
//Test main method
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate yesterday = LocalDate.now();
yesterday = yesterday.minusDays(1);
Task testTask = new Task("water", "apply water", date);
JsonTaskDatabase jsonTaskDatabase = new JsonTaskDatabase();
JsonPlantDatabase jsonPlantDatabase = new JsonPlantDatabase();
try {
//test load file
jsonPlantDatabase.getPlantList(HardinessZone.ZONE_8A);
jsonTaskDatabase.loadTaskListFromFile();
} catch (Exception e){
System.out.println("Task load failed!: " + e.getMessage());
} }
} }
} }

View File

@ -30,5 +30,5 @@ public interface PlantDatabase {
* @throws IOException If the database cannot be accessed * @throws IOException If the database cannot be accessed
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified * @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
*/ */
Optional<Plant> getPlantById(HardinessZone zone, long id) throws IOException, HardinessZoneNotSetException; Optional<Plant> getPlantById(long id) throws IOException, HardinessZoneNotSetException;
} }

View File

@ -1,7 +1,5 @@
package ch.zhaw.gartenverwaltung.io; package ch.zhaw.gartenverwaltung.io;
import ch.zhaw.gartenverwaltung.types.HardinessZone;
import ch.zhaw.gartenverwaltung.types.Plant;
import ch.zhaw.gartenverwaltung.types.Task; import ch.zhaw.gartenverwaltung.types.Task;
import java.io.IOException; import java.io.IOException;
@ -9,34 +7,8 @@ import java.time.LocalDate;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
/**
* A database of {@link Task}s.
* The interface specifies the minimal required operations.
*/
public interface TaskDatabase { public interface TaskDatabase {
/**
* Yields a list of all {@link Task}s in the database with the start and end date of a period of time.
*
* @param start The start date of the wanted time period
* @param end The end date of the wanted time period
* @return A list of {@link Task}s planned in the specified time period
* @throws IOException If the database cannot be accessed
*/
List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException; List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException;
/**
* Saves the {@link Task} in the Cache.
*
* @param task The {@link Task} which is wanted to be saved in the TaskList
* @throws IOException If the database cannot be accessed
*/
void saveTask(Task task) throws IOException; void saveTask(Task task) throws IOException;
/**
* Removes the {@link Task}from the Cache.
*
* @param task The {@link Task} which has to be removed from the {@link Task} list.
* @throws IOException If the database cannot be accessed
*/
void removeTask(Task task) throws IOException; void removeTask(Task task) throws IOException;
} }

View File

@ -1,100 +0,0 @@
package ch.zhaw.gartenverwaltung.plantList;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase;
import ch.zhaw.gartenverwaltung.io.PlantDatabase;
import ch.zhaw.gartenverwaltung.types.HardinessZone;
import ch.zhaw.gartenverwaltung.types.Plant;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
public class PlantListModel {
private PlantDatabase plantDatabase;
private HardinessZone currentZone;
/**
* Comparators to create sorted Plant List
*/
static final Comparator<Plant> sortByName = (Plant o1, Plant o2) -> o1.name().compareTo(o2.name());
static final Comparator<Plant> SortById = (Plant o1, Plant o2) -> Long.compare(o1.id(), o2.id());
/**
* Constructor to create Database Object.
*/
public PlantListModel() {
plantDatabase = new JsonPlantDatabase();
setDefaultZone();
}
public PlantListModel(PlantDatabase plantDatabase){
this.plantDatabase = plantDatabase;
setDefaultZone();
}
private void setDefaultZone(){
currentZone = HardinessZone.ZONE_8A; // TODO: get Default Zone from Config
}
public void setCurrentZone(HardinessZone currentZone) {
this.currentZone = currentZone;
}
public HardinessZone getCurrentZone() {
return currentZone;
}
/**
* Method to get actual Plant List in alphabetic Order
* @return actual Plant List in alphabetic Order
*/
public List<Plant> getPlantList(HardinessZone zone) throws HardinessZoneNotSetException, IOException {
setCurrentZone(zone);
return getSortedPlantList(zone, sortByName);
}
/**
* Method to get the actual Plant list in custom Order
* @param zone selected hardiness zone
* @param comparator comparator to sort the list
* @return sorted list with plants in the given hardiness zone
* @throws IOException If the database cannot be accessed
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
*/
public List<Plant> getSortedPlantList(HardinessZone zone, Comparator<Plant> comparator) throws HardinessZoneNotSetException, IOException {
setCurrentZone(zone);
return plantDatabase.getPlantList(zone).stream().sorted(comparator).toList();
}
/**
* Method to get Filtered plant list
* @param predicate predicate to filter the list
* @param zone selected hardiness zone
* @return filterd list with plants in the hardinness zone
* @throws IOException If the database cannot be accessed
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
*/
public List<Plant> getFilteredPlantList(HardinessZone zone, Predicate<Plant> predicate) throws HardinessZoneNotSetException, IOException {
setCurrentZone(zone);
return getPlantList(zone).stream().filter(predicate).toList();
}
/**
* Method to get Filtered plant list by id by exact match
* @param zone selected hardiness zone
* @param id id of plant
* @return if id doesn't exist: empty List, else list with 1 plant entry.
* @throws IOException If the database cannot be accessed
* @throws HardinessZoneNotSetException If no {@link HardinessZone} was specified
*/
public List<Plant> getFilteredPlantListById(HardinessZone zone, Long id) throws HardinessZoneNotSetException, IOException {
setCurrentZone(zone);
List<Plant> plantList = new ArrayList<>();
plantDatabase.getPlantById(zone, id).ifPresent(plantList::add);
return plantList;
}
}

View File

@ -5,6 +5,5 @@ package ch.zhaw.gartenverwaltung.types;
* (Subject to later expansion) * (Subject to later expansion)
*/ */
public enum HardinessZone { public enum HardinessZone {
ZONE_1A,
ZONE_8A ZONE_8A
} }

View File

@ -16,16 +16,6 @@ public class Task {
private Integer interval; private Integer interval;
private LocalDate endDate; private LocalDate endDate;
/**
* default constructor
* (used by Json deserializer)
*/
public Task(){
name= "";
description= "";
startDate = LocalDate.now();
}
public Task(String name, String description, LocalDate startDate) { public Task(String name, String description, LocalDate startDate) {
this.name = name; this.name = name;
this.description = description; this.description = description;

View File

@ -2,49 +2,49 @@
{ {
"id" : 1, "id" : 1,
"name" : "sow plant", "name" : "sow plant",
"description": "Plant the seeds, crops in de bed.", "description": "Plant the seeds/ crops in de bed.",
"startDate" : "2022-05-01", "startDate" : "22-05",
"endDate" : "2022-05-01", "endDate" : "22-05",
"interval" : 0 "interval" : 0
}, },
{ {
"id" : 2, "id" : 2,
"name" : "water plant", "name" : "water plant",
"description": "water the plant, so that the soil is wet around the plant.", "description": "water the plant, so that the soil is wet around the plant.",
"startDate" : "2022-05-01", "startDate" : "22-05",
"endDate" : "2022-09-01", "endDate" : "22-09",
"interval" : 2 "interval" : 2
}, },
{ {
"id" : 3, "id" : 3,
"name" : "fertilize plant", "name" : "fertilize plant",
"description": "The fertilizer has to be mixed with water. Then fertilize the plants soil with the mixture", "description": "The fertilizer has to be mixed with water. Then fertilize the plant's soil with the mixture",
"startDate" : "2022-06-01", "startDate" : "22-06",
"endDate" : "2022-08-01", "endDate" : "22-08",
"interval" : 28 "interval" : 28
}, },
{ {
"id" : 4, "id" : 4,
"name" : "covering plant", "name" : "covering plant",
"description": "Take a big enough coverage for the plants. Cover the whole plant with a bit space between the plant and the coverage", "description": "Take a big enough coverage for the plants. Cover the whole plant with a bit space between the plant and the coverage",
"startDate" : "2022-07-01", "startDate" : "22-07",
"endDate" : "2022-07-01", "endDate" : "22-07",
"interval" : 0 "interval" : 0
}, },
{ {
"id" : 5, "id" : 5,
"name" : "look after plant", "name" : "look after plant",
"description": "Look for pest or illness at the leaves of the plant. Check the soil around the plant, if the roots are enough covered with soil", "description": "Look for pest or illness at the leaves of the plant. Check the soil around the plant, if the roots are enough covered with soil",
"startDate" : "2022-05-01", "startDate" : "22-05",
"endDate" : "2022-09-01", "endDate" : "22-09",
"interval" : 5 "interval" : 5
}, },
{ {
"id" : 6, "id" : 6,
"name" : "harvest plant", "name" : "harvest plant",
"description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ", "description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ",
"startDate" : "2022-09-01", "startDate" : "22-09",
"endDate" : "2022-09-01", "endDate" : "22-09",
"interval" : 0 "interval" : 0
} }
] ]

View File

@ -1,149 +0,0 @@
package ch.zhaw.gartenverwaltung.plantList;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase;
import ch.zhaw.gartenverwaltung.io.PlantDatabase;
import ch.zhaw.gartenverwaltung.types.HardinessZone;
import ch.zhaw.gartenverwaltung.types.Plant;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Predicate;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class PlantListModelTest {
PlantDatabase plantDatabase;
List<Plant> examplePlantList;
PlantListModel model;
@BeforeEach
void setUp() throws HardinessZoneNotSetException, IOException {
createExamplePlantList();
plantDatabase = mockPlantDatabase(examplePlantList);
model = new PlantListModel(plantDatabase);
}
@AfterEach
void tearDown() {
}
void createExamplePlantList(){
examplePlantList = new ArrayList<>();
examplePlantList.add(new Plant(
20,
"summertime onion",
"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.",
"15,30,2",
0,
"sandy to loamy, loose soil, free of stones",
new ArrayList<>(),
new ArrayList<>())
);
examplePlantList.add(new Plant(
0,
"Potato",
"The potato is a tuber, round or oval, with small white roots called 'eyes', that are growth buds. The size varies depending on the variety; the colour of the skin can be white, yellow or even purple.",
"35",
6,
"sandy",
new ArrayList<>(),
new ArrayList<>())
);
examplePlantList.add(new Plant(
1,
"Early Carrot",
"Carrot, (Daucus carota), herbaceous, generally biennial plant of the Apiaceae family that produces an edible taproot. Among common varieties root shapes range from globular to long, with lower ends blunt to pointed. Besides the orange-coloured roots, white-, yellow-, and purple-fleshed varieties are known.",
"5,35,2.5",
0,
"sandy to loamy, loose soil, free of stones",
new ArrayList<>(),
new ArrayList<>())
);
}
PlantDatabase mockPlantDatabase(List<Plant> plantList) throws HardinessZoneNotSetException, IOException {
PlantDatabase plantDatabase = mock(JsonPlantDatabase.class);
when(plantDatabase.getPlantList(HardinessZone.ZONE_8A)).thenReturn(plantList);
when(plantDatabase.getPlantList(HardinessZone.ZONE_1A)).thenReturn(new ArrayList<>());
when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 0)).thenReturn(Optional.of(plantList.get(1)));
when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 1)).thenReturn(Optional.of(plantList.get(2)));
when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 20)).thenReturn(Optional.of(plantList.get(0)));
when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 2)).thenReturn(Optional.empty());
return plantDatabase;
}
void checkCurrentZone(HardinessZone expectedZone) {
assertEquals(expectedZone, model.getCurrentZone());
}
@Test
void setCurrentZone() {
checkCurrentZone(HardinessZone.ZONE_8A); // TODO change to get default zone from config
model.setCurrentZone(HardinessZone.ZONE_1A);
checkCurrentZone(HardinessZone.ZONE_1A);
model.setCurrentZone(HardinessZone.ZONE_8A);
checkCurrentZone(HardinessZone.ZONE_8A);
}
@Test
void getPlantList() throws HardinessZoneNotSetException, IOException {
model.setCurrentZone(HardinessZone.ZONE_1A);
List<Plant> plantListResult = model.getPlantList(HardinessZone.ZONE_8A);
checkCurrentZone(HardinessZone.ZONE_8A);
assertEquals(examplePlantList.size(), plantListResult.size());
assertEquals(examplePlantList.get(2), plantListResult.get(0));
assertEquals(examplePlantList.get(1), plantListResult.get(1));
assertEquals(examplePlantList.get(0), plantListResult.get(2));
assertEquals(0, model.getPlantList(HardinessZone.ZONE_1A).size());
}
@Test
void getSortedPlantList() throws HardinessZoneNotSetException, IOException {
model.setCurrentZone(HardinessZone.ZONE_1A);
List<Plant> plantListResult = model.getSortedPlantList(HardinessZone.ZONE_8A, PlantListModel.sortByName);
checkCurrentZone(HardinessZone.ZONE_8A);
assertEquals(examplePlantList.size(), plantListResult.size());
assertEquals(examplePlantList.get(2), plantListResult.get(0));
assertEquals(examplePlantList.get(1), plantListResult.get(1));
assertEquals(examplePlantList.get(0), plantListResult.get(2));
plantListResult = model.getSortedPlantList(HardinessZone.ZONE_8A, PlantListModel.SortById);
assertEquals(examplePlantList.size(), plantListResult.size());
assertEquals(examplePlantList.get(1), plantListResult.get(0));
assertEquals(examplePlantList.get(2), plantListResult.get(1));
assertEquals(examplePlantList.get(0), plantListResult.get(2));
}
@Test
void getFilteredPlantList() throws HardinessZoneNotSetException, IOException {
model.setCurrentZone(HardinessZone.ZONE_1A);
Predicate<Plant> predicate = plant -> plant.name().toUpperCase(Locale.ROOT).contains("E");
List<Plant> plantListResult = model.getFilteredPlantList(HardinessZone.ZONE_8A, predicate);
checkCurrentZone(HardinessZone.ZONE_8A);
assertEquals(2, plantListResult.size());
assertEquals(examplePlantList.get(2), plantListResult.get(0));
assertEquals(examplePlantList.get(0), plantListResult.get(1));
}
@Test
void getFilteredPlantListById() throws HardinessZoneNotSetException, IOException {
model.setCurrentZone(HardinessZone.ZONE_1A);
List<Plant> plantListResult = model.getFilteredPlantListById(HardinessZone.ZONE_8A, 2L);
assertEquals(0, plantListResult.size());
plantListResult = model.getFilteredPlantListById(HardinessZone.ZONE_8A, 20L);
assertEquals(1, plantListResult.size());
assertEquals(examplePlantList.get(0), plantListResult.get(0));
}
}