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.40) (#19319)
  • Loading branch information
ameenhere authored Nov 29, 2024
1 parent ef91f33 commit 92089c0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public void validate(Reporter reporter, TrackerBundle bundle, Event event) {
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(
Reporter reporter, TrackerBundle bundle, Event event, Program program) {
User actingUser = bundle.getUser();

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

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

Expand All @@ -120,9 +120,16 @@ private void validatePeriodType(Reporter reporter, Event event, Program program)
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 @@ -93,7 +93,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 Down Expand Up @@ -230,27 +230,71 @@ void testEventIsNotValidWhenOccurredAtAndScheduledAtAreNotPresent() {
}

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

// when
validator.validate(reporter, bundle, event);

// then
assertHasError(reporter, event, E1047);
}

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);

validator.validate(reporter, bundle, event);

assertIsEmpty(reporter.getErrors());
}

@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);

validator.validate(reporter, bundle, event);

assertIsEmpty(reporter.getErrors());
}

@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);

validator.validate(reporter, bundle, event);

assertIsEmpty(reporter.getErrors());
}

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 @@ -279,4 +323,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 92089c0

Please sign in to comment.