How to create Functional tests for IntegrationFlows

20 Views Asked by At

I have a StandardIntegrationFlow which runs each day and deletes files that are older than 10 days.

@Bean
  public StandardIntegrationFlow oldReceiptsCleaner(
      @Qualifier("receiptSftpRemoteFileTemplate")
          RecursiveSftpRemoteFileTemplate receiptSftpRemoteFileTemplate,
      RemoteSftpFileRemover remoteSftpFileRemover) {

    return IntegrationFlows.from(
            Sftp.inboundStreamingAdapter(receiptSftpRemoteFileTemplate)
                .remoteDirectory(properties.getSftpPath()),
            e ->
                e.poller(
                    Pollers.cron(properties.getCronExpression())
                        .maxMessagesPerPoll(-1)).id("oldReceiptsCleanerPoller"))
        .handle(remoteSftpFileRemover::removeFile)
        .get();
  }

I want to create a functional test to verify that it works. I have tested the code manually, by setting the cron expression to run each 2 minutes and remove files that are older than 5 mins and but of course I need some automated tests.

I have thought to create 2 files, one which is older than 10 days and one that is not. Fetch those 2 files and verify that they exist. Then manually trigger the StandardIntegrationFlow using an object of SourcePollingChannelAdapter and calling the .start() function, and then try to fetch them again and verify that the one is deleted.

The first question will be if that is a proper way to test an IntegrationFlow. Also, I can't find a simple way to create the files inside the test, and change their modifiedTime.

I am using spring-integration 5.5.15 and Spock framework for testing. Also using Minio Containers to store files on server for the functional tests

1

There are 1 best solutions below

0
Artem Bilan On

You can have a PollerSpec as a bean and reference it from your oldReceiptsCleaner flow definition. Then you override this bean in your test configuration. Then you have your LastModifiedFileListFilter which can be based on some property for age and specify desired in the test.

I'm not familiar with Minio, but probably there is API how to create files with required lastModified.

Then yes. You can use @SpringIntegrationTest with noAutoStartup for a bean name of your Sftp.inboundStreamingAdapter. After you have created files, you can start that SourcePollingChannelAdapter manually in the test method.