Fix a bug in handling of recurring transactions with HWT NEXT_WORKDAY

If a recurring transaction has its firstOccurrence on a saturday it was falsely recognized as due today, if today was a sunday
This commit is contained in:
2019-05-19 11:57:57 +02:00
parent 7d1f915dd8
commit 8a62bb8bea
2 changed files with 45 additions and 3 deletions

View File

@@ -169,6 +169,12 @@ public class RecurringTransactionService {
return false; // early return 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 weekend;
boolean holiday; boolean holiday;
LocalDate yesterday = now; LocalDate yesterday = now;

View File

@@ -47,7 +47,7 @@ public class RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest
.findByDeletedFalseAndLastOccurrenceIsNullOrLastOccurrenceGreaterThanEqual(Mockito.any())) .findByDeletedFalseAndLastOccurrenceIsNullOrLastOccurrenceGreaterThanEqual(Mockito.any()))
.thenReturn(Collections.singletonList(createRecurringTransaction(-1))); .thenReturn(Collections.singletonList(createRecurringTransaction(-1)));
// Today is not a holiday but yesterday was // 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(); final LocalDate now = LocalDate.now();
// Act // Act
@@ -86,10 +86,10 @@ public class RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest
.thenReturn(Collections.singletonList(createRecurringTransaction(-(now.getDayOfWeek().getValue() + 2)))); .thenReturn(Collections.singletonList(createRecurringTransaction(-(now.getDayOfWeek().getValue() + 2))));
// First False for the dueToday check, 2x True for actual weekend, second False for Friday // First False for the dueToday check, 2x True for actual weekend, second False for Friday
Mockito.when(this.ruleService.isWeekend(Mockito.any())) 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 // First False for the dueToday check, 2x False for actual weekend, True for Friday
Mockito.when(this.ruleService.isHoliday(Mockito.any())) 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 // Act
final Iterable<RecurringTransaction> recurringDueToday = this.classUnderTest.getAllDueToday(monday); final Iterable<RecurringTransaction> recurringDueToday = this.classUnderTest.getAllDueToday(monday);
@@ -156,6 +156,31 @@ public class RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest
Assert.assertEquals(1, IterableUtils.size(recurringDueToday)); Assert.assertEquals(1, IterableUtils.size(recurringDueToday));
} }
/**
* This method tests whether a recurring transaction with firstOccurrence yesterday (a saturday) is <b>not</b> 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<RecurringTransaction> recurringDueToday = this.classUnderTest.getAllDueToday(now);
// Assert
Assert.assertEquals(0, IterableUtils.size(recurringDueToday));
}
private RecurringTransaction createRecurringTransaction(int days) { private RecurringTransaction createRecurringTransaction(int days) {
final RecurringTransaction recurringTransaction = new RecurringTransaction(); final RecurringTransaction recurringTransaction = new RecurringTransaction();
@@ -167,4 +192,15 @@ public class RecurringTransactionService_getAllDueToday_MONTHLY_NEXT_WORKDAYTest
return recurringTransaction; 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;
}
} }