parent
10905f7f22
commit
84e80f8b33
@ -0,0 +1,120 @@
|
||||
/**
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright (c) 2019, 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.jeasy.rules.mvel;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.jeasy.rules.api.Rule;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Rule definition reader based on <a href="https://github.com/FasterXML/jackson">Jackson</a>.
|
||||
*
|
||||
* This reader expects an array of rule definitions as input even for a single rule. For example:
|
||||
*
|
||||
* <pre>
|
||||
* [{rule1}, {rule2}]
|
||||
* </pre>
|
||||
*
|
||||
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public class MVELJsonRuleDefinitionReader implements MVELRuleDefinitionReader {
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
/**
|
||||
* Create a new {@link MVELJsonRuleDefinitionReader}.
|
||||
*/
|
||||
public MVELJsonRuleDefinitionReader() {
|
||||
objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MVELJsonRuleDefinitionReader}.
|
||||
*
|
||||
* @param objectMapper to use to read rule definitions
|
||||
*/
|
||||
public MVELJsonRuleDefinitionReader(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public List<MVELRuleDefinition> read(Reader reader) throws Exception {
|
||||
List<MVELRuleDefinition> ruleDefinitions = new ArrayList<>();
|
||||
Object[] rules = objectMapper.readValue(reader, Object[].class );
|
||||
|
||||
for (Object rule : rules) {
|
||||
Map<String, Object> map = (Map<String, Object>) rule;
|
||||
ruleDefinitions.add(createRuleDefinitionFrom(map));
|
||||
}
|
||||
return ruleDefinitions;
|
||||
}
|
||||
|
||||
private MVELRuleDefinition createRuleDefinitionFrom(Map<String, Object> map) {
|
||||
MVELRuleDefinition ruleDefinition = new MVELRuleDefinition();
|
||||
|
||||
String name = (String) map.get("name");
|
||||
ruleDefinition.setName(name != null ? name : Rule.DEFAULT_NAME);
|
||||
|
||||
String description = (String) map.get("description");
|
||||
ruleDefinition.setDescription(description != null ? description : Rule.DEFAULT_DESCRIPTION);
|
||||
|
||||
Integer priority = (Integer) map.get("priority");
|
||||
ruleDefinition.setPriority(priority != null ? priority : Rule.DEFAULT_PRIORITY);
|
||||
|
||||
String compositeRuleType = (String) map.get("compositeRuleType");
|
||||
|
||||
String condition = (String) map.get("condition");
|
||||
if (condition == null && compositeRuleType == null) {
|
||||
throw new IllegalArgumentException("The rule condition must be specified");
|
||||
}
|
||||
ruleDefinition.setCondition(condition);
|
||||
|
||||
List<String> actions = (List<String>) map.get("actions");
|
||||
if ((actions == null || actions.isEmpty()) && compositeRuleType == null) {
|
||||
throw new IllegalArgumentException("The rule action(s) must be specified");
|
||||
}
|
||||
ruleDefinition.setActions(actions);
|
||||
|
||||
List<Object> composingRules = (List<Object>) map.get("composingRules");
|
||||
if (composingRules != null && compositeRuleType == null) {
|
||||
throw new IllegalArgumentException("Non-composite rules cannot have composing rules");
|
||||
} else if ((composingRules == null || composingRules.isEmpty()) && compositeRuleType != null) {
|
||||
throw new IllegalArgumentException("Composite rules must have composing rules specified");
|
||||
} else if (composingRules != null) {
|
||||
List<MVELRuleDefinition> composingRuleDefinitions = new ArrayList<>();
|
||||
for (Object rule : composingRules){
|
||||
Map<String, Object> composingRuleMap = (Map<String, Object>) rule;
|
||||
composingRuleDefinitions.add(createRuleDefinitionFrom(composingRuleMap));
|
||||
}
|
||||
ruleDefinition.setComposingRules(composingRuleDefinitions);
|
||||
ruleDefinition.setCompositeRuleType(compositeRuleType);
|
||||
}
|
||||
|
||||
return ruleDefinition;
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
/**
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright (c) 2019, 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.jeasy.rules.mvel;
|
||||
|
||||
import org.jeasy.rules.api.Rule;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Rule definition reader based on <a href="https://github.com/FasterXML/jackson-dataformats-text/tree/master/yaml">Jackson Yaml</a>.
|
||||
*
|
||||
* This reader expects a collection of rule definitions as input even for a single rule. For example:
|
||||
*
|
||||
* <pre>
|
||||
* rule1
|
||||
* ---
|
||||
* rule2
|
||||
* </pre>
|
||||
*
|
||||
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public class MVELYamlRuleDefinitionReader implements MVELRuleDefinitionReader {
|
||||
|
||||
private Yaml yaml;
|
||||
|
||||
/**
|
||||
* Create a new {@link MVELYamlRuleDefinitionReader}.
|
||||
*/
|
||||
public MVELYamlRuleDefinitionReader() {
|
||||
yaml = new Yaml();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MVELYamlRuleDefinitionReader}.
|
||||
*
|
||||
* @param yaml to use to read rule definitions
|
||||
*/
|
||||
public MVELYamlRuleDefinitionReader(Yaml yaml) {
|
||||
this.yaml = yaml;
|
||||
}
|
||||
|
||||
public List<MVELRuleDefinition> read(Reader reader) {
|
||||
List<MVELRuleDefinition> ruleDefinitions = new ArrayList<>();
|
||||
Iterable<Object> rules = yaml.loadAll(reader);
|
||||
for (Object rule : rules) {
|
||||
Map<String, Object> map = (Map<String, Object>) rule;
|
||||
ruleDefinitions.add(createRuleDefinitionFrom(map));
|
||||
}
|
||||
return ruleDefinitions;
|
||||
}
|
||||
|
||||
private MVELRuleDefinition createRuleDefinitionFrom(Map<String, Object> map) {
|
||||
MVELRuleDefinition ruleDefinition = new MVELRuleDefinition();
|
||||
|
||||
String name = (String) map.get("name");
|
||||
ruleDefinition.setName(name != null ? name : Rule.DEFAULT_NAME);
|
||||
|
||||
String description = (String) map.get("description");
|
||||
ruleDefinition.setDescription(description != null ? description : Rule.DEFAULT_DESCRIPTION);
|
||||
|
||||
Integer priority = (Integer) map.get("priority");
|
||||
ruleDefinition.setPriority(priority != null ? priority : Rule.DEFAULT_PRIORITY);
|
||||
|
||||
String compositeRuleType = (String) map.get("compositeRuleType");
|
||||
|
||||
String condition = (String) map.get("condition");
|
||||
if (condition == null && compositeRuleType == null) {
|
||||
throw new IllegalArgumentException("The rule condition must be specified");
|
||||
}
|
||||
ruleDefinition.setCondition(condition);
|
||||
|
||||
List<String> actions = (List<String>) map.get("actions");
|
||||
if ((actions == null || actions.isEmpty()) && compositeRuleType == null) {
|
||||
throw new IllegalArgumentException("The rule action(s) must be specified");
|
||||
}
|
||||
ruleDefinition.setActions(actions);
|
||||
|
||||
List<Object> composingRules = (List<Object>) map.get("composingRules");
|
||||
if (composingRules != null && compositeRuleType == null) {
|
||||
throw new IllegalArgumentException("Non-composite rules cannot have composing rules");
|
||||
} else if (composingRules == null && compositeRuleType != null) {
|
||||
throw new IllegalArgumentException("Composite rules must have composing rules specified");
|
||||
} else if (composingRules != null) {
|
||||
List<MVELRuleDefinition> composingRuleDefinitions = new ArrayList<>();
|
||||
for (Object rule : composingRules){
|
||||
Map<String, Object> composingRuleMap = (Map<String, Object>) rule;
|
||||
composingRuleDefinitions.add(createRuleDefinitionFrom(composingRuleMap));
|
||||
}
|
||||
ruleDefinition.setComposingRules(composingRuleDefinitions);
|
||||
ruleDefinition.setCompositeRuleType(compositeRuleType);
|
||||
}
|
||||
|
||||
return ruleDefinition;
|
||||
}
|
||||
}
|
@ -0,0 +1,205 @@
|
||||
/**
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright (c) 2019, 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.jeasy.rules.mvel;
|
||||
|
||||
import org.jeasy.rules.api.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.StringReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
// TODO use parametrized test to merge this test class with MVELYamlRuleDefinitionReaderTest
|
||||
public class MVELJsonRuleDefinitionReaderTest {
|
||||
|
||||
private MVELRuleDefinitionReader ruleDefinitionReader = new MVELJsonRuleDefinitionReader();
|
||||
|
||||
@Test
|
||||
public void testRuleDefinitionReadingFromFile() throws Exception {
|
||||
// given
|
||||
File adultRuleDescriptor = new File("src/test/resources/adult-rule.json");
|
||||
|
||||
// when
|
||||
List<MVELRuleDefinition> ruleDefinitions = ruleDefinitionReader.read(new FileReader(adultRuleDescriptor));
|
||||
|
||||
// then
|
||||
assertThat(ruleDefinitions).hasSize(1);
|
||||
MVELRuleDefinition adultRuleDefinition = ruleDefinitions.get(0);
|
||||
assertThat(adultRuleDefinition).isNotNull();
|
||||
assertThat(adultRuleDefinition.getName()).isEqualTo("adult rule");
|
||||
assertThat(adultRuleDefinition.getDescription()).isEqualTo("when age is greater then 18, then mark as adult");
|
||||
assertThat(adultRuleDefinition.getPriority()).isEqualTo(1);
|
||||
assertThat(adultRuleDefinition.getCondition()).isEqualTo("person.age > 18");
|
||||
assertThat(adultRuleDefinition.getActions()).isEqualTo(Collections.singletonList("person.setAdult(true);"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleDefinitionReadingFromString() throws Exception {
|
||||
// given
|
||||
String adultRuleDescriptor = new String(Files.readAllBytes(Paths.get("src/test/resources/adult-rule.json")));
|
||||
|
||||
// when
|
||||
List<MVELRuleDefinition> ruleDefinitions = ruleDefinitionReader.read(new StringReader(adultRuleDescriptor));
|
||||
|
||||
// then
|
||||
assertThat(ruleDefinitions).hasSize(1);
|
||||
MVELRuleDefinition adultRuleDefinition = ruleDefinitions.get(0);
|
||||
assertThat(adultRuleDefinition).isNotNull();
|
||||
assertThat(adultRuleDefinition.getName()).isEqualTo("adult rule");
|
||||
assertThat(adultRuleDefinition.getDescription()).isEqualTo("when age is greater then 18, then mark as adult");
|
||||
assertThat(adultRuleDefinition.getPriority()).isEqualTo(1);
|
||||
assertThat(adultRuleDefinition.getCondition()).isEqualTo("person.age > 18");
|
||||
assertThat(adultRuleDefinition.getActions()).isEqualTo(Collections.singletonList("person.setAdult(true);"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleDefinitionReading_withDefaultValues() throws Exception {
|
||||
// given
|
||||
File adultRuleDescriptor = new File("src/test/resources/adult-rule-with-default-values.json");
|
||||
|
||||
// when
|
||||
List<MVELRuleDefinition> ruleDefinitions = ruleDefinitionReader.read(new FileReader(adultRuleDescriptor));
|
||||
|
||||
// then
|
||||
assertThat(ruleDefinitions).hasSize(1);
|
||||
MVELRuleDefinition adultRuleDefinition = ruleDefinitions.get(0);
|
||||
assertThat(adultRuleDefinition).isNotNull();
|
||||
assertThat(adultRuleDefinition.getName()).isEqualTo(Rule.DEFAULT_NAME);
|
||||
assertThat(adultRuleDefinition.getDescription()).isEqualTo(Rule.DEFAULT_DESCRIPTION);
|
||||
assertThat(adultRuleDefinition.getPriority()).isEqualTo(Rule.DEFAULT_PRIORITY);
|
||||
assertThat(adultRuleDefinition.getCondition()).isEqualTo("person.age > 18");
|
||||
assertThat(adultRuleDefinition.getActions()).isEqualTo(Collections.singletonList("person.setAdult(true);"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testInvalidRuleDefinitionReading_whenNoCondition() throws Exception {
|
||||
// given
|
||||
File adultRuleDescriptor = new File("src/test/resources/adult-rule-without-condition.json");
|
||||
|
||||
// when
|
||||
List<MVELRuleDefinition> ruleDefinitions = ruleDefinitionReader.read(new FileReader(adultRuleDescriptor));
|
||||
|
||||
// then
|
||||
// expected exception
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testInvalidRuleDefinitionReading_whenNoActions() throws Exception {
|
||||
// given
|
||||
File adultRuleDescriptor = new File("src/test/resources/adult-rule-without-actions.json");
|
||||
|
||||
// when
|
||||
List<MVELRuleDefinition> ruleDefinitions = ruleDefinitionReader.read(new FileReader(adultRuleDescriptor));
|
||||
|
||||
// then
|
||||
// expected exception
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRulesDefinitionReading() throws Exception {
|
||||
// given
|
||||
File rulesDescriptor = new File("src/test/resources/rules.json");
|
||||
|
||||
// when
|
||||
List<MVELRuleDefinition> ruleDefinitions = ruleDefinitionReader.read(new FileReader(rulesDescriptor));
|
||||
|
||||
// then
|
||||
assertThat(ruleDefinitions).hasSize(2);
|
||||
MVELRuleDefinition ruleDefinition = ruleDefinitions.get(0);
|
||||
assertThat(ruleDefinition).isNotNull();
|
||||
assertThat(ruleDefinition.getName()).isEqualTo("adult rule");
|
||||
assertThat(ruleDefinition.getDescription()).isEqualTo("when age is greater then 18, then mark as adult");
|
||||
assertThat(ruleDefinition.getPriority()).isEqualTo(1);
|
||||
assertThat(ruleDefinition.getCondition()).isEqualTo("person.age > 18");
|
||||
assertThat(ruleDefinition.getActions()).isEqualTo(Collections.singletonList("person.setAdult(true);"));
|
||||
|
||||
ruleDefinition = ruleDefinitions.get(1);
|
||||
assertThat(ruleDefinition).isNotNull();
|
||||
assertThat(ruleDefinition.getName()).isEqualTo("weather rule");
|
||||
assertThat(ruleDefinition.getDescription()).isEqualTo("when it rains, then take an umbrella");
|
||||
assertThat(ruleDefinition.getPriority()).isEqualTo(2);
|
||||
assertThat(ruleDefinition.getCondition()).isEqualTo("rain == true");
|
||||
assertThat(ruleDefinition.getActions()).isEqualTo(Collections.singletonList("System.out.println(\"It rains, take an umbrella!\");"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyRulesDefinitionReading() throws Exception {
|
||||
// given
|
||||
File rulesDescriptor = new File("src/test/resources/rules-empty.json");
|
||||
|
||||
// when
|
||||
List<MVELRuleDefinition> ruleDefinitions = ruleDefinitionReader.read(new FileReader(rulesDescriptor));
|
||||
|
||||
// then
|
||||
assertThat(ruleDefinitions).hasSize(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleDefinitionReading_withCompositeAndBasicRules() throws Exception {
|
||||
// given
|
||||
File compositeRuleDescriptor = new File("src/test/resources/composite-rules.json");
|
||||
|
||||
// when
|
||||
List<MVELRuleDefinition> ruleDefinitions = ruleDefinitionReader.read(new FileReader(compositeRuleDescriptor));
|
||||
|
||||
// then
|
||||
assertThat(ruleDefinitions).hasSize(2);
|
||||
|
||||
// then
|
||||
MVELRuleDefinition ruleDefinition = ruleDefinitions.get(0);
|
||||
assertThat(ruleDefinition).isNotNull();
|
||||
assertThat(ruleDefinition.getName()).isEqualTo("Movie id rule");
|
||||
assertThat(ruleDefinition.getDescription()).isEqualTo("description");
|
||||
assertThat(ruleDefinition.getPriority()).isEqualTo(1);
|
||||
assertThat(ruleDefinition.getCompositeRuleType()).isEqualTo("UnitRuleGroup");
|
||||
assertThat(ruleDefinition.getComposingRules()).isNotEmpty();
|
||||
|
||||
List<MVELRuleDefinition> subrules = ruleDefinition.getComposingRules();
|
||||
assertThat(subrules).hasSize(2);
|
||||
|
||||
MVELRuleDefinition subrule = subrules.get(0);
|
||||
assertThat(subrule.getName()).isEqualTo("Time is evening");
|
||||
assertThat(subrule.getDescription()).isEqualTo("If it's later than 7pm");
|
||||
assertThat(subrule.getPriority()).isEqualTo(1);
|
||||
|
||||
subrule = subrules.get(1);
|
||||
assertThat(subrule.getName()).isEqualTo("Movie is rated R");
|
||||
assertThat(subrule.getDescription()).isEqualTo("If the movie is rated R");
|
||||
assertThat(subrule.getPriority()).isEqualTo(1);
|
||||
|
||||
ruleDefinition = ruleDefinitions.get(1);
|
||||
assertThat(ruleDefinition).isNotNull();
|
||||
assertThat(ruleDefinition.getName()).isEqualTo("weather rule");
|
||||
assertThat(ruleDefinition.getDescription()).isEqualTo("when it rains, then take an umbrella");
|
||||
assertThat(ruleDefinition.getComposingRules()).isEmpty();
|
||||
assertThat(ruleDefinition.getCondition()).isEqualTo("rain == True");
|
||||
assertThat(ruleDefinition.getActions()).isEqualTo(Collections.singletonList("System.out.println(\"It rains, take an umbrella!\");"));
|
||||
}
|
||||
}
|
@ -0,0 +1,170 @@
|
||||
/**
|
||||
* The MIT License
|
||||
*
|
||||
* Copyright (c) 2019, 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.jeasy.rules.mvel;
|
||||
|
||||
import org.jeasy.rules.api.Rule;
|
||||
import org.jeasy.rules.api.Rules;
|
||||
import org.jeasy.rules.support.UnitRuleGroup;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Iterator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
// TODO use parametrized test to merge this test class with MVELYamlRuleFactoryTest
|
||||
public class MVELJsonRuleFactoryTest {
|
||||
|
||||
@org.junit.Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
private MVELRuleFactory factory = new MVELRuleFactory(new MVELJsonRuleDefinitionReader());
|
||||
|
||||
@Test
|
||||
public void testRulesCreation() throws Exception {
|
||||
// given
|
||||
File rulesDescriptor = new File("src/test/resources/rules.json");
|
||||
|
||||
// when
|
||||
Rules rules = factory.createRules(new FileReader(rulesDescriptor));
|
||||
|
||||
// then
|
||||
assertThat(rules).hasSize(2);
|
||||
Iterator<Rule> iterator = rules.iterator();
|
||||
|
||||
Rule rule = iterator.next();
|
||||
assertThat(rule).isNotNull();
|
||||
assertThat(rule.getName()).isEqualTo("adult rule");
|
||||
assertThat(rule.getDescription()).isEqualTo("when age is greater then 18, then mark as adult");
|
||||
assertThat(rule.getPriority()).isEqualTo(1);
|
||||
|
||||
rule = iterator.next();
|
||||
assertThat(rule).isNotNull();
|
||||
assertThat(rule.getName()).isEqualTo("weather rule");
|
||||
assertThat(rule.getDescription()).isEqualTo("when it rains, then take an umbrella");
|
||||
assertThat(rule.getPriority()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleCreationFromFileReader() throws Exception{
|
||||
// given
|
||||
Reader adultRuleDescriptorAsReader = new FileReader("src/test/resources/adult-rule.json");
|
||||
|
||||
// when
|
||||
Rule adultRule = factory.createRule(adultRuleDescriptorAsReader);
|
||||
|
||||
// then
|
||||
assertThat(adultRule.getName()).isEqualTo("adult rule");
|
||||
assertThat(adultRule.getDescription()).isEqualTo("when age is greater then 18, then mark as adult");
|
||||
assertThat(adultRule.getPriority()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleCreationFromStringReader() throws Exception{
|
||||
// given
|
||||
Reader adultRuleDescriptorAsReader = new StringReader(new String(Files.readAllBytes(Paths.get("src/test/resources/adult-rule.json"))));
|
||||
|
||||
// when
|
||||
Rule adultRule = factory.createRule(adultRuleDescriptorAsReader);
|
||||
|
||||
// then
|
||||
assertThat(adultRule.getName()).isEqualTo("adult rule");
|
||||
assertThat(adultRule.getDescription()).isEqualTo("when age is greater then 18, then mark as adult");
|
||||
assertThat(adultRule.getPriority()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleCreationFromFileReader_withCompositeRules() throws Exception {
|
||||
// given
|
||||
File rulesDescriptor = new File("src/test/resources/composite-rules.json");
|
||||
|
||||
// when
|
||||
Rules rules = factory.createRules(new FileReader(rulesDescriptor));
|
||||
|
||||
// then
|
||||
assertThat(rules).hasSize(2);
|
||||
Iterator<Rule> iterator = rules.iterator();
|
||||
|
||||
Rule rule = iterator.next();
|
||||
assertThat(rule).isNotNull();
|
||||
assertThat(rule.getName()).isEqualTo("Movie id rule");
|
||||
assertThat(rule.getDescription()).isEqualTo("description");
|
||||
assertThat(rule.getPriority()).isEqualTo(1);
|
||||
assertThat(rule).isInstanceOf(UnitRuleGroup.class);
|
||||
|
||||
rule = iterator.next();
|
||||
assertThat(rule).isNotNull();
|
||||
assertThat(rule.getName()).isEqualTo("weather rule");
|
||||
assertThat(rule.getDescription()).isEqualTo("when it rains, then take an umbrella");
|
||||
assertThat(rule.getPriority()).isEqualTo(1);;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleCreationFromFileReader_withInvalidCompositeRuleType() throws Exception {
|
||||
// given
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectMessage("Invalid composite rule type, must be one of [UnitRuleGroup, ConditionalRuleGroup, ActivationRuleGroup]");
|
||||
File rulesDescriptor = new File("src/test/resources/composite-rule-invalid-composite-rule-type.json");
|
||||
|
||||
// when
|
||||
Rule rule = factory.createRule(new FileReader(rulesDescriptor));
|
||||
|
||||
// then
|
||||
// expected exception
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleCreationFromFileReader_withEmptyComposingRules() throws Exception {
|
||||
// given
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectMessage("Composite rules must have composing rules specified");
|
||||
File rulesDescriptor = new File("src/test/resources/composite-rule-invalid-empty-composing-rules.json");
|
||||
|
||||
// when
|
||||
Rule rule = factory.createRule(new FileReader(rulesDescriptor));
|
||||
|
||||
// then
|
||||
// expected exception
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRuleCreationFromFileReader_withNonCompositeRuleDeclaresComposingRules() throws Exception {
|
||||
// given
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectMessage("Non-composite rules cannot have composing rules");
|
||||
File rulesDescriptor = new File("src/test/resources/non-composite-rule-with-composing-rules.json");
|
||||
|
||||
// when
|
||||
Rule rule = factory.createRule(new FileReader(rulesDescriptor));
|
||||
|
||||
// then
|
||||
// expected exception
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
[
|
||||
{
|
||||
"condition": "person.age > 18",
|
||||
"actions": [
|
||||
"person.setAdult(true);"
|
||||
]
|
||||
}
|
||||
]
|
@ -0,0 +1,5 @@
|
||||
[
|
||||
{
|
||||
"condition": "person.age > 18"
|
||||
}
|
||||
]
|
@ -0,0 +1,7 @@
|
||||
[
|
||||
{
|
||||
"actions": [
|
||||
"person.setAdult(true);"
|
||||
]
|
||||
}
|
||||
]
|
@ -0,0 +1,11 @@
|
||||
[
|
||||
{
|
||||
"name": "adult rule",
|
||||
"description": "when age is greater then 18, then mark as adult",
|
||||
"priority": 1,
|
||||
"condition": "person.age > 18",
|
||||
"actions": [
|
||||
"person.setAdult(true);"
|
||||
]
|
||||
}
|
||||
]
|
@ -0,0 +1,23 @@
|
||||
[
|
||||
{
|
||||
"name": "invalid rule",
|
||||
"compositeRuleType": "foo",
|
||||
"priority": 1,
|
||||
"composingRules": [
|
||||
{
|
||||
"name": "rule1",
|
||||
"condition": "true",
|
||||
"actions": [
|
||||
"System.out.println();"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "rule2",
|
||||
"condition": "false",
|
||||
"actions": [
|
||||
"System.out.println();"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
@ -0,0 +1,8 @@
|
||||
[
|
||||
{
|
||||
"name": "invalid rule",
|
||||
"compositeRuleType": "UnitRuleGroup",
|
||||
"priority": 1,
|
||||
"composingRules": []
|
||||
}
|
||||
]
|
@ -0,0 +1,36 @@
|
||||
[
|
||||
{
|
||||
"name": "Movie id rule",
|
||||
"compositeRuleType": "UnitRuleGroup",
|
||||
"priority": 1,
|
||||
"composingRules": [
|
||||
{
|
||||
"name": "Time is evening",
|
||||
"description": "If it's later than 7pm",
|
||||
"priority": 1,
|
||||
"condition": "day.hour > 19",
|
||||
"actions": [
|
||||
"person.shouldProvideId(true);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Movie is rated R",
|
||||
"description": "If the movie is rated R",
|
||||
"priority": 1,
|
||||
"condition": "movie.rating == R",
|
||||
"actions": [
|
||||
"person.shouldProvideId(true);"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "weather rule",
|
||||
"description": "when it rains, then take an umbrella",
|
||||
"priority": 1,
|
||||
"condition": "rain == True",
|
||||
"actions": [
|
||||
"System.out.println(\"It rains, take an umbrella!\");"
|
||||
]
|
||||
}
|
||||
]
|
@ -0,0 +1,30 @@
|
||||
[
|
||||
{
|
||||
"name": "Movie id rule",
|
||||
"priority": 1,
|
||||
"condition": "true",
|
||||
"actions": [
|
||||
"System.out.println();"
|
||||
],
|
||||
"composingRules": [
|
||||
{
|
||||
"name": "Time is evening",
|
||||
"description": "If it's later than 7pm",
|
||||
"priority": 1,
|
||||
"condition": "day.hour > 19",
|
||||
"actions": [
|
||||
"person.shouldProvideId(true);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Movie is rated R",
|
||||
"description": "If the movie is rated R",
|
||||
"priority": 1,
|
||||
"condition": "movie.rating == R",
|
||||
"actions": [
|
||||
"person.shouldProvideId(true);"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
@ -0,0 +1 @@
|
||||
[]
|
@ -0,0 +1,20 @@
|
||||
[
|
||||
{
|
||||
"name": "adult rule",
|
||||
"description": "when age is greater then 18, then mark as adult",
|
||||
"priority": 1,
|
||||
"condition": "person.age > 18",
|
||||
"actions": [
|
||||
"person.setAdult(true);"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "weather rule",
|
||||
"description": "when it rains, then take an umbrella",
|
||||
"priority": 2,
|
||||
"condition": "rain == true",
|
||||
"actions": [
|
||||
"System.out.println(\"It rains, take an umbrella!\");"
|
||||
]
|
||||
}
|
||||
]
|
Loading…
Reference in New Issue