Skip to content

Commit

Permalink
Merge branch 'hotfix/Issue107' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Clayton7510 authored Dec 11, 2017
2 parents 238fe63 + f2ad77b commit 209f089
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public boolean invoke(NameValueReferableMap facts) {
//only use facts of the specified type
NameValueReferableMap<T> typeFilteredFacts =
new FactMap<T>((Map<String, NameValueReferable<T>>) facts.values().stream()
.filter((Object fact) -> _factType.isAssignableFrom(((NameValueReferable) fact).getValue().getClass()))
.filter((Object fact) -> ((NameValueReferable)fact).getValue() == null
|| _factType.isAssignableFrom(((NameValueReferable) fact).getValue().getClass()))
.collect(Collectors.toMap(fact ->
((NameValueReferable<Object>) fact).getName(), fact -> (NameValueReferable<T>) fact)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.deliveredtechnologies.rulebook.RuleState;
import com.deliveredtechnologies.rulebook.Result;

import com.deliveredtechnologies.rulebook.model.rulechain.cor.CoRRuleBook;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.deliveredtechnologies.rulebook.model;

import com.deliveredtechnologies.rulebook.FactMap;
import com.deliveredtechnologies.rulebook.NameValueReferableMap;
import com.deliveredtechnologies.rulebook.Result;
import com.deliveredtechnologies.rulebook.lang.RuleBookBuilder;
import com.deliveredtechnologies.rulebook.lang.RuleBuilder;
import com.deliveredtechnologies.rulebook.model.rulechain.cor.CoRRuleBook;
import org.junit.Assert;
import org.junit.Test;

import java.util.function.BiConsumer;

/**
* Tests for {@link RuleBookAuditor}.
*/
Expand All @@ -33,26 +37,26 @@ public void ruleBookAuditorCanAuditRules() {
Assert.assertEquals(auditor.getRuleStatus("Rule3"), RuleStatus.EXECUTED);
}

@Test
public void ruleBookAuditorCanHaveDefaultResult() {
RuleBook rulebook = RuleBookBuilder.create().withResultType(String.class).withDefaultResult("foo").asAuditor()
.addRule(rule -> rule.withName("Rule1").withFactType(String.class)
.when(facts -> true)
.then(facts -> { } ))
.addRule(rule -> rule.withName("Rule2").withNoSpecifiedFactType()
.when(facts -> false)
.then(facts -> { } ))
.addRule(rule -> rule.withName("Rule3").withFactType(String.class)
.when(facts -> true)
.then(facts -> { } ).build())
.build();
@SuppressWarnings("unchecked")
public void rulesAreStillExecutedWithNullFacts() {
Rule rule = new GoldenRule(Object.class);
BiConsumer<NameValueReferableMap, Result> action = (facts, result)
-> result.setValue("Rule was triggered with status=" + facts.get("status")
+ " and object=" + facts.get("object"));
rule.addAction(action);

rulebook.run(new FactMap());
Auditor auditor = (Auditor)rulebook;
AuditableRule auditableRule = new AuditableRule(rule, "SimpleRule");

Assert.assertEquals(auditor.getRuleStatus("Rule1"), RuleStatus.EXECUTED);
Assert.assertEquals(auditor.getRuleStatus("Rule2"), RuleStatus.SKIPPED);
Assert.assertEquals(auditor.getRuleStatus("Rule3"), RuleStatus.EXECUTED);
Assert.assertEquals("foo", ((Result<String>)rulebook.getResult().get()).getValue());
FactMap facts = new FactMap();
facts.setValue("status", 1);
facts.setValue("object", null);

RuleBook ruleBook = new CoRRuleBook();

RuleBookAuditor auditor = new RuleBookAuditor(ruleBook);
auditor.addRule(auditableRule);
ruleBook.run(facts);

Assert.assertEquals(RuleStatus.EXECUTED, auditor.getRuleStatus("SimpleRule"));
}
}

0 comments on commit 209f089

Please sign in to comment.