Skip to content

Commit

Permalink
Fix Date Validation to Fail for Future Enrollment Dates (Including To…
Browse files Browse the repository at this point in the history
…morrow)
  • Loading branch information
zubaira committed Nov 25, 2024
1 parent ff9444a commit 5e20c87
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import static org.hisp.dhis.tracker.imports.validation.ValidationCode.E1052;

import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZoneId;
import java.util.Objects;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.tracker.imports.bundle.TrackerBundle;
Expand Down Expand Up @@ -84,13 +84,13 @@ private void validateEnrollmentDatesNotInFuture(
final LocalDate now = LocalDate.now();
if (Objects.nonNull(enrollment.getEnrolledAt())
&& Boolean.FALSE.equals(program.getSelectEnrollmentDatesInFuture())
&& enrollment.getEnrolledAt().atOffset(ZoneOffset.UTC).toLocalDate().isAfter(now)) {
&& enrollment.getEnrolledAt().atZone(ZoneId.systemDefault()).toLocalDate().isAfter(now)) {
reporter.addError(enrollment, E1020, enrollment.getEnrolledAt());
}

if (Objects.nonNull(enrollment.getOccurredAt())
&& Boolean.FALSE.equals(program.getSelectIncidentDatesInFuture())
&& enrollment.getOccurredAt().atOffset(ZoneOffset.UTC).toLocalDate().isAfter(now)) {
&& enrollment.getOccurredAt().atZone(ZoneId.systemDefault()).toLocalDate().isAfter(now)) {
reporter.addError(enrollment, E1021, enrollment.getOccurredAt());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void testMandatoryDatesMustBePresent() {

@Test
void testDatesMustNotBeInTheFuture() {
final Instant dateInTheFuture = now().plus(Duration.ofDays(2));
final Instant dateInTheFuture = now().plus(Duration.ofDays(1));
Enrollment enrollment =
Enrollment.builder()
.enrollment(UID.generate())
Expand All @@ -122,8 +122,7 @@ void testDatesMustNotBeInTheFuture() {
}

@Test
void testDatesMustNotBeInTheFuture1() {
// Get "tomorrow" based on the system's default time zone
void testDatesWithNoTimeZoneMustNotBeInTheFuture() {
ZoneId systemZone = ZoneId.systemDefault();
LocalDate tomorrow = LocalDate.now(systemZone).plusDays(1);
Instant dateTomorrow = tomorrow.atStartOfDay(systemZone).toInstant();
Expand All @@ -137,11 +136,7 @@ void testDatesMustNotBeInTheFuture1() {
.enrolledAt(dateTomorrow)
.build();

// Mock a program that does not allow future enrollment/incident dates
Program program = new Program();
program.setSelectEnrollmentDatesInFuture(false);
program.setSelectIncidentDatesInFuture(false);
when(preheat.getProgram(enrollment.getProgram())).thenReturn(program);
when(preheat.getProgram(enrollment.getProgram())).thenReturn(new Program());

// Run validation
validator.validate(reporter, bundle, enrollment);
Expand Down

0 comments on commit 5e20c87

Please sign in to comment.