Search in sources :

Example 1 with VerifyRuleFired

use of org.drools.workbench.models.testscenarios.shared.VerifyRuleFired in project drools by kiegroup.

the class ScenarioRunnerTest method testIntegrationInfiniteLoop.

@Test
public void testIntegrationInfiniteLoop() throws Exception {
    Scenario sc = new Scenario();
    sc.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Cheese"));
    sc.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Person"));
    FactData[] facts = new FactData[] { new FactData("Cheese", "c1", Arrays.<Field>asList(new FieldData("type", "cheddar"), new FieldData("price", "42")), false) };
    sc.getGlobals().add(new FactData("Person", "p", new ArrayList(), false));
    sc.getFixtures().addAll(Arrays.asList(facts));
    ExecutionTrace executionTrace = new ExecutionTrace();
    sc.getRules().add("rule1");
    sc.getRules().add("rule2");
    sc.setInclusive(true);
    sc.getFixtures().add(executionTrace);
    Expectation[] assertions = new Expectation[5];
    assertions[0] = new VerifyFact("c1", ls(new VerifyField("type", "cheddar", "==")));
    assertions[1] = new VerifyFact("p", ls(new VerifyField("name", "rule1", "=="), new VerifyField("status", "rule2", "==")));
    assertions[2] = new VerifyRuleFired("rule1", 1, null);
    assertions[3] = new VerifyRuleFired("rule2", 1, null);
    assertions[4] = new VerifyRuleFired("rule3", 0, null);
    sc.getFixtures().addAll(Arrays.asList(assertions));
    KieSession ksession = getKieSession("test_rules_infinite_loop.drl");
    ScenarioRunner run = new ScenarioRunner(ksession);
    run.run(sc);
    assertEquals(sc.getMaxRuleFirings(), executionTrace.getNumberOfRulesFired().intValue());
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) VerifyRuleFired(org.drools.workbench.models.testscenarios.shared.VerifyRuleFired) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) ArrayList(java.util.ArrayList) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) FactData(org.drools.workbench.models.testscenarios.shared.FactData) KieSession(org.kie.api.runtime.KieSession) Expectation(org.drools.workbench.models.testscenarios.shared.Expectation) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Test(org.junit.Test)

Example 2 with VerifyRuleFired

use of org.drools.workbench.models.testscenarios.shared.VerifyRuleFired in project drools by kiegroup.

the class ScenarioRunnerTest method testRuleFlowGroupActivation.

@Test
public void testRuleFlowGroupActivation() throws Exception {
    Scenario scenario = new Scenario();
    scenario.getImports().addImport(new Import("foo.bar.Coolness"));
    Fixture[] given = new Fixture[] { new FactData("Coolness", "c", Arrays.<Field>asList(new FieldData("num", "42"), new FieldData("name", "mic")), false) };
    scenario.getFixtures().addAll(Arrays.asList(given));
    ExecutionTrace executionTrace = new ExecutionTrace();
    scenario.getRules().add("rule1");
    scenario.setInclusive(true);
    scenario.getFixtures().add(executionTrace);
    Expectation[] assertions = new Expectation[2];
    assertions[0] = new VerifyFact("c", ls(new VerifyField("num", "42", "==")));
    assertions[1] = new VerifyRuleFired("rule1", 1, null);
    scenario.getFixtures().addAll(Arrays.asList(assertions));
    KieSession ksession = getKieSession("rule_flow_actication.drl");
    ClassLoader classLoader = ((KnowledgeBaseImpl) ksession.getKieBase()).getRootClassLoader();
    HashSet<String> imports = new HashSet<String>();
    imports.add("foo.bar.*");
    TypeResolver resolver = new ClassTypeResolver(imports, classLoader);
    Class<?> coolnessClass = classLoader.loadClass("foo.bar.Coolness");
    assertNotNull(coolnessClass);
    ClassLoader cl_ = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(classLoader);
    // resolver will need to have generated beans in it - possibly using a composite classloader from the package,
    // including whatever CL has the generated beans...
    ScenarioRunner scenarioRunner = new ScenarioRunner(ksession);
    scenarioRunner.run(scenario);
    assertEquals(0, executionTrace.getNumberOfRulesFired().intValue());
    assertFalse(scenario.wasSuccessful());
    // Activate rule flow
    scenario.getFixtures().clear();
    given = new Fixture[] { new FactData("Coolness", "c", Arrays.<Field>asList(new FieldData("num", "42"), new FieldData("name", "mic")), false), new ActivateRuleFlowGroup("asdf") };
    scenario.getFixtures().addAll(Arrays.asList(given));
    scenario.getFixtures().add(executionTrace);
    ((InternalAgendaGroup) ksession.getAgenda().getRuleFlowGroup("asdf")).setAutoDeactivate(false);
    scenarioRunner = new ScenarioRunner(ksession);
    scenarioRunner.run(scenario);
    assertTrue(scenario.wasSuccessful());
    Thread.currentThread().setContextClassLoader(cl_);
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) ActivateRuleFlowGroup(org.drools.workbench.models.testscenarios.shared.ActivateRuleFlowGroup) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) Field(org.drools.workbench.models.testscenarios.shared.Field) FactData(org.drools.workbench.models.testscenarios.shared.FactData) ProjectClassLoader(org.drools.core.common.ProjectClassLoader) KieSession(org.kie.api.runtime.KieSession) Fixture(org.drools.workbench.models.testscenarios.shared.Fixture) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) HashSet(java.util.HashSet) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) VerifyRuleFired(org.drools.workbench.models.testscenarios.shared.VerifyRuleFired) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) Expectation(org.drools.workbench.models.testscenarios.shared.Expectation) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) Test(org.junit.Test)

Example 3 with VerifyRuleFired

use of org.drools.workbench.models.testscenarios.shared.VerifyRuleFired in project drools by kiegroup.

the class ScenarioRunnerTest method populateScenarioForFailure.

private Expectation[] populateScenarioForFailure(Scenario sc) {
    FactData[] facts = new FactData[] { new FactData("Cheese", "c1", Arrays.<Field>asList(new FieldData("type", "cheddar"), new FieldData("price", "42")), false) };
    sc.getFixtures().addAll(Arrays.asList(facts));
    sc.getGlobals().add(new FactData("Person", "p", new ArrayList(), false));
    ExecutionTrace executionTrace = new ExecutionTrace();
    sc.getRules().add("rule1");
    sc.getRules().add("rule2");
    sc.setInclusive(true);
    sc.getFixtures().add(executionTrace);
    Expectation[] assertions = new Expectation[5];
    assertions[0] = new VerifyFact("c1", ls(new VerifyField("type", "cheddar", "==")));
    assertions[1] = new VerifyFact("p", ls(new VerifyField("name", "XXX", "=="), new VerifyField("status", "rule2", "==")));
    assertions[2] = new VerifyRuleFired("rule1", 1, null);
    assertions[3] = new VerifyRuleFired("rule2", 1, null);
    assertions[4] = new VerifyRuleFired("rule3", 2, null);
    sc.getFixtures().addAll(Arrays.asList(assertions));
    return assertions;
}
Also used : FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) VerifyRuleFired(org.drools.workbench.models.testscenarios.shared.VerifyRuleFired) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) FactData(org.drools.workbench.models.testscenarios.shared.FactData) ArrayList(java.util.ArrayList) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) Expectation(org.drools.workbench.models.testscenarios.shared.Expectation) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact)

Example 4 with VerifyRuleFired

use of org.drools.workbench.models.testscenarios.shared.VerifyRuleFired in project drools by kiegroup.

the class ScenarioRunnerTest method testIntegrationWithSuccess.

/**
 * Do a kind of end to end test with some real rules.
 */
@Test
public void testIntegrationWithSuccess() throws Exception {
    Scenario sc = new Scenario();
    sc.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Cheese"));
    sc.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Person"));
    FactData[] facts = new FactData[] { new FactData("Cheese", "c1", Arrays.<Field>asList(new FieldData("type", "cheddar"), new FieldData("price", "42")), false) };
    sc.getGlobals().add(new FactData("Person", "p", new ArrayList(), false));
    sc.getFixtures().addAll(Arrays.asList(facts));
    ExecutionTrace executionTrace = new ExecutionTrace();
    sc.getRules().add("rule1");
    sc.getRules().add("rule2");
    sc.setInclusive(true);
    sc.getFixtures().add(executionTrace);
    Expectation[] assertions = new Expectation[5];
    assertions[0] = new VerifyFact("c1", ls(new VerifyField("type", "cheddar", "==")));
    assertions[1] = new VerifyFact("p", ls(new VerifyField("name", "rule1", "=="), new VerifyField("status", "rule2", "==")));
    assertions[2] = new VerifyRuleFired("rule1", 1, null);
    assertions[3] = new VerifyRuleFired("rule2", 1, null);
    assertions[4] = new VerifyRuleFired("rule3", 0, null);
    sc.getFixtures().addAll(Arrays.asList(assertions));
    KieSession ksession = getKieSession("test_rules2.drl");
    ScenarioRunner run = new ScenarioRunner(ksession);
    run.run(sc);
    assertEquals(2, executionTrace.getNumberOfRulesFired().intValue());
    assertTrue(sc.wasSuccessful());
    Thread.sleep(50);
    assertTrue((new Date()).after(sc.getLastRunResult()));
    assertTrue(executionTrace.getExecutionTimeResult() != null);
    assertTrue(executionTrace.getRulesFired().length > 0);
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) VerifyRuleFired(org.drools.workbench.models.testscenarios.shared.VerifyRuleFired) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) ArrayList(java.util.ArrayList) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) Date(java.util.Date) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) FactData(org.drools.workbench.models.testscenarios.shared.FactData) KieSession(org.kie.api.runtime.KieSession) Expectation(org.drools.workbench.models.testscenarios.shared.Expectation) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Test(org.junit.Test)

Example 5 with VerifyRuleFired

use of org.drools.workbench.models.testscenarios.shared.VerifyRuleFired in project drools by kiegroup.

the class ScenarioTest method testInsertBetween.

@Test
public void testInsertBetween() {
    Scenario sc = new Scenario();
    VerifyRuleFired vf = new VerifyRuleFired();
    sc.insertBetween(null, vf);
    assertEquals(1, sc.getFixtures().size());
    assertEquals(vf, sc.getFixtures().get(0));
    VerifyRuleFired vf2 = new VerifyRuleFired();
    sc.getFixtures().add(vf2);
    VerifyRuleFired vf3 = new VerifyRuleFired();
    sc.insertBetween(vf, vf3);
    assertEquals(3, sc.getFixtures().size());
    assertEquals(vf, sc.getFixtures().get(0));
    assertEquals(vf2, sc.getFixtures().get(1));
    assertEquals(vf3, sc.getFixtures().get(2));
    VerifyRuleFired vf4 = new VerifyRuleFired();
    sc.insertBetween(vf2, vf4);
    assertEquals(4, sc.getFixtures().size());
    assertEquals(3, sc.getFixtures().indexOf(vf4));
    assertEquals(2, sc.getFixtures().indexOf(vf3));
    // assertEquals(vf4, sc.fixtures.get(3));
    assertEquals(1, sc.getFixtures().indexOf(vf2));
    // assertEquals(vf2, sc.fixtures.get(2));
    assertEquals(0, sc.getFixtures().indexOf(vf));
    VerifyRuleFired vf5 = new VerifyRuleFired();
    sc.insertBetween(null, vf5);
    assertEquals(5, sc.getFixtures().size());
    assertEquals(4, sc.getFixtures().indexOf(vf5));
    sc = new Scenario();
    sc.getFixtures().add(vf);
    ExecutionTrace ex = new ExecutionTrace();
    sc.getFixtures().add(ex);
    sc.insertBetween(null, vf2);
    assertEquals(0, sc.getFixtures().indexOf(vf));
    assertEquals(1, sc.getFixtures().indexOf(vf2));
    assertEquals(2, sc.getFixtures().indexOf(ex));
    assertEquals(3, sc.getFixtures().size());
    sc.insertBetween(ex, vf3);
    assertEquals(4, sc.getFixtures().size());
    assertEquals(0, sc.getFixtures().indexOf(vf));
    assertEquals(1, sc.getFixtures().indexOf(vf2));
    assertEquals(2, sc.getFixtures().indexOf(ex));
    assertEquals(3, sc.getFixtures().indexOf(vf3));
    ExecutionTrace ex2 = new ExecutionTrace();
    sc.getFixtures().add(ex2);
    sc.insertBetween(ex, vf4);
    assertEquals(6, sc.getFixtures().size());
    assertEquals(0, sc.getFixtures().indexOf(vf));
    assertEquals(1, sc.getFixtures().indexOf(vf2));
    assertEquals(2, sc.getFixtures().indexOf(ex));
    assertEquals(3, sc.getFixtures().indexOf(vf3));
    assertEquals(4, sc.getFixtures().indexOf(vf4));
    assertEquals(5, sc.getFixtures().indexOf(ex2));
    sc.insertBetween(ex2, vf5);
    assertEquals(7, sc.getFixtures().size());
    assertEquals(0, sc.getFixtures().indexOf(vf));
    assertEquals(1, sc.getFixtures().indexOf(vf2));
    assertEquals(2, sc.getFixtures().indexOf(ex));
    assertEquals(3, sc.getFixtures().indexOf(vf3));
    assertEquals(4, sc.getFixtures().indexOf(vf4));
    assertEquals(5, sc.getFixtures().indexOf(ex2));
    assertEquals(6, sc.getFixtures().indexOf(vf5));
    sc = new Scenario();
    sc.getFixtures().add(ex);
    sc.insertBetween(null, vf);
    assertEquals(2, sc.getFixtures().size());
    assertEquals(0, sc.getFixtures().indexOf(vf));
    assertEquals(1, sc.getFixtures().indexOf(ex));
}
Also used : VerifyRuleFired(org.drools.workbench.models.testscenarios.shared.VerifyRuleFired) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) Test(org.junit.Test)

Aggregations

VerifyRuleFired (org.drools.workbench.models.testscenarios.shared.VerifyRuleFired)16 Test (org.junit.Test)11 FactData (org.drools.workbench.models.testscenarios.shared.FactData)9 Scenario (org.drools.workbench.models.testscenarios.shared.Scenario)9 VerifyFact (org.drools.workbench.models.testscenarios.shared.VerifyFact)9 ExecutionTrace (org.drools.workbench.models.testscenarios.shared.ExecutionTrace)8 VerifyField (org.drools.workbench.models.testscenarios.shared.VerifyField)7 Expectation (org.drools.workbench.models.testscenarios.shared.Expectation)6 FieldData (org.drools.workbench.models.testscenarios.shared.FieldData)5 KieSession (org.kie.api.runtime.KieSession)5 Import (org.kie.soup.project.datamodel.imports.Import)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 Fixture (org.drools.workbench.models.testscenarios.shared.Fixture)3 HashSet (java.util.HashSet)2 ProjectClassLoader (org.drools.core.common.ProjectClassLoader)2 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)2 ActivateRuleFlowGroup (org.drools.workbench.models.testscenarios.shared.ActivateRuleFlowGroup)2 Field (org.drools.workbench.models.testscenarios.shared.Field)2 FixtureList (org.drools.workbench.models.testscenarios.shared.FixtureList)2