2017-09-15 87 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