From c8b541b32c20ac6c8585653410f31382b6b99da9 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Sun, 10 May 2020 18:22:21 +0200 Subject: [PATCH] Use parametrized tests for rule factories --- ...toryTest.java => MVELRuleFactoryTest.java} | 53 ++++-- .../rules/mvel/MVELYamlRuleFactoryTest.java | 171 ------------------ ...toryTest.java => SpELRuleFactoryTest.java} | 53 ++++-- .../rules/spel/SpELYamlRuleFactoryTest.java | 171 ------------------ 4 files changed, 72 insertions(+), 376 deletions(-) rename easy-rules-mvel/src/test/java/org/jeasy/rules/mvel/{MVELJsonRuleFactoryTest.java => MVELRuleFactoryTest.java} (84%) delete mode 100644 easy-rules-mvel/src/test/java/org/jeasy/rules/mvel/MVELYamlRuleFactoryTest.java rename easy-rules-spel/src/test/java/org/jeasy/rules/spel/{SpELJsonRuleFactoryTest.java => SpELRuleFactoryTest.java} (84%) delete mode 100644 easy-rules-spel/src/test/java/org/jeasy/rules/spel/SpELYamlRuleFactoryTest.java diff --git a/easy-rules-mvel/src/test/java/org/jeasy/rules/mvel/MVELJsonRuleFactoryTest.java b/easy-rules-mvel/src/test/java/org/jeasy/rules/mvel/MVELRuleFactoryTest.java similarity index 84% rename from easy-rules-mvel/src/test/java/org/jeasy/rules/mvel/MVELJsonRuleFactoryTest.java rename to easy-rules-mvel/src/test/java/org/jeasy/rules/mvel/MVELRuleFactoryTest.java index e8c3e80..23a942b 100644 --- a/easy-rules-mvel/src/test/java/org/jeasy/rules/mvel/MVELJsonRuleFactoryTest.java +++ b/easy-rules-mvel/src/test/java/org/jeasy/rules/mvel/MVELRuleFactoryTest.java @@ -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 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)); diff --git a/easy-rules-mvel/src/test/java/org/jeasy/rules/mvel/MVELYamlRuleFactoryTest.java b/easy-rules-mvel/src/test/java/org/jeasy/rules/mvel/MVELYamlRuleFactoryTest.java deleted file mode 100644 index c7530b7..0000000 --- a/easy-rules-mvel/src/test/java/org/jeasy/rules/mvel/MVELYamlRuleFactoryTest.java +++ /dev/null @@ -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 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 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 - } -} diff --git a/easy-rules-spel/src/test/java/org/jeasy/rules/spel/SpELJsonRuleFactoryTest.java b/easy-rules-spel/src/test/java/org/jeasy/rules/spel/SpELRuleFactoryTest.java similarity index 84% rename from easy-rules-spel/src/test/java/org/jeasy/rules/spel/SpELJsonRuleFactoryTest.java rename to easy-rules-spel/src/test/java/org/jeasy/rules/spel/SpELRuleFactoryTest.java index 2ed05f5..dbdd64d 100644 --- a/easy-rules-spel/src/test/java/org/jeasy/rules/spel/SpELJsonRuleFactoryTest.java +++ b/easy-rules-spel/src/test/java/org/jeasy/rules/spel/SpELRuleFactoryTest.java @@ -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 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)); diff --git a/easy-rules-spel/src/test/java/org/jeasy/rules/spel/SpELYamlRuleFactoryTest.java b/easy-rules-spel/src/test/java/org/jeasy/rules/spel/SpELYamlRuleFactoryTest.java deleted file mode 100644 index 3f78f39..0000000 --- a/easy-rules-spel/src/test/java/org/jeasy/rules/spel/SpELYamlRuleFactoryTest.java +++ /dev/null @@ -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 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 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 - } -}