Skip to content

Commit

Permalink
Bug/define rules from decorated auditor aren't audited (deliveredtech…
Browse files Browse the repository at this point in the history
…nologies#181)

* made defineRules rules in decorated RuleBookAuditor audited

* added attribution for mikelear

* updated tests to test decorating a RuleBookAuditor with another RuleBookAuditor
  • Loading branch information
Clayton7510 authored Feb 3, 2020
1 parent 7838ecf commit 3101432
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ project("rulebook-core") {
role 'raised bug for v0.11'
}
}
contributor {
name 'mikelear'
url 'https://github.com/mikelear'
roles {
role 'raised RuleBookAuditor bug where decorated auditors with defineRules aren not audited and proposed fix'
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Auditors are used for auditing rules. They maintain a record of each Rule and their state.
*/
public abstract class Auditor {
private Map<String, Map<Long, RuleStatus>> _auditMap = new HashMap<>();
protected Map<String, Map<Long, RuleStatus>> _auditMap = new HashMap<>();
private ReentrantReadWriteLock _lock = new ReentrantReadWriteLock();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
import java.util.Optional;

/**
* Created by clong on 9/3/17.
* Decorates RuleBook with auditing functionality.
*/
public class RuleBookAuditor<T> extends Auditor implements RuleBook<T> {
private RuleBook<T> _ruleBook;

/**
* Costructor - Decorates the supplied RuleBook; if a RuleBookAuditor is supplied, it assumes its audited rules.
* @param ruleBook RuleBook to decorate with auditing functionality
*/
public RuleBookAuditor(RuleBook<T> ruleBook) {
_ruleBook = ruleBook;
if ( _ruleBook instanceof RuleBookAuditor ) {
RuleBookAuditor auditableRuleBook = (RuleBookAuditor) _ruleBook;
_auditMap = auditableRuleBook._auditMap;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ public void ruleBookAuditorCanAuditRules() {
Assert.assertEquals(auditor.getRuleStatus("Rule3"), RuleStatus.EXECUTED);
}

@Test
public void ruleBookAuditorAuditsRulesFromDecoratedRuleBookAuditor() {
RuleBook rulebook = new RuleBookAuditor(new CoRRuleBook()) {
@Override
public void defineRules() {
addRule(RuleBuilder.create().withName("Rule1")
.when(facts -> true)
.then(facts -> {
}).build());
addRule(RuleBuilder.create().withName("Rule2")
.when(facts -> false)
.then(facts -> {
}).build());
addRule(RuleBuilder.create().withName("Rule3")
.when(facts -> true)
.then(facts -> {
}).build());
}
};

RuleBookAuditor auditor = new RuleBookAuditor(rulebook);

rulebook.run(new FactMap());

Assert.assertEquals(auditor.getRuleStatus("Rule1"), RuleStatus.EXECUTED);
Assert.assertEquals(auditor.getRuleStatus("Rule2"), RuleStatus.SKIPPED);
Assert.assertEquals(auditor.getRuleStatus("Rule3"), RuleStatus.EXECUTED);
}

/**
* Test to ensure that rules invoked using null facts don't error just because that facts are null.
*/
Expand Down

0 comments on commit 3101432

Please sign in to comment.