Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: (2.40)Schedule notification in capture app while using system variable #19820

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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