Search in sources :

Example 1 with RuleActionSendMessage

use of org.hisp.dhis.rules.models.RuleActionSendMessage in project dhis2-core by dhis2.

the class NotificationRuleActionImplementerTest method setUpInstances.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private void setUpInstances() {
    template = new ProgramNotificationTemplate();
    template.setUid(NOTIFICATION_UID);
    ruleActionSendMessage = new RuleActionSendMessage() {

        @Nonnull
        @Override
        public String notification() {
            return NOTIFICATION_UID;
        }

        @Nonnull
        @Override
        public String data() {
            return null;
        }
    };
    ruleEffectWithActionSendMessage = RuleEffect.create("", ruleActionSendMessage);
    setMandatoryFieldFalse = RuleActionSetMandatoryField.create(MANDATORY_FIELD);
    OrganisationUnit organisationUnitA = createOrganisationUnit('A');
    Program programA = createProgram('A', new HashSet<>(), organisationUnitA);
    programRuleA = createProgramRule('R', programA);
    programRuleA.setProgram(programA);
    programInstance = new ProgramInstance();
    programInstance.setProgram(programA);
    programInstance.setAutoFields();
    ProgramStage programStageA = createProgramStage('S', programA);
    programA.getProgramStages().add(programStageA);
    programStageInstance = new ProgramStageInstance();
    programStageInstance.setProgramStage(programStageA);
    programStageInstance.setProgramInstance(programInstance);
    programStageInstance.setAutoFields();
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) RuleActionSendMessage(org.hisp.dhis.rules.models.RuleActionSendMessage) Nonnull(javax.annotation.Nonnull) ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Mockito.anyString(org.mockito.Mockito.anyString) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 2 with RuleActionSendMessage

use of org.hisp.dhis.rules.models.RuleActionSendMessage in project dhis2-core by dhis2.

the class NotificationRuleActionImplementer method getNotificationTemplate.

@Transactional(readOnly = true)
public ProgramNotificationTemplate getNotificationTemplate(RuleAction action) {
    String uid = "";
    if (action instanceof RuleActionSendMessage) {
        RuleActionSendMessage sendMessage = (RuleActionSendMessage) action;
        uid = sendMessage.notification();
    } else if (action instanceof RuleActionScheduleMessage) {
        RuleActionScheduleMessage scheduleMessage = (RuleActionScheduleMessage) action;
        uid = scheduleMessage.notification();
    }
    return programNotificationTemplateService.getByUid(uid);
}
Also used : RuleActionSendMessage(org.hisp.dhis.rules.models.RuleActionSendMessage) RuleActionScheduleMessage(org.hisp.dhis.rules.models.RuleActionScheduleMessage) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with RuleActionSendMessage

use of org.hisp.dhis.rules.models.RuleActionSendMessage 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);
}
Also used : RuleEffect(org.hisp.dhis.rules.models.RuleEffect) RuleActionSendMessage(org.hisp.dhis.rules.models.RuleActionSendMessage) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramRuleAction(org.hisp.dhis.programrule.ProgramRuleAction) RuleAction(org.hisp.dhis.rules.models.RuleAction) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) DhisConvenienceTest(org.hisp.dhis.DhisConvenienceTest)

Aggregations

RuleActionSendMessage (org.hisp.dhis.rules.models.RuleActionSendMessage)3 ProgramInstance (org.hisp.dhis.program.ProgramInstance)2 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)1 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)1 Program (org.hisp.dhis.program.Program)1 ProgramStage (org.hisp.dhis.program.ProgramStage)1 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)1 ProgramNotificationTemplate (org.hisp.dhis.program.notification.ProgramNotificationTemplate)1 ProgramRuleAction (org.hisp.dhis.programrule.ProgramRuleAction)1 RuleAction (org.hisp.dhis.rules.models.RuleAction)1 RuleActionScheduleMessage (org.hisp.dhis.rules.models.RuleActionScheduleMessage)1 RuleEffect (org.hisp.dhis.rules.models.RuleEffect)1 Test (org.junit.jupiter.api.Test)1 Mockito.anyString (org.mockito.Mockito.anyString)1 Transactional (org.springframework.transaction.annotation.Transactional)1