使用Apache Camel对FTP使用者进行单元测试

Ara*_*ram 4 java esb apache-camel

我有以下路线.在单元测试中,由于我没有可用的FTP服务器,我想使用camel的测试支持并发送无效消息"ftp://hostname/input"并验证它是否失败并路由到"ftp://hostname/error".

我浏览了主要讨论使用"mock:"端点的文档,但我不确定如何在这种情况下使用它.

public class MyRoute extends RouteBuilder
{
    @Override
    public void configure()
    {
        onException(EdiOrderParsingException.class).handled(true).to("ftp://hostname/error");

        from("ftp://hostname/input")
            .bean(new OrderEdiTocXml())
            .convertBodyTo(String.class)
            .convertBodyTo(Document.class)
            .choice()
            .when(xpath("/cXML/Response/Status/@text='OK'"))
            .to("ftp://hostname/valid").otherwise()
            .to("ftp://hostname/invalid");
    }
}
Run Code Online (Sandbox Code Playgroud)

Cla*_*sen 9

正如Ben所说,您可以设置FTP服务器并使用真实组件.可以嵌入FTP服务器,也可以在内部设置FTP服务器.后者更像是集成测试,您可以在其中拥有专用的测试环境.

Camel在其测试工具包中非常灵活,如果您想构建一个不使用真实FTP组件的单元测试,那么您可以在测试之前替换它.例如,在您的示例中,您可以将路由的输入端点替换为直接端点,以便更容易向路径发送消息.然后你可以使用拦截器拦截发送到ftp端点,并绕过消息.

部分测试工具包的建议提供了以下功能:http://camel.apache.org/advicewith.html.并且还在Camel in action book的第6章中讨论,例如6.3节,讨论模拟错误.

在你的例子中,你可以做一些类似的事情

public void testSendError() throws Exception {
    // first advice the route to replace the input, and catch sending to FTP servers
    context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            replaceFromWith("direct:input");

            // intercept valid messages
            interceptSendToEndpoint("ftp://hostname/valid")
                .skipSendToOriginalEndpoint()
                .to("mock:valid");

            // intercept invalid messages
            interceptSendToEndpoint("ftp://hostname/invalid")
                .skipSendToOriginalEndpoint()
                .to("mock:invalid");
        }
    });

     // we must manually start when we are done with all the advice with
    context.start();

    // setup expectations on the mocks
    getMockEndpoint("mock:invalid").expectedMessageCount(1);
    getMockEndpoint("mock:valid").expectedMessageCount(0);

    // send the invalid message to the route
    template.sendBody("direct:input", "Some invalid content here");

    // assert that the test was okay
    assertMockEndpointsSatisfied();
}
Run Code Online (Sandbox Code Playgroud)

从Camel 2.10开始,我们将使用建议进行拦截和模拟更容易.我们还介绍了一个存根组件.http://camel.apache.org/stub


Bri*_*edt 5

看看MockFtPServer

<dependency>
    <groupId>org.mockftpserver</groupId>
    <artifactId>MockFtpServer</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

通过这一步骤,您可以模拟各种行为,例如权限问题等:

例:

fakeFtpServer = new FakeFtpServer();

fakeFtpServer.setServerControlPort(FTPPORT);

FileSystem fileSystem = new UnixFakeFileSystem();
fileSystem.add(new DirectoryEntry(FTPDIRECTORY));
fakeFtpServer.setFileSystem(fileSystem);
fakeFtpServer.addUserAccount(new UserAccount(USERNAME, PASSWORD, FTPDIRECTORY));

...

assertTrue("Expected file to be transferred", fakeFtpServer.getFileSystem().exists(FTPDIRECTORY + "/" + FILENAME)); 
Run Code Online (Sandbox Code Playgroud)