camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Hehl <t...@heavyweightsoftware.com>
Subject Re: Handling the reply of a request in InOut mode
Date Tue, 28 Jun 2016 19:32:05 GMT
package com.xxxx.inout;
import java.util.concurrent.TimeUnit;

import org.apache.activemq.junit.EmbeddedActiveMQBroker;
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Rule;
import org.junit.Test;

public class JavaRequestResponseTest extends CamelTestSupport {
     @Rule
     public EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker();

     Processor requestProcessor = new Processor() {
         @Override
         public void process(Exchange exchange) throws Exception {
             String body = exchange.getIn().getBody(String.class);
             exchange.getIn().setBody("Request: " + body);

             return;
         }
     };

     Processor resultProcessor = new Processor() {
         @Override
         public void process(Exchange exchange) throws Exception {
             String body = exchange.getIn().getBody(String.class);
             System.err.println("Body:" + body);
             if ( body.startsWith("Response") ) {
                 exchange.getIn().setBody( "We're good");
             } else {
                 exchange.getIn().setBody( "Bad response");
             }
             return;
         }
     };

     @EndpointInject( uri = "mock://result")
     MockEndpoint result;

     @Override
     protected JndiRegistry createRegistry() throws Exception {
         JndiRegistry registry = super.createRegistry();

         ActiveMQComponent activeMQComponent = new ActiveMQComponent();
         activeMQComponent.setBrokerURL( broker.getVmURL() );

         registry.bind( "test-broker", activeMQComponent);

         return registry;
     }

     @Override
     protected RouteBuilder[] createRouteBuilders() throws Exception {
         RouteBuilder builders[] = new RouteBuilder[2];

         builders[1] = new RouteBuilder() {
             @Override
             public void configure() throws Exception {
from("file://data?fileName=some-file.txt&noop=true")
                         .id( "file-consumer")
                         .log("Processing ${file:name}")
                         .process(requestProcessor)
                         .log("Generated ${body}")
                         .to(ExchangePattern.InOut, 
"test-broker://queue:testQueue")
                         .process(resultProcessor)
                         .to( "mock://result")
                 ;
             }
         };

         builders[0] = new RouteBuilder() {
             @Override
             public void configure() throws Exception {
                 from( "test-broker://queue:testQueue")
                         .id( "dummey-response-generator")
                         .setBody().constant( "Response Message")
                         .to(ExchangePattern.InOnly, 
"test-broker://queue:dummy-queue")
                 ;

             }
         };

         return builders;
     }

     @Test
     public void testRoute() throws Exception {
         result.expectedBodiesReceived("We're good");

         assertMockEndpointsSatisfied(5, TimeUnit.SECONDS);
     }

}


On 6/28/2016 2:51 PM, Quinn Stevenson wrote:
> Hmmm…
>
> Can you share the test you’re using?
>
>> On Jun 28, 2016, at 12:45 PM, Thomas Hehl <thom@heavyweightsoftware.com> wrote:
>>
>> My results:
>>
>> java.lang.AssertionError: mock://result Received message count. Expected: <1>
but was: <0>
>>     at org.apache.camel.component.mock.MockEndpoint.fail(MockEndpoint.java:1394)
>>     at org.apache.camel.component.mock.MockEndpoint.assertEquals(MockEndpoint.java:1376)
>>     at org.apache.camel.component.mock.MockEndpoint.doAssertIsSatisfied(MockEndpoint.java:399)
>>     at org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied(MockEndpoint.java:378)
>>     at org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied(MockEndpoint.java:366)
>>     at org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied(MockEndpoint.java:230)
>>     at org.apache.camel.test.junit4.CamelTestSupport.assertMockEndpointsSatisfied(CamelTestSupport.java:682)
>>     at com.logisticare.inout.JavaRequestResponseTest.testRoute(JavaRequestResponseTest.java:98)
>>     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>>     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>>     at java.lang.reflect.Method.invoke(Method.java:497)
>>     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
>>     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
>>     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
>>     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
>>     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
>>     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
>>     at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
>>     at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
>>     at org.junit.rules.RunRules.evaluate(RunRules.java:20)
>>     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
>>     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
>>     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
>>     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
>>     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
>>     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
>>     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
>>     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
>>     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
>>     at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
>>     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
>>     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>>     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
>>     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
>>     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
>>     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
>>
>>
>>
>> On 6/28/2016 2:12 PM, Quinn Stevenson wrote:
>>> The assertions in the test verify I’m getting the response and not the original
request.
>>>
>>> First, I setup and embedded ActiveMQ broker using the JUnit Rule so I have a
real broker to test with.
>>>
>>> Then I create two RouteBuilders in the createRouteBuilders method.   One RouteBuilder
reads the file, makes the JMS request-response call and processes the result  - basically
the route you posted initially, but with a mock endpoint at the end to make verification of
the result easier.  The other RouteBuilder is a stub for the process that receives the JMS
Messages and generates the JMS response - it just sets the response to “Response Message”
- no real logic in this one.
>>>
>>> The second Processor looks at the response, and if it starts with “Response”,
it sets the body to “We’re good”.  Otherwise it sets the body to “Bad Message”.
>>>
>>> Finally, the test method sets the assertion for the expected body (i.e. “We’re
good”).
>>>
>>> Does that make sense?
>>>
>>>> On Jun 28, 2016, at 11:06 AM, Thomas Hehl <thom@heavyweightsoftware.com>
wrote:
>>>>
>>>> Just trying to get the response from the message.
>>>>
>>>> I'm still getting errors in developer studio on your blueprint, but I simply
changed the file outside and am running it with your unit test successfully.
>>>>
>>>> Now, explain to me how your unit test proves that the response coming back
is being processed? I see that you're using InOut and that is working for me, but the problem
is the response body.
>>>>
>>>> What's coming into process is the message I'm sending into the queue. I need
to know how to process the response. Did I miss it?
>>>>
>>>> Thanks!
>>>>
>>>>
>>>> On 6/28/2016 12:44 PM, Quinn Stevenson wrote:
>>>>> I’m not sure exactly what you’re trying to do with the processors,
but here’s a simple unit test that works for me.  You can mess with the processors and make
them act more like your real route.
>>>>>
>>>>> package com.pronoia.camel;
>>>>>
>>>>> import java.util.concurrent.TimeUnit;
>>>>>
>>>>> import org.apache.activemq.camel.component.ActiveMQComponent;
>>>>> import org.apache.activemq.junit.EmbeddedActiveMQBroker;
>>>>> import org.apache.camel.EndpointInject;
>>>>> import org.apache.camel.Exchange;
>>>>> import org.apache.camel.ExchangePattern;
>>>>> import org.apache.camel.Processor;
>>>>> import org.apache.camel.RoutesBuilder;
>>>>> import org.apache.camel.builder.RouteBuilder;
>>>>> import org.apache.camel.component.mock.MockEndpoint;
>>>>> import org.apache.camel.impl.JndiRegistry;
>>>>> import org.apache.camel.test.junit4.CamelTestSupport;
>>>>> import org.junit.Rule;
>>>>> import org.junit.Test;
>>>>>
>>>>> public class JavaRequestResponseTest extends CamelTestSupport {
>>>>>      @Rule
>>>>>      public EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker();
>>>>>
>>>>>      Processor requestProcessor = new Processor() {
>>>>>          @Override
>>>>>          public void process(Exchange exchange) throws Exception {
>>>>>              String body = exchange.getIn().getBody(String.class);
>>>>>              exchange.getIn().setBody("Request: " + body);
>>>>>
>>>>>              return;
>>>>>          }
>>>>>      };
>>>>>
>>>>>      Processor resultProcessor = new Processor() {
>>>>>          @Override
>>>>>          public void process(Exchange exchange) throws Exception {
>>>>>              String body = exchange.getIn().getBody(String.class);
>>>>>              if ( body.startsWith("Response") ) {
>>>>>                  exchange.getIn().setBody( "We're good");
>>>>>              } else {
>>>>>                  exchange.getIn().setBody( "Bad response");
>>>>>              }
>>>>>              return;
>>>>>          }
>>>>>      };
>>>>>
>>>>>      @EndpointInject( uri = "mock://result")
>>>>>      MockEndpoint result;
>>>>>
>>>>>      @Override
>>>>>      protected JndiRegistry createRegistry() throws Exception {
>>>>>          JndiRegistry registry = super.createRegistry();
>>>>>
>>>>>          ActiveMQComponent activeMQComponent = new ActiveMQComponent();
>>>>>          activeMQComponent.setBrokerURL( broker.getVmURL() );
>>>>>
>>>>>          registry.bind( "test-broker", activeMQComponent);
>>>>>
>>>>>          return registry;
>>>>>      }
>>>>>
>>>>>      @Override
>>>>>      protected RouteBuilder[] createRouteBuilders() throws Exception
{
>>>>>          RouteBuilder builders[] = new RouteBuilder[2];
>>>>>
>>>>>          builders[1] = new RouteBuilder() {
>>>>>              @Override
>>>>>              public void configure() throws Exception {
>>>>>                  from("file://data?fileName=some-file.txt&noop=true")
>>>>>                          .id( "file-consumer")
>>>>>                          .log("Processing ${file:name}")
>>>>>                          .process(requestProcessor)
>>>>>                          .log("Generated ${body}")
>>>>>                          .to(ExchangePattern.InOut, "test-broker://queue:testQueue")
>>>>>                          .process(resultProcessor)
>>>>>                          .to( "mock://result")
>>>>>                  ;
>>>>>              }
>>>>>          };
>>>>>
>>>>>          builders[0] = new RouteBuilder() {
>>>>>              @Override
>>>>>              public void configure() throws Exception {
>>>>>                  from( "test-broker://queue:testQueue")
>>>>>                          .id( "dummey-response-generator")
>>>>>                          .setBody().constant( "Response Message")
>>>>>                          .to(ExchangePattern.InOnly, "test-broker://queue:dummy-queue")
>>>>>                  ;
>>>>>
>>>>>              }
>>>>>          };
>>>>>
>>>>>          return builders;
>>>>>      }
>>>>>
>>>>>      @Test
>>>>>      public void testRoute() throws Exception {
>>>>>          result.expectedBodiesReceived("We're good");
>>>>>
>>>>>          assertMockEndpointsSatisfied(5, TimeUnit.SECONDS);
>>>>>      }
>>>>>
>>>>> }
>>>>>
>>>>>> On Jun 28, 2016, at 8:47 AM, Thomas Hehl <thom@heavyweightsoftware.com>
wrote:
>>>>>>
>>>>>> Referring to: http://camel.apache.org/jms.html
>>>>>>
>>>>>> I'm trying to handle a reply in request-reply mode of an active MQ
queue. Here is the code form the page above:
>>>>>>
>>>>>> I have tried to do this:
>>>>>>
>>>>>> from("file://C:\\Users\\Thom\\git\\brms-poc-esb\\rule-cancel\\data").
>>>>>>                         log("Processing ${file:name}").
>>>>>>                         process(requestProcess).
>>>>>>                         log("Generated ${body}").
>>>>>>                         to(queueUri).inOut().
>>>>>>                         process(new ResultProcess());
>>>>>>
>>>>>> When I process this, I am still getting the stream I sent to my active
MQ.
>>>>>>
>>>>>> How do I process the reply?
>>>>>>
>>>>>>


Mime
View raw message