Merge branch 'dev' into feature_gardenplan-model_M2
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
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,7 +8,6 @@ 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;
|
||||
@@ -16,7 +15,6 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class JsonPlantDatabaseTest {
|
||||
PlantDatabase testDatabase;
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
|
||||
|
||||
@BeforeEach
|
||||
void connectToDb() {
|
||||
@@ -26,7 +24,7 @@ public class JsonPlantDatabaseTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("Check if results are retrieved completely")
|
||||
void getPlantList() {
|
||||
void getPlantListNotEmpty() {
|
||||
List<Plant> testList;
|
||||
try {
|
||||
testList = testDatabase.getPlantList(HardinessZone.ZONE_8A);
|
||||
@@ -40,12 +38,26 @@ 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 getPlantById() {
|
||||
void getPlantByIdAndZone() {
|
||||
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);
|
||||
}
|
||||
@@ -53,12 +65,23 @@ 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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
package ch.zhaw.gartenverwaltung.taskList;
|
||||
|
||||
import ch.zhaw.gartenverwaltung.io.*;
|
||||
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||
import ch.zhaw.gartenverwaltung.types.Task;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class TaskListModelTest {
|
||||
TaskDatabase taskDatabase;
|
||||
PlantDatabase plantDatabase;
|
||||
List<Task> exampleTaskList;
|
||||
Map<Long, Plant> examplePlantMap;
|
||||
TaskListModel model;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
createExampleTaskList();
|
||||
taskDatabase = mockTaskDatabase(exampleTaskList);
|
||||
plantDatabase = mockPlantDatabase(examplePlantMap);
|
||||
model = new TaskListModel(taskDatabase, plantDatabase);
|
||||
}
|
||||
|
||||
private TaskDatabase mockTaskDatabase(List<Task> exampleTaskList) throws IOException {
|
||||
TaskDatabase taskDatabase = mock(JsonTaskDatabase.class);
|
||||
when(taskDatabase.getTaskList(LocalDate.MIN, LocalDate.MAX)).thenReturn(exampleTaskList);
|
||||
|
||||
when(taskDatabase.getTaskList(LocalDate.now(), LocalDate.MAX)).thenReturn((exampleTaskList.subList(1, 4)));
|
||||
|
||||
List<Task> pastTasks = new ArrayList<>();
|
||||
pastTasks.add(exampleTaskList.get(0));
|
||||
pastTasks.add(exampleTaskList.get(2));
|
||||
pastTasks.add(exampleTaskList.get(4));
|
||||
when(taskDatabase.getTaskList(LocalDate.MIN, LocalDate.now())).thenReturn(pastTasks);
|
||||
|
||||
|
||||
when(taskDatabase.getTaskList(LocalDate.now(), LocalDate.now())).thenReturn(List.of(exampleTaskList.get(2)));
|
||||
when(taskDatabase.getTaskList(LocalDate.now().plusDays(1L), LocalDate.now().plusDays(1L))).thenReturn(List.of(exampleTaskList.get(1)));
|
||||
when(taskDatabase.getTaskList(LocalDate.now().plusDays(2L), LocalDate.now().plusDays(2L))).thenReturn(List.of());
|
||||
when(taskDatabase.getTaskList(LocalDate.now().plusDays(3L), LocalDate.now().plusDays(3L))).thenReturn(List.of());
|
||||
when(taskDatabase.getTaskList(LocalDate.now().plusDays(4L), LocalDate.now().plusDays(4L))).thenReturn(List.of());
|
||||
when(taskDatabase.getTaskList(LocalDate.now().plusDays(5L), LocalDate.now().plusDays(5L))).thenReturn(List.of());
|
||||
when(taskDatabase.getTaskList(LocalDate.now().plusDays(6L), LocalDate.now().plusDays(6L))).thenReturn(List.of());
|
||||
|
||||
return taskDatabase;
|
||||
}
|
||||
|
||||
private PlantDatabase mockPlantDatabase(Map<Long, Plant> examplePlantMap) {
|
||||
return new PlantDatabase() {
|
||||
@Override
|
||||
public List<Plant> getPlantList(HardinessZone zone) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Plant> getPlantById(HardinessZone zone, long id) {
|
||||
return Optional.ofNullable(examplePlantMap.get(id));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void createExampleTaskList() {
|
||||
exampleTaskList = new ArrayList<>();
|
||||
exampleTaskList.add(new Task("name", "description", LocalDate.now().minusDays(1), 1L));
|
||||
exampleTaskList.add(new Task("name", "description", LocalDate.now().plusDays(1), 2L));
|
||||
exampleTaskList.add(new Task("name", "description", LocalDate.now(), 1L));
|
||||
exampleTaskList.add(new Task("name", "description", LocalDate.of(9019, 5, 5), 1L));
|
||||
exampleTaskList.add(new Task("name", "description", LocalDate.of(2019, 5, 5), 2L));
|
||||
}
|
||||
|
||||
void createExamplePlantMap() {
|
||||
examplePlantMap = new HashMap<>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void addTask() throws IOException {
|
||||
Task taskToAdd = new Task("name", "description", LocalDate.now(), 1L);
|
||||
model.addTask(taskToAdd);
|
||||
verify(taskDatabase, times(1)).saveTask(taskToAdd);
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeTask() throws IOException {
|
||||
Task taskToRemove = new Task("name", "description", LocalDate.now(), 1L);
|
||||
model.removeTask(taskToRemove);
|
||||
verify(taskDatabase, times(1)).removeTask(taskToRemove);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTaskList() throws IOException {
|
||||
List<Task> listToCheck = model.getTaskList();
|
||||
assertEquals(5, listToCheck.size());
|
||||
assertEquals(exampleTaskList.get(4), listToCheck.get(0));
|
||||
assertEquals(exampleTaskList.get(0), listToCheck.get(1));
|
||||
assertEquals(exampleTaskList.get(2), listToCheck.get(2));
|
||||
assertEquals(exampleTaskList.get(1), listToCheck.get(3));
|
||||
assertEquals(exampleTaskList.get(3), listToCheck.get(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFutureTasks() throws IOException {
|
||||
List<Task> listToCheck = model.getFutureTasks();
|
||||
assertEquals(3, listToCheck.size());
|
||||
assertEquals(exampleTaskList.get(2), listToCheck.get(0));
|
||||
assertEquals(exampleTaskList.get(1), listToCheck.get(1));
|
||||
assertEquals(exampleTaskList.get(3), listToCheck.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPastTasks() throws IOException {
|
||||
List<Task> listToCheck = model.getPastTasks();
|
||||
assertEquals(3, listToCheck.size());
|
||||
assertEquals(exampleTaskList.get(4), listToCheck.get(0));
|
||||
assertEquals(exampleTaskList.get(0), listToCheck.get(1));
|
||||
assertEquals(exampleTaskList.get(2), listToCheck.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTasksUpcomingWeek() throws IOException {
|
||||
List<List<Task>> dayList = model.getTasksUpcomingWeek();
|
||||
assertEquals(7, dayList.size());
|
||||
|
||||
//Check day 0
|
||||
assertEquals(1, dayList.get(0).size());
|
||||
assertEquals(exampleTaskList.get(2), dayList.get(0).get(0));
|
||||
|
||||
//Check day 1
|
||||
assertEquals(1, dayList.get(1).size());
|
||||
assertEquals(exampleTaskList.get(1), dayList.get(1).get(0));
|
||||
|
||||
//Check day 2
|
||||
assertEquals(0, dayList.get(2).size());
|
||||
//Check day 3
|
||||
assertEquals(0, dayList.get(3).size());
|
||||
//Check day 4
|
||||
assertEquals(0, dayList.get(4).size());
|
||||
//Check day 5
|
||||
assertEquals(0, dayList.get(5).size());
|
||||
//Check day 6
|
||||
assertEquals(0, dayList.get(6).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeTasksForCrop() throws IOException {
|
||||
model.removeTasksForCrop(1L);
|
||||
verify(taskDatabase, times(1)).removeTasksForCrop(1L);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user