[jboss-svn-commits] JBL Code SVN: r30684 - in labs/jbossrules/trunk/drools-pipeline/drools-camel: src/main/java/org/drools/camel/component and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Dec 15 13:35:51 EST 2009
Author: hzbarcea
Date: 2009-12-15 13:35:50 -0500 (Tue, 15 Dec 2009)
New Revision: 30684
Added:
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelProxyEndpointTest.java
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/DroolsCamelTestSupport.java
Removed:
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsPayload.java
Modified:
labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsComponent.java
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsConverter.java
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java
labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointTest.java
Log:
JBRULES-2347 Drools Pipeline integration with Camel
* Refactored camel component
* Added abstract test support
Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml 2009-12-15 17:06:00 UTC (rev 30683)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/pom.xml 2009-12-15 18:35:50 UTC (rev 30684)
@@ -76,6 +76,16 @@
<artifactId>xmlunit</artifactId>
<version>1.2</version>
</dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.5.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-jdk14</artifactId>
+ <version>1.5.2</version>
+ </dependency>
<dependency>
<groupId>org.apache.camel</groupId>
Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsComponent.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsComponent.java 2009-12-15 17:06:00 UTC (rev 30683)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsComponent.java 2009-12-15 18:35:50 UTC (rev 30684)
@@ -23,8 +23,14 @@
import org.drools.vsm.ServiceManager;
public class DroolsComponent extends DefaultComponent {
+ // Property name *must* follow the Camel conventions (see org.apache.camel.Exchange)
+ public static final String DROOLS_LOOKUP = "DroolsLookup";
+ public static final String DROOLS_OUT_IDENTIFIER = "DroolsOutIdentifier";
+ public static final String DROOLS_HANDLE = "DroolsHandle";
+
+ private CamelContext embeddedContext;
+ private ServiceManager serviceManager;
private String smId = "";
- private ServiceManager serviceManager;
public DroolsComponent() {
}
@@ -33,6 +39,14 @@
super(context);
}
+ public CamelContext getEmbeddedContext() {
+ return embeddedContext;
+ }
+
+ public void setEmbeddedContext(CamelContext context) {
+ embeddedContext = context;
+ }
+
public String getServiceManagerId() {
return smId;
}
@@ -49,9 +63,22 @@
serviceManager = sm;
}
+ /**
+ * There are two kinds of drools endpoints. One is the regular endpoint, one would refer two in
+ * a camel route and is the only one a user should be aware of. However such drools endpoint
+ * are actually proxies for an entire hidden route (see documentation) because of many things that
+ * have to happen within a drools context that is normally not available on a regular camel route.
+ * This kind of endpoints would set up a new drools context aware route in a separate, hidden
+ * CamelContext embedded in the DroolsComponent. The second kind of endpoint is the one
+ * referred to in such an embedded route and must have a 'pipeline' parameter set.
+ *
+ * The choice of using a pipeline parameter may be revisited. Another option would be to have the url
+ * contain a keyword something like drools:proxy://sm/ksession1.
+ */
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
- Endpoint endpoint = new DroolsEndpoint(uri, remaining, this);
+ Endpoint endpoint = parameters.containsKey("pipeline") ?
+ new DroolsEndpoint(uri, remaining, this) : new DroolsProxyEndpoint(uri, remaining, this);
setProperties(endpoint, parameters);
return endpoint;
}
Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsConverter.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsConverter.java 2009-12-15 17:06:00 UTC (rev 30683)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsConverter.java 2009-12-15 18:35:50 UTC (rev 30684)
@@ -18,44 +18,39 @@
import org.apache.camel.Converter;
import org.apache.camel.Exchange;
import org.drools.runtime.ExecutionResults;
-import org.drools.runtime.help.BatchExecutionHelper;
-import org.drools.runtime.pipeline.PipelineContext;
-import org.drools.runtime.pipeline.impl.XStreamFromXmlVsmTransformer;
-import org.drools.runtime.pipeline.impl.XStreamResolverStrategy;
-import org.drools.runtime.pipeline.impl.XStreamToXmlVsmTransformer;
import org.w3c.dom.Document;
-import com.thoughtworks.xstream.XStream;
-
@Converter
public final class DroolsConverter {
- private XStreamFromXmlVsmTransformer inTransformer;
- private XStreamToXmlVsmTransformer outTransformer;
public DroolsConverter() {
// The XStreamFromXmlVsmTransformer will throw an IllegalArgumentException if lookup is null
// this *will* be the case if the ksession is specified in the url, so we need a different
// kind of transformer there.
+ /*
inTransformer = new XStreamFromXmlVsmTransformer(new XStreamResolverStrategy() {
public XStream lookup(String name) {
return BatchExecutionHelper.newXStreamMarshaller();
}
});
outTransformer = new XStreamToXmlVsmTransformer();
+ */
}
@Converter
- public DroolsPayload toVsmPayload(Document payload, Exchange exchange) {
+ public static void toVsmPayload(Document payload, Exchange exchange) {
+ /*
PipelineContext context = (PipelineContext)exchange.getProperty(DroolsEndpoint.DROOLS_CONTEXT_PROPERTY);
// check for null context and throw CamelRuntimeException?
inTransformer.processPayload(payload, context);
// this was done in the initial example, is it really necessary? why reset the context?
exchange.setProperty(DroolsEndpoint.DROOLS_CONTEXT_PROPERTY, inTransformer.getContext());
return new DroolsPayload(inTransformer.getPayload());
+ */
}
@Converter
- public Object toXmlPayload(ExecutionResults payload, Exchange exchange) {
- return outTransformer.transform((PipelineContext)exchange.getProperty(DroolsEndpoint.DROOLS_CONTEXT_PROPERTY), payload);
+ public static Object toXmlPayload(ExecutionResults payload, Exchange exchange) {
+ return exchange;
}
}
Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java 2009-12-15 17:06:00 UTC (rev 30683)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java 2009-12-15 18:35:50 UTC (rev 30684)
@@ -26,11 +26,9 @@
import org.drools.vsm.ServiceManager;
public class DroolsEndpoint extends DefaultEndpoint {
- // Property name *must* follow the Camel conventions (see org.apache.camel.Exchange)
- public static final String DROOLS_CONTEXT_PROPERTY = "CamelDroolsContext";
- private String id;
- private String method;
+ private String ksession;
+ private String pipeline;
private CommandExecutor executor;
private ServiceManager serviceManager;
@@ -51,18 +49,22 @@
return true;
}
- public String getId() {
- return id;
+ public String getKsession() {
+ return ksession;
}
- public String getMethod() {
- return method;
+ public void setKsession(String ksession) {
+ this.ksession = ksession;
}
- public void setMethod(String method) {
- this.method = method;
+ public String getPipeline() {
+ return pipeline;
}
+ public void setPipeline(String pipeline) {
+ this.pipeline = pipeline;
+ }
+
public CommandExecutor getExecutor() {
return executor;
}
@@ -74,8 +76,8 @@
protected void configure(DroolsComponent component, String uri) {
int pos = uri.indexOf('/');
String smId = (pos < 0) ? uri : uri.substring(0, pos);
- id = (pos < 0) ? "" : uri.substring(pos + 1);
-
+ ksession = (pos < 0) ? "" : uri.substring(pos + 1);
+
if (smId.length() > 0) {
// initialize the component if needed
serviceManager = component.getServiceManager();
@@ -93,13 +95,13 @@
// make sure we deal with the same ServiceManager.
// having multiple ServiceManagers instances in the same process is not supported
throw new RuntimeCamelException("ServiceManager already initialized from id=\""
- + component.getServiceManagerId() + "\" yet current endpoint requries id=\"" + id + "\"");
+ + component.getServiceManagerId() + "\" yet current endpoint requries id=\"" + smId + "\"");
}
// if id is empty this endpoint is not attached to a CommandExecutor and will have to look it up at runtime.
- if (id.length() > 0) {
- // lookup command executor on
- executor = serviceManager.lookup(id);
+ if (ksession.length() > 0) {
+ // lookup command executor
+ executor = serviceManager.lookup(ksession);
if (executor == null) {
throw new RuntimeCamelException("Failed to instantiate DroolsEndpoint. "
+ "Lookup of CommandExecutor with id=\"" + uri + "\" failed. Check configuration.");
@@ -107,7 +109,7 @@
}
} else {
// this is a hanging entity, not attached to an SM
- executor = component.getCamelContext().getRegistry().lookup(id, CommandExecutor.class);
+ executor = component.getCamelContext().getRegistry().lookup(ksession, CommandExecutor.class);
// TODO: test this scenario...
}
Deleted: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsPayload.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsPayload.java 2009-12-15 17:06:00 UTC (rev 30683)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsPayload.java 2009-12-15 18:35:50 UTC (rev 30684)
@@ -1,35 +0,0 @@
-/*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * under the License.
- */
-
-package org.drools.camel.component;
-
-public class DroolsPayload {
- private Object value;
-
- DroolsPayload() {
- }
-
- DroolsPayload(Object value) {
- this.value = value;
- }
-
- public Object getValue() {
- return value;
- }
-
- public void setValue(Object value) {
- this.value = value;
- }
-}
Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java 2009-12-15 17:06:00 UTC (rev 30683)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java 2009-12-15 18:35:50 UTC (rev 30684)
@@ -18,66 +18,47 @@
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.RuntimeCamelException;
-import org.apache.camel.TypeConverter;
import org.apache.camel.impl.DefaultProducer;
+import org.drools.command.Command;
+import org.drools.runtime.CommandExecutor;
import org.drools.runtime.ExecutionResults;
-import org.drools.runtime.pipeline.PipelineContext;
-import org.drools.runtime.pipeline.PipelineFactory;
import org.drools.runtime.pipeline.ResultHandler;
-import org.drools.runtime.pipeline.impl.AssignObjectAsResult;
-import org.drools.runtime.pipeline.impl.ExecuteResultHandler;
-import org.drools.runtime.pipeline.impl.ExecutorStage;
-import org.drools.runtime.pipeline.impl.ServiceManagerPipelineContextImpl;
import org.drools.vsm.ServiceManager;
-import org.w3c.dom.Document;
public class DroolsProducer extends DefaultProducer {
private ServiceManager serviceManager;
- private DroolsConverter converter;
+ private CommandExecutor executor;
public DroolsProducer(Endpoint endpoint, ServiceManager serviceManager) {
super(endpoint);
this.serviceManager = serviceManager;
- converter = new DroolsConverter();
+
+ DroolsEndpoint de = (DroolsEndpoint) endpoint;
+ executor = serviceManager.lookup(de.getKsession());
}
public void process(Exchange exchange) throws Exception {
- // The method is available via getEndpoint().getMethod();
- // how should we use it here?
- // for now the default method 'execute' is used...
- ResultHandlerImpl handler = new ResultHandlerImpl();
-
- exchange.setProperty(DroolsEndpoint.DROOLS_CONTEXT_PROPERTY,
- new ServiceManagerPipelineContextImpl(serviceManager, null, handler));
-
- // TypeConverter converter = exchange.getContext().getTypeConverter();
- // DroolsPayload payload = converter.convertTo(DroolsPayload.class, exchange, body);
- DroolsPayload payload = converter.toVsmPayload(exchange.getIn().getBody(Document.class), exchange);
- // The value type inside DroolsPayload is BatchExecutionImpl. We would not need the DroolsPayload wrapper
- // if the payload would always be something like a subtype of GenericCommand.
- if (payload == null) {
- throw new RuntimeCamelException("Conversion to a drools payload type failed.");
+ CommandExecutor exec = executor;
+ if (exec == null) {
+ // need to look it up
+ String ksession = exchange.getIn().getHeader(DroolsComponent.DROOLS_LOOKUP, String.class);
+ exec = serviceManager.lookup(ksession == null ? "" : ksession);
+
+ // cannot continue if executor is not available
+ if (exec == null) {
+ throw new RuntimeCamelException("Null executor");
+ }
}
- ExecutorStage batchExecution = (ExecutorStage) PipelineFactory.newCommandExecutor();
- // only need to get the PipelineContext from the exchange property
- // if we knew it could have been changed by the converter
- PipelineContext ctx = (PipelineContext)exchange.getProperty(DroolsEndpoint.DROOLS_CONTEXT_PROPERTY);
- ExecutionResults results = batchExecution.execute(payload.getValue(), ctx);
- Object xml = converter.toXmlPayload(results, exchange);
-
- AssignObjectAsResult assignResult = (AssignObjectAsResult) PipelineFactory.newAssignObjectAsResult();
- assignResult.assignResult(ctx, xml);
- ExecuteResultHandler executeResult = (ExecuteResultHandler) PipelineFactory.newExecuteResultHandler();
- executeResult.handleResult(ctx, xml);
-
- exchange.getOut().setBody(handler.getObject());
+ Command cmd = exchange.getIn().getBody(Command.class);
+ ExecutionResults results = exec.execute(cmd);
+ exchange.getOut().setBody(results);
}
- // There are nicer ways of doint this
+ // There are nicer ways of doing this
public static class ResultHandlerImpl implements ResultHandler {
Object object;
-
+
public void handleResult(Object object) {
this.object = object;
}
Modified: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointTest.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointTest.java 2009-12-15 17:06:00 UTC (rev 30683)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointTest.java 2009-12-15 18:35:50 UTC (rev 30684)
@@ -15,93 +15,74 @@
package org.drools.camel.component;
-import java.util.Collection;
-
-import javax.naming.Context;
-
-import org.apache.camel.CamelException;
-import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
-import org.drools.KnowledgeBase;
-import org.drools.builder.KnowledgeBuilder;
-import org.drools.builder.ResourceType;
-import org.drools.definition.KnowledgePackage;
-import org.drools.io.ResourceFactory;
+import org.drools.command.CommandFactory;
+import org.drools.command.runtime.rule.GetObjectCommand;
+import org.drools.command.runtime.rule.InsertObjectCommand;
+import org.drools.common.DisconnectedFactHandle;
+import org.drools.pipeline.camel.Person;
+import org.drools.runtime.ExecutionResults;
import org.drools.runtime.StatefulKnowledgeSession;
-import org.drools.vsm.ServiceManager;
-import org.drools.vsm.local.ServiceManagerLocalClient;
+import org.drools.runtime.rule.FactHandle;
-public class CamelEndpointTest extends ContextTestSupport {
- private ServiceManager sm;
+public class CamelEndpointTest extends DroolsCamelTestSupport {
+ private String handle;
- public void testBasic() throws Exception {
- String inXml = "";
- inXml += "<batch-execution lookup=\"ksession1\">";
- inXml += " <insert out-identifier='salaboy'>";
- inXml += " <org.drools.pipeline.camel.Person>";
- inXml += " <name>salaboy</name>";
- inXml += " </org.drools.pipeline.camel.Person>";
- inXml += " </insert>";
- inXml += " <fire-all-rules />";
- inXml += "</batch-execution>";
+ public void testSessionInsert() throws Exception {
+ Person person = new Person();
+ person.setName("Mauricio");
- Object response = template.requestBody("direct:in", inXml);
-
- // Urgh, ugly stuff, but it's getting late...
- // Ideally we need an abstract test that defines the xml assert,
- // the bootstrapping, the default input message and the response
- // so the only thing left is to define the route builder with
- // various kinds of urls, testing different scenarios
- System.out.println(response);
+ InsertObjectCommand cmd = (InsertObjectCommand) CommandFactory.newInsert(person,"salaboy");
+
+ ExecutionResults response = (ExecutionResults) template.requestBody("direct:test-with-session", cmd);
+ assertTrue("Expected valid ExecutionResults object", response != null);
+ assertTrue("ExecutionResults missing expected fact", response.getFactHandle("salaboy") != null);
}
+ public void testNoSessionInsert() throws Exception {
+ Person person = new Person();
+ person.setName("Mauricio");
+
+ InsertObjectCommand cmd = (InsertObjectCommand) CommandFactory.newInsert(person,"salaboy");
+
+ ExecutionResults response = (ExecutionResults) template.requestBodyAndHeader("direct:test-no-session", cmd,
+ DroolsComponent.DROOLS_LOOKUP, "ksession1");
+ assertTrue("Expected valid ExecutionResults object", response != null);
+ assertTrue("ExecutionResults missing expected fact", response.getFactHandle("salaboy") != null);
+ }
+
+ public void testSessionGetObject() throws Exception {
+ FactHandle factHandle = new DisconnectedFactHandle(handle);
+ GetObjectCommand cmd = (GetObjectCommand) CommandFactory.newGetObject(factHandle);
+ cmd.setOutIdentifier("rider");
+
+ ExecutionResults response = (ExecutionResults) template.requestBody("direct:test-with-session", cmd);
+ assertTrue("Expected valid ExecutionResults object", response != null);
+ assertTrue("ExecutionResults missing expected object", response.getValue("rider") != null);
+ assertTrue("FactHandle object not of expected type", response.getValue("rider") instanceof Person);
+ assertEquals("Hadrian", ((Person)response.getValue("rider")).getName());
+ }
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
- from("direct:in").to("drools:sm/ksession1?method=execute");
+ from("direct:test-with-session").to("drools://sm/ksession1?pipeline=test-with-session");
+ from("direct:test-no-session").to("drools://sm?pipeline=test-no-session");
}
};
}
-
+
@Override
- protected Context createJndiContext() throws Exception {
- // Overriding this method is necessary in the absence of a spring application context
- // to bootstrap the whole thing. Create another Spring based unit test with all the beans
- // defined as below and remove this comment from here.
- Context context = super.createJndiContext();
+ protected void configureDroolsContext() {
+ Person me = new Person();
+ me.setName("Hadrian");
- String rule = "";
- rule += "package org.drools.pipeline.camel;\n" +
- "import org.drools.pipeline.camel.Person;\n" +
- "rule 'Check for Person'\n" +
- " when\n" +
- " $p: Person()\n" +
- " then\n" +
- " System.out.println(\"Person Name: \" + $p.getName());\n" +
- "end\n";
-
- sm = new ServiceManagerLocalClient();
- StatefulKnowledgeSession ksession = getVmsSessionStateful(sm, rule);
- sm.register("ksession1", ksession);
-
- context.bind("sm", sm);
- return context;
+ StatefulKnowledgeSession ksession = registerKnowledgeRuntime("ksession1", null);
+ InsertObjectCommand cmd = new InsertObjectCommand(me);
+ cmd.setOutIdentifier("camel-rider");
+ cmd.setReturnObject(false);
+ ExecutionResults results = ksession.execute(cmd);
+ handle = ((FactHandle)results.getFactHandle("camel-rider")).toExternalForm();
}
-
- private StatefulKnowledgeSession getVmsSessionStateful(ServiceManager sm, String rule) throws Exception {
- KnowledgeBuilder kbuilder = sm.getKnowledgeBuilderFactory().newKnowledgeBuilder();
- kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
-
- if (kbuilder.hasErrors()) {
- throw new CamelException(kbuilder.getErrors().toString());
- }
-
- Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
- KnowledgeBase kbase = sm.getKnowledgeBaseFactory().newKnowledgeBase();
- kbase.addKnowledgePackages(pkgs);
- StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
-
- return session;
- }
}
Added: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelProxyEndpointTest.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelProxyEndpointTest.java (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelProxyEndpointTest.java 2009-12-15 18:35:50 UTC (rev 30684)
@@ -0,0 +1,52 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package org.drools.camel.component;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.drools.command.runtime.rule.InsertObjectCommand;
+import org.drools.pipeline.camel.Person;
+import org.drools.runtime.ExecutionResults;
+import org.drools.runtime.StatefulKnowledgeSession;
+import org.drools.runtime.rule.FactHandle;
+
+public class CamelProxyEndpointTest extends DroolsCamelTestSupport {
+ private String handle;
+
+ public void testSessionInsert() throws Exception {
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from("direct:test-no-marshal").to("drools://sm/ksession1");
+ }
+ };
+ }
+
+ @Override
+ protected void configureDroolsContext() {
+ Person me = new Person();
+ me.setName("Hadrian");
+
+ StatefulKnowledgeSession ksession = registerKnowledgeRuntime("ksession1", null);
+ InsertObjectCommand cmd = new InsertObjectCommand(me);
+ cmd.setOutIdentifier("camel-rider");
+ cmd.setReturnObject(false);
+ ExecutionResults results = ksession.execute(cmd);
+ handle = ((FactHandle)results.getFactHandle("camel-rider")).toExternalForm();
+ }
+}
Added: labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/DroolsCamelTestSupport.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/DroolsCamelTestSupport.java (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/DroolsCamelTestSupport.java 2009-12-15 18:35:50 UTC (rev 30684)
@@ -0,0 +1,104 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package org.drools.camel.component;
+
+import java.util.Collection;
+
+import javax.naming.Context;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.custommonkey.xmlunit.Diff;
+import org.custommonkey.xmlunit.XMLAssert;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
+import org.drools.KnowledgeBase;
+import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.ResourceType;
+import org.drools.definition.KnowledgePackage;
+import org.drools.io.ResourceFactory;
+import org.drools.runtime.KnowledgeRuntime;
+import org.drools.runtime.StatefulKnowledgeSession;
+import org.drools.vsm.ServiceManager;
+import org.drools.vsm.local.ServiceManagerLocalClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class DroolsCamelTestSupport extends ContextTestSupport {
+ private static final Logger LOG = LoggerFactory.getLogger(DroolsCamelTestSupport.class);
+ private ServiceManager serviceManager;
+
+ public void setServiceManager(ServiceManager serviceManager) {
+ this.serviceManager = serviceManager;
+ }
+
+ public ServiceManager getServiceManager() {
+ return serviceManager;
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ XMLUnit.setIgnoreComments(true);
+ XMLUnit.setIgnoreWhitespace(true);
+ XMLUnit.setIgnoreAttributeOrder(true);
+ XMLUnit.setNormalizeWhitespace(true);
+ XMLUnit.setNormalize(true);
+ }
+
+ @Override
+ protected Context createJndiContext() throws Exception {
+ // Overriding this method is necessary in the absence of a spring application context
+ // to bootstrap the whole thing. Create another Spring based unit test with all the beans
+ // defined as below and remove this comment from here.
+ Context context = super.createJndiContext();
+
+ serviceManager = new ServiceManagerLocalClient();
+ context.bind("sm", serviceManager);
+
+ configureDroolsContext();
+ return context;
+ }
+
+ protected abstract void configureDroolsContext();
+
+ protected StatefulKnowledgeSession registerKnowledgeRuntime(String identifier, String rule) {
+ KnowledgeBuilder kbuilder = serviceManager.getKnowledgeBuilderFactory().newKnowledgeBuilder();
+
+ if (rule != null && rule.length() > 0) {
+ kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
+
+ if (kbuilder.hasErrors()) {
+ LOG.info("Errors while adding rule. ", kbuilder.getErrors());
+ }
+ }
+ assertFalse(kbuilder.hasErrors());
+ Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
+ KnowledgeBase kbase = serviceManager.getKnowledgeBaseFactory().newKnowledgeBase();
+
+ kbase.addKnowledgePackages(pkgs);
+ StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
+ serviceManager.register(identifier, session);
+ return session;
+ }
+
+ protected void assertXMLEqual(String expected, String result) throws Exception {
+ Diff diff = new Diff(expected, result);
+ diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
+ XMLAssert.assertXMLEqual(diff, true);
+ }
+}
More information about the jboss-svn-commits
mailing list