Use parametrized tests for rule factories

pull/284/head
Mahmoud Ben Hassine 5 years ago
parent 0575a9929c
commit c8b541b32c
No known key found for this signature in database
GPG Key ID: 79FCFB0A184E0036

@ -23,35 +23,53 @@
*/
package org.jeasy.rules.mvel;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.support.reader.JsonRuleDefinitionReader;
import org.jeasy.rules.support.composite.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.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.support.composite.UnitRuleGroup;
import org.jeasy.rules.support.reader.JsonRuleDefinitionReader;
import org.jeasy.rules.support.reader.YamlRuleDefinitionReader;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.assertj.core.api.Assertions.assertThat;
// TODO use parametrized test to merge this test class with MVELYamlRuleFactoryTest
public class MVELJsonRuleFactoryTest {
@RunWith(Parameterized.class)
public class MVELRuleFactoryTest {
@Parameterized.Parameters
public static Collection<Object[]> parameters() {
return Arrays.asList(new Object[][] {
{ new MVELRuleFactory(new YamlRuleDefinitionReader()), "yml" },
{ new MVELRuleFactory(new JsonRuleDefinitionReader()), "json" },
});
}
@org.junit.Rule
public ExpectedException expectedException = ExpectedException.none();
private MVELRuleFactory factory = new MVELRuleFactory(new JsonRuleDefinitionReader());
@Parameterized.Parameter(0)
public MVELRuleFactory factory;
@Parameterized.Parameter(1)
public String fileExtension;
@Test
public void testRulesCreation() throws Exception {
// given
File rulesDescriptor = new File("src/test/resources/rules.json");
File rulesDescriptor = new File("src/test/resources/rules." + fileExtension);
// when
Rules rules = factory.createRules(new FileReader(rulesDescriptor));
@ -76,7 +94,7 @@ public class MVELJsonRuleFactoryTest {
@Test
public void testRuleCreationFromFileReader() throws Exception {
// given
Reader adultRuleDescriptorAsReader = new FileReader("src/test/resources/adult-rule.json");
Reader adultRuleDescriptorAsReader = new FileReader("src/test/resources/adult-rule." + fileExtension);
// when
Rule adultRule = factory.createRule(adultRuleDescriptorAsReader);
@ -90,7 +108,8 @@ public class MVELJsonRuleFactoryTest {
@Test
public void testRuleCreationFromStringReader() throws Exception {
// given
Reader adultRuleDescriptorAsReader = new StringReader(new String(Files.readAllBytes(Paths.get("src/test/resources/adult-rule.json"))));
Path ruleDescriptor = Paths.get("src/test/resources/adult-rule." + fileExtension);
Reader adultRuleDescriptorAsReader = new StringReader(new String(Files.readAllBytes(ruleDescriptor)));
// when
Rule adultRule = factory.createRule(adultRuleDescriptorAsReader);
@ -104,7 +123,7 @@ public class MVELJsonRuleFactoryTest {
@Test
public void testRuleCreationFromFileReader_withCompositeRules() throws Exception {
// given
File rulesDescriptor = new File("src/test/resources/composite-rules.json");
File rulesDescriptor = new File("src/test/resources/composite-rules." + fileExtension);
// when
Rules rules = factory.createRules(new FileReader(rulesDescriptor));
@ -132,7 +151,7 @@ public class MVELJsonRuleFactoryTest {
// 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");
File rulesDescriptor = new File("src/test/resources/composite-rule-invalid-composite-rule-type." + fileExtension);
// when
Rule rule = factory.createRule(new FileReader(rulesDescriptor));
@ -146,7 +165,7 @@ public class MVELJsonRuleFactoryTest {
// 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");
File rulesDescriptor = new File("src/test/resources/composite-rule-invalid-empty-composing-rules." + fileExtension);
// when
Rule rule = factory.createRule(new FileReader(rulesDescriptor));
@ -160,7 +179,7 @@ public class MVELJsonRuleFactoryTest {
// 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");
File rulesDescriptor = new File("src/test/resources/non-composite-rule-with-composing-rules." + fileExtension);
// when
Rule rule = factory.createRule(new FileReader(rulesDescriptor));

@ -1,171 +0,0 @@
/*
* The MIT License
*
* Copyright (c) 2020, 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.reader.YamlRuleDefinitionReader;
import org.jeasy.rules.support.composite.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 MVELJsonRuleFactoryTest
public class MVELYamlRuleFactoryTest {
@org.junit.Rule
public ExpectedException expectedException = ExpectedException.none();
private MVELRuleFactory factory = new MVELRuleFactory(new YamlRuleDefinitionReader());
@Test
public void testRulesCreation() throws Exception {
// given
File rulesDescriptor = new File("src/test/resources/rules.yml");
// 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 than 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.yml");
// when
Rule adultRule = factory.createRule(adultRuleDescriptorAsReader);
// then
assertThat(adultRule.getName()).isEqualTo("adult rule");
assertThat(adultRule.getDescription()).isEqualTo("when age is greater than 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.yml"))));
// when
Rule adultRule = factory.createRule(adultRuleDescriptorAsReader);
// then
assertThat(adultRule.getName()).isEqualTo("adult rule");
assertThat(adultRule.getDescription()).isEqualTo("when age is greater than 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.yml");
// 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.yml");
// 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.yml");
// 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.yml");
// when
Rule rule = factory.createRule(new FileReader(rulesDescriptor));
// then
// expected exception
}
}

@ -23,35 +23,53 @@
*/
package org.jeasy.rules.spel;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.support.reader.JsonRuleDefinitionReader;
import org.jeasy.rules.support.composite.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.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.support.composite.UnitRuleGroup;
import org.jeasy.rules.support.reader.JsonRuleDefinitionReader;
import org.jeasy.rules.support.reader.YamlRuleDefinitionReader;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.assertj.core.api.Assertions.assertThat;
// TODO use parametrized test to merge this test class with SpELYamlRuleFactoryTest
public class SpELJsonRuleFactoryTest {
@RunWith(Parameterized.class)
public class SpELRuleFactoryTest {
@Parameterized.Parameters
public static Collection<Object[]> parameters() {
return Arrays.asList(new Object[][] {
{ new SpELRuleFactory(new YamlRuleDefinitionReader()), "yml" },
{ new SpELRuleFactory(new JsonRuleDefinitionReader()), "json" },
});
}
@org.junit.Rule
public ExpectedException expectedException = ExpectedException.none();
private SpELRuleFactory factory = new SpELRuleFactory(new JsonRuleDefinitionReader());
@Parameterized.Parameter(0)
public SpELRuleFactory factory;
@Parameterized.Parameter(1)
public String fileExtension;
@Test
public void testRulesCreation() throws Exception {
// given
File rulesDescriptor = new File("src/test/resources/rules.json");
File rulesDescriptor = new File("src/test/resources/rules." + fileExtension);
// when
Rules rules = factory.createRules(new FileReader(rulesDescriptor));
@ -76,7 +94,7 @@ public class SpELJsonRuleFactoryTest {
@Test
public void testRuleCreationFromFileReader() throws Exception {
// given
Reader adultRuleDescriptorAsReader = new FileReader("src/test/resources/adult-rule.json");
Reader adultRuleDescriptorAsReader = new FileReader("src/test/resources/adult-rule." + fileExtension);
// when
Rule adultRule = factory.createRule(adultRuleDescriptorAsReader);
@ -90,7 +108,8 @@ public class SpELJsonRuleFactoryTest {
@Test
public void testRuleCreationFromStringReader() throws Exception {
// given
Reader adultRuleDescriptorAsReader = new StringReader(new String(Files.readAllBytes(Paths.get("src/test/resources/adult-rule.json"))));
Path ruleDescriptor = Paths.get("src/test/resources/adult-rule." + fileExtension);
Reader adultRuleDescriptorAsReader = new StringReader(new String(Files.readAllBytes(ruleDescriptor)));
// when
Rule adultRule = factory.createRule(adultRuleDescriptorAsReader);
@ -104,7 +123,7 @@ public class SpELJsonRuleFactoryTest {
@Test
public void testRuleCreationFromFileReader_withCompositeRules() throws Exception {
// given
File rulesDescriptor = new File("src/test/resources/composite-rules.json");
File rulesDescriptor = new File("src/test/resources/composite-rules." + fileExtension);
// when
Rules rules = factory.createRules(new FileReader(rulesDescriptor));
@ -132,7 +151,7 @@ public class SpELJsonRuleFactoryTest {
// 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");
File rulesDescriptor = new File("src/test/resources/composite-rule-invalid-composite-rule-type." + fileExtension);
// when
Rule rule = factory.createRule(new FileReader(rulesDescriptor));
@ -146,7 +165,7 @@ public class SpELJsonRuleFactoryTest {
// 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");
File rulesDescriptor = new File("src/test/resources/composite-rule-invalid-empty-composing-rules." + fileExtension);
// when
Rule rule = factory.createRule(new FileReader(rulesDescriptor));
@ -160,7 +179,7 @@ public class SpELJsonRuleFactoryTest {
// 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");
File rulesDescriptor = new File("src/test/resources/non-composite-rule-with-composing-rules." + fileExtension);
// when
Rule rule = factory.createRule(new FileReader(rulesDescriptor));

@ -1,171 +0,0 @@
/*
* The MIT License
*
* Copyright (c) 2020, 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.spel;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.support.composite.UnitRuleGroup;
import org.jeasy.rules.support.reader.YamlRuleDefinitionReader;
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 SpELJsonRuleFactoryTest
public class SpELYamlRuleFactoryTest {
@org.junit.Rule
public ExpectedException expectedException = ExpectedException.none();
private SpELRuleFactory factory = new SpELRuleFactory(new YamlRuleDefinitionReader());
@Test
public void testRulesCreation() throws Exception {
// given
File rulesDescriptor = new File("src/test/resources/rules.yml");
// 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 than 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.yml");
// when
Rule adultRule = factory.createRule(adultRuleDescriptorAsReader);
// then
assertThat(adultRule.getName()).isEqualTo("adult rule");
assertThat(adultRule.getDescription()).isEqualTo("when age is greater than 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.yml"))));
// when
Rule adultRule = factory.createRule(adultRuleDescriptorAsReader);
// then
assertThat(adultRule.getName()).isEqualTo("adult rule");
assertThat(adultRule.getDescription()).isEqualTo("when age is greater than 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.yml");
// 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.yml");
// 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.yml");
// 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.yml");
// when
Rule rule = factory.createRule(new FileReader(rulesDescriptor));
// then
// expected exception
}
}
Loading…
Cancel
Save