KafkaConsumerAuthManualTest.java

Overview

`KafkaConsumerAuthManualTest.java` is a JUnit integration test class designed to validate the ability of Apache Camel's Kafka component to authenticate and consume messages from a remote Kafka cluster secured with SASL authentication.

This test class connects to a Kafka broker specified through system properties, configures authentication parameters (username, password, SASL mechanism, security protocol), and verifies that messages produced to a secure Kafka topic are correctly consumed by a Camel route. The test ensures that Camel’s Kafka consumer can handle authentication and message consumption end-to-end against a real Kafka cluster with security enabled.

The class uses Camel's testing infrastructure (`CamelContextExtension`) and Kafka client libraries to produce and consume messages. It also leverages Awaitility to wait asynchronously for message consumption.


Class: KafkaConsumerAuthManualTest

Description

This is a JUnit 5 test class that performs manual testing of Kafka consumer authentication using SASL mechanisms in a secure environment. It runs only if required system properties (e.g., Kafka bootstrap servers, topic, username, password) are correctly provided, enabling flexible manual test execution.

Annotations


Constants / Fields

Field

Description

`TOPIC`

Kafka topic to consume messages from, read from system property `kafka.manual.test.topic`.

`BOOTSTRAP_SERVERS`

Kafka bootstrap servers URL, read from system property `bootstrapServers`.

`USERNAME`

Kafka SASL username, read from system property `kafka.manual.test.username`.

`PASSWORD`

Kafka SASL password, read from system property `kafka.manual.test.password`.

`SECURITY_PROTOCOL`

Kafka security protocol (default: "SASL_PLAINTEXT").

`SASL_MECHANISM`

SASL mechanism (default: "PLAIN").

`MESSAGE_COUNT`

Number of test messages to consume (default: 5).

`contextExtension`

CamelContext extension to manage Camel lifecycle in tests.

`receivedMessages`

Volatile counter to track number of consumed messages.

`producer`

Kafka producer instance used to send messages in tests.


Methods

protected Properties getDefaultProperties()

Returns a `Properties` object with Kafka client configuration, including SASL authentication settings.

Properties props = getDefaultProperties();
// Use props to configure KafkaProducer or KafkaConsumer

@BeforeEach public void before()

Sets up the Kafka producer before each test method.


@AfterEach public void after()

Cleans up resources after each test.


@RouteFixture public void createRouteBuilder(CamelContext context)

Adds the Camel route to the Camel context before tests.


protected RouteBuilder createRouteBuilder()

Creates and returns a Camel `RouteBuilder` defining the Kafka consumer route.

RouteBuilder routeBuilder = createRouteBuilder();
camelContext.addRoutes(routeBuilder);

@DisplayName("Tests that Camel can adequately connect and consume from an authenticated remote Kafka instance")

`@Test public void kafkaMessageIsConsumedByCamel() throws InterruptedException`

Main integration test method.


Implementation Details and Algorithms


Interaction with Other Components


Usage Example

To run this integration test manually, supply the following JVM system properties:

-DbootstrapServers=localhost:9093
-Dkafka.manual.test.topic=test-topic
-Dkafka.manual.test.username=testuser
-Dkafka.manual.test.password=testpassword
-Dkafka.manual.test.security.protocol=SASL_PLAINTEXT
-Dkafka.manual.test.sasl.mechanism=PLAIN
-Dkafka.manual.test.message.count=5

Then execute the test via Maven or your IDE. The test will produce messages externally (outside this class) and verify that Camel consumes them correctly.


Mermaid Class Diagram

classDiagram
    class KafkaConsumerAuthManualTest {
        -static final String TOPIC
        -static final String BOOTSTRAP_SERVERS
        -static final String USERNAME
        -static final String PASSWORD
        -static final String SECURITY_PROTOCOL
        -static final String SASL_MECHANISM
        -static final int MESSAGE_COUNT
        -static CamelContextExtension contextExtension
        -volatile int receivedMessages
        -KafkaProducer<String,String> producer
        +Properties getDefaultProperties()
        +void before()
        +void after()
        +void createRouteBuilder(CamelContext)
        +RouteBuilder createRouteBuilder()
        +void kafkaMessageIsConsumedByCamel() throws InterruptedException
    }

Summary

`KafkaConsumerAuthManualTest.java` is a focused integration test class that verifies Apache Camel's Kafka consumer can authenticate using SASL and consume messages from a secured Kafka cluster. It establishes a Camel route with SASL configuration, uses a Kafka producer to send messages, and asserts message consumption via Camel's testing framework. This class is intended for manual or CI environments where secure Kafka clusters are available and credentials are supplied via system properties.