From 8a9a5775f4493e444edad0dbff97efcba98e5eb5 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Sun, 24 Mar 2024 10:31:47 +0100 Subject: [PATCH] [inmemory] Fix boundaries for queries (#16563) Queries should include the boundaries, but the previous code did not. Signed-off-by: Jan N. Klug Signed-off-by: Ciprian Pascu --- .../internal/InMemoryPersistenceService.java | 4 +- .../internal/InMemoryPersistenceTests.java | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.persistence.inmemory/src/main/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceService.java b/bundles/org.openhab.persistence.inmemory/src/main/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceService.java index 14af3f72f71..0caf9721fb5 100644 --- a/bundles/org.openhab.persistence.inmemory/src/main/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceService.java +++ b/bundles/org.openhab.persistence.inmemory/src/main/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceService.java @@ -271,11 +271,11 @@ public class InMemoryPersistenceService implements ModifiablePersistenceService @SuppressWarnings({ "rawType", "unchecked" }) private boolean applies(PersistEntry entry, FilterCriteria filter) { ZonedDateTime beginDate = filter.getBeginDate(); - if (beginDate != null && entry.timestamp().isBefore(beginDate)) { + if (beginDate != null && beginDate.isAfter(entry.timestamp())) { return false; } ZonedDateTime endDate = filter.getEndDate(); - if (endDate != null && entry.timestamp().isAfter(endDate)) { + if (endDate != null && endDate.isBefore(entry.timestamp())) { return false; } diff --git a/bundles/org.openhab.persistence.inmemory/src/test/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceTests.java b/bundles/org.openhab.persistence.inmemory/src/test/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceTests.java index ce945a6b6da..ef00db6ef76 100644 --- a/bundles/org.openhab.persistence.inmemory/src/test/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceTests.java +++ b/bundles/org.openhab.persistence.inmemory/src/test/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceTests.java @@ -211,4 +211,68 @@ public class InMemoryPersistenceTests { assertThat(storedStates.last().getState(), is(historicState3)); assertThat(storedStates.last().getTimestamp(), is(expectedTime.plusHours(4))); } + + @Test + public void endDateProperlyObserved() { + TreeSet storedStates = new TreeSet<>(Comparator.comparing(HistoricItem::getTimestamp)); + + State historicState1 = new StringType("value1"); + State historicState2 = new StringType("value2"); + + ZonedDateTime historicTime1 = ZonedDateTime.of(2022, 05, 31, 10, 0, 0, 0, ZoneId.systemDefault()); + ZonedDateTime historicTime2 = historicTime1.plusHours(2); + service.store(item, historicTime1, historicState1); + service.store(item, historicTime2, historicState2); + + // end date is between first and second date, only return one dataset + filterCriteria = new FilterCriteria(); + filterCriteria.setItemName(ITEM_NAME); + filterCriteria.setEndDate(historicTime1.plusHours(1)); + + service.query(filterCriteria).forEach(storedStates::add); + assertThat(storedStates.size(), is(1)); + + // end date is exactly second date, return both dataset + storedStates.clear(); + filterCriteria = new FilterCriteria(); + filterCriteria.setItemName(ITEM_NAME); + filterCriteria.setEndDate(historicTime2); + + service.query(filterCriteria).forEach(storedStates::add); + assertThat(storedStates.size(), is(2)); + + // end date is after second date is already covered by case #1 + } + + @Test + public void beginDateProperlyObserved() { + TreeSet storedStates = new TreeSet<>(Comparator.comparing(HistoricItem::getTimestamp)); + + State historicState1 = new StringType("value1"); + State historicState2 = new StringType("value2"); + + ZonedDateTime historicTime1 = ZonedDateTime.of(2022, 05, 31, 10, 0, 0, 0, ZoneId.systemDefault()); + ZonedDateTime historicTime2 = historicTime1.plusHours(2); + service.store(item, historicTime1, historicState1); + service.store(item, historicTime2, historicState2); + + // begin date is between first and second date, only return one dataset + filterCriteria = new FilterCriteria(); + filterCriteria.setItemName(ITEM_NAME); + filterCriteria.setEndDate(historicTime2.minusHours(1)); + + service.query(filterCriteria).forEach(storedStates::add); + assertThat(storedStates.size(), is(1)); + + // begin date is exactly first date, return both dataset + storedStates.clear(); + filterCriteria = new FilterCriteria(); + filterCriteria.setItemName(ITEM_NAME); + filterCriteria.setBeginDate(historicTime1); + + service.query(filterCriteria).forEach(storedStates::add); + assertThat(storedStates.size(), is(2)); + + // begin date is before first date is already covered by case #1 + } }