Search in sources :

Example 6 with ProgramRuleVariable

use of org.hisp.dhis.programrule.ProgramRuleVariable in project dhis2-core by dhis2.

the class ProgramRuleEngineTest method setupProgramRuleEngine.

private void setupProgramRuleEngine() {
    programRuleA = createProgramRule('C', programA);
    programRuleA.setCondition(expressionA);
    programRuleService.addProgramRule(programRuleA);
    programRuleA2 = createProgramRule('Z', programA);
    programRuleA2.setCondition(calculatedDateExpression);
    programRuleService.addProgramRule(programRuleA2);
    programRuleC = createProgramRule('X', programA);
    programRuleC.setCondition(expressionC);
    programRuleService.addProgramRule(programRuleC);
    programRuleE = createProgramRule('E', programB);
    programRuleE.setCondition(expressionE);
    programRuleService.addProgramRule(programRuleE);
    programRuleS = createProgramRule('S', programS);
    programRuleS.setCondition(expressionS);
    programRuleService.addProgramRule(programRuleS);
    ProgramRuleVariable programRuleVariableEmail = createProgramRuleVariableWithTEA('E', programB, attributeEmail);
    programRuleVariableEmail.setName("attribute_email");
    programRuleVariableService.addProgramRuleVariable(programRuleVariableEmail);
    ProgramRuleVariable programRuleVariableA = createProgramRuleVariableWithDataElement('A', programA, dataElementA);
    programRuleVariableService.addProgramRuleVariable(programRuleVariableA);
    ProgramRuleVariable programRuleVariableB = createProgramRuleVariableWithDataElement('B', programA, dataElementB);
    programRuleVariableService.addProgramRuleVariable(programRuleVariableB);
    ProgramRuleVariable programRuleVariableDate = createProgramRuleVariableWithDataElement('X', programA, dataElementDate);
    programRuleVariableDate.setName("DOB");
    programRuleVariableService.addProgramRuleVariable(programRuleVariableDate);
    ProgramRuleVariable programRuleVariableAge = createProgramRuleVariableWithDataElement('K', programA, dataElementAge);
    programRuleVariableAge.setName("AGE");
    programRuleVariableService.addProgramRuleVariable(programRuleVariableAge);
    ProgramRuleVariable programRuleVariableD = createProgramRuleVariableWithTEA('D', programA, attributeB);
    programRuleVariableService.addProgramRuleVariable(programRuleVariableD);
    ProgramRuleVariable programRuleVariableS = createConstantProgramRuleVariable('S', programS);
    programRuleVariableS.setSourceType(ProgramRuleVariableSourceType.TEI_ATTRIBUTE);
    programRuleVariableS.setAttribute(attributeB);
    programRuleVariableS.setValueType(attributeB.getValueType());
    programRuleVariableService.addProgramRuleVariable(programRuleVariableS);
    ProgramRuleVariable programRuleVariableC = createConstantProgramRuleVariable('C', programA);
    programRuleVariableC.setSourceType(ProgramRuleVariableSourceType.TEI_ATTRIBUTE);
    programRuleVariableC.setAttribute(attributeA);
    programRuleVariableC.setValueType(attributeA.getValueType());
    programRuleVariableService.addProgramRuleVariable(programRuleVariableC);
}
Also used : ProgramRuleVariable(org.hisp.dhis.programrule.ProgramRuleVariable)

Example 7 with ProgramRuleVariable

use of org.hisp.dhis.programrule.ProgramRuleVariable in project dhis2-core by dhis2.

the class ProgramRuleVariableObjectBundleHookTest method shouldFailUpdateExistingDifferentUid.

@Test
void shouldFailUpdateExistingDifferentUid() {
    when(programRuleVariable.getProgram()).thenReturn(program);
    when(objectBundle.getImportMode()).thenReturn(ImportStrategy.CREATE_AND_UPDATE);
    ProgramRuleVariable existingProgramRuleVariable = new ProgramRuleVariable();
    existingProgramRuleVariable.setName("word");
    existingProgramRuleVariable.setUid("uid1");
    when(query.getResultList()).thenReturn(Collections.singletonList(existingProgramRuleVariable));
    when(programRuleVariable.getName()).thenReturn("word");
    when(programRuleVariable.getUid()).thenReturn("uid2");
    List<ErrorReport> errorReports = programRuleVariableObjectBundleHook.validate(programRuleVariable, objectBundle);
    assertEquals(1, errorReports.size());
    assertTrue(errorReports.stream().anyMatch(e -> e.getErrorCode().equals(E4051)));
}
Also used : ProgramRuleVariable(org.hisp.dhis.programrule.ProgramRuleVariable) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) BeforeEach(org.junit.jupiter.api.BeforeEach) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ErrorReport(org.hisp.dhis.feedback.ErrorReport) Mock(org.mockito.Mock) Session(org.hibernate.Session) ProgramRuleVariable(org.hisp.dhis.programrule.ProgramRuleVariable) Captor(org.mockito.Captor) Program(org.hisp.dhis.program.Program) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ImmutableList(com.google.common.collect.ImmutableList) Query(org.hibernate.query.Query) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) E4051(org.hisp.dhis.feedback.ErrorCode.E4051) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) E4052(org.hisp.dhis.feedback.ErrorCode.E4052) PROGRAM_RULE_VARIABLE_NAME_INVALID_KEYWORDS(org.hisp.dhis.dxf2.Constants.PROGRAM_RULE_VARIABLE_NAME_INVALID_KEYWORDS) SessionFactory(org.hibernate.SessionFactory) Mockito.when(org.mockito.Mockito.when) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 8 with ProgramRuleVariable

use of org.hisp.dhis.programrule.ProgramRuleVariable in project dhis2-core by dhis2.

the class ProgramRuleVariableObjectBundleHookTest method shouldFailValidationInvalidName.

@Test
void shouldFailValidationInvalidName() {
    when(programRuleVariable.getProgram()).thenReturn(program);
    when(objectBundle.getImportMode()).thenReturn(ImportStrategy.CREATE_AND_UPDATE);
    List<ErrorReport> errorReports;
    for (String invalidKeyWord : PROGRAM_RULE_VARIABLE_NAME_INVALID_KEYWORDS) {
        when(programRuleVariable.getName()).thenReturn("Word " + invalidKeyWord + " Word");
        errorReports = programRuleVariableObjectBundleHook.validate(programRuleVariable, objectBundle);
        assertEquals(1, errorReports.size());
        assertTrue(errorReports.stream().anyMatch(e -> e.getErrorCode().equals(E4052)));
        when(programRuleVariable.getName()).thenReturn(invalidKeyWord + " Word");
        errorReports = programRuleVariableObjectBundleHook.validate(programRuleVariable, objectBundle);
        assertEquals(1, errorReports.size());
        assertTrue(errorReports.stream().anyMatch(e -> e.getErrorCode().equals(E4052)));
        when(programRuleVariable.getName()).thenReturn("Word " + invalidKeyWord);
        errorReports = programRuleVariableObjectBundleHook.validate(programRuleVariable, objectBundle);
        assertEquals(1, errorReports.size());
        assertTrue(errorReports.stream().anyMatch(e -> e.getErrorCode().equals(E4052)));
    }
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) BeforeEach(org.junit.jupiter.api.BeforeEach) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ErrorReport(org.hisp.dhis.feedback.ErrorReport) Mock(org.mockito.Mock) Session(org.hibernate.Session) ProgramRuleVariable(org.hisp.dhis.programrule.ProgramRuleVariable) Captor(org.mockito.Captor) Program(org.hisp.dhis.program.Program) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ImmutableList(com.google.common.collect.ImmutableList) Query(org.hibernate.query.Query) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) E4051(org.hisp.dhis.feedback.ErrorCode.E4051) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) E4052(org.hisp.dhis.feedback.ErrorCode.E4052) PROGRAM_RULE_VARIABLE_NAME_INVALID_KEYWORDS(org.hisp.dhis.dxf2.Constants.PROGRAM_RULE_VARIABLE_NAME_INVALID_KEYWORDS) SessionFactory(org.hibernate.SessionFactory) Mockito.when(org.mockito.Mockito.when) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 9 with ProgramRuleVariable

use of org.hisp.dhis.programrule.ProgramRuleVariable in project dhis2-core by dhis2.

the class ProgramRuleVariableObjectBundleHookTest method shouldNotFailUpdateExistingMoreThanOneSameUid.

@Test
void shouldNotFailUpdateExistingMoreThanOneSameUid() {
    when(programRuleVariable.getProgram()).thenReturn(program);
    when(objectBundle.getImportMode()).thenReturn(ImportStrategy.CREATE_AND_UPDATE);
    ProgramRuleVariable existingProgramRuleVariable = new ProgramRuleVariable();
    existingProgramRuleVariable.setName("word");
    existingProgramRuleVariable.setUid("uid1");
    ProgramRuleVariable anotherExistingProgramRuleVariable = new ProgramRuleVariable();
    anotherExistingProgramRuleVariable.setName("word");
    anotherExistingProgramRuleVariable.setUid("uid2");
    when(query.getResultList()).thenReturn(ImmutableList.of(existingProgramRuleVariable, anotherExistingProgramRuleVariable));
    when(programRuleVariable.getName()).thenReturn("word");
    when(programRuleVariable.getUid()).thenReturn("uid1");
    List<ErrorReport> errorReports = programRuleVariableObjectBundleHook.validate(programRuleVariable, objectBundle);
    assertEquals(0, errorReports.size());
}
Also used : ProgramRuleVariable(org.hisp.dhis.programrule.ProgramRuleVariable) ErrorReport(org.hisp.dhis.feedback.ErrorReport) Test(org.junit.jupiter.api.Test)

Example 10 with ProgramRuleVariable

use of org.hisp.dhis.programrule.ProgramRuleVariable in project dhis2-core by dhis2.

the class ProgramRuleVariableObjectBundleHookTest method shouldNotFailUpdateExistingSameUid.

@Test
void shouldNotFailUpdateExistingSameUid() {
    when(programRuleVariable.getProgram()).thenReturn(program);
    when(objectBundle.getImportMode()).thenReturn(ImportStrategy.CREATE_AND_UPDATE);
    ProgramRuleVariable existingProgramRuleVariable = new ProgramRuleVariable();
    existingProgramRuleVariable.setName("word");
    existingProgramRuleVariable.setUid("uid1");
    when(query.getResultList()).thenReturn(Collections.singletonList(existingProgramRuleVariable));
    when(programRuleVariable.getName()).thenReturn("word");
    when(programRuleVariable.getUid()).thenReturn("uid1");
    List<ErrorReport> errorReports = programRuleVariableObjectBundleHook.validate(programRuleVariable, objectBundle);
    assertEquals(0, errorReports.size());
}
Also used : ProgramRuleVariable(org.hisp.dhis.programrule.ProgramRuleVariable) ErrorReport(org.hisp.dhis.feedback.ErrorReport) Test(org.junit.jupiter.api.Test)

Aggregations

ProgramRuleVariable (org.hisp.dhis.programrule.ProgramRuleVariable)14 Test (org.junit.jupiter.api.Test)6 Program (org.hisp.dhis.program.Program)5 ErrorReport (org.hisp.dhis.feedback.ErrorReport)4 List (java.util.List)3 Session (org.hibernate.Session)3 ObjectBundle (org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle)3 ImmutableList (com.google.common.collect.ImmutableList)2 Collections (java.util.Collections)2 SessionFactory (org.hibernate.SessionFactory)2 Query (org.hibernate.query.Query)2 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)2 PROGRAM_RULE_VARIABLE_NAME_INVALID_KEYWORDS (org.hisp.dhis.dxf2.Constants.PROGRAM_RULE_VARIABLE_NAME_INVALID_KEYWORDS)2 E4051 (org.hisp.dhis.feedback.ErrorCode.E4051)2 E4052 (org.hisp.dhis.feedback.ErrorCode.E4052)2 ImportStrategy (org.hisp.dhis.importexport.ImportStrategy)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2