Compare commits

...

12 Commits

Author SHA1 Message Date
gulerdav 1e14a07ef8 Merge branch 'dev' into feature_json-gardenplan_M2 2022-10-28 11:43:45 +02:00
gulerdav b851c11f7d Merge pull request #33 from schrom01/feature_plantList_M2
Feature plant list model m2
2022-10-24 21:40:30 +02:00
schrom01 4e15a706fb code cleanup 2022-10-24 21:34:19 +02:00
schrom01 d686469336 Created Tests for PlantListModel
#15
2022-10-24 21:30:20 +02:00
schrom01 fb237e47c0 added new HardinessZone for testing 2022-10-24 19:08:24 +02:00
schrom01 9fa4762b9c Merge remote-tracking branch 'origin/feature_plantList_M2' into feature_plantList_M2 2022-10-24 19:01:19 +02:00
schrom01 e5b5e1b88a adapted class PlantListModel to PlantDatabase
#12
2022-10-24 19:00:06 +02:00
schrom01 d1c5c58468 adapted class PlantListModel to PlantDatabase 2022-10-24 18:55:26 +02:00
schrom01 f9149c48fd Merge branch 'dev' into feature_plantList_M2 2022-10-24 12:51:52 +02:00
schrom01 e51da071bc updated method getFilteredPlantList 2022-10-24 12:44:11 +02:00
schrom01 5b0e472ec7 implemented Methods to sort and filter PlantList 2022-10-20 21:33:42 +02:00
schrom01 7dd157b9d5 implemented Methods to sort and filter PlantList 2022-10-20 21:26:30 +02:00
7 changed files with 255 additions and 4 deletions

View File

@ -40,6 +40,7 @@ dependencies {
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'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.4' implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8: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(long id) throws HardinessZoneNotSetException, IOException { public Optional<Plant> getPlantById(HardinessZone zone, long id) throws HardinessZoneNotSetException, IOException {
if (plantMap.isEmpty()) { if (plantMap.isEmpty()) {
loadPlantList(currentZone); loadPlantList(zone);
} }
return Optional.ofNullable(plantMap.get(id)); return Optional.ofNullable(plantMap.get(id));
} }

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(long id) throws IOException, HardinessZoneNotSetException; Optional<Plant> getPlantById(HardinessZone zone, long id) throws IOException, HardinessZoneNotSetException;
} }

View File

@ -0,0 +1,100 @@
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,5 +5,6 @@ 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

@ -251,4 +251,4 @@
} }
] ]
} }
] ]

View File

@ -0,0 +1,149 @@
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));
}
}