Search in sources :

Example 6 with Expectation

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

the class ScenarioRunner method applyFixtures.

private void applyFixtures(final List<Fixture> fixtures, final ScenarioSettings scenarioSettings) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InvalidClockTypeException {
    for (Iterator<Fixture> iterator = fixtures.iterator(); iterator.hasNext(); ) {
        Fixture fixture = iterator.next();
        if (fixture instanceof FactData) {
            factPopulator.add(factPopulatorFactory.createFactPopulator((FactData) fixture));
        } else if (fixture instanceof RetractFact) {
            factPopulator.retractFact(((RetractFact) fixture).getName());
        } else if (fixture instanceof CallMethod) {
            workingMemoryWrapper.executeMethod((CallMethod) fixture);
        } else if (fixture instanceof ActivateRuleFlowGroup) {
            workingMemoryWrapper.activateRuleFlowGroup(((ActivateRuleFlowGroup) fixture).getName());
        } else if (fixture instanceof ExecutionTrace) {
            factPopulator.populate();
            workingMemoryWrapper.executeSubScenario((ExecutionTrace) fixture, scenarioSettings);
        } else if (fixture instanceof Expectation) {
            factPopulator.populate();
            workingMemoryWrapper.verifyExpectation((Expectation) fixture);
        } else {
            throw new IllegalArgumentException("Not sure what to do with " + fixture);
        }
    }
    factPopulator.populate();
}
Also used : FactData(org.drools.workbench.models.testscenarios.shared.FactData) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) Fixture(org.drools.workbench.models.testscenarios.shared.Fixture) ActivateRuleFlowGroup(org.drools.workbench.models.testscenarios.shared.ActivateRuleFlowGroup) Expectation(org.drools.workbench.models.testscenarios.shared.Expectation) RetractFact(org.drools.workbench.models.testscenarios.shared.RetractFact) CallMethod(org.drools.workbench.models.testscenarios.shared.CallMethod)

Example 7 with Expectation

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

the class ScenarioRunnerTest method testIntegrationWithFailure.

@Test
public void testIntegrationWithFailure() 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"));
    Expectation[] assertions = populateScenarioForFailure(sc);
    KieSession ksession = getKieSession("test_rules2.drl");
    ScenarioRunner run = new ScenarioRunner(ksession);
    run.run(sc);
    assertFalse(sc.wasSuccessful());
    VerifyFact vf = (VerifyFact) assertions[1];
    assertFalse((vf.getFieldValues().get(0)).getSuccessResult());
    assertEquals("XXX", vf.getFieldValues().get(0).getExpected());
    assertEquals("rule1", vf.getFieldValues().get(0).getActualResult());
    assertNotNull(vf.getFieldValues().get(0).getExplanation());
    VerifyRuleFired vr = (VerifyRuleFired) assertions[4];
    assertFalse(vr.getSuccessResult());
    assertEquals(2, vr.getExpectedCount().intValue());
    assertEquals(0, vr.getActualResult().intValue());
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) VerifyRuleFired(org.drools.workbench.models.testscenarios.shared.VerifyRuleFired) KieSession(org.kie.api.runtime.KieSession) Expectation(org.drools.workbench.models.testscenarios.shared.Expectation) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) Test(org.junit.Test)

Example 8 with Expectation

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

the class ScenarioRunnerTest method testIntegrationWithDeclaredTypes.

@Test
public void testIntegrationWithDeclaredTypes() throws Exception {
    Scenario scenario = new Scenario();
    scenario.getImports().addImport(new Import("foo.bar.Coolness"));
    FactData[] facts = new FactData[] { new FactData("Coolness", "c", Arrays.<Field>asList(new FieldData("num", "42"), new FieldData("name", "mic")), false) };
    scenario.getFixtures().addAll(Arrays.asList(facts));
    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("test_rules3.drl");
    ClassLoader cl = ((KnowledgeBaseImpl) ksession.getKieBase()).getRootClassLoader();
    HashSet<String> imports = new HashSet<String>();
    imports.add("foo.bar.*");
    assertNotNull(cl.loadClass("foo.bar.Coolness"));
    ClassLoader cl_ = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(cl);
    // 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 run = new ScenarioRunner(ksession);
    run.run(scenario);
    assertEquals(1, executionTrace.getNumberOfRulesFired().intValue());
    assertTrue(scenario.wasSuccessful());
    Thread.currentThread().setContextClassLoader(cl_);
}
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) 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) FactData(org.drools.workbench.models.testscenarios.shared.FactData) ProjectClassLoader(org.drools.core.common.ProjectClassLoader) KieSession(org.kie.api.runtime.KieSession) Expectation(org.drools.workbench.models.testscenarios.shared.Expectation) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Expectation (org.drools.workbench.models.testscenarios.shared.Expectation)8 FactData (org.drools.workbench.models.testscenarios.shared.FactData)7 VerifyFact (org.drools.workbench.models.testscenarios.shared.VerifyFact)7 ExecutionTrace (org.drools.workbench.models.testscenarios.shared.ExecutionTrace)6 Scenario (org.drools.workbench.models.testscenarios.shared.Scenario)6 VerifyField (org.drools.workbench.models.testscenarios.shared.VerifyField)6 VerifyRuleFired (org.drools.workbench.models.testscenarios.shared.VerifyRuleFired)6 Test (org.junit.Test)6 Import (org.kie.soup.project.datamodel.imports.Import)6 FieldData (org.drools.workbench.models.testscenarios.shared.FieldData)5 KieSession (org.kie.api.runtime.KieSession)5 ArrayList (java.util.ArrayList)4 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 Fixture (org.drools.workbench.models.testscenarios.shared.Fixture)2 Date (java.util.Date)1 InternalAgendaGroup (org.drools.core.common.InternalAgendaGroup)1 CallMethod (org.drools.workbench.models.testscenarios.shared.CallMethod)1