Search in sources :

Example 11 with VerifyField

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

the class FactVerifierTest method testVerifyFactExplanation.

@Test
public void testVerifyFactExplanation() throws Exception {
    Cheese f1 = new Cheese();
    f1.setType(null);
    TypeResolver typeResolver = mock(TypeResolver.class);
    HashMap<String, Object> populatedData = new HashMap<String, Object>();
    populatedData.put("f1", f1);
    // configure the mock to return the value
    Set o = Collections.singleton((Object) f1);
    when(ksession.getObjects()).thenReturn(o);
    FactVerifier factVerifier = new FactVerifier(populatedData, typeResolver, ksession, new HashMap<String, Object>());
    VerifyFact vf = new VerifyFact();
    vf.setName("f1");
    vf.getFieldValues().add(new VerifyField("type", "boo", "!="));
    factVerifier.verify(vf);
    VerifyField vfl = vf.getFieldValues().get(0);
    assertEquals("[f1] field [type] was not [boo].", vfl.getExplanation());
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) Cheese(org.drools.workbench.models.testscenarios.backend.Cheese) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Test(org.junit.Test)

Example 12 with VerifyField

use of org.drools.workbench.models.testscenarios.shared.VerifyField in project drools-wb by kiegroup.

the class TestScenarioIndexVisitor method visit.

private void visit(final Fixture fixture) {
    if (fixture instanceof FixtureList) {
        for (Fixture child : ((FixtureList) fixture)) {
            visit(child);
        }
    } else if (fixture instanceof FixturesMap) {
        for (Fixture child : ((FixturesMap) fixture).values()) {
            visit(child);
        }
    } else if (fixture instanceof FactData) {
        final FactData factData = (FactData) fixture;
        final String typeName = factData.getType();
        final String fullyQualifiedClassName = getFullyQualifiedClassName(typeName);
        ResourceReference resRef = addResourceReference(fullyQualifiedClassName, ResourceType.JAVA);
        factDataToFullyQualifiedClassNameMap.put(factData.getName(), fullyQualifiedClassName);
        for (Field field : factData.getFieldData()) {
            final String fieldName = field.getName();
            final String fieldFullyQualifiedClassName = getFieldFullyQualifiedClassName(fullyQualifiedClassName, fieldName);
            resRef.addPartReference(fieldName, PartType.FIELD);
            addResourceReference(fieldFullyQualifiedClassName, ResourceType.JAVA);
        }
    } else if (fixture instanceof VerifyFact) {
        final VerifyFact verifyFact = (VerifyFact) fixture;
        final String typeName = verifyFact.getName();
        // If VerifyFact is not anonymous lookup FQCN from previous FactData elements
        String fullyQualifiedClassName = null;
        if (!verifyFact.anonymous) {
            fullyQualifiedClassName = factDataToFullyQualifiedClassNameMap.get(verifyFact.getName());
        } else {
            fullyQualifiedClassName = getFullyQualifiedClassName(typeName);
        }
        ResourceReference resRef = null;
        if (fullyQualifiedClassName != null) {
            resRef = addResourceReference(fullyQualifiedClassName, ResourceType.JAVA);
            for (VerifyField field : verifyFact.getFieldValues()) {
                final String fieldName = field.getFieldName();
                final String fieldFullyQualifiedClassName = getFieldFullyQualifiedClassName(fullyQualifiedClassName, fieldName);
                resRef.addPartReference(fieldName, PartType.FIELD);
                addResourceReference(fieldFullyQualifiedClassName, ResourceType.JAVA);
            }
        }
    } else if (fixture instanceof VerifyRuleFired) {
        final VerifyRuleFired verifyRuleFired = (VerifyRuleFired) fixture;
        addResourceReference(verifyRuleFired.getRuleName(), ResourceType.RULE);
    }
}
Also used : Field(org.drools.workbench.models.testscenarios.shared.Field) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) ModelField(org.kie.soup.project.datamodel.oracle.ModelField) VerifyRuleFired(org.drools.workbench.models.testscenarios.shared.VerifyRuleFired) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) FactData(org.drools.workbench.models.testscenarios.shared.FactData) FixturesMap(org.drools.workbench.models.testscenarios.shared.FixturesMap) Fixture(org.drools.workbench.models.testscenarios.shared.Fixture) ResourceReference(org.kie.workbench.common.services.refactoring.ResourceReference) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) FixtureList(org.drools.workbench.models.testscenarios.shared.FixtureList)

Example 13 with VerifyField

use of org.drools.workbench.models.testscenarios.shared.VerifyField in project drools-wb by kiegroup.

the class TestScenarioFactory method makeTestScenarioWithVerifyFact.

public static Scenario makeTestScenarioWithVerifyFact(final String packageName, final Collection<Import> imports, final String name) {
    final Scenario model = new Scenario();
    model.getImports().getImports().addAll(imports);
    model.setPackageName(packageName);
    model.setName(name);
    model.getFixtures().add(new FactData("Applicant", "$a", new ArrayList<Field>() {

        {
            add(new FieldData("age", "33"));
        }
    }, false));
    model.getFixtures().add(new VerifyFact("Mortgage", new ArrayList<VerifyField>() {

        {
            add(new VerifyField("amount", "10000", "=="));
        }
    }, true));
    return model;
}
Also used : FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) FactData(org.drools.workbench.models.testscenarios.shared.FactData) ArrayList(java.util.ArrayList) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario)

Example 14 with VerifyField

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

the class FactVerifier method verify.

public void verify(VerifyFact verifyFact) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
    // Clear existing results
    for (VerifyField vf : verifyFact.getFieldValues()) {
        vf.setSuccessResult(null);
        vf.setExplanation("Fact of type [" + verifyFact.getName() + "] was not found in the results.");
    }
    if (!verifyFact.anonymous) {
        FactFieldValueVerifier fieldVerifier = new FactFieldValueVerifier(populatedData, verifyFact.getName(), getFactObject(verifyFact.getName(), populatedData, globalData), resolver);
        fieldVerifier.checkFields(verifyFact.getFieldValues());
    } else {
        for (Object object : ksession.getObjects()) {
            if (verifyFact(object, verifyFact, populatedData, resolver)) {
                return;
            }
        }
        for (VerifyField verifyField : verifyFact.getFieldValues()) {
            if (verifyField.getSuccessResult() == null) {
                verifyField.setSuccessResult(Boolean.FALSE);
                verifyField.setActualResult("No match");
            }
        }
    }
}
Also used : VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField)

Example 15 with VerifyField

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

the class ScenarioRunnerTest method testIntegrationWithModify.

@Test
public void testIntegrationWithModify() throws Exception {
    Scenario sc = new Scenario();
    sc.getImports().addImport(new Import("org.drools.workbench.models.testscenarios.backend.Cheese"));
    sc.getFixtures().add(new FactData("Cheese", "c1", Arrays.<Field>asList(new FieldData("price", "1")), false));
    sc.getFixtures().add(new ExecutionTrace());
    sc.getFixtures().add(new VerifyFact("c1", ls(new VerifyField("type", "rule1", "=="))));
    sc.getFixtures().add(new FactData("Cheese", "c1", Arrays.<Field>asList(new FieldData("price", "42")), true));
    sc.getFixtures().add(new ExecutionTrace());
    sc.getFixtures().add(new VerifyFact("c1", ls(new VerifyField("type", "rule3", "=="))));
    KieSession ksession = getKieSession("test_stateful.drl");
    ScenarioRunner run = new ScenarioRunner(ksession);
    run.run(sc);
    assertTrue(sc.wasSuccessful());
}
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) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) KieSession(org.kie.api.runtime.KieSession) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) Test(org.junit.Test)

Aggregations

VerifyField (org.drools.workbench.models.testscenarios.shared.VerifyField)23 VerifyFact (org.drools.workbench.models.testscenarios.shared.VerifyFact)20 Test (org.junit.Test)16 FactData (org.drools.workbench.models.testscenarios.shared.FactData)15 Scenario (org.drools.workbench.models.testscenarios.shared.Scenario)13 FieldData (org.drools.workbench.models.testscenarios.shared.FieldData)11 Import (org.kie.soup.project.datamodel.imports.Import)10 ExecutionTrace (org.drools.workbench.models.testscenarios.shared.ExecutionTrace)9 ArrayList (java.util.ArrayList)8 VerifyRuleFired (org.drools.workbench.models.testscenarios.shared.VerifyRuleFired)7 KieSession (org.kie.api.runtime.KieSession)7 TypeResolver (org.kie.soup.project.datamodel.commons.types.TypeResolver)7 Expectation (org.drools.workbench.models.testscenarios.shared.Expectation)6 Field (org.drools.workbench.models.testscenarios.shared.Field)6 HashMap (java.util.HashMap)5 Set (java.util.Set)5 Cheese (org.drools.workbench.models.testscenarios.backend.Cheese)5 HashSet (java.util.HashSet)2 ProjectClassLoader (org.drools.core.common.ProjectClassLoader)2 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)2