From 9f510ea4887e541cfc4416e2c04ca025289a689b Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Sun, 12 Nov 2023 22:56:16 +0100 Subject: [PATCH] GH-450 - AccountancyRepository now finds custom entries by Interval. --- .../AccountancyEntryRepository.java | 11 ++----- .../AccountancyRepositoryTests.java | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/salespointframework/accountancy/AccountancyEntryRepository.java b/src/main/java/org/salespointframework/accountancy/AccountancyEntryRepository.java index f99d1cf1..17c91481 100644 --- a/src/main/java/org/salespointframework/accountancy/AccountancyEntryRepository.java +++ b/src/main/java/org/salespointframework/accountancy/AccountancyEntryRepository.java @@ -67,13 +67,8 @@ Streamable findByDateBetween(LocalDateTime from, * @param to must not be {@literal null}. * @return will never be {@literal null}. */ - default Streamable findByDateBetween(LocalDateTime from, LocalDateTime to) { - - Assert.notNull(from, "Start date must not be null!"); - Assert.notNull(to, "End date must not be null!"); - - return findByDateBetween(from, to, AccountancyEntry.class); - } + @Query("select ae from AccountancyEntry ae where ae.date > :from and ae.date < :to") + Streamable findByDateBetween(LocalDateTime from, LocalDateTime to); /** * Returns all {@link AccountancyEntry}s within the given {@link Interval}. @@ -85,7 +80,7 @@ default Streamable findByDateIn(Interval interval) { Assert.notNull(interval, "Interval must not be null!"); - return findByDateBetween(interval.getStart(), interval.getEnd()); + return findByDateIn(interval, AccountancyEntry.class); } /** diff --git a/src/test/java/org/salespointframework/accountancy/AccountancyRepositoryTests.java b/src/test/java/org/salespointframework/accountancy/AccountancyRepositoryTests.java index fea9f917..41865180 100644 --- a/src/test/java/org/salespointframework/accountancy/AccountancyRepositoryTests.java +++ b/src/test/java/org/salespointframework/accountancy/AccountancyRepositoryTests.java @@ -23,6 +23,7 @@ import lombok.RequiredArgsConstructor; import java.time.LocalDateTime; +import java.util.stream.Stream; import javax.money.MonetaryAmount; @@ -104,6 +105,27 @@ void findsEntriesByConcreteType() { assertThat(repository.findAll(CustomAccountancyEntry.class)).containsExactly(customEntry); } + @Test // GH-450 + void findsCustomEntryWithinInterval() { + + var now = LocalDateTime.now(); + + var entries = Stream.of( + new AccountancyEntry(Money.of(10, Currencies.EURO)), + new CustomAccountancyEntry(Money.of(10, Currencies.EURO))) + .map(it -> it.setDate(now)) + .map(repository::save) + .toList(); + + var start = now.minusMinutes(1); + var end = now.plusMinutes(1); + + assertThat(repository.findByDateBetween(start, end)) + .containsExactlyInAnyOrderElementsOf(entries); + assertThat(repository.findByDateIn(Interval.from(start).to(end))) + .containsExactlyInAnyOrderElementsOf(entries); + } + @Entity @NoArgsConstructor(force = true) static class CustomAccountancyEntry extends AccountancyEntry { @@ -111,5 +133,14 @@ static class CustomAccountancyEntry extends AccountancyEntry { CustomAccountancyEntry(MonetaryAmount amount) { super(amount); } + + /* + * (non-Javadoc) + * @see org.salespointframework.accountancy.AccountancyEntry#toString() + */ + @Override + public String toString() { + return "Custom" + super.toString(); + } } }