From 239928edf08010361a927e73502fd4f3cb303609 Mon Sep 17 00:00:00 2001 From: Ameen Mohamed Date: Wed, 27 Nov 2024 15:53:00 +0100 Subject: [PATCH 1/3] fix: [DHIS2-15272] Fix program expiryDays check with periodType for events (2.41) --- .../validator/event/DateValidator.java | 22 ++-- .../validator/event/DateValidatorTest.java | 100 ++++++++++++------ 2 files changed, 83 insertions(+), 39 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java index dcb83178696f..52347c0fd660 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java @@ -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(); @@ -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); @@ -120,9 +120,17 @@ private void validatePeriodType(Reporter reporter, Event event, Program program) return; } - Period period = periodType.createPeriod(new Date()); - - if (referenceDate.isBefore(period.getStartDate().toInstant())) { + Period eventPeriod = periodType.createPeriod(Date.from(referenceDate)); + + if (eventPeriod + .getEndDate() + .toInstant() // This will be 00:00 time of the period end date. + .plus( + ofDays( + program.getExpiryDays() + + 1)) // An extra 1 day has to be added to account for the full 24 hours of the + // final expiring day. + .isBefore(Instant.now())) { reporter.addError(event, E1047, event); } } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidatorTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidatorTest.java index df85ae8b3bb1..9d097f5a77b5 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidatorTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidatorTest.java @@ -91,8 +91,6 @@ public void setUp() { bundle = TrackerBundle.builder().user(user).preheat(preheat).build(); - when(preheat.getProgram(MetadataIdentifier.ofUid(PROGRAM_WITH_REGISTRATION_ID))) - .thenReturn(getProgramWithRegistration()); when(preheat.getProgram(MetadataIdentifier.ofUid(PROGRAM_WITHOUT_REGISTRATION_ID))) .thenReturn(getProgramWithoutRegistration()); @@ -102,7 +100,6 @@ public void setUp() { @Test void testEventIsValid() { - // given Event event = new Event(); event.setProgram(MetadataIdentifier.ofUid(PROGRAM_WITHOUT_REGISTRATION_ID)); event.setOccurredAt(now()); @@ -111,92 +108,86 @@ void testEventIsValid() { TrackerBundle bundle = TrackerBundle.builder().user(getEditExpiredUser()).preheat(preheat).build(); - // when validator.validate(reporter, bundle, event); - // then assertIsEmpty(reporter.getErrors()); } @Test void testEventIsNotValidWhenOccurredDateIsNotPresentAndProgramIsWithoutRegistration() { - // given + 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_WITHOUT_REGISTRATION_ID)); - // when validator.validate(reporter, bundle, event); - // then assertHasError(reporter, event, E1031); } @Test void testEventIsNotValidWhenOccurredDateIsNotPresentAndEventIsActive() { - // given + 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.setStatus(EventStatus.ACTIVE); - // when validator.validate(reporter, bundle, event); - // then assertHasError(reporter, event, E1031); } @Test void testEventIsNotValidWhenOccurredDateIsNotPresentAndEventIsCompleted() { - // given + 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.setStatus(EventStatus.COMPLETED); - // when validator.validate(reporter, bundle, event); - // then assertHasError(reporter, event, E1031); } @Test void testEventIsNotValidWhenScheduledDateIsNotPresentAndEventIsSchedule() { - // given + 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.setOccurredAt(Instant.now()); event.setStatus(EventStatus.SCHEDULE); - // when validator.validate(reporter, bundle, event); - // then assertHasError(reporter, event, E1050); } @Test void testEventIsNotValidWhenCompletedAtIsNotPresentAndEventIsCompleted() { - // given + 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.setOccurredAt(now()); event.setStatus(EventStatus.COMPLETED); - // when validator.validate(reporter, bundle, event); - // then assertHasError(reporter, event, E1042); } @Test void testEventIsNotValidWhenCompletedAtIsTooSoonAndEventIsCompleted() { - // given + 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)); @@ -204,16 +195,15 @@ void testEventIsNotValidWhenCompletedAtIsTooSoonAndEventIsCompleted() { event.setCompletedAt(sevenDaysAgo()); event.setStatus(EventStatus.COMPLETED); - // when validator.validate(reporter, bundle, event); - // then assertHasError(reporter, event, E1043); } @Test void testEventIsNotValidWhenOccurredAtAndScheduledAtAreNotPresent() { - // given + 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)); @@ -221,35 +211,77 @@ void testEventIsNotValidWhenOccurredAtAndScheduledAtAreNotPresent() { event.setScheduledAt(null); event.setStatus(EventStatus.SKIPPED); - // when validator.validate(reporter, bundle, event); - // then assertHasError(reporter, event, E1046); } @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; } @@ -278,4 +310,8 @@ private Instant now() { private Instant sevenDaysAgo() { return LocalDateTime.now().minusDays(7).toInstant(ZoneOffset.UTC); } + + private Instant sevenDaysLater() { + return LocalDateTime.now().plusDays(7).toInstant(ZoneOffset.UTC); + } } From a7287d31cc8dd74787989e1bf6826ae52c404907 Mon Sep 17 00:00:00 2001 From: Ameen Mohamed Date: Wed, 27 Nov 2024 15:56:40 +0100 Subject: [PATCH 2/3] Spotless fix --- .../imports/validation/validator/event/DateValidator.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java index 52347c0fd660..055267da59f9 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java @@ -128,8 +128,7 @@ private void validateExpiryPeriodType(Reporter reporter, Event event, Program pr .plus( ofDays( program.getExpiryDays() - + 1)) // An extra 1 day has to be added to account for the full 24 hours of the - // final expiring day. + + 1)) // Extra day added to account for final 24 hours of expiring day .isBefore(Instant.now())) { reporter.addError(event, E1047, event); } From a595120e109c89b19857aac9fb403612aee66cc0 Mon Sep 17 00:00:00 2001 From: Ameen Mohamed Date: Wed, 27 Nov 2024 16:06:28 +0100 Subject: [PATCH 3/3] Sonar fix --- .../imports/validation/validator/event/DateValidator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java index 055267da59f9..a6e0e08e19b0 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/event/DateValidator.java @@ -128,7 +128,7 @@ private void validateExpiryPeriodType(Reporter reporter, Event event, Program pr .plus( ofDays( program.getExpiryDays() - + 1)) // Extra day added to account for final 24 hours of expiring day + + 1L)) // Extra day added to account for final 24 hours of expiring day .isBefore(Instant.now())) { reporter.addError(event, E1047, event); }