在基于内容的路由上编写Camel测试的最佳方法

时间:2016-05-20 10:06:36

标签: apache-camel

我对如何继续为CBR编写简单Camel测试用例感到困惑。说我有这条简单的路线:

<route id="_route1">
    <from id="_to1" uri="file:/home/user/data/eip-demo/in?noop=true"/>
    <choice id="_choice1">
        <when id="_when1">
            <simple>${header.CamelFileName} regex '^.*xml$'</simple>
            <to id="_to2" uri="file:/home/user/data/eip-demo/xml"/>
        </when>
        <when id="_when2">
            <simple>${header.CamelFileName} regex '^.*txt$'</simple>
            <to id="_to3" uri="file:/home/user/data/eip-demo/txt"/>
        </when>
        <otherwise id="_otherwise1">
            <log id="_log1" message="File unknown!"/>
             <to id="_to2" uri="file:/tmp"/>
        </otherwise>
    </choice>
</route>

测试此路线的最正确方法是什么?我想我可以用直接:组件替换文件:component并在Test Case中生成文件。但是,我将无法直接从IDE(Jboss Developer Studio)运行该路由。对需要测试的路径进行编码的最正确方法是什么?

更新:我对JBoss Developer Studio创建的Camel Test进行了一些改进:

public class CamelContextXmlTest extends CamelSpringTestSupport {

    // TODO Create test message bodies that work for the route(s) being tested
    // Expected message bodies
    protected Object[] expectedBodies = { "<something id='1'>expectedBody1</something>",
            "textfile" };
    protected Object[] name = { "test.xml",
    "test.txt" };

    // Templates to send to input endpoints

    @Produce(uri = "file:/home/data/eip-demo/in?noop=true")
    protected ProducerTemplate inputEndpoint;

    // Mock endpoints used to consume messages from the output endpoints and then perform assertions
    @EndpointInject(uri = "mock:output")
    protected MockEndpoint outputEndpoint;

    @EndpointInject(uri = "mock:output2")
    protected MockEndpoint output2Endpoint;

    @EndpointInject(uri = "mock:output3")
    protected MockEndpoint output3Endpoint;

    @Test
    public void testCamelRoute() throws Exception {
        // Create routes from the output endpoints to our mock endpoints so we can assert expectations

        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("file:/home/user/data/eip-demo/xml").to(outputEndpoint);

                from("file:/home/user/data/eip-demo/txt").to(output2Endpoint);
            }
        });

        // Define some expectations

        // TODO Ensure expectations make sense for the route(s) we're testing
        //outputEndpoint.expectedBodiesReceivedInAnyOrder(expectedBodies);

        // Send some messages to input endpoints
        int index=0;
        for (Object expectedBody : expectedBodies) {
            inputEndpoint.sendBodyAndHeader(expectedBody,"CamelFileName",name[index]);
            index++;
        }

        outputEndpoint.expectedMessageCount(1);
        output2Endpoint.expectedMessageCount(1);

        // Validate our expectations
        assertMockEndpointsSatisfied();
    }

    @Override
    protected ClassPathXmlApplicationContext createApplicationContext() {
        return new ClassPathXmlApplicationContext("META-INF/spring/camel-context.xml");
    }

}

这是一个好的解决方案还是可以改进?

1 个答案:

答案 0 :(得分:0)

这里需要的是AdviseWith: http://camel.apache.org/advicewith.html

您只需动态更换/修改您的终端,或者只需在您的CBR的每个条件后即时添加模拟。如果你不能这样做,请告诉我,我将帮助你完成一些为你完成这项工作的实际代码。

R上。