我有一個駱駝路線:FTP路線 - 單元測試
- 輪詢新XML文件的FTP服務器
- 下載本地文件
- 驗證XML文件對一個XSD
- 分裂XML按類別分爲實體
- 將實體轉換爲json
- 將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
有什麼辦法來定義哪些路線應該包括在我的單元測試?
任何幫助,將不勝感激。由於
爲什麼你要問約2情境?你期待檢索2並獲得0?以下克勞斯的回答是正確的,因爲您需要在發送之前指定預期。最後。你是否通過路由來限制文件名?如果不是,那麼我想這將是一個發現它在鏈中失敗的情況,然後分析 – user3206236