diff --git a/easyrules-spring-boot-starter/pom.xml b/easyrules-spring-boot-starter/pom.xml new file mode 100644 index 0000000..16390ab --- /dev/null +++ b/easyrules-spring-boot-starter/pom.xml @@ -0,0 +1,85 @@ + + + 4.0.0 + + + org.easyrules + easyrules + 2.4.1-SNAPSHOT + + + easyrules-spring-boot-starter + jar + + Easy Rules Spring Boot Starter module + Module providing Spring Boot autoconfiguration + + + git@github.com:EasyRules/easyrules.git + scm:git:git@github.com:EasyRules/easyrules.git + scm:git:git@github.com:EasyRules/easyrules.git + HEAD + + + + GitHub + https://github.com/EasyRules/easyrules/issues + + + + Travis CI + https://travis-ci.org/EasyRules/easyrules + + + + + andersonkyle + Kyle Anderson + https://github.com/andersonkyle + + Contributor + + + + + + + MIT License + http://opensource.org/licenses/mit-license.php + + + + + + org.easyrules + easyrules-core + + + org.easyrules + easyrules-spring + + + org.springframework.boot + spring-boot-autoconfigure + + + org.springframework.boot + spring-boot-test + test + + + junit + junit + test + + + commons-logging + commons-logging + test + + + + + \ No newline at end of file diff --git a/easyrules-spring-boot-starter/src/main/java/org/easyrules/spring/boot/autoconfigure/EasyRulesAutoConfiguration.java b/easyrules-spring-boot-starter/src/main/java/org/easyrules/spring/boot/autoconfigure/EasyRulesAutoConfiguration.java new file mode 100644 index 0000000..226a2e7 --- /dev/null +++ b/easyrules-spring-boot-starter/src/main/java/org/easyrules/spring/boot/autoconfigure/EasyRulesAutoConfiguration.java @@ -0,0 +1,60 @@ +/** + * The MIT License + * + * Copyright (c) 2017, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.easyrules.spring.boot.autoconfigure; + +import org.easyrules.annotation.Rule; +import org.easyrules.api.RulesEngine; +import org.easyrules.spring.RulesEngineFactoryBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +/** + * AutoConfiguration for {@link RulesEngine} + * + * @author Kyle Anderson + */ +@Configuration +@ConditionalOnClass(RulesEngineFactoryBean.class) +@ConditionalOnMissingBean(RulesEngineFactoryBean.class) +public class EasyRulesAutoConfiguration { + + @Bean + public RulesEngineFactoryBean rulesEngineFactoryBean(ApplicationContext context) { + List rulesFromRuleAnnotation = new ArrayList<>(context.getBeansWithAnnotation(Rule.class).values()); + List rulesFromRuleImplementation = new ArrayList<>(context.getBeansOfType(org.easyrules.api.Rule.class).values()); + List rules = new ArrayList<>(); + rules.addAll(rulesFromRuleAnnotation); + rules.addAll(rulesFromRuleImplementation); + RulesEngineFactoryBean rulesEngineFactoryBean = new RulesEngineFactoryBean(); + rulesEngineFactoryBean.setRules(rules); + return rulesEngineFactoryBean; + } + +} diff --git a/easyrules-spring-boot-starter/src/main/resources/META-INF/spring.factories b/easyrules-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..9249340 --- /dev/null +++ b/easyrules-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ + +org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.easyrules.spring.boot.autoconfigure.EasyRulesAutoConfiguration \ No newline at end of file diff --git a/easyrules-spring-boot-starter/src/test/java/org/easyrules/spring/boot/autoconfigure/EasyRulesAutoConfigurationTest.java b/easyrules-spring-boot-starter/src/test/java/org/easyrules/spring/boot/autoconfigure/EasyRulesAutoConfigurationTest.java new file mode 100644 index 0000000..ce15dbc --- /dev/null +++ b/easyrules-spring-boot-starter/src/test/java/org/easyrules/spring/boot/autoconfigure/EasyRulesAutoConfigurationTest.java @@ -0,0 +1,161 @@ +/** + * The MIT License + * + * Copyright (c) 2017, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.easyrules.spring.boot.autoconfigure; + +import org.easyrules.annotation.Action; +import org.easyrules.annotation.Condition; +import org.easyrules.annotation.Rule; +import org.easyrules.api.RulesEngine; +import org.easyrules.core.BasicRule; +import org.easyrules.spring.RulesEngineFactoryBean; +import org.easyrules.spring.SpringRule; +import org.junit.After; +import org.junit.Test; +import org.springframework.boot.test.util.EnvironmentTestUtils; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Tests for {@link EasyRulesAutoConfiguration} + * + * @author Kyle Anderson + */ +public class EasyRulesAutoConfigurationTest { + + private AnnotationConfigApplicationContext context; + + @After + public void tearDown() { + if(this.context != null) { + this.context.close(); + } + } + + @Test + public void rulesEngineFactoryBeanTest() { + load(EmptyConfiguration.class); + RulesEngineFactoryBean rulesEngineFactoryBean = this.context.getBean(RulesEngineFactoryBean.class); + assertNotNull(rulesEngineFactoryBean); + } + + @Test + public void rulesEngineBeanTest() { + load(EmptyConfiguration.class); + RulesEngine rulesEngine = this.context.getBean(RulesEngine.class); + assertNotNull(rulesEngine); + } + + @Test + public void noRulesConfigured() { + load(EmptyConfiguration.class); + RulesEngine rulesEngine = this.context.getBean(RulesEngine.class); + assertEquals(0, rulesEngine.getRules().size()); + } + + @Test + public void ruleImplementationBeanTest() { + load(RuleImplementationBeanConfiguration.class); + RulesEngine rulesEngine = this.context.getBean(RulesEngine.class); + assertEquals(1, rulesEngine.getRules().size()); + } + + @Test + public void ruleComponentAnnotationTest() { + load(RuleComponentAnnotationConfiguration.class); + RulesEngine rulesEngine = this.context.getBean(RulesEngine.class); + assertEquals(1, rulesEngine.getRules().size()); + } + + @Test + public void springRuleAnnotationTest() { + load(SpringRuleAnnotationConfiguration.class); + RulesEngine rulesEngine = this.context.getBean(RulesEngine.class); + assertEquals(1, rulesEngine.getRules().size()); + } + + @Configuration + static class EmptyConfiguration {} + + @Configuration + static class RuleImplementationBeanConfiguration { + @Bean + public RuleImplementationBean ruleImplementationBean() { + return new RuleImplementationBean(); + } + + static class RuleImplementationBean extends BasicRule { + @Override + public boolean evaluate() { + return true; + } + + @Override + public void execute() throws Exception {} + } + + } + + @Configuration + static class RuleComponentAnnotationConfiguration { + @Component + @Rule + static class RuleComponentAnnotation { + @Condition + public boolean when() { + return true; + } + + @Action + public void then() {} + } + } + + @Configuration + static class SpringRuleAnnotationConfiguration { + @SpringRule + static class SpringRuleAnnotation { + @Condition + public boolean when() { + return true; + } + + @Action + public void then() {} + } + } + + private void load(Class config, String... environment) { + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(applicationContext, environment); + applicationContext.register(config); + applicationContext.register(EasyRulesAutoConfiguration.class); + applicationContext.refresh(); + this.context = applicationContext; + } +} diff --git a/easyrules-spring/pom.xml b/easyrules-spring/pom.xml index c767a20..a6d4ca2 100755 --- a/easyrules-spring/pom.xml +++ b/easyrules-spring/pom.xml @@ -57,12 +57,10 @@ org.springframework spring-beans - ${spring.version} org.springframework spring-context - ${spring.version} junit diff --git a/pom.xml b/pom.xml index ccd6aae..dcae574 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,7 @@ easyrules-core easyrules-quartz easyrules-spring + easyrules-spring-boot-starter pom @@ -32,7 +33,7 @@ 2.6.0 2.7.1 2.2.3 - 4.3.6.RELEASE + Brussels-SR1 2.7 4.3.0 2.5.3 @@ -98,6 +99,12 @@ ${project.version} + + org.easyrules + easyrules-spring-boot-starter + ${project.version} + + junit junit @@ -118,6 +125,14 @@ ${mockito.version} test + + + io.spring.platform + platform-bom + ${spring-io-platform.version} + pom + import + @@ -212,6 +227,24 @@ + + org.apache.maven.plugins + maven-enforcer-plugin + + + enforce-dependency-convergence + + enforce + + + + + + true + + + +