spring-bootapache-camelintegration-testingcitrus-framework

Using Citrus to mock SFTP and Kafka for Integration Testing Spring-Boot apache-camel xml based routes?


I am working with a Spring Boot application that was written using Apache Camel spring-xml routes. There is very little java based application logic, and it is nearly entirely written in xml and based on the various camel routes.

The routes are configured to connect to the different environments and systems through property files, using a property such as KAFKA_URL and KAFKA_PORT. Inside one of the implemented routes, the application connects with the following and consumes/produces messages to it:

<to id="route_id_replaced_for_question" uri="kafka:{{env:KAFKA_URL:{{KAFKA_URL}}}}:{{env:KAFKA_PORT:{{KAFKA_PORT}}}}?topic={{env:KAFKA_TOPIC:{{topic_to_connect_to}}}}&amp;kerberosRenewJitter=1&amp;kerberosRenewWindowFactor=1&amp;{{kafka.ssl.props}}&amp;{{kafka.encryption.props}}"/>

Additionally, we connect to an SFTP server, which I am also trying to mock using Citrus. That follows a similar pattern where:

<from id="_requestFile" uri="{{env:FTP_URL:{{FTP_URL}}}}:{{env:FTP_PORT:{{FTP_PORT}}}}/{{env:FTP_FILE_DIR:{{FTP_FILE_DIR}}}}/?delete=true&amp;fileExist=Append&amp;password={{env:FTP_PASSWORD:{{FTP_PASSWORD}}}}&amp;delay={{env:FTP_POLL_DELAY:{{FTP_POLL_DELAY}}}}&amp;username={{env:FTP_USER:{{FTP_USER}}}}"/>

Inside of my integration test, I have configured a Citrus' EmbeddedKafkaServer class with the following:

 @Bean
    public EmbeddedKafkaServer embeddedKafkaServer() {
        return new EmbeddedKafkaServerBuilder()
                .kafkaServerPort(9092)
                .topics("topic_to_connect_to")
                .build();
    }

and a Citrus FTP server with:

    @Bean
    public SftpServer sftpServer() {
        return CitrusEndpoints.sftp()
                .server()
                .port(2222)
                .autoStart(true)
                .user("username")
                .password("passwordtoconnectwith")
                .userHomePath("filedirectory/filestoreadfrom")
                .build();
    }

Ideally, my test will connect to the mock sftp server, and I will push a file to the appropriate directory using Citrus, which my application will then read in, process, and publish to a topic on the embedded kafka cluster and verify in the test.

I was under the impression that I would set KAFKA_PORT to 9092 and KAFKA_URL to localhost, as well as FTP_URL to localhost and FTP_PORT to 2222 (amongst the other properties needed) inside of my properties file, but that does not seem to connect me to the embedded cluster or sftp servers..

What piece of the puzzle am I missing to have my spring boot application connect to both of these mocked instances and run its' business logic processing from there?


Solution

  • I resolved this issue - it was due to using a very old version of Kafka (1.0.0 or older), which was missing some of the methods that are called when Citrus attempts to build new topics. If someone encounters a similar problem to this using Citrus, I recommend starting with evaluating the version of Kafka your service is on and determining if it needs to be updated.

    For the sftp connection, the server or client was not being autowired, and therefore never starting.