Skip to content

Commit

Permalink
fix: skip empty predicate for employee report
Browse files Browse the repository at this point in the history
  • Loading branch information
extern-maksim-kuzmin1 committed Feb 24, 2024
1 parent 627604a commit c4e9bbc
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Iterable<Payload> convert(@Nonnull ReportData<Employee> data) {
}

private Payload createReport(ReportData<Employee> data, ReportContext context) {
var skipEmpty = context.isSkipEmpty();
var skipEmpty = context.getSkipEmpty().test(data.requestDate());

List<EmployeeDto> birthdays = data.data().stream()
.filter(employee -> context.getBirthdayPredicate().test(employee, data.requestDate()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@AllArgsConstructor
public class ReportContext {
private final Function<LocalDate, String> headerMapper;
private final boolean skipEmpty;
private final Predicate<LocalDate> skipEmpty;
private final BiPredicate<Employee, LocalDate> birthdayPredicate;
private final BiPredicate<Employee, LocalDate> anniversaryPredicate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class BeforeEventReportContext extends ReportContext {
public BeforeEventReportContext(String headerPrefix, int daysBefore) {
super(date -> STR."\{headerPrefix}\{reportDate(date)}",
false,
_ -> true,
(employee, date) -> {
var birthday = employee.getBirthday();
return isSameDay(birthday, date.plusDays(daysBefore));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public class DailyReportContext extends ReportContext {
};

public DailyReportContext(String headerPrefix) {
super(date -> STR."\{headerPrefix}\{reportDate(date)}", true, BIRTHDAY_PREDICATE, ANNIVERSARY_PREDICATE);
super(date -> STR."\{headerPrefix}\{reportDate(date)}", _ -> true, BIRTHDAY_PREDICATE, ANNIVERSARY_PREDICATE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import static com.whiskels.notifier.utilities.DateTimeUtil.isLater;
import static com.whiskels.notifier.utilities.DateTimeUtil.isSameMonth;
import static com.whiskels.notifier.utilities.DateTimeUtil.reportDate;

public class MonthMiddleReportContext extends ReportContext {
private static final BiPredicate<Employee, LocalDate> BIRTHDAY_PREDICATE = (employee, date) -> {
Expand All @@ -21,7 +22,7 @@ public class MonthMiddleReportContext extends ReportContext {
};

public MonthMiddleReportContext(String header) {
super(_ignored -> header, false, BIRTHDAY_PREDICATE, ANNIVERSARY_PREDICATE);
super(_ignored -> header, MonthMiddleReportContext::isMiddleOfMonth, BIRTHDAY_PREDICATE, ANNIVERSARY_PREDICATE);
}

private static boolean isMiddleOfMonth(LocalDate date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MonthStartReportContext extends ReportContext {
};

public MonthStartReportContext(String header) {
super(_ignored -> header, false, BIRTHDAY_PREDICATE, ANNIVERSARY_PREDICATE);
super(_ignored -> header, MonthStartReportContext::isStartOfMonth, BIRTHDAY_PREDICATE, ANNIVERSARY_PREDICATE);
}

private static boolean isStartOfMonth(LocalDate date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ void shouldConvertEmployeePayload() {
"metadata" : null
}""";
ReportContext birthdayContext = mock(ReportContext.class);
when(birthdayContext.isSkipEmpty()).thenReturn(false);
when(birthdayContext.getSkipEmpty()).thenReturn(_ -> false);
when(birthdayContext.getBirthdayPredicate()).thenReturn((_, _) -> true);
when(birthdayContext.getAnniversaryPredicate()).thenReturn((_, _) -> false);
when(birthdayContext.getHeaderMapper()).thenReturn(_ -> "Birthday Header");

ReportContext anniversaryContext = mock(ReportContext.class);
when(anniversaryContext.isSkipEmpty()).thenReturn(true);
when(anniversaryContext.getSkipEmpty()).thenReturn(_ -> true);
when(anniversaryContext.getBirthdayPredicate()).thenReturn((_, _) -> false);
when(anniversaryContext.getAnniversaryPredicate()).thenReturn((_, _) -> true);
when(anniversaryContext.getHeaderMapper()).thenReturn(_ -> "Anniversary Header");
Expand All @@ -192,7 +192,7 @@ void shouldConvertEmployeePayload() {
@DisplayName("Should return null if skip empty and no data")
void shouldReturnNullIfSkipEmptyAndNoData() {
ReportContext context = mock(ReportContext.class);
when(context.isSkipEmpty()).thenReturn(true);
when(context.getSkipEmpty()).thenReturn(_ -> true);
when(context.getBirthdayPredicate()).thenReturn((_, _) -> false);
when(context.getAnniversaryPredicate()).thenReturn((_, _) -> false);

Expand Down

0 comments on commit c4e9bbc

Please sign in to comment.