forked from deliveredtechnologies/rulebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Spring context awareness support in rulebook-spring module (del…
…iveredtechnologies#134) * Added Spring context awareness support in rulebook-spring module * Fixed checkstyle warnings * Fixed errors. All unit tests pass now. * Skipping rule needs continue not break * SpringAwareRuleRunner - unit tests and javadocs * Test to increase code coverage
- Loading branch information
1 parent
e23cb40
commit 518a4c1
Showing
7 changed files
with
168 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ng/src/main/java/com/deliveredtechnologies/rulebook/spring/SpringAwareRuleBookRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.deliveredtechnologies.rulebook.spring; | ||
|
||
import com.deliveredtechnologies.rulebook.model.runner.RuleBookRunner; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
|
||
/** | ||
* Runs the POJO Rules that can be Spring aware beans in a specified package as a RuleBook. | ||
* | ||
* <p>POJO rule classes in the package may or may not have the @Component/@Service/etc. annotation. If they | ||
* do, the bean will be fetched and @Autowired variables will be populated. If not, an instance of the | ||
* POJO rule will be created and used. | ||
*/ | ||
public class SpringAwareRuleBookRunner extends RuleBookRunner implements ApplicationContextAware { | ||
|
||
private ApplicationContext _applicationContext; | ||
|
||
private static Logger LOGGER = LoggerFactory.getLogger(SpringAwareRuleBookRunner.class); | ||
|
||
public SpringAwareRuleBookRunner(String rulePackage) { | ||
super(rulePackage); | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
this._applicationContext = applicationContext; | ||
} | ||
|
||
@Override | ||
protected Object getRuleInstance(Class<?> rule) { | ||
if (_applicationContext == null) { | ||
throw new IllegalStateException("Cannot instantiate RuleBookRunner because " | ||
+ "Spring application context is not available."); | ||
} | ||
|
||
try { | ||
// Spring bean POJO rule found | ||
return _applicationContext.getBean(rule); | ||
} catch (BeansException e) { | ||
// POJO rule isn't a Spring bean | ||
return super.getRuleInstance(rule); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...rc/test/java/com/deliveredtechnologies/rulebook/spring/SpringAwareRuleBookRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.deliveredtechnologies.rulebook.spring; | ||
|
||
import com.deliveredtechnologies.rulebook.FactMap; | ||
import com.deliveredtechnologies.rulebook.NameValueReferableMap; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import static com.deliveredtechnologies.rulebook.spring.SpringTestService.EXPECTED_RESULT; | ||
|
||
@ContextConfiguration(classes = TestConfig.class) | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
public class SpringAwareRuleBookRunnerTest { | ||
@Autowired | ||
private SpringAwareRuleBookRunner _ruleBook; | ||
|
||
private NameValueReferableMap<String> _facts = new FactMap<>(); | ||
|
||
@Before | ||
public void setUp() { | ||
_facts.setValue("value1", "Value"); | ||
_facts.setValue("value2", "Value"); | ||
} | ||
|
||
@Test | ||
public void ruleBookShouldRunRulesInPackage() { | ||
_ruleBook.run(_facts); | ||
Assert.assertEquals(EXPECTED_RESULT, _facts.getValue("value2")); | ||
} | ||
|
||
@Test | ||
public void ruleRunnerShouldReturnNullRuleClassIsInvalid() { | ||
Class clazz = Class.class; | ||
Assert.assertNull(_ruleBook.getRuleInstance(clazz)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...src/test/java/com/deliveredtechnologies/rulebook/spring/SpringAwareRuleWithoutResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.deliveredtechnologies.rulebook.spring; | ||
|
||
import com.deliveredtechnologies.rulebook.Fact; | ||
import com.deliveredtechnologies.rulebook.annotation.Given; | ||
import com.deliveredtechnologies.rulebook.annotation.Rule; | ||
import com.deliveredtechnologies.rulebook.annotation.Then; | ||
import com.deliveredtechnologies.rulebook.annotation.When; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Rule(order = 3) | ||
public class SpringAwareRuleWithoutResult { | ||
@Autowired | ||
private SpringTestService _springTestService; | ||
|
||
@Given("value2") | ||
private Fact<String> _value2; | ||
|
||
@When | ||
public boolean when() { | ||
return _springTestService != null; | ||
} | ||
|
||
@Then | ||
public void then() { | ||
_value2.setValue(_springTestService.getValue()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...ook-spring/src/test/java/com/deliveredtechnologies/rulebook/spring/SpringTestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.deliveredtechnologies.rulebook.spring; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class SpringTestService { | ||
public static final String EXPECTED_RESULT = "Springified!"; | ||
|
||
public String getValue() { | ||
return EXPECTED_RESULT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters