Add Spring Boot Starter for Easy Rules
parent
c9c9f47f82
commit
4523234255
@ -0,0 +1,85 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.easyrules</groupId>
|
||||
<artifactId>easyrules</artifactId>
|
||||
<version>2.4.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>easyrules-spring-boot-starter</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Easy Rules Spring Boot Starter module</name>
|
||||
<description>Module providing Spring Boot autoconfiguration</description>
|
||||
|
||||
<scm>
|
||||
<url>git@github.com:EasyRules/easyrules.git</url>
|
||||
<connection>scm:git:git@github.com:EasyRules/easyrules.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:EasyRules/easyrules.git</developerConnection>
|
||||
<tag>HEAD</tag>
|
||||
</scm>
|
||||
|
||||
<issueManagement>
|
||||
<system>GitHub</system>
|
||||
<url>https://github.com/EasyRules/easyrules/issues</url>
|
||||
</issueManagement>
|
||||
|
||||
<ciManagement>
|
||||
<system>Travis CI</system>
|
||||
<url>https://travis-ci.org/EasyRules/easyrules</url>
|
||||
</ciManagement>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<id>andersonkyle</id>
|
||||
<name>Kyle Anderson</name>
|
||||
<url>https://github.com/andersonkyle</url>
|
||||
<roles>
|
||||
<role>Contributor</role>
|
||||
</roles>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>MIT License</name>
|
||||
<url>http://opensource.org/licenses/mit-license.php</url>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.easyrules</groupId>
|
||||
<artifactId>easyrules-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.easyrules</groupId>
|
||||
<artifactId>easyrules-spring</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
@ -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<Object> rulesFromRuleAnnotation = new ArrayList<>(context.getBeansWithAnnotation(Rule.class).values());
|
||||
List<org.easyrules.api.Rule> rulesFromRuleImplementation = new ArrayList<>(context.getBeansOfType(org.easyrules.api.Rule.class).values());
|
||||
List<Object> rules = new ArrayList<>();
|
||||
rules.addAll(rulesFromRuleAnnotation);
|
||||
rules.addAll(rulesFromRuleImplementation);
|
||||
RulesEngineFactoryBean rulesEngineFactoryBean = new RulesEngineFactoryBean();
|
||||
rulesEngineFactoryBean.setRules(rules);
|
||||
return rulesEngineFactoryBean;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.easyrules.spring.boot.autoconfigure.EasyRulesAutoConfiguration
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue