Search in sources :

Example 6 with FactData

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

the class ScenarioRunnerTest method testWithGlobalList.

/**
 * Check if global list is empty.
 */
@Test
public void testWithGlobalList() throws Exception {
    Scenario sc = new Scenario();
    sc.getImports().addImport(new Import("java.util.List"));
    ArrayList testList = new ArrayList();
    sc.getGlobals().add(new FactData("List", "testList", testList, false));
    Expectation[] assertions = new Expectation[2];
    assertions[0] = new VerifyFact("testList", ls(new VerifyField("empty", "true", "==")));
    assertions[1] = new VerifyFact("testList", ls(new VerifyField("size", "0", "==")));
    sc.getFixtures().addAll(Arrays.asList(assertions));
    ScenarioRunner run = new ScenarioRunner(ksession);
    run.run(sc);
    verify(ksession).setGlobal("testList", testList);
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) FactData(org.drools.workbench.models.testscenarios.shared.FactData) ArrayList(java.util.ArrayList) 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 7 with FactData

use of org.drools.workbench.models.testscenarios.shared.FactData 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 8 with FactData

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

the class ScenarioRunnerTest method testVerifyFactsWithEnum.

@Test
public void testVerifyFactsWithEnum() throws Exception {
    FieldData fieldData = new FieldData("cheeseType", "CheeseType.CHEDDAR");
    fieldData.setNature(FieldData.TYPE_ENUM);
    FactData cheeseFactData = new FactData("Cheese", "c1", Arrays.<Field>asList(fieldData), false);
    FieldData cheeseType = new FieldData("cheeseType", "CheeseType.CHEDDAR");
    cheeseType.setNature(FieldData.TYPE_ENUM);
    FactData f1 = new FactData("Cheese", "f1", Arrays.<Field>asList(cheeseType), false);
    Scenario scenario = new Scenario();
    scenario.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Cheese"));
    scenario.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.CheeseType"));
    scenario.getFixtures().add(cheeseFactData);
    scenario.getFixtures().add(f1);
    ScenarioRunner runner = new ScenarioRunner(ksession);
    VerifyFact vf = new VerifyFact();
    vf.setName("f1");
    VerifyField verifyField = new VerifyField("cheeseType", "CheeseType.CHEDDAR", "==");
    verifyField.setNature(VerifyField.TYPE_ENUM);
    vf.setFieldValues(ls(verifyField));
    scenario.getFixtures().add(vf);
    runner.run(scenario);
    for (VerifyField field : vf.getFieldValues()) {
        assertTrue(field.getSuccessResult());
    }
}
Also used : FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) Import(org.kie.soup.project.datamodel.imports.Import) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) FactData(org.drools.workbench.models.testscenarios.shared.FactData) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) Test(org.junit.Test)

Example 9 with FactData

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

the class ScenarioRunnerTest method testVerifyFacts.

@Test
public void testVerifyFacts() throws Exception {
    Scenario scenario = new Scenario();
    scenario.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Cheese"));
    scenario.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Person"));
    ScenarioRunner runner = new ScenarioRunner(ksession);
    scenario.getFixtures().add(new FactData("Cheese", "f1", Arrays.<Field>asList(new FieldData("type", "cheddar"), new FieldData("price", "42")), false));
    scenario.getFixtures().add(new FactData("Person", "f2", Arrays.<Field>asList(new FieldData("name", "michael"), new FieldData("age", "33")), false));
    // test all true
    VerifyFact verifyCheddar = new VerifyFact();
    verifyCheddar.setName("f1");
    verifyCheddar.setFieldValues(asList(new VerifyField("type", "cheddar", "=="), new VerifyField("price", "42", "==")));
    scenario.getFixtures().add(verifyCheddar);
    VerifyFact michaelVerifyFact = new VerifyFact();
    michaelVerifyFact.setName("f2");
    michaelVerifyFact.setFieldValues(asList(new VerifyField("name", "michael", "=="), new VerifyField("age", "33", "==")));
    scenario.getFixtures().add(michaelVerifyFact);
    // test one false
    VerifyFact markVerifyFact = new VerifyFact();
    markVerifyFact.setName("f2");
    markVerifyFact.setFieldValues(asList(new VerifyField("name", "mark", "=="), new VerifyField("age", "33", "==")));
    scenario.getFixtures().add(markVerifyFact);
    // test 2 false
    VerifyFact mark2VerifyFact = new VerifyFact();
    mark2VerifyFact.setName("f2");
    mark2VerifyFact.setFieldValues(asList(new VerifyField("name", "mark", "=="), new VerifyField("age", "32", "==")));
    scenario.getFixtures().add(mark2VerifyFact);
    runner.run(scenario);
    for (VerifyField verifyField : verifyCheddar.getFieldValues()) {
        assertTrue(verifyField.getSuccessResult());
    }
    for (VerifyField verifyField : michaelVerifyFact.getFieldValues()) {
        assertTrue(verifyField.getSuccessResult());
    }
    assertFalse((markVerifyFact.getFieldValues().get(0)).getSuccessResult());
    assertTrue((markVerifyFact.getFieldValues().get(1)).getSuccessResult());
    assertEquals("michael", markVerifyFact.getFieldValues().get(0).getActualResult());
    assertEquals("mark", markVerifyFact.getFieldValues().get(0).getExpected());
    assertFalse((mark2VerifyFact.getFieldValues().get(0)).getSuccessResult());
    assertFalse((mark2VerifyFact.getFieldValues().get(1)).getSuccessResult());
    assertEquals("michael", mark2VerifyFact.getFieldValues().get(0).getActualResult());
    assertEquals("mark", mark2VerifyFact.getFieldValues().get(0).getExpected());
    assertEquals("33", mark2VerifyFact.getFieldValues().get(1).getActualResult());
    assertEquals("32", mark2VerifyFact.getFieldValues().get(1).getExpected());
}
Also used : FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) Field(org.drools.workbench.models.testscenarios.shared.Field) Import(org.kie.soup.project.datamodel.imports.Import) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) FactData(org.drools.workbench.models.testscenarios.shared.FactData) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) Test(org.junit.Test)

Example 10 with FactData

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

the class ScenarioTest method testIsFactNameUsed.

@Test
public void testIsFactNameUsed() {
    Scenario sc = new Scenario();
    sc.getGlobals().add(new FactData("X", "x", null, false));
    sc.getFixtures().add(new FactData("Q", "q", null, false));
    sc.getFixtures().add(new ExecutionTrace());
    assertTrue(sc.isFactNameReserved("x"));
    assertTrue(sc.isFactNameReserved("q"));
    assertFalse(sc.isFactNameReserved("w"));
    sc = new Scenario();
    assertFalse(sc.isFactNameReserved("w"));
}
Also used : FactData(org.drools.workbench.models.testscenarios.shared.FactData) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) Test(org.junit.Test)

Aggregations

FactData (org.drools.workbench.models.testscenarios.shared.FactData)49 Test (org.junit.Test)36 FieldData (org.drools.workbench.models.testscenarios.shared.FieldData)29 Scenario (org.drools.workbench.models.testscenarios.shared.Scenario)24 VerifyFact (org.drools.workbench.models.testscenarios.shared.VerifyFact)19 ArrayList (java.util.ArrayList)17 ExecutionTrace (org.drools.workbench.models.testscenarios.shared.ExecutionTrace)16 VerifyField (org.drools.workbench.models.testscenarios.shared.VerifyField)15 CollectionFieldData (org.drools.workbench.models.testscenarios.shared.CollectionFieldData)13 Field (org.drools.workbench.models.testscenarios.shared.Field)12 Import (org.kie.soup.project.datamodel.imports.Import)11 VerifyRuleFired (org.drools.workbench.models.testscenarios.shared.VerifyRuleFired)9 KieSession (org.kie.api.runtime.KieSession)8 Cheese (org.drools.workbench.models.testscenarios.backend.Cheese)7 Expectation (org.drools.workbench.models.testscenarios.shared.Expectation)7 ClassTypeResolver (org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)7 TypeResolver (org.kie.soup.project.datamodel.commons.types.TypeResolver)7 RetractFact (org.drools.workbench.models.testscenarios.shared.RetractFact)6 Fixture (org.drools.workbench.models.testscenarios.shared.Fixture)5 HashMap (java.util.HashMap)3