我有以下路线:
public void configure() throws Exception {
from(ftpEndpoint)
.routeId("import-lib-files")
.log(INFO, "Processing file: '${headers.CamelFileName}' from Libri-FTP")
.choice()
.when(method(isFilenameAlreadyImported))
.log(DEBUG, "'${headers.CamelFileName}' is already imported.")
.endChoice()
.otherwise()
.bean(method(unzipLibFile))
.bean(method(persistFilename))
.log(DEBUG, "Import file '${headers.CamelFileName}'.")
.endChoice()
.end()
.end();
}
在 unzipLibFile
处理器 bean 中,来自 ftp 的文件被解压缩并写入 HD。
我想测试(集成测试)这条路线,比如:
- 复制文件到ftp
- 开始路线
- 评估“结果”
我喜欢:
@Before
public void setUp() throws Exception {
// delete test-file from sftp
final String uploaded = ftpPath + "/" + destination + "/libri-testfile.zip";
final File uploadedFile = new File(uploaded);
uploadedFile.delete();
// delete unzipped test-file
final String unzippedFile = unzipped + "/libri-testfile.xml";
final File expectedFile = new File(unzippedFile);
expectedFile.delete();
// delete entries from db
importedLibFilenameRepository.deleteAll();
// copy file to ftp
final File source =
new ClassPathResource("vendors/references/lib.zip/libri-testfile.zip").getFile();
final String target = ftpPath + "/" + destination + "/libri-testfile.zip";
FileUtils.copyFile(new File(source.getAbsolutePath()), new File(target));
}
@Test
@Ignore
public void testStuff() throws Exception {
// Well here is a problem, I can't fix at the moment
// the Camel-Context within the SpringContext get started when the tests starts
// during this process the Camel-Routes are executed and because i copied the file to
// the ftp all is fine... but I don't want to have a sleep in a test, I want to start the
// route (like commented code beneath the sleep)
Thread.sleep(2000);
// final Map<String, Object> headers = Maps.newHashMap();
// headers.put("CamelFileName", "libri-testfile.zip");
//
// final File file =
// new ClassPathResource("vendors/references/lib.zip/libri-testfile.zip").getFile();
// final GenericFile<File> genericFile =
// FileConsumer.asGenericFile(file.getParent(), file, StandardCharsets.UTF_8.name(), false);
//
// final String uri = libFtpConfiguration.getFtpEndpoint();
// producer.sendBodyAndHeaders(uri, InOut, genericFile, headers);
// test if entry was made in the database
final List<ImportedLibFilename> filenames = importedLibFilenameRepository.findAll();
assertThat(filenames).usingElementComparatorIgnoringFields("id", "timestamp")
.containsExactly(expectedFilename("libri-testfile.zip"));
// test if content of unzipped file is valid
final String expected = unzipped + "/libri-testfile.xml";
final Path targetFile = Paths.get(expected);
final byte[] encoded = Files.readAllBytes(targetFile);
final String actualFileContent = new String(encoded, Charset.defaultCharset());
final String expectedFileContent = "This is my little test file for Libri import";
assertThat(actualFileContent).isEqualTo(expectedFileContent);
}
private ImportedLibFilename expectedFilename(final String filename) {
final ImportedLibFilename entity = new ImportedLibFilename();
entity.setFilename(filename);
return entity;
}
问题是:
所有 Camel 路线都是自动启动的,因为我将文件复制到 FTP,所以测试是绿色的。但是我的测试中有一个#sleep,我不想要它。我不希望 Camel 路线开始,只开始我需要的路线。
我的问题是:
- 如何防止 Camel-Routes 自动启动
- 注释代码(在测试方法中)是否是手动启动路由的正确方法?
- 使用 ftp 测试 Camel 路线的最佳做法是什么
最佳答案
- 在您的 route 使用
.autoStartup(yourVariable)
使其启动可配置。在正常环境中将变量设置为true
,在测试用例中设置为false
。 - 我没有看到启动路线的代码?!?
- 好吧,退后一步。考虑拆分您的 FTP 路由。出于测试和其他原因:
例如将路由拆分为FTP和处理路由。第一个只进行 FTP 传输,然后将接收到的消息发送到处理路由(例如 direct:
路由)。
好处:
- SRP : 两条路线都只做一件事,你可以专注于它。
- 可测试性:您可以通过将消息发送到处理路由的
direct:
端点来轻松测试处理路由。测试也可以专注于一件事。 - 可扩展性:假设有一个新的输入 channel (JMS、HTTP 等)。然后您只需添加另一个输入路由,该路由也会发送到您的处理路由。完毕。
当你真的想测试从 FTP 文件下降到结束的整个过程时,考虑使用 Citrus test framework或类似工具。 Camel 路线测试(在我看来)是一种“ Camel 路线的单元测试”,而不是完整的集成测试。
关于java - 测试 Camel sFTP 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49359447/