FTP路由 - 单元测试

时间:2017-09-15 04:32:49

标签: spring-boot apache-camel spring-boot-test

我有一条骆驼路线:

  1. 轮询FTP服务器以获取新的XML文件
  2. 在本地下载文件
  3. 根据XSD验证XML文件
  4. 按类别将XML拆分为实体
  5. 将实体转换为json
  6. 将json发送到HTTP端点
  7. 更新:现在可以使用

    @Component
    public class FTPPoller extends RouteBuilder {
        XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
    
        @Override
        public void configure() throws Exception {
    
            from("{{endpoint.ftp.server}}")
                    .id("ftp-poller")
                    .log("Found file ${file:name}.")
                    .to("{{endpoint.local.validation}}");
    
    
            from("{{endpoint.local.validation}}")
                    .id("xml-validator")
                    .log("Processing file ${file:name}.")
                    .doTry()
                        .to("validator:classpath:schema/fr-masterdata.xsd")
                        .log("File ${file:name} is valid.")
                        .to("{{endpoint.local.processing}}")
                    .doCatch(org.apache.camel.ValidationException.class)
                        .log("File ${file:name} is invalid.")
                        .to("{{endpoint.local.error}}")
                    .end();
    
            from("{{endpoint.local.processing}}")
                    .id("xml-processor")
                    .split(xpath("//flu:entities/category")
                            .namespace("flu", "hxxx://www.xxx.com")
                    ).streaming()
                    .marshal(xmlJsonFormat)
                    .to("direct:category")
                    .end();
    
            from("direct:category")
                    .id("requestbin")
                    .log("Processing category ${body}")
                    .setHeader(Exchange.HTTP_METHOD, constant("POST"))
                    .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                    .to("{{endpoint.requestbin}}");
    
        }
    }
    
    
    @RunWith(CamelSpringBootRunner.class)
    @SpringBootTest(classes = {HbIntegrationApplication.class},
            properties = { "camel.springboot.java-routes-include-pattern=**/FTPPoller*"})
    public class FTPPollerTest {
    
        @Autowired
        protected ProducerTemplate producerTemplate;
    
        @EndpointInject(uri = "{{endpoint.requestbin}}")
        protected MockEndpoint requestbinEndpoint;
    
        @EndpointInject(uri = "{{endpoint.local.error}}")
        protected MockEndpoint localErrorEndpoint;
    
        @Before
        public void cleanDir() throws Exception {
            deleteDirectory("hb");
        }
    
    
        @Test
        @DirtiesContext
        public void testFileUploadSuccess() throws Exception {
            String fileContent = FileUtils.readFileToString(new File("src/test/resources/test-files/category.xml"));
    
            requestbinEndpoint.expectedMessageCount(2);
            producerTemplate.sendBody("file://hb/incoming", fileContent);
    
            requestbinEndpoint.assertIsSatisfied();
        }
    
        @Test
        @DirtiesContext
        public void testFileUploadFailure() throws Exception {
    
            localErrorEndpoint.expectedMessageCount(1);
            requestbinEndpoint.expectedMessageCount(0);
    
            producerTemplate.sendBody("file://hb/incoming", "invalidContent");
    
            localErrorEndpoint.assertIsSatisfied();
            requestbinEndpoint.assertIsSatisfied();
        }
    }
    

    application.properties:

    endpoint.ftp.server=file://hb/incoming
    endpoint.local.validation=file://hb/validation
    endpoint.local.processing=file://hb/processing
    endpoint.local.error=mock:file://hb/error
    endpoint.requestbin=mock:requestbin
    

    剩下的问题是:

    如果我已定义以下属性: endpoint.local.processing =模拟:文件:// HB /处理 我的测试失败了:

    Caused by: java.lang.UnsupportedOperationException: You cannot consume from this endpoint
    

    有没有办法定义哪些路线应该包含在我的单元测试中?

    任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

您需要在发送数据到Camel之前设置对模拟的期望,例如此代码

    localValidationEndpoint.sendBody(xml);

    requestbinEndpoint.expectedMessageCount(2);
    requestbinEndpoint.assertIsSatisfied();

应该是

    requestbinEndpoint.expectedMessageCount(2);

    localValidationEndpoint.sendBody(xml);

    requestbinEndpoint.assertIsSatisfied();