|
| 1 | +package io.quarkus.kafka.client.runtime; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.when; |
| 6 | + |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Optional; |
| 9 | + |
| 10 | +import org.eclipse.microprofile.config.Config; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import io.quarkus.kafka.client.tls.QuarkusKafkaSslEngineFactory; |
| 14 | +import io.quarkus.runtime.ApplicationConfig; |
| 15 | +import io.smallrye.config.SmallRyeConfig; |
| 16 | +import io.smallrye.config.SmallRyeConfigBuilder; |
| 17 | + |
| 18 | +public class KafkaRuntimeConfigProducerTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testTlsConfigurationNameWithHyphens() { |
| 22 | + // Test when property is set via application.yaml as kafka.tls-configuration-name |
| 23 | + Config config = new SmallRyeConfigBuilder() |
| 24 | + .withDefaultValues(Map.of("kafka.tls-configuration-name", "my-tls-config")) |
| 25 | + .build(); |
| 26 | + ApplicationConfig appConfig = mock(ApplicationConfig.class); |
| 27 | + when(appConfig.name()).thenReturn(Optional.empty()); |
| 28 | + |
| 29 | + KafkaRuntimeConfigProducer producer = new KafkaRuntimeConfigProducer(); |
| 30 | + Map<String, Object> result = producer.createKafkaRuntimeConfig(config, appConfig); |
| 31 | + |
| 32 | + assertThat(result) |
| 33 | + .containsEntry("tls-configuration-name", "my-tls-config") |
| 34 | + .containsEntry("ssl.engine.factory.class", QuarkusKafkaSslEngineFactory.class.getName()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testTlsConfigurationNameWithUnderscores() { |
| 39 | + // Test when property is set via environment variable as KAFKA_TLS_CONFIGURATION_NAME |
| 40 | + Config config = new SmallRyeConfigBuilder() |
| 41 | + .withDefaultValues(Map.of("KAFKA_TLS_CONFIGURATION_NAME", "my-tls-config")) |
| 42 | + .build(); |
| 43 | + ApplicationConfig appConfig = mock(ApplicationConfig.class); |
| 44 | + when(appConfig.name()).thenReturn(Optional.empty()); |
| 45 | + |
| 46 | + KafkaRuntimeConfigProducer producer = new KafkaRuntimeConfigProducer(); |
| 47 | + Map<String, Object> result = producer.createKafkaRuntimeConfig(config, appConfig); |
| 48 | + |
| 49 | + assertThat(result) |
| 50 | + .containsEntry("tls-configuration-name", "my-tls-config") |
| 51 | + .containsEntry("ssl.engine.factory.class", QuarkusKafkaSslEngineFactory.class.getName()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testOtherKafkaProperties() { |
| 56 | + // Test that other Kafka properties are processed correctly |
| 57 | + Config config = new SmallRyeConfigBuilder() |
| 58 | + .withDefaultValues(Map.of("kafka.bootstrap.servers", "localhost:9092", |
| 59 | + "KAFKA_CONSUMER_MAX_POLL_RECORDS", "100")) |
| 60 | + .build(); |
| 61 | + ApplicationConfig appConfig = mock(ApplicationConfig.class); |
| 62 | + when(appConfig.name()).thenReturn(Optional.empty()); |
| 63 | + |
| 64 | + KafkaRuntimeConfigProducer producer = new KafkaRuntimeConfigProducer(); |
| 65 | + Map<String, Object> result = producer.createKafkaRuntimeConfig(config, appConfig); |
| 66 | + |
| 67 | + assertThat(result) |
| 68 | + .containsEntry("bootstrap.servers", "localhost:9092") |
| 69 | + .containsEntry("consumer.max.poll.records", "100"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testGroupIdDefaultsToApplicationName() { |
| 74 | + SmallRyeConfig config = new SmallRyeConfigBuilder() |
| 75 | + .withDefaultValues(Map.of("kafka.bootstrap.servers", "localhost:9092")) |
| 76 | + .build(); |
| 77 | + ApplicationConfig appConfig = mock(ApplicationConfig.class); |
| 78 | + when(appConfig.name()).thenReturn(Optional.of("my-app")); |
| 79 | + |
| 80 | + KafkaRuntimeConfigProducer producer = new KafkaRuntimeConfigProducer(); |
| 81 | + Map<String, Object> result = producer.createKafkaRuntimeConfig(config, appConfig); |
| 82 | + |
| 83 | + assertThat(result) |
| 84 | + .containsEntry("bootstrap.servers", "localhost:9092") |
| 85 | + .containsEntry("group.id", "my-app"); |
| 86 | + } |
| 87 | +} |
0 commit comments