2017-09-15 90 views
0

我有一个骆驼路线:FTP路线 - 单元测试

  1. 轮询新XML文件的FTP服务器
  2. 下载本地文件
  3. 验证XML文件对一个XSD
  4. 分裂XML按类别分为实体
  5. 将实体转换为json
  6. 将json发送到HTTP端点

升级:现在工作

@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 = mock:file:// hb/processing 我的测试失败:

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

有什么办法来定义哪些路线应该包括在我的单元测试?

任何帮助,将不胜感激。由于

+1

为什么你要问约2情境?你期待检索2并获得0?以下克劳斯的回答是正确的,因为您需要在发送之前指定预期。最后。你是否通过路由来限制文件名?如果不是,那么我想这将是一个发现它在链中失败的情况,然后分析 – user3206236

回答

0

您需要设置在嘲笑的期望发送数据到骆驼前,如该代码

localValidationEndpoint.sendBody(xml); 

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

应该

requestbinEndpoint.expectedMessageCount(2); 

    localValidationEndpoint.sendBody(xml); 

    requestbinEndpoint.assertIsSatisfied(); 
+0

谢谢克劳斯。我已经试过这个结果。 我认为问题是加载了2个上下文(DefaultCamelContext和SpringCamelContext)。 对于使用Annotations的Spring Boot/Apache Camel应用程序应该使用哪些Annoations/config? – eistropfen

+0

骆驼在行动第二版本书有关于骆驼和Spring Boot测试的一章,它的源代码也有例子。否则,你可以检查骆驼春季启动的源代码和它的单元测试它是如何做到的。 –

+0

谢谢克劳斯。我根据您的示例修改了代码,但测试用例仍然失败,而应用程序本身运行良好。我已经添加了上面的详细日志。 我最近才开始使用Camel作为POC(而不是Spring集成),到目前为止我倾向于使用Camel,但是单元测试似乎并不那么简单?我可以在多条路线上编写单元测试吗?例如向{{endpoint.ftp.server}}端点发送一个文件,并确认已在{{endpoint.requestbin}}端点接收到2条消息? – eistropfen