interceptSendToEndpoint不使用Camel和Springboot拦截Route中的http请求

问题描述 投票:0回答:1

我正在尝试为我的路由编写单元测试,它接收消息,进行外部Web服务调用,然后处理响应。但是,我似乎无法模拟外部Web服务调用。我试过以下:

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode = 
DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints
@UseAdviceWith
public class TypeServiceRoutersTest
{

@Autowired
private CamelContext camelContext;

@Autowired
private GetTypesProcess getTypesProcess;

@Produce(uri = "direct:start")
private ProducerTemplate incomingJmsRequestMessage;

@EndpointInject(uri = "mock:JMSReplyTo")
private MockEndpoint jmsReplyTo;

@Before
public void setUp()throws Exception{

    camelContext.getRouteDefinition("Build XML Response").adviceWith(camelContext,
            new AdviceWithRouteBuilder() {
                @Override
                public void configure() throws Exception {
                    weaveAddLast().to("mock:JMSReplyTo");
                }
            });

    camelContext.getRouteDefinition("Build & Send Http Request").adviceWith(camelContext,
            new AdviceWithRouteBuilder() {
                @Override
                public void configure() throws Exception {
                    interceptSendToEndpoint("http://*")
                            .skipSendToOriginalEndpoint()
                            .setBody("Hello");

    camelContext.start();
}

@Test
@DirtiesContext
public void RouteFlowTest() throws Exception{

    Map<String,Object> jmsHeaders = new HashMap<>();
    jmsHeaders.put("Auth","helloWorld");
    jmsHeaders.put("JMSReplyTo","sample");

    String jmsBody = "Hi"
    incomingJmsRequestMessage.sendBodyAndHeaders("direct:start", jmsBody, jmsHeaders);

    jmsReplyTo.expectedMessageCount(1);

    jmsReplyTo.assertIsSatisfied();


}

@After
public void cleanUpCamelContext() throws Exception{
    camelContext.stop();
}

}

我的路线如下:

@Override
public void configure() throws Exception {

    onException(Exception.class)
            .handled(true)
            .log(LoggingLevel.INFO,log,"${exception.stacktrace}")
            .bean(getTypesProcess,"processFailure")
            .end();

    from("{{jms.input.queue}}"+"?exchangePattern=InOut").routeId("Receive JMS Message")
        .log(LoggingLevel.INFO,log,"New message received on Queue")
        .to("direct:start");


    from("direct:start").routeId("Build & Send Http Request")
            .bean(getTypesProcess,"processRequest")
            .log(LoggingLevel.DEBUG,log,"Processed Request and built http request")
            .to("{{http.endpoints.GetTypes}}")
            .to("direct:processResponse");

    from("direct:processResponse").routeId("Build XML Response")
            .convertBodyTo(String.class)
            .bean(getTypesProcess, "processResponse");

}

但是当我运行我的测试时,对webservice的调用仍会运行:

.to("{{http.endpoints.GetTypes}}")

由于来自webservice的403导致我的测试失败。我不确定我做错了什么?

日志表明路由已更改,但也显示调用端点:

2019-04-30 14:44:13.938  INFO 1007 --- [           main] org.apache.camel.model.RouteDefinition   : Adviced route before/after as XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<route xmlns="http://camel.apache.org/schema/spring" customId="true" id="Build Send Http Request">
    <from uri="direct:start"/>
    <onException>
        <log loggingLevel="INFO" message="${exception.stacktrace}"/>
        <bean method="processFailure"/>
    </onException>
    <bean method="processRequest"/>
    <log loggingLevel="DEBUG" message="Processed Request and built http request"/>
    <to uri="http://help.co.za:10108"/>
    <to uri="direct:processResponse"/>
</route>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<route xmlns="http://camel.apache.org/schema/spring" customId="true" id="Build Send Http Request">
    <from uri="direct:start"/>
    <onException>
        <log loggingLevel="INFO" message="${exception.stacktrace}"/>
        <bean method="processFailure"/>
    </onException>
    <interceptSendToEndpoint skipSendToOriginalEndpoint="true" uri="http*">
        <setBody>
            <simple>Hello</simple>
        </setBody>
    </interceptSendToEndpoint>
    <bean method="processRequest"/>
    <log loggingLevel="DEBUG" message="Processed Request and built http request"/>
    <to uri="http://help.co.za:10108"/>
    <to uri="direct:processResponse"/>
</route>

2019-04-30 14:44:13.940  INFO 1007 --- [           main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.23.2 (CamelContext: camel-1) is starting
2019-04-30 14:44:13.941  INFO 1007 --- [           main] o.a.c.m.ManagedManagementStrategy        : JMX is enabled
2019-04-30 14:44:14.120  INFO 1007 --- [           main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [http://help.co.za:10108] with mock endpoint [mock:http:help.co.za:10108]
2019-04-30 14:44:14.125  INFO 1007 --- [           main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [direct://processResponse] with mock endpoint [mock:direct:processResponse]
2019-04-30 14:44:14.151  INFO 1007 --- [           main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [jms://queue.sample?exchangePattern=InOut] with mock endpoint [mock:jms:queue.sample]
2019-04-30 14:44:14.165  INFO 1007 --- [           main] o.a.camel.spring.SpringCamelContext      : StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2019-04-30 14:44:14.259  INFO 1007 --- [           main] o.a.camel.spring.SpringCamelContext      : Route: Build Send Http Request started and consuming from: direct://start
2019-04-30 14:44:14.259  INFO 1007 --- [           main] o.a.camel.spring.SpringCamelContext      : Route: Build XML Response started and consuming from: direct://processResponse
2019-04-30 14:44:14.291  INFO 1007 --- [           main] o.a.camel.spring.SpringCamelContext      : Route: Receive JMS Message started and consuming from: jms://queue.sample?exchangePattern=InOut
2019-04-30 14:44:14.293  INFO 1007 --- [           main] o.a.camel.spring.SpringCamelContext      : Route: route1 started and consuming from: direct://jmsMessage
2019-04-30 14:44:14.294  INFO 1007 --- [           main] o.a.camel.spring.SpringCamelContext      : Route: route2 started and consuming from: direct://jsonResponse
2019-04-30 14:44:14.294  INFO 1007 --- [           main] o.a.camel.spring.SpringCamelContext      : Total 5 routes, of which 5 are started
2019-04-30 14:44:14.295  INFO 1007 --- [           main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.23.2 (CamelContext: camel-1) started in 0.355 seconds
2019-04-30 14:44:14.478  INFO 1007 --- [           main] c.i.o.r.OP_GetTypes.TypeServiceRouter    : org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://help.co.za:10108/ with statusCode: 404
    at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:273)```
java spring-boot junit apache-camel
1个回答
0
投票

尝试将参数更改为interceptSendToEndpoint,如下所示。

interceptSendToEndpoint("http://.*")

看起来RegEx与http端点不匹配,因为单独的chracter *不匹配。但.*匹配http://之后的所有角色

© www.soinside.com 2019 - 2024. All rights reserved.