Search in sources :

Example 1 with Fixture

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

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

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

the class ScenarioTestEditorServiceImplTest method runScenarioWithDependentImports.

@Test
public void runScenarioWithDependentImports() throws Exception {
    final ArrayList<Fixture> fixtures = new ArrayList<Fixture>() {

        {
            add(factData("java.sql.ClientInfoStatus"));
        }
    };
    final Map<String, ModelField[]> modelFields = new HashMap<String, ModelField[]>() {

        {
            put("java.sql.ClientInfoStatus", new ModelField[] { modelField("java.sql.JDBCType") });
        }
    };
    when(scenario.getFixtures()).thenReturn(fixtures);
    when(dataModelService.getDataModel(path)).thenReturn(modelOracle);
    when(modelOracle.getModuleModelFields()).thenReturn(modelFields);
    when(scenario.getImports()).thenReturn(new Imports());
    testEditorService.addDependentImportsToScenario(scenario, path);
    assertEquals(2, scenario.getImports().getImports().size());
}
Also used : ModelField(org.kie.soup.project.datamodel.oracle.ModelField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Fixture(org.drools.workbench.models.testscenarios.shared.Fixture) Mockito.anyString(org.mockito.Mockito.anyString) Imports(org.kie.soup.project.datamodel.imports.Imports) Test(org.junit.Test)

Example 4 with Fixture

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

the class ScenarioTestEditorServiceImplTest method checkDependentImportsWithPrimitiveTypes.

@Test
public void checkDependentImportsWithPrimitiveTypes() throws Exception {
    final ArrayList<Fixture> fixtures = new ArrayList<>();
    final Imports imports = new Imports() {

        {
            addImport(new Import("int"));
        }
    };
    final Map<String, ModelField[]> modelFields = new HashMap<String, ModelField[]>() {

        {
            put("java.sql.ClientInfoStatus", new ModelField[] { modelField("java.sql.JDBCType") });
        }
    };
    when(scenario.getFixtures()).thenReturn(fixtures);
    when(dataModelService.getDataModel(path)).thenReturn(modelOracle);
    when(modelOracle.getModuleModelFields()).thenReturn(modelFields);
    when(scenario.getImports()).thenCallRealMethod();
    doCallRealMethod().when(scenario).setImports(any(Imports.class));
    scenario.setImports(imports);
    testEditorService.addDependentImportsToScenario(scenario, path);
    assertEquals(1, scenario.getImports().getImports().size());
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) ModelField(org.kie.soup.project.datamodel.oracle.ModelField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Fixture(org.drools.workbench.models.testscenarios.shared.Fixture) Imports(org.kie.soup.project.datamodel.imports.Imports) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.Test)

Example 5 with Fixture

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

the class ScenarioTestEditorServiceImplTest method checkSingleScenarioMultipleExecution.

@Test
public void checkSingleScenarioMultipleExecution() throws Exception {
    final ArrayList<Fixture> fixtures = new ArrayList<>();
    final Imports imports = new Imports() {

        {
            addImport(new Import("java.sql.ClientInfoStatus"));
        }
    };
    final Map<String, ModelField[]> modelFields = new HashMap<String, ModelField[]>() {

        {
            put("java.sql.ClientInfoStatus", new ModelField[] { modelField("java.sql.JDBCType") });
        }
    };
    when(scenario.getFixtures()).thenReturn(fixtures);
    when(dataModelService.getDataModel(path)).thenReturn(modelOracle);
    when(modelOracle.getModuleModelFields()).thenReturn(modelFields);
    when(scenario.getImports()).thenCallRealMethod();
    doCallRealMethod().when(scenario).setImports(any(Imports.class));
    scenario.setImports(imports);
    testEditorService.runScenario("userName", path, scenario);
    assertEquals(1, scenario.getImports().getImports().size());
    testEditorService.runScenario("userName", path, scenario);
    assertEquals(1, scenario.getImports().getImports().size());
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) ModelField(org.kie.soup.project.datamodel.oracle.ModelField) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Fixture(org.drools.workbench.models.testscenarios.shared.Fixture) Imports(org.kie.soup.project.datamodel.imports.Imports) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.Test)

Aggregations

Fixture (org.drools.workbench.models.testscenarios.shared.Fixture)12 ArrayList (java.util.ArrayList)5 FactData (org.drools.workbench.models.testscenarios.shared.FactData)5 Test (org.junit.Test)5 ModelField (org.kie.soup.project.datamodel.oracle.ModelField)5 HashMap (java.util.HashMap)4 ActivateRuleFlowGroup (org.drools.workbench.models.testscenarios.shared.ActivateRuleFlowGroup)4 ExecutionTrace (org.drools.workbench.models.testscenarios.shared.ExecutionTrace)4 Import (org.kie.soup.project.datamodel.imports.Import)4 Imports (org.kie.soup.project.datamodel.imports.Imports)4 Mockito.anyString (org.mockito.Mockito.anyString)4 FixturesMap (org.drools.workbench.models.testscenarios.shared.FixturesMap)3 RetractFact (org.drools.workbench.models.testscenarios.shared.RetractFact)3 VerifyFact (org.drools.workbench.models.testscenarios.shared.VerifyFact)3 VerifyRuleFired (org.drools.workbench.models.testscenarios.shared.VerifyRuleFired)3 CallFixtureMap (org.drools.workbench.models.testscenarios.shared.CallFixtureMap)2 CallMethod (org.drools.workbench.models.testscenarios.shared.CallMethod)2 Expectation (org.drools.workbench.models.testscenarios.shared.Expectation)2 Field (org.drools.workbench.models.testscenarios.shared.Field)2 FixtureList (org.drools.workbench.models.testscenarios.shared.FixtureList)2