Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this commit fix bug if i try to put null value for one fact #137

Merged
merged 3 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private List<Object> getActualParameters(Method method, Facts facts) {
if (annotations.length == 1) {
String factName = ((Fact) (annotations[0])).value(); //validated upfront.
Object fact = facts.get(factName);
if (fact == null) {
if (fact == null && !facts.asMap().containsKey(factName)) {
throw new NoSuchFactException(format("No fact named '%s' found in known facts: \n%s", factName, facts), factName);
}
actualParameters.add(fact);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.jeasy.rules.core;

import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.junit.Assert;
import org.junit.Test;

import java.util.Map;

/**
* Null value in facts must be accepted, this is not same thing that fact missing
*/
public class NullFactAnnotationParameterTest extends AbstractTest {

@Test
public void testNullFact() {
Rules rules = new Rules();
rules.register(new AnnotatedParametersRule());

Facts facts = new Facts();
facts.put("fact1", new Object());
facts.put("fact2", null);

Map<org.jeasy.rules.api.Rule, Boolean> results = rulesEngine.check(rules, facts);

for (boolean b : results.values()) {
Assert.assertTrue(b);
}
}

@Test
public void testMissingFact() {
Rules rules = new Rules();
rules.register(new AnnotatedParametersRule());

Facts facts = new Facts();
facts.put("fact1", new Object());

Map<org.jeasy.rules.api.Rule, Boolean> results = rulesEngine.check(rules, facts);

for (boolean b : results.values()) {
Assert.assertFalse(b);
}
}

@Rule
public class AnnotatedParametersRule {

@Condition
public boolean when(@Fact("fact1") Object fact1, @Fact("fact2") Object fact2) {
return fact1 != null && fact2 == null;
}

@Action
public void then(@Fact("fact1") Object fact1, @Fact("fact2") Object fact2) {
}

}
}