You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by aturkin <at...@rencap.com> on 2016/03/22 12:14:03 UTC
Apache camel. InterceptSendTo doesn't work with bean endpoint
I can't understand why my tests don't work. I try intercept and skip sending
to endpoint which is bean reference and nothing happens. I'm using version
2.16.2.
test-camel.xml
<bean id="eb" class="com.rencap.emf.bpipe.EndpointBean"/>
<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
<camel:endpoint id="requestEP" uri="direct:request"/>
<endpoint id="beanEP" uri="bean:eb?method=processMessage" />
<camel:route id="testRoute">
<camel:from ref="requestEP"/>
<camel:to ref="sendEP" />
</camel:route>
</camel:camelContext>
EndpointBean.java
package com.rencap.emf.bpipe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EndpointBean {
private static Logger LOG = LoggerFactory.getLogger(EndpointBean.class);
public void processMessage( String msg ){
LOG.info("Processing message: {} ",msg);
}
}
Unit test:
@EndpointInject(ref="requestEP")
ProducerTemplate requestEP;
@EndpointInject(ref="beanEP")
ProducerTemplate beanEP;
@Autowired
ModelCamelContext camelContext;
@Test
public void test() throws Exception{
camelContext.getRouteDefinition("testRoute").adviceWith( camelContext ,
new AdviceWithRouteBuilder(){
@Override
public void configure() throws Exception {
interceptSendToEndpoint(
beanEP.getDefaultEndpoint().getEndpointUri() ).
to("mock:send").
skipSendToOriginalEndpoint();
}
});
TestUtils.waitingFor("Configuration applied", 2000);
MockEndpoint mockEP =
camelContext.getEndpoint("mock:send",MockEndpoint.class);
mockEP.setExpectedCount( 1 );
requestEP.sendBody("Message");
mockEP.assertIsSatisfied();
TestUtils.waitingFor("All rows commited", 2000);
}
Test always fails. Logs:
13:11:02.512 [main] INFO o.apache.camel.model.RouteDefinition - AdviceWith
route after: Route(testRoute)[[From[ref:requestEP]] ->
[InterceptSendToEndpoint[bean://eb?method=processMessage ->
[To[mock:send]]], To[ref:beanEP]]]
13:11:02.537 [main] INFO o.a.camel.spring.SpringCamelContext - Route:
testRoute started and consuming from: Endpoint[direct://request]
13:11:02.538 [main] INFO com.rencap.emf.bpipe.test.TestUtils - Wait 2000
ms. Configuration applied
13:11:04.554 [main] INFO com.rencap.emf.bpipe.EndpointBean - Processing
message: Message
13:11:04.556 [main] INFO o.a.c.component.mock.MockEndpoint - Asserting:
Endpoint[mock://send] is satisfied
It means that sending to endpoint isn't being intercepted and skipped. May
be I don't understand something but I coudn't find any restriction on use
this method.
In additional I noticed the same problem for endpoint with log. If I replace
beanEP on :
<endpoint id="beanEP" uri="log:LOGMESSAGE" />
that I receive the same result. But if I replace it on
<endpoint id="beanEP" uri="seda:send" />
and add new route :
<camel:route id="route2">
<camel:from ref="sendEP"/>
<camel:log message="msg received ${body}"/>
</camel:route>
that I will get expected result and test will be successed.
What do I do wrong? Or maybe Are there some restrictions on this method?
--
View this message in context: http://camel.465427.n5.nabble.com/Apache-camel-InterceptSendTo-doesn-t-work-with-bean-endpoint-tp5779474.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Apache camel. InterceptSendTo doesn't work with bean endpoint
Posted by aturkin <at...@rencap.com>.
Hello. thank you for your reply!
I tried to use CamelSpringTestSupport and it works. My working example:
public class TestTest extends CamelSpringTestSupport {
@Override
protected AbstractApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext("test-camel.xml");
}
@Override
public boolean isUseAdviceWith(){ return true; }
@Test
public void test() throws Exception{ ...context.start(); .... }
}
But I'd like to use annotation. I have found example:
https://github.com/CamelCookbook/camel-cookbook-examples/blob/master/camel-cookbook-testing/src/test/java/org/camelcookbook/examples/testing/advicewith/FixedEndpointEnhancedSpringTest.java
I tried to use it in my case but my test didn't passed again:
@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:test-camel.xml"})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@UseAdviceWith(true)
public class TestTest2 {
@EndpointInject(ref="requestEP")
ProducerTemplate requestEP;
@EndpointInject(ref="beanEP")
ProducerTemplate beanEP;
@Autowired
ModelCamelContext context;
@Test
public void test() throws Exception{
context.getRouteDefinition("testRoute").adviceWith( context , new
AdviceWithRouteBuilder(){
@Override
public void configure() throws Exception {
interceptSendToEndpoint(
beanEP.getDefaultEndpoint().getEndpointUri() ).
to("mock:send").
skipSendToOriginalEndpoint();
}
});
context.start();
TestUtils.waitingFor("Configuration applied", 2000);
MockEndpoint mockEP =
context.getEndpoint("mock:send",MockEndpoint.class);
mockEP.setExpectedCount( 1 );
context.createProducerTemplate().sendBody(requestEP.getDefaultEndpoint(),
"Message");
mockEP.assertIsSatisfied();
TestUtils.waitingFor("All rows commited", 2000);
}
}
And I have detected that if I comment string "context.start();" in my first
example I get "Exception" that there are no consumers, but if I comment this
string in my second example, I don't get the same error.
As I understand http://camel.apache.org/spring-testing.html In case using
@UseAdviceWith(true) CamelContexts don't start automatically. What is wrong
and how to make test with annotation corrected?
Thanks,
Andrew
--
View this message in context: http://camel.465427.n5.nabble.com/Apache-camel-InterceptSendTo-doesn-t-work-with-bean-endpoint-tp5779474p5779704.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Apache camel. InterceptSendTo doesn't work with bean endpoint
Posted by Quinn Stevenson <qu...@pronoia-solutions.com>.
Have you tried overriding isUseAdviceWith in your test? It’s mentioned in the docs http://camel.apache.org/advicewith.html <http://camel.apache.org/advicewith.html>
> On Mar 22, 2016, at 5:14 AM, aturkin <at...@rencap.com> wrote:
>
> I can't understand why my tests don't work. I try intercept and skip sending
> to endpoint which is bean reference and nothing happens. I'm using version
> 2.16.2.
>
> test-camel.xml
>
> <bean id="eb" class="com.rencap.emf.bpipe.EndpointBean"/>
> <camel:camelContext xmlns="http://camel.apache.org/schema/spring">
> <camel:endpoint id="requestEP" uri="direct:request"/>
> <endpoint id="beanEP" uri="bean:eb?method=processMessage" />
> <camel:route id="testRoute">
> <camel:from ref="requestEP"/>
> <camel:to ref="sendEP" />
> </camel:route>
> </camel:camelContext>
> EndpointBean.java
>
> package com.rencap.emf.bpipe;
>
> import org.slf4j.Logger;
> import org.slf4j.LoggerFactory;
>
> public class EndpointBean {
> private static Logger LOG = LoggerFactory.getLogger(EndpointBean.class);
>
> public void processMessage( String msg ){
> LOG.info("Processing message: {} ",msg);
> }
> }
> Unit test:
>
> @EndpointInject(ref="requestEP")
> ProducerTemplate requestEP;
> @EndpointInject(ref="beanEP")
> ProducerTemplate beanEP;
> @Autowired
> ModelCamelContext camelContext;
> @Test
> public void test() throws Exception{
>
> camelContext.getRouteDefinition("testRoute").adviceWith( camelContext ,
> new AdviceWithRouteBuilder(){
> @Override
> public void configure() throws Exception {
> interceptSendToEndpoint(
> beanEP.getDefaultEndpoint().getEndpointUri() ).
> to("mock:send").
> skipSendToOriginalEndpoint();
> }
> });
> TestUtils.waitingFor("Configuration applied", 2000);
>
> MockEndpoint mockEP =
> camelContext.getEndpoint("mock:send",MockEndpoint.class);
> mockEP.setExpectedCount( 1 );
>
> requestEP.sendBody("Message");
>
> mockEP.assertIsSatisfied();
>
> TestUtils.waitingFor("All rows commited", 2000);
> }
> Test always fails. Logs:
>
> 13:11:02.512 [main] INFO o.apache.camel.model.RouteDefinition - AdviceWith
> route after: Route(testRoute)[[From[ref:requestEP]] ->
> [InterceptSendToEndpoint[bean://eb?method=processMessage ->
> [To[mock:send]]], To[ref:beanEP]]]
> 13:11:02.537 [main] INFO o.a.camel.spring.SpringCamelContext - Route:
> testRoute started and consuming from: Endpoint[direct://request]
> 13:11:02.538 [main] INFO com.rencap.emf.bpipe.test.TestUtils - Wait 2000
> ms. Configuration applied
> 13:11:04.554 [main] INFO com.rencap.emf.bpipe.EndpointBean - Processing
> message: Message
> 13:11:04.556 [main] INFO o.a.c.component.mock.MockEndpoint - Asserting:
> Endpoint[mock://send] is satisfied
> It means that sending to endpoint isn't being intercepted and skipped. May
> be I don't understand something but I coudn't find any restriction on use
> this method.
>
> In additional I noticed the same problem for endpoint with log. If I replace
> beanEP on :
>
> <endpoint id="beanEP" uri="log:LOGMESSAGE" />
> that I receive the same result. But if I replace it on
>
> <endpoint id="beanEP" uri="seda:send" />
> and add new route :
>
> <camel:route id="route2">
> <camel:from ref="sendEP"/>
> <camel:log message="msg received ${body}"/>
> </camel:route>
> that I will get expected result and test will be successed.
>
> What do I do wrong? Or maybe Are there some restrictions on this method?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Apache-camel-InterceptSendTo-doesn-t-work-with-bean-endpoint-tp5779474.html
> Sent from the Camel - Users mailing list archive at Nabble.com.