Skip to content

Commit

Permalink
fix: [DHIS2-15272] Fix program expiryDays check with periodType for e…
Browse files Browse the repository at this point in the history
…vents (2.39) (#19335)
  • Loading branch information
ameenhere authored Nov 29, 2024
1 parent e7dc55e commit e5806d5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public void validateEvent(ValidationErrorReporter reporter, TrackerBundle bundle
return;
}

validateExpiryDays(reporter, bundle, event, program);
validatePeriodType(reporter, event, program);
validateCompletionExpiryDays(reporter, bundle, event, program);
validateExpiryPeriodType(reporter, event, program);
}

private void validateExpiryDays(
private void validateCompletionExpiryDays(
ValidationErrorReporter reporter, TrackerBundle bundle, Event event, Program program) {
User actingUser = bundle.getUser();

Expand All @@ -102,7 +102,8 @@ private void validateExpiryDays(
}
}

private void validatePeriodType(ValidationErrorReporter reporter, Event event, Program program) {
private void validateExpiryPeriodType(
ValidationErrorReporter reporter, Event event, Program program) {
checkNotNull(event, TrackerImporterAssertErrors.EVENT_CANT_BE_NULL);
checkNotNull(program, TrackerImporterAssertErrors.PROGRAM_CANT_BE_NULL);

Expand All @@ -121,9 +122,16 @@ private void validatePeriodType(ValidationErrorReporter reporter, Event event, P
return;
}

Period period = periodType.createPeriod(new Date());
Period eventPeriod = periodType.createPeriod(Date.from(referenceDate));

if (referenceDate.isBefore(period.getStartDate().toInstant())) {
if (eventPeriod
.getEndDate()
.toInstant() // This will be 00:00 time of the period end date.
.plus(
ofDays(
program.getExpiryDays()
+ 1L)) // Extra day added to account for final 24 hours of expiring day
.isBefore(Instant.now())) {
reporter.addError(event, E1047, event);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void setUp() {
bundle = TrackerBundle.builder().user(user).preheat(preheat).build();

when(preheat.getProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID)))
.thenReturn(getProgramWithRegistration());
.thenReturn(getProgramWithRegistration(5));
when(preheat.getProgram(MetadataIdentifier.ofUid(PROGRAM_WITHOUT_REGISTRATION_ID)))
.thenReturn(getProgramWithoutRegistration());

Expand All @@ -104,7 +104,6 @@ public void setUp() {

@Test
void testEventIsValid() {
// given
Event event = new Event();
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITHOUT_REGISTRATION_ID));
event.setOccurredAt(now());
Expand All @@ -113,145 +112,164 @@ void testEventIsValid() {
TrackerBundle bundle =
TrackerBundle.builder().user(getEditExpiredUser()).preheat(preheat).build();

// when
this.hookToTest.validateEvent(reporter, bundle, event);

// then
assertFalse(reporter.hasErrors());
}

@Test
void testEventIsNotValidWhenOccurredDateIsNotPresentAndProgramIsWithoutRegistration() {
// given
Event event = new Event();
event.setEvent(CodeGenerator.generateUid());
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITHOUT_REGISTRATION_ID));

// when
this.hookToTest.validateEvent(reporter, bundle, event);

// then
hasTrackerError(reporter, E1031, EVENT, event.getUid());
}

@Test
void testEventIsNotValidWhenOccurredDateIsNotPresentAndEventIsActive() {
// given
Event event = new Event();
event.setEvent(CodeGenerator.generateUid());
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID));
event.setStatus(EventStatus.ACTIVE);

// when
this.hookToTest.validateEvent(reporter, bundle, event);

// then
hasTrackerError(reporter, E1031, EVENT, event.getUid());
}

@Test
void testEventIsNotValidWhenOccurredDateIsNotPresentAndEventIsCompleted() {
// given
Event event = new Event();
event.setEvent(CodeGenerator.generateUid());
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID));
event.setStatus(EventStatus.COMPLETED);

// when
this.hookToTest.validateEvent(reporter, bundle, event);

// then
hasTrackerError(reporter, E1031, EVENT, event.getUid());
}

@Test
void testEventIsNotValidWhenScheduledDateIsNotPresentAndEventIsSchedule() {
// given
Event event = new Event();
event.setEvent(CodeGenerator.generateUid());
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID));
event.setOccurredAt(Instant.now());
event.setStatus(EventStatus.SCHEDULE);

// when
this.hookToTest.validateEvent(reporter, bundle, event);

// then
hasTrackerError(reporter, E1050, EVENT, event.getUid());
}

@Test
void testEventIsNotValidWhenCompletedAtIsNotPresentAndEventIsCompleted() {
// given
Event event = new Event();
event.setEvent(CodeGenerator.generateUid());
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID));
event.setOccurredAt(now());
event.setStatus(EventStatus.COMPLETED);

// when
this.hookToTest.validateEvent(reporter, bundle, event);

// then
hasTrackerError(reporter, E1042, EVENT, event.getUid());
}

@Test
void testEventIsNotValidWhenCompletedAtIsTooSoonAndEventIsCompleted() {
// given
Event event = new Event();
event.setEvent(CodeGenerator.generateUid());
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID));
event.setOccurredAt(now());
event.setCompletedAt(sevenDaysAgo());
event.setStatus(EventStatus.COMPLETED);

// when
this.hookToTest.validateEvent(reporter, bundle, event);

// then
hasTrackerError(reporter, E1043, EVENT, event.getUid());
}

@Test
void testEventIsNotValidWhenOccurredAtAndScheduledAtAreNotPresent() {
// given
Event event = new Event();
event.setEvent(CodeGenerator.generateUid());
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID));
event.setOccurredAt(null);
event.setScheduledAt(null);
event.setStatus(EventStatus.SKIPPED);

// when
this.hookToTest.validateEvent(reporter, bundle, event);

// then
hasTrackerError(reporter, E1046, EVENT, event.getUid());
}

@Test
void testEventIsNotValidWhenDateBelongsToExpiredPeriod() {
// given
void shouldFailValidationForEventWhenDateBelongsToExpiredPeriod() {
Event event = new Event();
event.setEvent(CodeGenerator.generateUid());
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID));
event.setOccurredAt(sevenDaysAgo());
event.setStatus(EventStatus.ACTIVE);

// when
this.hookToTest.validateEvent(reporter, bundle, event);

// then
hasTrackerError(reporter, E1047, EVENT, event.getUid());
}

private Program getProgramWithRegistration() {
@Test
void shouldPassValidationForEventWhenDateBelongsToPastPeriodWithZeroExpiryDays() {
when(preheat.getProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID)))
.thenReturn(getProgramWithRegistration(0));
Event event = new Event();
event.setEvent(CodeGenerator.generateUid());
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID));
event.setOccurredAt(sevenDaysAgo());
event.setStatus(EventStatus.ACTIVE);

this.hookToTest.validateEvent(reporter, bundle, event);

assertFalse(reporter.hasErrors());
}

@Test
void shouldPassValidationForEventWhenDateBelongsPastEventPeriodButWithinExpiryDays() {
when(preheat.getProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID)))
.thenReturn(getProgramWithRegistration(7));
Event event = new Event();
event.setEvent(CodeGenerator.generateUid());
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID));
event.setOccurredAt(sevenDaysAgo());
event.setStatus(EventStatus.ACTIVE);

this.hookToTest.validateEvent(reporter, bundle, event);

assertFalse(reporter.hasErrors());
}

@Test
void shouldPassValidationForEventWhenScheduledDateBelongsToFuturePeriod() {
when(preheat.getProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID)))
.thenReturn(getProgramWithRegistration(5));
Event event = new Event();
event.setEvent(CodeGenerator.generateUid());
event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID));
event.setScheduledAt(sevenDaysLater());
event.setStatus(EventStatus.SCHEDULE);

this.hookToTest.validateEvent(reporter, bundle, event);

assertFalse(reporter.hasErrors());
}

private Program getProgramWithRegistration(int expiryDays) {
Program program = createProgram('A');
program.setUid(PROGRAM_WITH_REGISTRATION_ID);
program.setProgramType(ProgramType.WITH_REGISTRATION);
program.setCompleteEventsExpiryDays(5);
program.setExpiryDays(5);
program.setExpiryDays(expiryDays);
program.setExpiryPeriodType(new DailyPeriodType());
return program;
}
Expand Down Expand Up @@ -280,4 +298,8 @@ private Instant now() {
private Instant sevenDaysAgo() {
return LocalDateTime.now().minus(7, ChronoUnit.DAYS).toInstant(ZoneOffset.UTC);
}

private Instant sevenDaysLater() {
return LocalDateTime.now().plusDays(7).toInstant(ZoneOffset.UTC);
}
}

0 comments on commit e5806d5

Please sign in to comment.