Skip to content

Commit

Permalink
fix: (2.40)Schedule notification in capture app while using system va…
Browse files Browse the repository at this point in the history
…riable (#19820)
  • Loading branch information
zubaira authored Feb 3, 2025
1 parent 9cec66c commit bd28c22
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private TrackerRuleEngineSideEffect toTrackerScheduleMessageSideEffect(RuleEffec

return TrackerScheduleMessageSideEffect.builder()
.notification(ruleActionScheduleMessage.notification())
.data(ruleActionScheduleMessage.data())
.data(ruleEffect.data())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand All @@ -54,6 +55,7 @@
import org.hisp.dhis.tracker.report.TrackerTypeReport;
import org.hisp.dhis.tracker.report.ValidationReport;
import org.hisp.dhis.tracker.sideeffect.TrackerRuleEngineSideEffect;
import org.hisp.dhis.tracker.sideeffect.TrackerScheduleMessageSideEffect;
import org.hisp.dhis.tracker.sideeffect.TrackerSendMessageSideEffect;
import org.hisp.dhis.tracker.validation.ValidationCode;
import org.hisp.dhis.util.DateUtils;
Expand Down Expand Up @@ -307,7 +309,7 @@ public static void assertHasNoNotificationSideEffects(ImportReport report) {
"Unexpected notification side effect (TrackerSendMessageSideEffect) found.");
}

public static void assertHasNotificationSideEffects(ImportReport report) {
public static void assertHasSendNotificationSideEffects(ImportReport report) {
assertNotNull(report, "The ImportReport should not be null.");

TrackerTypeReport typeReport =
Expand All @@ -330,6 +332,36 @@ public static void assertHasNotificationSideEffects(ImportReport report) {
"Expected notification side effect (TrackerSendMessageSideEffect) but none were found.");
}

public static void assertHasScheduleNotificationForCurrentDate(ImportReport report) {
assertNotNull(report, "The ImportReport should not be null.");

TrackerTypeReport typeReport =
report.getPersistenceReport().getTypeReportMap().get(TrackerType.EVENT);
assertNotNull(typeReport, "The TrackerTypeReport for EVENT should not be null.");
assertFalse(
typeReport.getSideEffectDataBundles().isEmpty(),
"Expected side effect data bundles but none were found.");

Optional<TrackerScheduleMessageSideEffect> optionalSideEffect =
typeReport.getSideEffectDataBundles().stream()
.flatMap(bundle -> bundle.getEventRuleEffects().values().stream())
.flatMap(List::stream)
.filter(TrackerScheduleMessageSideEffect.class::isInstance)
.map(TrackerScheduleMessageSideEffect.class::cast)
.findFirst();

assertTrue(
optionalSideEffect.isPresent(),
"Expected notification side effect (TrackerScheduleMessageSideEffect) but none were found.");

TrackerScheduleMessageSideEffect sideEffect = optionalSideEffect.get();

// Assuming sideEffect.getData() returns a date string
String dateString = sideEffect.getData();
assertNotNull(dateString, "The scheduled date string should not be null.");
assertTrue(DateUtils.dateIsValid(dateString));
}

public static void assertNoErrors(ImportReport report) {
assertNotNull(report);
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
import static org.hisp.dhis.programrule.ProgramRuleActionType.SHOWWARNING;
import static org.hisp.dhis.tracker.Assertions.assertHasError;
import static org.hisp.dhis.tracker.Assertions.assertHasNoNotificationSideEffects;
import static org.hisp.dhis.tracker.Assertions.assertHasNotificationSideEffects;
import static org.hisp.dhis.tracker.Assertions.assertHasOnlyErrors;
import static org.hisp.dhis.tracker.Assertions.assertHasOnlyWarnings;
import static org.hisp.dhis.tracker.Assertions.assertHasScheduleNotificationForCurrentDate;
import static org.hisp.dhis.tracker.Assertions.assertHasSendNotificationSideEffects;
import static org.hisp.dhis.tracker.Assertions.assertNoErrors;
import static org.hisp.dhis.tracker.Assertions.assertNoErrorsAndNoWarnings;
import static org.hisp.dhis.tracker.validation.ValidationCode.E1300;
Expand Down Expand Up @@ -202,7 +203,29 @@ void shouldImportEventWithWarningsWhenPayloadEventDataIsPrioritized() throws IOE
importParams.setImportStrategy(TrackerImportStrategy.UPDATE);
report = trackerImportService.importTracker(importParams);

assertHasNotificationSideEffects(report);
assertHasSendNotificationSideEffects(report);
}

@Test
void shouldTestScheduleNotificationUsingSystemVariable() throws IOException {
storeScheduleNotificationProgramRule(
'P',
programWithRegistration,
programStageOnInsert,
TEMPLATE_UID,
"#{integer_prv_de6} > 10");

ImportReport report =
trackerImportService.importTracker(
fromJson("tracker/programrule/tei_enrollment_with_event_and_no_datavalues.json"));
assertHasNoNotificationSideEffects(report);

TrackerImportParams importParams =
fromJson("tracker/programrule/event_updated_datavalues.json");
importParams.setImportStrategy(TrackerImportStrategy.UPDATE);
report = trackerImportService.importTracker(importParams);

assertHasScheduleNotificationForCurrentDate(report);
}

@Test
Expand Down Expand Up @@ -496,6 +519,23 @@ private void storeNotificationProgramRule(
programRuleService.updateProgramRule(programRule);
}

private void storeScheduleNotificationProgramRule(
char uniqueCharacter,
Program program,
ProgramStage programStage,
String notification,
String condition) {
ProgramRule programRule = createProgramRule(uniqueCharacter, program, programStage, condition);
programRuleService.addProgramRule(programRule);
ProgramRuleAction sendMessageProgramRuleAction =
createProgramRuleAction(
programRule, ProgramRuleActionType.SCHEDULEMESSAGE, null, "V{current_date}");
sendMessageProgramRuleAction.setTemplateUid(notification);
programRuleActionService.addProgramRuleAction(sendMessageProgramRuleAction);
programRule.getProgramRuleActions().add(sendMessageProgramRuleAction);
programRuleService.updateProgramRule(programRule);
}

private ProgramRule createProgramRule(
char uniqueCharacter, Program program, ProgramStage programStage, String condition) {
ProgramRule programRule = createProgramRule(uniqueCharacter, program);
Expand Down

0 comments on commit bd28c22

Please sign in to comment.