diff --git a/src/main/java/de/financer/service/RecurringTransactionService.java b/src/main/java/de/financer/service/RecurringTransactionService.java index 4c5a7bb..b921365 100644 --- a/src/main/java/de/financer/service/RecurringTransactionService.java +++ b/src/main/java/de/financer/service/RecurringTransactionService.java @@ -169,6 +169,12 @@ public class RecurringTransactionService { return false; // early return } + // If today is a weekend day or holiday the recurring transaction cannot be due today, because the + // holiday weekend type says NEXT_WORKDAY. + if (this.ruleService.isHoliday(now) || this.ruleService.isWeekend(now)) { + return false; // early return + } + boolean weekend; boolean holiday; LocalDate yesterday = now; diff --git a/src/test/java/de/financer/service/RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest.java b/src/test/java/de/financer/service/RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest.java index db5f517..85d0d4b 100644 --- a/src/test/java/de/financer/service/RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest.java +++ b/src/test/java/de/financer/service/RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest.java @@ -47,7 +47,7 @@ public class RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest .findByDeletedFalseAndLastOccurrenceIsNullOrLastOccurrenceGreaterThanEqual(Mockito.any())) .thenReturn(Collections.singletonList(createRecurringTransaction(-1))); // Today is not a holiday but yesterday was - Mockito.when(this.ruleService.isHoliday(Mockito.any())).thenReturn(Boolean.FALSE, Boolean.TRUE); + Mockito.when(this.ruleService.isHoliday(Mockito.any())).thenReturn(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE); final LocalDate now = LocalDate.now(); // Act @@ -86,10 +86,10 @@ public class RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest .thenReturn(Collections.singletonList(createRecurringTransaction(-(now.getDayOfWeek().getValue() + 2)))); // First False for the dueToday check, 2x True for actual weekend, second False for Friday Mockito.when(this.ruleService.isWeekend(Mockito.any())) - .thenReturn(Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE); + .thenReturn(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE); // First False for the dueToday check, 2x False for actual weekend, True for Friday Mockito.when(this.ruleService.isHoliday(Mockito.any())) - .thenReturn(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE); + .thenReturn(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE); // Act final Iterable recurringDueToday = this.classUnderTest.getAllDueToday(monday); @@ -156,6 +156,31 @@ public class RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest Assert.assertEquals(1, IterableUtils.size(recurringDueToday)); } + /** + * This method tests whether a recurring transaction with firstOccurrence yesterday (a saturday) is not due + * today (a sunday). + * + * relates to: test_getAllDueToday_duePast_weekend_sunday + */ + @Test + public void test_getAllDueToday_duePast_weekend_not_due_on_sunday() { + // Arrange + final LocalDate now = LocalDate.of(2019, 5, 19); // A sunday + Mockito.when(this.recurringTransactionRepository + .findByDeletedFalseAndLastOccurrenceIsNullOrLastOccurrenceGreaterThanEqual(Mockito.any())) + .thenReturn(Collections.singletonList(createRecurringTransaction(LocalDate.of(2019, 5, 18)))); + // First False for the dueToday check, 2x True for actual weekend, second False for Friday + Mockito.when(this.ruleService.isWeekend(Mockito.any())) + .thenReturn(Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE); + + + // Act + final Iterable recurringDueToday = this.classUnderTest.getAllDueToday(now); + + // Assert + Assert.assertEquals(0, IterableUtils.size(recurringDueToday)); + } + private RecurringTransaction createRecurringTransaction(int days) { final RecurringTransaction recurringTransaction = new RecurringTransaction(); @@ -167,4 +192,15 @@ public class RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest return recurringTransaction; } + + private RecurringTransaction createRecurringTransaction(LocalDate firstOccurrence) { + final RecurringTransaction recurringTransaction = new RecurringTransaction(); + + recurringTransaction.setFirstOccurrence(firstOccurrence); + recurringTransaction.setHolidayWeekendType(HolidayWeekendType.NEXT_WORKDAY); + recurringTransaction.setIntervalType(IntervalType.MONTHLY); + recurringTransaction.setDeleted(false); + + return recurringTransaction; + } }