Search in sources :

Example 1 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver 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 TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactAssignmentFieldPopulatorTest method testSimpleFields.

@Test
public void testSimpleFields() throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    TypeResolver typeResolver = new ClassTypeResolver(new HashSet<String>(), classLoader);
    typeResolver.addImport("org.drools.workbench.models.testscenarios.backend.Cheese");
    Mouse mouse = new Mouse();
    FactAssignmentField factAssignmentField = new FactAssignmentField("cheese", "Cheese");
    factAssignmentField.getFact().getFieldData().add(new FieldData("type", "Best cheddar EVER! (tm)"));
    FactAssignmentFieldPopulator factAssignmentFieldPopulator = new FactAssignmentFieldPopulator(mouse, factAssignmentField, typeResolver);
    factAssignmentFieldPopulator.populate(new HashMap<String, Object>());
    assertEquals("Best cheddar EVER! (tm)", mouse.getCheese().getType());
}
Also used : FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) Mouse(org.drools.workbench.models.testscenarios.backend.Mouse) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) FactAssignmentField(org.drools.workbench.models.testscenarios.shared.FactAssignmentField) Test(org.junit.Test)

Example 3 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactAssignmentFieldPopulatorTest method testFactAssignmentField.

@Test
public void testFactAssignmentField() throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    TypeResolver typeResolver = new ClassTypeResolver(new HashSet<String>(), classLoader);
    typeResolver.addImport("org.drools.workbench.models.testscenarios.backend.Cheese");
    Mouse mouse = new Mouse();
    FactAssignmentField factAssignmentField = new FactAssignmentField("cheese", "Cheese");
    FactAssignmentFieldPopulator factAssignmentFieldPopulator = new FactAssignmentFieldPopulator(mouse, factAssignmentField, typeResolver);
    factAssignmentFieldPopulator.populate(new HashMap<String, Object>());
    assertNotNull(mouse.getCheese());
}
Also used : Mouse(org.drools.workbench.models.testscenarios.backend.Mouse) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) FactAssignmentField(org.drools.workbench.models.testscenarios.shared.FactAssignmentField) Test(org.junit.Test)

Example 4 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactPopulatorTest method testPopulateNestedWrongOrder.

@Test
public void testPopulateNestedWrongOrder() throws Exception {
    TypeResolver typeResolver = getTypeResolver();
    FactData outerFactData = new FactData("OuterFact", "p1", Arrays.<Field>asList(new FieldData("name", "mic"), new FieldData("innerFact", "=c1")), false);
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, outerFactData));
    FactData cheeseFactData = new FactData("Cheese", "c1", Arrays.<Field>asList(new FieldData("type", "cheddar"), new FieldData("price", "42")), false);
    factPopulator.add(new NewFactPopulator(populatedData, typeResolver, cheeseFactData));
    factPopulator.populate();
    assertTrue(populatedData.containsKey("c1"));
    assertTrue(populatedData.containsKey("p1"));
    OuterFact o = (OuterFact) populatedData.get("p1");
    assertEquals(populatedData.get("c1"), o.getInnerFact());
}
Also used : CollectionFieldData(org.drools.workbench.models.testscenarios.shared.CollectionFieldData) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) FactData(org.drools.workbench.models.testscenarios.shared.FactData) OuterFact(org.drools.workbench.models.testscenarios.backend.OuterFact) Test(org.junit.Test)

Example 5 with TypeResolver

use of org.kie.soup.project.datamodel.commons.types.TypeResolver in project drools by kiegroup.

the class FactPopulatorTest method getTypeResolver.

private TypeResolver getTypeResolver() {
    TypeResolver resolver = new ClassTypeResolver(new HashSet<String>(), getClassLoader());
    resolver.addImport("org.drools.workbench.models.testscenarios.backend.MyCollectionWrapper");
    resolver.addImport("org.drools.workbench.models.testscenarios.backend.Cheesery");
    resolver.addImport("org.drools.workbench.models.testscenarios.backend.Cheese");
    resolver.addImport("org.drools.workbench.models.testscenarios.backend.SqlDateWrapper");
    resolver.addImport("org.drools.workbench.models.testscenarios.backend.CheeseType");
    resolver.addImport("org.drools.workbench.models.testscenarios.backend.Person");
    resolver.addImport("org.drools.workbench.models.testscenarios.backend.OuterFact");
    return resolver;
}
Also used : ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)

Aggregations

TypeResolver (org.kie.soup.project.datamodel.commons.types.TypeResolver)24 Test (org.junit.Test)16 ClassTypeResolver (org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)14 FieldData (org.drools.workbench.models.testscenarios.shared.FieldData)8 Cheese (org.drools.workbench.models.testscenarios.backend.Cheese)7 FactData (org.drools.workbench.models.testscenarios.shared.FactData)7 VerifyField (org.drools.workbench.models.testscenarios.shared.VerifyField)7 HashMap (java.util.HashMap)6 VerifyFact (org.drools.workbench.models.testscenarios.shared.VerifyFact)6 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 CollectionFieldData (org.drools.workbench.models.testscenarios.shared.CollectionFieldData)5 Field (org.drools.workbench.models.testscenarios.shared.Field)4 HashSet (java.util.HashSet)3 FactAssignmentField (org.drools.workbench.models.testscenarios.shared.FactAssignmentField)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 Cheesery (org.drools.workbench.models.testscenarios.backend.Cheesery)2 Mouse (org.drools.workbench.models.testscenarios.backend.Mouse)2 OuterFact (org.drools.workbench.models.testscenarios.backend.OuterFact)2 KieSession (org.kie.api.runtime.KieSession)2