Search in sources :

Example 1 with IRuleEvaluator

use of org.jaffa.rules.rulemeta.IRuleEvaluator in project jaffa-framework by jaffa-projects.

the class FieldInitializerTest method testConditionalInitialization.

/**
 * If a condition exists for the rule, it should be evaluated.
 */
@Test
public void testConditionalInitialization() throws FrameworkException, ApplicationExceptions {
    TestModel testModel = new TestModel();
    IRuleEvaluator evaluator = mock(IRuleEvaluator.class);
    when(evaluator.checkCondition(anyObject(), any(RuleMetaData.class))).thenReturn(false);
    target.setRuleEvaluator(evaluator);
    target.initialize(testModel);
    // Neither field should have been initialized.
    assertNull(testModel.getField1());
    assertNull(testModel.getField2());
}
Also used : IRuleEvaluator(org.jaffa.rules.rulemeta.IRuleEvaluator) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) TestModel(org.jaffa.rules.TestModel) Test(org.junit.Test)

Example 2 with IRuleEvaluator

use of org.jaffa.rules.rulemeta.IRuleEvaluator in project jaffa-framework by jaffa-projects.

the class RuleValidatorTest method testConditionalValidation.

/**
 * When a condition is specified on a rule, property validation should only be called if the condition is true.
 */
@Test
public void testConditionalValidation() throws FrameworkException, ApplicationExceptions, ApplicationException {
    TestValidator target = new TestValidator();
    IRuleEvaluator ruleEvaluator = mock(IRuleEvaluator.class);
    target.setRuleEvaluator(ruleEvaluator);
    Map<String, List<RuleMetaData>> ruleMap = new HashMap<>();
    List<RuleMetaData> rules = new ArrayList<>();
    RuleMetaData trueCondition = new RuleMetaData();
    rules.add(trueCondition);
    RuleMetaData falseCondition = new RuleMetaData();
    rules.add(falseCondition);
    ruleMap.put("field1", rules);
    target.setRuleMap(ruleMap);
    TestModel obj = new TestModel();
    obj.setField1("value");
    when(ruleEvaluator.checkCondition(obj, trueCondition)).thenReturn(true);
    target.validate(obj);
    assertEquals(1, target.validatedRules.size());
    assertEquals(trueCondition, target.validatedRules.get(0));
}
Also used : IRuleEvaluator(org.jaffa.rules.rulemeta.IRuleEvaluator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) TestModel(org.jaffa.rules.TestModel) Test(org.junit.Test)

Aggregations

TestModel (org.jaffa.rules.TestModel)2 RuleMetaData (org.jaffa.rules.meta.RuleMetaData)2 IRuleEvaluator (org.jaffa.rules.rulemeta.IRuleEvaluator)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1