lundi 19 mars 2018

Test a Camel sFTP endpoint

I've got the following route:

  public void configure() throws Exception {
    // @formatter:off
    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();
    // @formatter:on
  }

inside the unzipLibFile processor bean the file from the ftp gets uncompressed and is written to the HD.

I want to test (integration test) this route, like:

  1. Copy file to ftp
  2. Start the route
  3. evaluate the 'outcome'

like:

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

The problem is: All camel route are started automaticly and because I copied the file to the FTP the test is green. But I've a #sleep inside my test, which I don't want. I want no camel route starting and start only the route I need.

My questions are:

  1. How can I prevent the Camel-Routes from starting automaticly
  2. Is the commented code (in the test method) the right way to start a route manually?
  3. What are best practices to test a camel route with a ftp

Aucun commentaire:

Enregistrer un commentaire