Search in sources :

Example 6 with RuleValidationResult

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

the class ProgramRuleEngineDescriptionTest method testDataFieldExpressionDescription.

@Test
void testDataFieldExpressionDescription() {
    RuleValidationResult result = programRuleEngineNew.getDataExpressionDescription("1 + 2 +", program);
    assertNotNull(result);
    assertFalse(result.isValid());
    assertThat(result.getException(), instanceOf(IllegalStateException.class));
    result = programRuleEngineNew.getDataExpressionDescription("d2:daysBetween(V{completed_date},V{current_date}) > 0 )", program);
    assertNotNull(result);
    assertFalse(result.isValid());
    assertThat(result.getException(), instanceOf(IllegalStateException.class));
    result = programRuleEngineNew.getDataExpressionDescription(conditionWithD2DaysBetween, program);
    assertNotNull(result);
    assertTrue(result.isValid());
    assertEquals("d2:daysBetween(Completed date,Current date) > 0", result.getDescription());
    result = programRuleEngineNew.getDataExpressionDescription(programRuleNumericDE.getCondition(), program);
    assertNotNull(result);
    assertTrue(result.isValid());
    assertEquals("DataElementE == 14", result.getDescription());
    result = programRuleEngineNew.getDataExpressionDescription(programRuleNumericAtt.getCondition(), program);
    assertNotNull(result);
    assertTrue(result.isValid());
    assertEquals("AttributeB == 12 || Current date", result.getDescription());
    result = programRuleEngineNew.getDataExpressionDescription("'2020-12-12'", program);
    assertNotNull(result);
    assertTrue(result.isValid());
    assertEquals("'2020-12-12'", result.getDescription());
    result = programRuleEngineNew.getDataExpressionDescription("1 + 1", program);
    assertNotNull(result);
    assertTrue(result.isValid());
    assertEquals("1 + 1", result.getDescription());
    result = programRuleEngineNew.getDataExpressionDescription("'sample text'", program);
    assertNotNull(result);
    assertTrue(result.isValid());
    assertEquals("'sample text'", result.getDescription());
}
Also used : RuleValidationResult(org.hisp.dhis.rules.models.RuleValidationResult) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 7 with RuleValidationResult

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

the class ProgramRuleEngineDescriptionTest method testProgramRuleWithD2DaysBetween.

@Test
void testProgramRuleWithD2DaysBetween() {
    RuleValidationResult result = validateRuleCondition(conditionWithD2DaysBetween, program);
    assertNotNull(result);
    assertEquals("d2:daysBetween(Completed date,Current date) > 0", result.getDescription());
    assertTrue(result.isValid());
}
Also used : RuleValidationResult(org.hisp.dhis.rules.models.RuleValidationResult) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 8 with RuleValidationResult

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

the class ProgramRuleEngineDescriptionTest method testProgramRuleWithCalculatedValueRuleVariable.

@Test
void testProgramRuleWithCalculatedValueRuleVariable() {
    RuleValidationResult result = validateRuleCondition("#{prv1}+#{prv2}>0", program);
    assertNotNull(result);
    assertEquals("prv1+prv2>0", result.getDescription());
    assertTrue(result.isValid());
}
Also used : RuleValidationResult(org.hisp.dhis.rules.models.RuleValidationResult) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 9 with RuleValidationResult

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

the class ProgramRuleEngineDescriptionTest method testIncorrectRuleWithLiterals.

@Test
void testIncorrectRuleWithLiterals() {
    RuleValidationResult result = validateRuleCondition("1 > 2 +", program);
    assertNotNull(result);
    assertFalse(result.isValid());
    assertThat(result.getException(), instanceOf(IllegalStateException.class));
}
Also used : RuleValidationResult(org.hisp.dhis.rules.models.RuleValidationResult) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 10 with RuleValidationResult

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

the class ProgramRuleController method validateCondition.

@PostMapping(value = "/condition/description", produces = APPLICATION_JSON_VALUE)
@ResponseBody
public WebMessage validateCondition(@RequestBody String condition, @RequestParam String programId) {
    I18n i18n = i18nManager.getI18n();
    RuleValidationResult result = programRuleEngineService.getDescription(condition, programId);
    if (result.isValid()) {
        return new DescriptiveWebMessage(Status.OK, HttpStatus.OK).setDescription(result.getDescription()).setMessage(i18n.getString(ProgramIndicator.VALID));
    }
    String description = null;
    if (result.getErrorMessage() != null) {
        description = result.getErrorMessage();
    } else if (result.getException() != null) {
        description = result.getException().getMessage();
    }
    return new DescriptiveWebMessage(Status.ERROR, HttpStatus.OK).setDescription(description).setMessage(i18n.getString(ProgramIndicator.EXPRESSION_NOT_VALID));
}
Also used : DescriptiveWebMessage(org.hisp.dhis.dxf2.webmessage.DescriptiveWebMessage) RuleValidationResult(org.hisp.dhis.rules.models.RuleValidationResult) I18n(org.hisp.dhis.i18n.I18n) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

RuleValidationResult (org.hisp.dhis.rules.models.RuleValidationResult)18 Test (org.junit.jupiter.api.Test)16 DhisSpringTest (org.hisp.dhis.DhisSpringTest)15 DescriptiveWebMessage (org.hisp.dhis.dxf2.webmessage.DescriptiveWebMessage)2 I18n (org.hisp.dhis.i18n.I18n)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)1