KafkaConsumerAuthInvalidIT.java

Overview

`KafkaConsumerAuthInvalidIT.java` is an integration test file within the Apache Camel Kafka component project. Its primary purpose is to verify that the Kafka consumer in Camel correctly handles authentication failures when connecting to a Kafka broker secured with JAAS+SASL (Simple Authentication and Security Layer).

Specifically, the test sets up a Kafka consumer with **invalid SASL credentials** and asserts that:

This test helps ensure that authentication errors are properly detected and handled in Camel Kafka integrations.


Class: KafkaConsumerAuthInvalidIT

This class contains JUnit 5 integration tests focused on Kafka consumer authentication failure scenarios.

Annotations and Test Configuration


Constants and Fields

Name

Type

Description

`TOPIC`

`String`

Kafka topic name `"test-auth-invalid-it"`

`kafkaAdminClient`

`AdminClient`

Kafka admin client for managing topics/groups

`LOG`

`Logger`

Logger instance

`service`

`KafkaService` (registered extension)

Kafka service container

`contextExtension`

`CamelContextExtension` (registered extension)

Camel context for route and endpoint management

`producer`

KafkaProducer

Kafka producer used to send messages


Lifecycle Methods

before()

setKafkaAdminClient()

after()


Route Setup

createRouteBuilder(CamelContext context)

createRouteBuilder() : RouteBuilder


Test Method

kafkaMessageIsConsumedByCamel()


Important Implementation Details


Interaction with Other Components


Usage Example

This is a test class and thus is run by a test runner (e.g., Maven Surefire or IDE JUnit runner). To execute the test:

  1. Ensure the system properties specify Kafka 3.x:

    • -Dkafka.instance.type=local-kafka3-container

  2. Run the test via Maven or IDE.

  3. The test will:

    • Setup Kafka container with SASL+JAAS authentication.

    • Start a Camel route consuming Kafka with invalid SASL credentials.

    • Verify the consumer fails to authenticate and routes failures to DLQ.


Mermaid Class Diagram

classDiagram
    class KafkaConsumerAuthInvalidIT {
        <<test class>>
        +static final String TOPIC
        -static AdminClient kafkaAdminClient
        -static final Logger LOG
        -static KafkaService service
        -static CamelContextExtension contextExtension
        -KafkaProducer<String,String> producer
        +void before()
        +void setKafkaAdminClient()
        +void after()
        +void createRouteBuilder(CamelContext)
        +RouteBuilder createRouteBuilder()
        +void kafkaMessageIsConsumedByCamel()
    }

Summary

`KafkaConsumerAuthInvalidIT.java` is a focused integration test validating that Apache Camel's Kafka consumer properly handles invalid SASL authentication by failing to consume messages and routing failed exchanges to a dead letter queue. It leverages Kafka test infrastructure, Camel routing, and JUnit 5 features to automate and verify this behavior in a controlled test Kafka environment.