refactor engine test class

pull/15/head
Mahmoud Ben Hassine
parent 14a10f9d7d
commit 71875779f0

@ -2,83 +2,99 @@ package org.easyrules.core;
import org.easyrules.annotation.Action; import org.easyrules.annotation.Action;
import org.easyrules.annotation.Condition; import org.easyrules.annotation.Condition;
import org.easyrules.annotation.Priority;
import org.easyrules.annotation.Rule; import org.easyrules.annotation.Rule;
import org.easyrules.api.RulesEngine; import org.easyrules.api.RulesEngine;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.easyrules.core.RulesEngineBuilder.aNewRulesEngine; import static org.easyrules.core.RulesEngineBuilder.aNewRulesEngine;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
/** /**
* Test class for {@link org.easyrules.core.DefaultRulesEngine}. * Test class for {@link org.easyrules.core.DefaultRulesEngine}.
* *
* @author Mahmoud Ben Hassine (mahmoud@benhassine.fr) * @author Mahmoud Ben Hassine (mahmoud@benhassine.fr)
*/ */
@RunWith(MockitoJUnitRunner.class)
public class DefaultRulesEngineTest { public class DefaultRulesEngineTest {
private SimpleRule simpleRule; @Mock
private BasicRule rule, anotherRule;
private SimpleAnnotatedRule simpleAnnotatedRule; private AnnotatedRule annotatedRule;
private RulesEngine rulesEngine; private RulesEngine rulesEngine;
@Before @Before
public void setup() { public void setup() {
simpleRule = new SimpleRule(); when(rule.getName()).thenReturn("r");
simpleAnnotatedRule = new SimpleAnnotatedRule(); when(rule.getDescription()).thenReturn("d");
when(rule.getPriority()).thenReturn(1);
annotatedRule = new AnnotatedRule();
rulesEngine = aNewRulesEngine().build(); rulesEngine = aNewRulesEngine().build();
} }
@Test @Test
public void whenConditionIsTrue_thenActionShouldBeExecuted() { public void whenConditionIsTrue_thenActionShouldBeExecuted() throws Exception {
rulesEngine.registerRule(simpleAnnotatedRule); when(rule.evaluate()).thenReturn(true);
rulesEngine.registerRule(rule);
rulesEngine.fireRules(); rulesEngine.fireRules();
assertThat(simpleAnnotatedRule.isExecuted()).isTrue();
verify(rule).execute();
} }
@Test @Test
public void actionsMustBeExecutedInTheDefinedOrder() { public void whenConditionIsFalse_thenActionShouldNotBeExecuted() throws Exception {
rulesEngine.registerRule(simpleAnnotatedRule); when(rule.evaluate()).thenReturn(false);
rulesEngine.registerRule(rule);
rulesEngine.fireRules(); rulesEngine.fireRules();
assertEquals("012", simpleAnnotatedRule.getActionSequence());
verify(rule, never()).execute();
} }
@Test @Test
public void testRulesWithDifferentNameAndDescriptionButWithSamePriority() throws Exception { public void rulesMustBeTriggeredInTheirNaturalOrder() throws Exception {
when(rule.evaluate()).thenReturn(true);
SimpleRule rule1 = new SimpleRule("rule 1", "description 1", 0); when(anotherRule.evaluate()).thenReturn(true);
SimpleRule rule2 = new SimpleRule("rule 2", "description 2", 0); when(rule.compareTo(anotherRule)).thenReturn(-1);
SimpleRule rule3 = new SimpleRule("rule 3", "description 3", 1); when(anotherRule.compareTo(rule)).thenReturn(1);
rulesEngine.registerRule(rule);
rulesEngine.registerRule(anotherRule);
rulesEngine.registerRule(rule1);
rulesEngine.registerRule(rule2);
rulesEngine.registerRule(rule3);
rulesEngine.fireRules(); rulesEngine.fireRules();
assertThat(rule1.isExecuted()).isTrue(); InOrder inOrder = inOrder(rule, anotherRule);
assertThat(rule2.isExecuted()).isTrue(); inOrder.verify(rule).execute();
assertThat(rule3.isExecuted()).isTrue(); inOrder.verify(anotherRule).execute();
} }
@Test @Test
public void testRulesWithSameNameAndDescriptionAndPriority() throws Exception { public void actionsMustBeExecutedInTheDefinedOrder() {
rulesEngine.registerRule(annotatedRule);
SimpleRule rule1 = new SimpleRule("rule 1", "description 1", 0); rulesEngine.fireRules();
SimpleRule rule2 = new SimpleRule("rule 1", "description 1", 0); assertEquals("012", annotatedRule.getActionSequence());
}
RulesEngine engine = aNewRulesEngine().build(); @Test
engine.registerRule(rule1); public void annotatedRulesAndNonAnnotatedRulesShouldBeUsableTogether() throws Exception {
engine.registerRule(rule2); when(rule.evaluate()).thenReturn(true);
engine.fireRules(); rulesEngine.registerRule(rule);
rulesEngine.registerRule(annotatedRule);
assertThat(rule1.isExecuted()).isTrue(); rulesEngine.fireRules();
assertThat(rule2.isExecuted()).isFalse();
verify(rule).execute();
assertThat(annotatedRule.isExecuted()).isTrue();
} }
@After @After
@ -86,46 +102,8 @@ public class DefaultRulesEngineTest {
rulesEngine.clearRules(); rulesEngine.clearRules();
} }
class SimpleRule extends BasicRule {
/**
* Has the rule been executed? .
*/
protected boolean executed;
public SimpleRule() {
}
public SimpleRule(String name) {
super(name);
}
public SimpleRule(String name, String description) {
super(name, description);
}
public SimpleRule(String name, String description, int priority) {
super(name, description, priority);
}
@Override
public boolean evaluate() {
return true;
}
@Override
public void execute() throws Exception {
executed = true;
}
public boolean isExecuted() {
return executed;
}
}
@Rule(name = "myRule", description = "my rule description") @Rule(name = "myRule", description = "my rule description")
public class SimpleAnnotatedRule { public class AnnotatedRule {
private boolean executed; private boolean executed;
@ -133,18 +111,6 @@ public class DefaultRulesEngineTest {
@Condition @Condition
public boolean when() { public boolean when() {
return condition1() && condition2() || condition3();
}
private boolean condition1() {
return true;
}
private boolean condition2() {
return false;
}
private boolean condition3() {
return true; return true;
} }
@ -164,6 +130,11 @@ public class DefaultRulesEngineTest {
executed = true; executed = true;
} }
@Priority
public int getPriority() {
return 0;
}
public boolean isExecuted() { public boolean isExecuted() {
return executed; return executed;
} }

Loading…
Cancel
Save