我有一个骆驼路线: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