use of org.hisp.dhis.rules.models.RuleEffect in project dhis2-core by dhis2.
the class ProgramRuleEngineServiceTest method testWithImplementableActionExist_programInstance.
@Test
void testWithImplementableActionExist_programInstance() {
doAnswer(invocationOnMock -> {
ruleEffects.add((RuleEffect) invocationOnMock.getArguments()[0]);
return ruleEffects;
}).when(ruleActionSendMessage).implement(any(), any(ProgramInstance.class));
List<RuleEffect> effects = new ArrayList<>();
effects.add(RuleEffect.create("", RuleActionSendMessage.create(NOTIFICATION_UID, DATA)));
when(programInstanceService.getProgramInstance(anyLong())).thenReturn(programInstance);
when(programRuleEngine.evaluate(any(), any())).thenReturn(effects);
setProgramRuleActionType_SendMessage();
ArgumentCaptor<ProgramInstance> argumentCaptor = ArgumentCaptor.forClass(ProgramInstance.class);
List<RuleEffect> ruleEffects = service.evaluateEnrollmentAndRunEffects(programInstance.getId());
assertEquals(1, ruleEffects.size());
RuleAction action = ruleEffects.get(0).ruleAction();
if (action instanceof RuleActionSendMessage) {
RuleActionSendMessage ruleActionSendMessage = (RuleActionSendMessage) action;
assertEquals(NOTIFICATION_UID, ruleActionSendMessage.notification());
}
verify(programRuleEngine, times(1)).evaluate(argumentCaptor.capture(), any());
assertEquals(programInstance, argumentCaptor.getValue());
verify(ruleActionSendMessage).accept(action);
verify(ruleActionSendMessage).implement(any(RuleEffect.class), argumentCaptor.capture());
assertEquals(1, this.ruleEffects.size());
assertTrue(this.ruleEffects.get(0).ruleAction() instanceof RuleActionSendMessage);
}
use of org.hisp.dhis.rules.models.RuleEffect in project dhis2-core by dhis2.
the class TrackerRuleEngineThread method call.
@Override
public void call() {
if (sideEffectDataBundle == null) {
return;
}
Map<String, List<RuleEffect>> enrollmentRuleEffects = trackerSideEffectConverterService.toRuleEffects(sideEffectDataBundle.getEnrollmentRuleEffects());
Map<String, List<RuleEffect>> eventRuleEffects = trackerSideEffectConverterService.toRuleEffects(sideEffectDataBundle.getEventRuleEffects());
for (RuleActionImplementer ruleActionImplementer : ruleActionImplementers) {
for (Map.Entry<String, List<RuleEffect>> entry : enrollmentRuleEffects.entrySet()) {
ProgramInstance pi = sideEffectDataBundle.getProgramInstance();
pi.setProgram(sideEffectDataBundle.getProgram());
entry.getValue().stream().filter(effect -> ruleActionImplementer.accept(effect.ruleAction())).forEach(effect -> ruleActionImplementer.implement(effect, pi));
}
for (Map.Entry<String, List<RuleEffect>> entry : eventRuleEffects.entrySet()) {
ProgramStageInstance psi = sideEffectDataBundle.getProgramStageInstance();
psi.getProgramStage().setProgram(sideEffectDataBundle.getProgram());
entry.getValue().stream().filter(effect -> ruleActionImplementer.accept(effect.ruleAction())).forEach(effect -> ruleActionImplementer.implement(effect, psi));
}
}
notifier.notify(sideEffectDataBundle.getJobConfiguration(), "Tracker Rule-engine side effects completed");
}
Aggregations