繁体   English   中英

测试 Camel sFTP 端点

[英]Test a Camel sFTP endpoint

我有以下路线:

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。

我想测试(集成测试)这条路线,比如:

    1. 复制文件到ftp
    1. 开始路线
    1. 评估“结果”

我喜欢:

  @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;
  }

问题是:
所有骆驼路线都是自动启动的,因为我将文件复制到 FTP,所以测试是绿色的。 但是我的测试中有一个#sleep,我不想要它。 我不希望骆驼路线开始,只开始我需要的路线。

我的问题是:

    1. 如何防止 Camel-Routes 自动启动
    1. 注释代码(在测试方法中)是手动启动路由的正确方法吗?
    1. 使用 ftp 测试骆驼路线的最佳做法是什么
  1. 在您的路线中使用.autoStartup(yourVariable)以使其启动可配置。 在正常环境中将变量设置为true ,在测试用例中设置为false
  2. 我没有看到启动路线的代码???
  3. 好吧,退后一步。 考虑拆分您的 FTP 路由。 出于测试和更多原因:

例如将路由拆分为FTP 和处理路由。 第一个只做 FTP 传输,然后将接收到的消息发送到处理路由(例如direct:路由)。

好处:

  • SRP :两条路线只做一件事,你可以专注于它。
  • 可测试性:您可以通过将消息发送到direct:处理路径的端点来轻松测试处理路径。 测试也可以专注于一件事。
  • 可扩展性:假设有一个新的输入通道(JMS、HTTP 等)。 然后您只需添加另一个输入路由,该路由也会发送到您的处理路由。 完毕。

当你真的想测试从 FTP 文件下降到最后的整个过程时,考虑使用Citrus 测试框架或类似的工具。 骆驼路线测试(在我看来)是一种“骆驼路线的单元测试”,而不是完整的集成测试。

感谢@burki ...

他建议拆分路线(单一职责)帮助我解决了我的问题:

这是路线:

从 sFTP 消耗的“主路由”:

  @Override
  public void configure() throws Exception {
    // @formatter:off
    from(endpoint)
      .setHeader("Address", constant(address))
      .log(INFO, "Import Libri changeset: Consuming from '${headers.Address}' the file '${headers.CamelFileName}'.")
      .to("direct:import-new-file");
    // @formatter:on
  }

第一条子路线:

  @Override
  public void configure() throws Exception {
    // @formatter:off
    from("direct:import-new-file")
        .choice()
          .when(method(isFilenameAlreadyImported))
          .log(TRACE, "'${headers.CamelFileName}' is already imported.")
        .endChoice()
        .otherwise()
          .log(TRACE, "Import file '${headers.CamelFileName}'.")
          .multicast()
          .to("direct:persist-filename", "direct:unzip-file")
        .endChoice()
      .end()
    .end();
    // @formatter:on
  }

两个多播:

  @Override
  public void configure() throws Exception {
    // @formatter:off
      from("direct:persist-filename")
        .log(TRACE, "Try to write filename '${headers.CamelFileName}' to database.")
        .bean(method(persistFilename))
      .end();
    // @formatter:on
  }

  @Override
  public void configure() throws Exception {
    // @formatter:off
      from("direct:unzip-file")
        .log(TRACE, "Try to unzip file '${headers.CamelFileName}'.")
        .bean(method(unzipFile))
      .end();
    // @formatter:on
  }

使用此设置,我可以编写如下测试:

  @Test
  public void testRoute_validExtractedFile() throws Exception {
    final File source = ZIP_FILE_RESOURCE.getFile();
    producer.sendBodyAndHeaders(URI, InOut, source, headers());

    final String actual = getFileContent(unzippedPath, FILENAME);
    final String expected = "This is my little test file for Libri import";
    assertThat(actual).isEqualTo(expected);
  }

  @Test
  public void testRoute_databaseEntryExists() throws Exception {
    final File source = ZIP_FILE_RESOURCE.getFile();
    producer.sendBodyAndHeaders(URI, InOut, source, headers());

    final List<ImportedFilename> actual = importedFilenameRepository.findAll();
    final ImportedFilename expected = importedFilename(ZIPPED_FILENAME);
    assertThat(actual).usingElementComparatorIgnoringFields("id", "timestamp")
    .containsExactly(expected);
  }

  private String getFileContent(final String path, final String filename) throws IOException {
    final String targetFile = path + "/" + filename;
    final byte[] encodedFileContent = Files.readAllBytes(Paths.get(targetFile));
    return new String(encodedFileContent, Charset.defaultCharset());
  }

  private Map<String, Object> headers() {
    final Map<String, Object> headers = Maps.newHashMap();
    headers.put("CamelFileName", ZIPPED_FILENAME);
    return headers;
  }

我可以使用ProducerTemplate (生产者)启动骆驼路线并将消息发送到直接端点(而不是 ftp 端点)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM