Skip to content

Commit

Permalink
When ERROR_ON_FAILURE, set new rule status ERROR for rule that fails. (
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavs authored and Clayton7510 committed Jul 9, 2019
1 parent 253738f commit e2946a7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ public List<Object> getActions() {

@Override
public boolean invoke(NameValueReferableMap facts) {
boolean isPassing = _rule.invoke(facts);
_auditor.updateRuleStatus(this, isPassing ? RuleStatus.EXECUTED : RuleStatus.SKIPPED);
boolean isPassing;
try {
isPassing = _rule.invoke(facts);
_auditor.updateRuleStatus(this, isPassing ? RuleStatus.EXECUTED : RuleStatus.SKIPPED);
} catch (RuleException ex) {
_auditor.updateRuleStatus(this, RuleStatus.ERROR);
throw ex;
}
return isPassing;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public enum RuleStatus {
PENDING,
SKIPPED,
EXECUTED,
NONE
NONE,
ERROR
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,20 @@ public void auditableRulesDelegateResultToTheirRule() {
Mockito.verify(rule, Mockito.times(1)).setResult(result);
Mockito.verify(rule, Mockito.times(1)).getResult();
}

@Test(expected = RuleException.class)
@SuppressWarnings("unchecked")
public void auditableRulesWithErrorOnFailureUpdateAuditorToErrorWhenInvokeFails() {
Rule<String, String> rule = new GoldenRule(String.class, RuleChainActionType.ERROR_ON_FAILURE);
rule.setCondition(facts -> facts.getValue("some fact").equals("nothing"));

AuditableRule auditableRule = new AuditableRule<String, String>(rule, "Simple rule");
Auditor auditor = Mockito.mock(Auditor.class);

auditableRule.setAuditor(auditor);

auditableRule.invoke(new FactMap());

Mockito.verify(auditor, Mockito.times(1)).updateRuleStatus(auditableRule, RuleStatus.ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void rulesAreStillExecutedWithNullFacts() {
*/
@Test
@SuppressWarnings("unchecked")
public void errorOnFailureRulesResultInPendingStatus() {
public void errorOnFailureRulesResultInErrorStatus() {
Rule<Object, Object> rule1 = new GoldenRule(Object.class);
rule1.addAction((facts, result)
-> result.setValue("Rule was triggered with status=" + facts.get("status")
Expand All @@ -79,8 +79,11 @@ public void errorOnFailureRulesResultInPendingStatus() {
Rule<Object, Object> rule2 = new GoldenRule(Object.class, RuleChainActionType.ERROR_ON_FAILURE);
rule2.setCondition(stuff -> stuff.getValue("invalid").equals("something"));

Rule<Object, Object> rule3 = new GoldenRule(Object.class, RuleChainActionType.ERROR_ON_FAILURE);

AuditableRule auditableRule1 = new AuditableRule(rule1, "SimpleRule");
AuditableRule auditableRule2 = new AuditableRule(rule2, "ErrorRule");
AuditableRule auditableRule3 = new AuditableRule(rule3, "AfterErrorRule");

FactMap facts = new FactMap();
facts.setValue("status", 1);
Expand All @@ -91,12 +94,14 @@ public void errorOnFailureRulesResultInPendingStatus() {
RuleBookAuditor auditor = new RuleBookAuditor(ruleBook);
auditor.addRule(auditableRule1);
auditor.addRule(auditableRule2);
auditor.addRule(auditableRule3);

try {
ruleBook.run(facts);
} catch (Exception e) {
Assert.assertEquals(RuleStatus.EXECUTED, auditor.getRuleStatus("SimpleRule"));
Assert.assertEquals(RuleStatus.PENDING, auditor.getRuleStatus("ErrorRule"));
Assert.assertEquals(RuleStatus.ERROR, auditor.getRuleStatus("ErrorRule"));
Assert.assertEquals(RuleStatus.PENDING, auditor.getRuleStatus("AfterErrorRule"));
Assert.assertTrue(e instanceof RuleException);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ public void errorOnConditionFailureRulesThrowErrorsInRuleBookRunners() {
Assert.assertTrue(err.getCause() instanceof Exception);
Assert.assertEquals("Sumthin' Broke!", err.getCause().getMessage());
Assert.assertEquals(ruleBook.getRuleStatus("SampleRule"), RuleStatus.EXECUTED);
Assert.assertEquals(ruleBook.getRuleStatus("ErrorRule"), RuleStatus.PENDING);
Assert.assertEquals(ruleBook.getRuleStatus("ErrorRule"), RuleStatus.ERROR);
Assert.assertEquals(ruleBook.getRuleStatus("AfterErrorRule"), RuleStatus.PENDING);
return;
}
Assert.fail();
Expand All @@ -250,7 +251,7 @@ public void errorOnActionFailureRulesThrowErrorsInRuleBookRunners() {
} catch (RuleException err) {
Assert.assertTrue(err.getCause() instanceof CustomException);
Assert.assertEquals("Sumthin' Broke!", err.getCause().getMessage());
Assert.assertEquals(ruleBook.getRuleStatus("ErrorRule"), RuleStatus.PENDING);
Assert.assertEquals(ruleBook.getRuleStatus("ErrorRule"), RuleStatus.ERROR);
return;
}
Assert.fail();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.deliveredtechnologies.rulebook.model.runner.test.rulebooks.error.condition;

import static com.deliveredtechnologies.rulebook.model.RuleChainActionType.ERROR_ON_FAILURE;

import com.deliveredtechnologies.rulebook.annotation.Rule;
import com.deliveredtechnologies.rulebook.annotation.Then;
import com.deliveredtechnologies.rulebook.annotation.When;

/**
* Created by harshavs on 20/6/19.
*/
@Rule(order = 3, ruleChainAction = ERROR_ON_FAILURE)
public class AfterErrorRule {
@When
public boolean when() {
return true;
}

@Then
public void then() {}
}

0 comments on commit e2946a7

Please sign in to comment.