[jboss-svn-commits] JBL Code SVN: r31675 - labs/jbossrules/branches/camel_jaxb_marshaller2-lucaz/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Feb 15 08:54:58 EST 2010
Author: lucazamador
Date: 2010-02-15 08:54:57 -0500 (Mon, 15 Feb 2010)
New Revision: 31675
Added:
labs/jbossrules/branches/camel_jaxb_marshaller2-lucaz/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointWithJaxbXSDModelTest.java
Log:
test with XSD Models
Added: labs/jbossrules/branches/camel_jaxb_marshaller2-lucaz/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointWithJaxbXSDModelTest.java
===================================================================
--- labs/jbossrules/branches/camel_jaxb_marshaller2-lucaz/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointWithJaxbXSDModelTest.java (rev 0)
+++ labs/jbossrules/branches/camel_jaxb_marshaller2-lucaz/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointWithJaxbXSDModelTest.java 2010-02-15 13:54:57 UTC (rev 31675)
@@ -0,0 +1,252 @@
+package org.drools.camel.component;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.xml.bind.JAXBContext;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.drools.KnowledgeBase;
+import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.ResourceType;
+import org.drools.builder.help.KnowledgeBuilderHelper;
+import org.drools.command.runtime.rule.InsertObjectCommand;
+import org.drools.io.ResourceFactory;
+import org.drools.pipeline.camel.Person;
+import org.drools.runtime.ExecutionResults;
+import org.drools.runtime.StatefulKnowledgeSession;
+import org.drools.runtime.rule.FactHandle;
+
+import com.sun.tools.xjc.Language;
+import com.sun.tools.xjc.Options;
+
+/**
+ *
+ * @author Lucas Amador
+ * @author Pablo Nussembaum
+ *
+ */
+public class CamelEndpointWithJaxbXSDModelTest extends DroolsCamelTestSupport {
+
+ private String handle;
+ private JAXBContext jaxbContext;
+
+ public void testSessionInsert() throws Exception {
+
+ String cmd = "";
+ cmd += "<batch-execution lookup='ksession1'>\n";
+ cmd += " <insert out-identifier='lucaz'>\n";
+ cmd += " <object>\n";
+ cmd += " <Person xmlns='http://drools.org/model' >\n";
+ cmd += " <name>lucaz</name>\n";
+ cmd += " <age>25</age>\n";
+ cmd += " </Person>\n";
+ cmd += " </object>\n";
+ cmd += " </insert>\n";
+ cmd += " <insert out-identifier='baunax'>\n";
+ cmd += " <object>\n";
+ cmd += " <Person xmlns='http://drools.org/model' >\n";
+ cmd += " <name>baunax</name>\n";
+ cmd += " <age>21</age>\n";
+ cmd += " </Person>\n";
+ cmd += " </object>\n";
+ cmd += " </insert>\n";
+ cmd += " <fire-all-rules />";
+ cmd += "</batch-execution>\n";
+
+ byte[] xmlResp = (byte[]) template.requestBodyAndHeader("direct:test-with-session", cmd, "jaxb-context", jaxbContext);
+ assertNotNull(xmlResp);
+ System.out.println(new String(xmlResp));
+
+ ExecutionResults resp = (ExecutionResults) jaxbContext.createUnmarshaller().unmarshal(new ByteArrayInputStream(xmlResp));
+ assertNotNull(resp);
+
+ assertEquals(2, resp.getIdentifiers().size());
+ assertNotNull(resp.getValue("lucaz"));
+ assertNotNull(resp.getValue("baunax"));
+
+ assertNotNull(resp.getFactHandle("lucaz"));
+ assertNotNull(resp.getFactHandle("baunax"));
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from("direct:test-with-session").to("drools:sm/ksession1?dataFormat=drools-jaxb");
+ from("direct:test-no-session").to("drools:sm?dataFormat=drools-jaxb");
+ }
+ };
+ }
+
+ @Override
+ protected void configureDroolsContext() {
+ Person me = new Person();
+ me.setName("Hadrian");
+
+ String rule = "";
+ rule += "package org.drools \n";
+ rule += "import org.drools.model.Person \n";
+ rule += "global java.util.List list \n";
+ rule += "query persons \n";
+ rule += " $p : Person(name != null) \n";
+ rule += "end \n";
+ rule += "query personWithName(String param)\n";
+ rule += " $p : Person(name == param) \n";
+ rule += "end \n";
+ rule += "rule rule1 \n";
+ rule += " when \n";
+ rule += " $p : Person() \n";
+ rule += " \n";
+ rule += " then \n";
+ rule += " System.out.println(\"executed\"); \n";
+ rule += "end\n";
+
+ StatefulKnowledgeSession ksession = registerKnowledgeRuntime("ksession1", rule);
+ InsertObjectCommand cmd = new InsertObjectCommand(me);
+ cmd.setOutIdentifier("camel-rider");
+ cmd.setReturnObject(false);
+ ExecutionResults results = ksession.execute(cmd);
+ handle = ((FactHandle)results.getFactHandle("camel-rider")).toExternalForm();
+ }
+
+ @Override
+ protected StatefulKnowledgeSession registerKnowledgeRuntime(String identifier, String rule) {
+ KnowledgeBuilder kbuilder = serviceManager.getKnowledgeBuilderFactoryService().newKnowledgeBuilder();
+
+ Options xjcOpts = new Options();
+ xjcOpts.setSchemaLanguage( Language.XMLSCHEMA );
+
+ String classNames[] = null;
+
+ try {
+ classNames = KnowledgeBuilderHelper.addXsdModel( ResourceFactory.newClassPathResource("person.xsd", getClass()),
+ kbuilder,
+ xjcOpts,
+ "xsd" );
+ } catch (IOException e) {
+ LOG.error("Errors while adding xsd model. ", kbuilder.getErrors());
+ }
+
+ assertFalse( kbuilder.hasErrors() );
+
+ 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());
+ }
+ }
+
+ String process1 = "";
+ process1 += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
+ process1 += "<process xmlns=\"http://drools.org/drools-5.0/process\"\n";
+ process1 += " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
+ process1 += " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n";
+ process1 += " type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n";
+ process1 += "\n";
+ process1 += " <header>\n";
+ process1 += " <imports>\n";
+ process1 += " <import name=\"org.drools.model.Person\" />\n";
+ process1 += " </imports>\n";
+ process1 += " <globals>\n";
+ process1 += " <global identifier=\"list\" type=\"java.util.List\" />\n";
+ process1 += " </globals>\n";
+ process1 += " <variables>\n";
+ process1 += " <variable name=\"person\" >\n";
+ process1 += " <type name=\"org.drools.process.core.datatype.impl.type.ObjectDataType\" className=\"Person\" />\n";
+ process1 += " </variable>\n";
+ process1 += " </variables>\n";
+ process1 += " </header>\n";
+ process1 += "\n";
+ process1 += " <nodes>\n";
+ process1 += " <start id=\"1\" name=\"Start\" />\n";
+ process1 += " <actionNode id=\"2\" name=\"MyActionNode\" >\n";
+ process1 += " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Triggered\");\n";
+// process1 += "list.add(person.name);\n";
+ process1 += "</action>\n";
+ process1 += " </actionNode>\n";
+ process1 += " <end id=\"3\" name=\"End\" />\n";
+ process1 += " </nodes>\n";
+ process1 += "\n";
+ process1 += " <connections>\n";
+ process1 += " <connection from=\"1\" to=\"2\" />\n";
+ process1 += " <connection from=\"2\" to=\"3\" />\n";
+ process1 += " </connections>\n" + "\n";
+ process1 += "</process>";
+
+ kbuilder.add(ResourceFactory.newByteArrayResource(process1.getBytes()), ResourceType.DRF);
+
+ if (kbuilder.hasErrors()) {
+ System.out.println("Errors while adding process rule 1. " + kbuilder.getErrors());
+ }
+
+ assertFalse(kbuilder.hasErrors());
+
+ String process2 = "";
+ process2 += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
+ process2 += "<process xmlns=\"http://drools.org/drools-5.0/process\"\n";
+ process2 += " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
+ process2 += " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n";
+ process2 += " type=\"RuleFlow\" name=\"flow\" id=\"org.drools.event\" package-name=\"org.drools\" version=\"1\" >\n";
+ process2 += "\n";
+ process2 += " <header>\n";
+ process2 += " <variables>\n";
+ process2 += " <variable name=\"MyVar\" >\n";
+ process2 += " <type name=\"org.drools.process.core.datatype.impl.type.StringDataType\" />\n";
+ process2 += " <value>SomeText</value>\n";
+ process2 += " </variable>\n";
+ process2 += " </variables>\n";
+ process2 += " </header>\n";
+ process2 += "\n";
+ process2 += " <nodes>\n";
+ process2 += " <start id=\"1\" name=\"Start\" />\n";
+ process2 += " <eventNode id=\"2\" name=\"Event\" variableName=\"MyVar\" >\n";
+ process2 += " <eventFilters>\n";
+ process2 += " <eventFilter type=\"eventType\" eventType=\"MyEvent\" />\n";
+ process2 += " </eventFilters>\n";
+ process2 += " </eventNode>\n";
+ process2 += " <join id=\"3\" name=\"Join\" type=\"1\" />\n";
+ process2 += " <end id=\"4\" name=\"End\" />\n";
+ process2 += " </nodes>\n";
+ process2 += "\n";
+ process2 += " <connections>\n";
+ process2 += " <connection from=\"1\" to=\"3\" />\n";
+ process2 += " <connection from=\"2\" to=\"3\" />\n";
+ process2 += " <connection from=\"3\" to=\"4\" />\n";
+ process2 += " </connections>\n";
+ process2 += "\n";
+ process2 += "</process>";
+
+ kbuilder.add(ResourceFactory.newByteArrayResource(process2.getBytes()), ResourceType.DRF);
+
+ if (kbuilder.hasErrors()) {
+ LOG.info("Errors while adding process rule 2. ", kbuilder.getErrors());
+ }
+
+ assertFalse(kbuilder.hasErrors());
+
+ KnowledgeBase kbase = serviceManager.getKnowledgeBaseFactoryService().newKnowledgeBase();
+ kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+
+ // Add object model to classes array
+ List<String> allClasses = new ArrayList<String>(Arrays.asList(classNames));
+ allClasses.add("org.drools.pipeline.camel.Person");
+
+ try {
+ jaxbContext = KnowledgeBuilderHelper.newJAXBContext( allClasses.toArray(new String[allClasses.size()]), kbase );
+ } catch (Exception e) {
+ LOG.info("Errors while creating JAXB Context. ", e);
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ }
+
+ StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
+ serviceManager.register(identifier, session);
+ return session;
+ }
+
+}
Property changes on: labs/jbossrules/branches/camel_jaxb_marshaller2-lucaz/drools-pipeline/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointWithJaxbXSDModelTest.java
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the jboss-svn-commits
mailing list