[jboss-svn-commits] JBL Code SVN: r36025 - in labs/jbossrules/trunk/drools-camel/src: test/java/org/drools/camel/component and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Nov 22 18:18:01 EST 2010


Author: tirelli
Date: 2010-11-22 18:18:01 -0500 (Mon, 22 Nov 2010)
New Revision: 36025

Added:
   labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsExecuteProducer.java
   labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsInsertProducer.java
   labs/jbossrules/trunk/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointActionInsertTest.java
Removed:
   labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java
Modified:
   labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java
Log:
JBRULES-2804: adding support to insert action on drools endpoint

Modified: labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java
===================================================================
--- labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java	2010-11-22 17:22:32 UTC (rev 36024)
+++ labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsEndpoint.java	2010-11-22 23:18:01 UTC (rev 36025)
@@ -52,16 +52,55 @@
 
 public class DroolsEndpoint extends DefaultEndpoint {
 
-    public String          ksessionId;
-    public CommandExecutor executor;
-    public GridNode        node;
+    /**
+     * An ENUM to define which action should be executed by the 
+     * producer into this end point
+     */
+    public static enum Action {
+        EXECUTE("execute"),
+        INSERT_BODY("insertBody"),
+        INSERT_MESSAGE("insertMessage"),
+        INSERT_EXCHANGE("insertExchange");
 
-    public String          dataFormatName;
+        private final String id;
 
-    public DataFormat      dataFormat;
+        Action(String id) {
+            this.id = id;
+        }
 
-    public static Pattern  p = Pattern.compile( "[\"']?lookup[\"']?\\s*[:=]\\s*[\"']([^\"']+)[\"']" );
+        public String getId() {
+            return this.id;
+        }
 
+        public static Action resolveAction(String id) {
+            if ( EXECUTE.getId().equalsIgnoreCase( id ) ) {
+                return EXECUTE;
+            } else if ( INSERT_BODY.getId().equalsIgnoreCase( id ) ) {
+                return INSERT_BODY;
+            } else if ( INSERT_MESSAGE.getId().equalsIgnoreCase( id ) ) {
+                return INSERT_MESSAGE;
+            } else if ( INSERT_EXCHANGE.getId().equalsIgnoreCase( id ) ) {
+                return INSERT_EXCHANGE;
+            } else {
+                throw new IllegalArgumentException( "Invalid action configuring EndPoint = " + id );
+            }
+        }
+    }
+
+    public static final Pattern p          = Pattern.compile( "[\"']?lookup[\"']?\\s*[:=]\\s*[\"']([^\"']+)[\"']" );
+
+    public String               ksessionId;
+    public CommandExecutor      executor;
+    public GridNode             node;
+
+    public String               dataFormatName;
+
+    public DataFormat           dataFormat;
+
+    public Action               action     = Action.EXECUTE;
+    public String               entryPoint = null;
+    public String               channel    = null;
+
     public DroolsEndpoint(String endpointUri,
                           String remaining,
                           DroolsComponent component) throws URISyntaxException {
@@ -76,8 +115,13 @@
     }
 
     public Producer createProducer() throws Exception {
-        return new DroolsProducer( this,
-                                   node );
+        if ( Action.EXECUTE.equals( action ) ) {
+            return new DroolsExecuteProducer( this,
+                                              node );
+        } else {
+            return new DroolsInsertProducer( this,
+                                             node );
+        }
     }
 
     public boolean isSingleton() {
@@ -210,4 +254,33 @@
 
         return cl;
     }
-}
+
+    public Action getAction() {
+        return action;
+    }
+
+    public void setAction(Action action) {
+        this.action = action;
+    }
+
+    public void setAction(String action) {
+        this.action = Action.resolveAction( action );
+    }
+
+    public String getEntryPoint() {
+        return entryPoint;
+    }
+
+    public void setEntryPoint(String entryPoint) {
+        this.entryPoint = entryPoint;
+    }
+
+    public String getChannel() {
+        return channel;
+    }
+
+    public void setChannel(String channel) {
+        this.channel = channel;
+    }
+
+}
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsExecuteProducer.java
===================================================================
--- labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsExecuteProducer.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsExecuteProducer.java	2010-11-22 23:18:01 UTC (rev 36025)
@@ -0,0 +1,103 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * 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.
+ */
+
+/*
+ *  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.Arrays;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.impl.DefaultProducer;
+import org.drools.command.Command;
+import org.drools.command.impl.GenericCommand;
+import org.drools.command.runtime.BatchExecutionCommandImpl;
+import org.drools.core.util.StringUtils;
+import org.drools.grid.GridNode;
+import org.drools.runtime.CommandExecutor;
+import org.drools.runtime.ExecutionResults;
+
+public class DroolsExecuteProducer extends DefaultProducer {
+
+    DroolsEndpoint de;
+
+    public DroolsExecuteProducer(Endpoint endpoint,
+                                 GridNode node) {
+        super( endpoint );
+        de = (DroolsEndpoint) endpoint;
+    }
+
+    public void process(Exchange exchange) throws Exception {
+
+        Command cmd = exchange.getIn().getBody( Command.class );
+
+        if ( cmd == null ) {
+            throw new RuntimeCamelException( "Body of in message not of the expected type 'org.drools.command.Command' for uri" + de.getEndpointUri() );
+        }
+
+        if ( !(cmd instanceof BatchExecutionCommandImpl) ) {
+            cmd = new BatchExecutionCommandImpl( Arrays.asList( new GenericCommand< ? >[]{(GenericCommand) cmd} ) );
+        }
+
+        CommandExecutor exec;
+        ExecutionNodePipelineContextImpl droolsContext = exchange.getProperty( "drools-context",
+                                                                                   ExecutionNodePipelineContextImpl.class );
+        if ( droolsContext != null ) {
+            exec = droolsContext.getCommandExecutor();
+        } else {
+            exec = de.getExecutor();
+            if ( exec == null ) {
+                String lookup = exchange.getIn().getHeader( DroolsComponent.DROOLS_LOOKUP,
+                                                                String.class );
+                if ( StringUtils.isEmpty( lookup ) && (cmd instanceof BatchExecutionCommandImpl) ) {
+                    lookup = ((BatchExecutionCommandImpl) cmd).getLookup();
+                }
+
+                if ( de.getGridNode() != null && !StringUtils.isEmpty( lookup ) ) {
+                    exec = de.getGridNode().get( lookup,
+                                                     CommandExecutor.class );
+                    if ( exec == null ) {
+                        throw new RuntimeException( "ExecutionNode is unable to find ksession=" + lookup + " for uri" + de.getEndpointUri() );
+                    }
+                } else {
+                    throw new RuntimeException( "No ExecutionNode, unable to find ksession=" + lookup + " for uri" + de.getEndpointUri() );
+                }
+            }
+        }
+
+        if ( exec == null ) {
+            throw new RuntimeException( "No defined ksession for uri" + de.getEndpointUri() );
+        }
+
+        ExecutionResults results = exec.execute( (BatchExecutionCommandImpl) cmd );;
+        exchange.getOut().setBody( results );
+    }
+}

Added: labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsInsertProducer.java
===================================================================
--- labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsInsertProducer.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsInsertProducer.java	2010-11-22 23:18:01 UTC (rev 36025)
@@ -0,0 +1,190 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * 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.
+ */
+
+/*
+ *  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.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultProducer;
+import org.drools.grid.GridNode;
+import org.drools.runtime.CommandExecutor;
+import org.drools.runtime.StatefulKnowledgeSession;
+import org.drools.runtime.StatelessKnowledgeSession;
+import org.drools.runtime.rule.WorkingMemoryEntryPoint;
+
+/**
+ * A producer that inserts incoming messages as facts into the
+ * Drools session.
+ * 
+ * It can be configured to insert the message body only, or the
+ * whole message (that includes headers) or even the whole Exchange 
+ * object.
+ * 
+ * @author etirelli
+ *
+ */
+public class DroolsInsertProducer extends DefaultProducer {
+
+    // the corresponding endpoint
+    private DroolsEndpoint de;
+    // the actual insert is executed by a worker class that 
+    // implements the GoF strategy pattern to avoid conditionals
+    // at insert time and hopefully improve performance
+    private InsertWorker   worker;
+
+    public DroolsInsertProducer(Endpoint endpoint,
+                                GridNode node) {
+        super( endpoint );
+        de = (DroolsEndpoint) endpoint;
+
+        // Configures this Producer with the proper action
+        // by composing strategy objects
+        DroolsEndpoint.Action action = de.getAction();
+        Unwrapper unwrapper = null;
+        switch ( action ) {
+            case INSERT_BODY :
+                unwrapper = BodyUnwrapper.INSTANCE;
+                break;
+            case INSERT_MESSAGE :
+                unwrapper = MessageUnwrapper.INSTANCE;
+                break;
+            case INSERT_EXCHANGE :
+                unwrapper = ExchangeUnwrapper.INSTANCE;
+                break;
+        }
+
+        // Creates the actual worker
+        CommandExecutor exec = de.getExecutor();
+        if ( exec instanceof StatefulKnowledgeSession ) {
+            WorkingMemoryEntryPoint wmep;
+            String ep = de.getEntryPoint();
+            if ( ep != null ) {
+                wmep = ((StatefulKnowledgeSession) exec).getWorkingMemoryEntryPoint( ep );
+            } else {
+                wmep = (WorkingMemoryEntryPoint) exec;
+            }
+            worker = new StatefulSessionInsertWorker( wmep,
+                                                      unwrapper );
+        } else {
+            worker = new StatelessSessionInsertWorker( (StatelessKnowledgeSession) exec,
+                                                       unwrapper );
+        }
+    }
+
+    public void process(Exchange exchange) throws Exception {
+        worker.process( exchange );
+    }
+
+    /**
+     * An interface for the worker strategy
+     */
+    private static interface InsertWorker {
+        public void process(Exchange exchange) throws Exception;
+    }
+
+    /**
+     * A stateful implementation for the worker
+     */
+    private static class StatefulSessionInsertWorker
+        implements
+        InsertWorker {
+        private WorkingMemoryEntryPoint wmep;
+        private Unwrapper               unwrapper;
+
+        public StatefulSessionInsertWorker(WorkingMemoryEntryPoint wmep,
+                                           Unwrapper unwrapper) {
+            this.wmep = wmep;
+            this.unwrapper = unwrapper;
+        }
+
+        public void process(Exchange exchange) throws Exception {
+            this.wmep.insert( unwrapper.getObject( exchange ) );
+        }
+    }
+
+    /**
+     * A stateless implementation for the worker
+     */
+    private static class StatelessSessionInsertWorker
+        implements
+        InsertWorker {
+        private StatelessKnowledgeSession ksession;
+        private Unwrapper                 unwrapper;
+
+        public StatelessSessionInsertWorker(StatelessKnowledgeSession ksession,
+                                            Unwrapper unwrapper) {
+            this.ksession = ksession;
+            this.unwrapper = unwrapper;
+        }
+
+        public void process(Exchange exchange) throws Exception {
+            this.ksession.execute( unwrapper.getObject( exchange ) );
+        }
+    }
+
+    /**
+     * Another strategy interface to properly process incoming objects
+     * selecting between body, message or exchange
+     */
+    private static interface Unwrapper {
+        public Object getObject(Exchange exchange);
+    }
+
+    private static class BodyUnwrapper
+        implements
+        Unwrapper {
+        public static final BodyUnwrapper INSTANCE = new BodyUnwrapper();
+
+        public Object getObject(Exchange exchange) {
+            return exchange.getIn().getBody();
+        }
+    }
+
+    private static class MessageUnwrapper
+        implements
+        Unwrapper {
+        public static final MessageUnwrapper INSTANCE = new MessageUnwrapper();
+
+        public Object getObject(Exchange exchange) {
+            return exchange.getIn();
+        }
+    }
+
+    private static class ExchangeUnwrapper
+        implements
+        Unwrapper {
+        public static final ExchangeUnwrapper INSTANCE = new ExchangeUnwrapper();
+
+        public Object getObject(Exchange exchange) {
+            return exchange;
+        }
+    }
+}


Property changes on: labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsInsertProducer.java
___________________________________________________________________
Name: svn:executable
   + *

Deleted: labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java
===================================================================
--- labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java	2010-11-22 17:22:32 UTC (rev 36024)
+++ labs/jbossrules/trunk/drools-camel/src/main/java/org/drools/camel/component/DroolsProducer.java	2010-11-22 23:18:01 UTC (rev 36025)
@@ -1,105 +0,0 @@
-/**
- * Copyright 2010 JBoss Inc
- *
- * 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.
- */
-
-/*
- *  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.Arrays;
-
-import org.apache.camel.Endpoint;
-import org.apache.camel.Exchange;
-import org.apache.camel.RuntimeCamelException;
-import org.apache.camel.impl.DefaultProducer;
-import org.drools.command.Command;
-import org.drools.command.impl.GenericCommand;
-import org.drools.command.runtime.BatchExecutionCommandImpl;
-import org.drools.core.util.StringUtils;
-import org.drools.grid.GridNode;
-import org.drools.runtime.CommandExecutor;
-import org.drools.runtime.ExecutionResults;
-import org.drools.runtime.impl.ExecutionResultImpl;
-
-public class DroolsProducer extends DefaultProducer {
-
-    DroolsEndpoint de;
-
-    public DroolsProducer(Endpoint endpoint,
-                          GridNode node) {
-        super( endpoint );
-        de = (DroolsEndpoint) endpoint;
-    }
-
-    public void process(Exchange exchange) throws Exception {
-
-            Command cmd = exchange.getIn().getBody( Command.class );
-            
-            
-            if ( cmd == null ) {
-                throw new RuntimeCamelException( "Body of in message not of the expected type 'org.drools.command.Command' for uri" + de.getEndpointUri() );
-            }
-
-            if ( !(cmd instanceof BatchExecutionCommandImpl) ) {
-                cmd = new BatchExecutionCommandImpl( Arrays.asList( new GenericCommand< ? >[]{(GenericCommand) cmd} ) );
-            }
-
-            CommandExecutor exec;
-            ExecutionNodePipelineContextImpl droolsContext = exchange.getProperty( "drools-context",
-                                                                                   ExecutionNodePipelineContextImpl.class );
-            if ( droolsContext != null ) {
-                exec = droolsContext.getCommandExecutor();
-            } else {
-                exec = de.getExecutor();
-                if ( exec == null ) {
-                    String lookup = exchange.getIn().getHeader( DroolsComponent.DROOLS_LOOKUP,
-                                                                String.class );
-                    if ( StringUtils.isEmpty( lookup ) && (cmd instanceof BatchExecutionCommandImpl) ) {
-                        lookup = ((BatchExecutionCommandImpl) cmd).getLookup();
-                    }
-
-                    if ( de.getGridNode() != null && !StringUtils.isEmpty( lookup ) ) {
-                        exec = de.getGridNode().get( lookup,
-                                                     CommandExecutor.class );
-                        if ( exec == null ) {
-                            throw new RuntimeException( "ExecutionNode is unable to find ksession=" + lookup + " for uri" + de.getEndpointUri() );
-                        }
-                    } else {
-                        throw new RuntimeException( "No ExecutionNode, unable to find ksession=" + lookup + " for uri" + de.getEndpointUri() );
-                    }
-                }
-            }
-
-            if ( exec == null ) {
-                throw new RuntimeException( "No defined ksession for uri" + de.getEndpointUri() );
-            }
-            
-            ExecutionResults results = exec.execute( (BatchExecutionCommandImpl) cmd );;
-            exchange.getOut().setBody( results );     
-    }
-}

Added: labs/jbossrules/trunk/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointActionInsertTest.java
===================================================================
--- labs/jbossrules/trunk/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointActionInsertTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointActionInsertTest.java	2010-11-22 23:18:01 UTC (rev 36025)
@@ -0,0 +1,171 @@
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * 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.
+ */
+
+/*
+ *  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 javax.naming.Context;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.drools.event.rule.ActivationCreatedEvent;
+import org.drools.event.rule.AgendaEventListener;
+import org.drools.event.rule.ObjectInsertedEvent;
+import org.drools.event.rule.WorkingMemoryEventListener;
+import org.drools.pipeline.camel.Person;
+import org.drools.runtime.StatefulKnowledgeSession;
+import org.mockito.ArgumentCaptor;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.CoreMatchers.is;
+
+public class CamelEndpointActionInsertTest extends DroolsCamelTestSupport {
+    private StatefulKnowledgeSession ksession;
+    private AgendaEventListener ael;
+    private WorkingMemoryEventListener wmel;
+
+    public void testSessionInsert() throws Exception {
+        Person person = new Person();
+        person.setName( "Bob" );
+
+        template.sendBody( "direct:test-no-ep", person );
+        
+        ArgumentCaptor<ObjectInsertedEvent> oie = ArgumentCaptor.forClass( ObjectInsertedEvent.class );
+        ArgumentCaptor<ActivationCreatedEvent> ace = ArgumentCaptor.forClass( ActivationCreatedEvent.class );
+        
+        verify( wmel ).objectInserted( oie.capture() );
+        assertThat( (Person) oie.getValue().getObject(), is( person ) );
+        
+        verify( ael ).activationCreated( ace.capture() );
+        assertThat( ace.getValue().getActivation().getRule().getName(), is("rule1") );
+    }
+
+    public void testSessionInsertEntryPoint() throws Exception {
+        Person person = new Person();
+        person.setName( "Bob" );
+
+        template.sendBody( "direct:test-with-ep", person );
+        
+        ArgumentCaptor<ObjectInsertedEvent> oie = ArgumentCaptor.forClass( ObjectInsertedEvent.class );
+        ArgumentCaptor<ActivationCreatedEvent> ace = ArgumentCaptor.forClass( ActivationCreatedEvent.class );
+        
+        verify( wmel ).objectInserted( oie.capture() );
+        assertThat( (Person) oie.getValue().getObject(), is( person ) );
+        
+        verify( ael ).activationCreated( ace.capture() );
+        assertThat( ace.getValue().getActivation().getRule().getName(), is("rule2") );
+    }
+
+    public void testSessionInsertMessage() throws Exception {
+        Person person = new Person();
+        person.setName( "Bob" );
+
+        template.sendBody( "direct:test-message", person );
+        
+        ArgumentCaptor<ObjectInsertedEvent> oie = ArgumentCaptor.forClass( ObjectInsertedEvent.class );
+        ArgumentCaptor<ActivationCreatedEvent> ace = ArgumentCaptor.forClass( ActivationCreatedEvent.class );
+        
+        verify( wmel ).objectInserted( oie.capture() );
+        assertThat( (Person) ((Message) oie.getValue().getObject()).getBody(), is( person ) );
+        
+        verify( ael ).activationCreated( ace.capture() );
+        assertThat( ace.getValue().getActivation().getRule().getName(), is("rule3") );
+    }
+
+    public void testSessionInsertExchange() throws Exception {
+        Person person = new Person();
+        person.setName( "Bob" );
+
+        template.sendBody( "direct:test-exchange", person );
+        
+        ArgumentCaptor<ObjectInsertedEvent> oie = ArgumentCaptor.forClass( ObjectInsertedEvent.class );
+        ArgumentCaptor<ActivationCreatedEvent> ace = ArgumentCaptor.forClass( ActivationCreatedEvent.class );
+        
+        verify( wmel ).objectInserted( oie.capture() );
+        assertThat( (Person) ((Exchange) oie.getValue().getObject()).getIn().getBody(), is( person ) );
+        
+        verify( ael ).activationCreated( ace.capture() );
+        assertThat( ace.getValue().getActivation().getRule().getName(), is("rule4") );
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from( "direct:test-no-ep" ).to( "drools://node/ksession1?action=insertBody" );
+                from( "direct:test-with-ep" ).to( "drools://node/ksession1?action=insertBody&entryPoint=ep1" );
+                from( "direct:test-message" ).to( "drools://node/ksession1?action=insertMessage" );
+                from( "direct:test-exchange" ).to( "drools://node/ksession1?action=insertExchange" );
+            }
+        };
+    }
+
+    @Override
+    protected void configureDroolsContext(Context jndiContext) {
+        String rule = "";
+        rule += "package org.drools.pipeline.camel \n";
+        rule += "import org.apache.camel.Exchange \n";
+        rule += "import org.apache.camel.Message \n";
+        rule += "rule rule1 \n";
+        rule += "  when \n";
+        rule += "    $p : Person() \n";
+        rule += "  then \n";
+        rule += "    # no-op \n";
+        rule += "end\n";
+        rule += "rule rule2 \n";
+        rule += "  when \n";
+        rule += "    $p : Person() from entry-point ep1 \n";
+        rule += "  then \n";
+        rule += "    # no-op \n";
+        rule += "end\n";
+        rule += "rule rule3 \n";
+        rule += "  when \n";
+        rule += "    $m : Message() \n";
+        rule += "  then \n";
+        rule += "    # no-op \n";
+        rule += "end\n";
+        rule += "rule rule4 \n";
+        rule += "  when \n";
+        rule += "    $e : Exchange() \n";
+        rule += "  then \n";
+        rule += "    # no-op \n";
+        rule += "end\n";
+
+        ksession = registerKnowledgeRuntime( "ksession1",
+                                             rule );
+        ael = mock( AgendaEventListener.class );
+        wmel = mock( WorkingMemoryEventListener.class );
+        ksession.addEventListener( ael );
+        ksession.addEventListener( wmel );
+    }
+}


Property changes on: labs/jbossrules/trunk/drools-camel/src/test/java/org/drools/camel/component/CamelEndpointActionInsertTest.java
___________________________________________________________________
Name: svn:executable
   + *



More information about the jboss-svn-commits mailing list