|
| 1 | +/* |
| 2 | + * Copyright 2024-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.stream.binder.kafka; |
| 18 | + |
| 19 | +import java.util.function.Consumer; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.mockito.ArgumentCaptor; |
| 23 | + |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 26 | +import org.springframework.boot.test.context.SpringBootTest; |
| 27 | +import org.springframework.cloud.stream.binder.ExtendedConsumerProperties; |
| 28 | +import org.springframework.cloud.stream.binder.kafka.config.KafkaBinderConfiguration; |
| 29 | +import org.springframework.cloud.stream.binder.kafka.properties.KafkaConsumerProperties; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.context.annotation.Primary; |
| 33 | +import org.springframework.kafka.listener.AbstractMessageListenerContainer; |
| 34 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 35 | +import org.springframework.test.annotation.DirtiesContext; |
| 36 | + |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | +import static org.mockito.ArgumentMatchers.any; |
| 40 | +import static org.mockito.ArgumentMatchers.eq; |
| 41 | +import static org.mockito.ArgumentMatchers.isNull; |
| 42 | +import static org.mockito.Mockito.mock; |
| 43 | +import static org.mockito.Mockito.timeout; |
| 44 | +import static org.mockito.Mockito.verify; |
| 45 | + |
| 46 | +/** |
| 47 | + * @author Soby Chacko |
| 48 | + * @since 4.2.0 |
| 49 | + */ |
| 50 | +@SpringBootTest( |
| 51 | + classes = {KafkaBinderConfiguration.class, KafkaListenerContainerCustomizerTests.TestConfig.class}, |
| 52 | + properties = { |
| 53 | + "spring.cloud.function.definition=testConsumer", |
| 54 | + "spring.cloud.stream.bindings.testConsumer-in-0.destination=test-topic", |
| 55 | + "spring.cloud.stream.bindings.testConsumer-in-0.group=test-group", |
| 56 | + "spring.cloud.stream.kafka.bindings.testConsumer-in-0.consumer.enableDlq=true" |
| 57 | + } |
| 58 | +) |
| 59 | +@DirtiesContext |
| 60 | +@EmbeddedKafka(partitions = 1, topics = "test-topic") |
| 61 | +class KafkaListenerContainerCustomizerTests { |
| 62 | + |
| 63 | + @Autowired |
| 64 | + private KafkaListenerContainerCustomizer compositeCustomizer; |
| 65 | + |
| 66 | + @Test |
| 67 | + @SuppressWarnings("unchecked") |
| 68 | + void customizersInvoked() { |
| 69 | + KafkaListenerContainerCustomizer kafkaCustomizer = |
| 70 | + ((TestConfig.CompositeCustomizer) compositeCustomizer).getKafkaCustomizer(); |
| 71 | + ListenerContainerWithDlqAndRetryCustomizer dlqCustomizer = |
| 72 | + ((TestConfig.CompositeCustomizer) compositeCustomizer).getDlqCustomizer(); |
| 73 | + |
| 74 | + ArgumentCaptor<ExtendedConsumerProperties<KafkaConsumerProperties>> kafkaPropertiesCaptor = ArgumentCaptor.forClass(ExtendedConsumerProperties.class); |
| 75 | + ArgumentCaptor<ExtendedConsumerProperties<KafkaConsumerProperties>> dlqPropertiesCaptor = ArgumentCaptor.forClass(ExtendedConsumerProperties.class); |
| 76 | + |
| 77 | + verify(kafkaCustomizer, timeout(5000).times(1)).configure( |
| 78 | + any(AbstractMessageListenerContainer.class), |
| 79 | + eq("test-topic"), |
| 80 | + eq("test-group"), |
| 81 | + kafkaPropertiesCaptor.capture() |
| 82 | + ); |
| 83 | + |
| 84 | + verify(dlqCustomizer, timeout(5000).times(1)).configure( |
| 85 | + any(AbstractMessageListenerContainer.class), |
| 86 | + eq("test-topic"), |
| 87 | + eq("test-group"), |
| 88 | + isNull(), |
| 89 | + isNull(), |
| 90 | + dlqPropertiesCaptor.capture() |
| 91 | + ); |
| 92 | + |
| 93 | + ExtendedConsumerProperties<KafkaConsumerProperties> kafkaProperties = kafkaPropertiesCaptor.getValue(); |
| 94 | + ExtendedConsumerProperties<KafkaConsumerProperties> dlqProperties = dlqPropertiesCaptor.getValue(); |
| 95 | + |
| 96 | + // Assert common properties |
| 97 | + assertThat(kafkaProperties.getBindingName()).isEqualTo("testConsumer-in-0"); |
| 98 | + assertThat(dlqProperties.getBindingName()).isEqualTo("testConsumer-in-0"); |
| 99 | + |
| 100 | + // Assert Kafka-specific properties |
| 101 | + assertThat(kafkaProperties.getExtension()) |
| 102 | + .satisfies(extension -> { |
| 103 | + assertThat(extension.isEnableDlq()).isTrue(); |
| 104 | + assertThat(extension.isAutoRebalanceEnabled()).isTrue(); |
| 105 | + }); |
| 106 | + |
| 107 | + // Assert that both captured properties are the same instance |
| 108 | + assertThat(kafkaProperties).isSameAs(dlqProperties); |
| 109 | + } |
| 110 | + |
| 111 | + @Configuration |
| 112 | + @EnableAutoConfiguration |
| 113 | + static class TestConfig { |
| 114 | + |
| 115 | + @Bean |
| 116 | + public Consumer<String> testConsumer() { |
| 117 | + return message -> { |
| 118 | + // Do nothing, just to trigger consumer binding |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + @Bean |
| 123 | + @Primary |
| 124 | + public KafkaListenerContainerCustomizer compositeCustomizer() { |
| 125 | + return new CompositeCustomizer( |
| 126 | + mock(KafkaListenerContainerCustomizer.class), |
| 127 | + mock(ListenerContainerWithDlqAndRetryCustomizer.class) |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + static class CompositeCustomizer implements KafkaListenerContainerCustomizer { |
| 132 | + private final KafkaListenerContainerCustomizer kafkaCustomizer; |
| 133 | + private final ListenerContainerWithDlqAndRetryCustomizer dlqCustomizer; |
| 134 | + |
| 135 | + CompositeCustomizer(KafkaListenerContainerCustomizer kafkaCustomizer, |
| 136 | + ListenerContainerWithDlqAndRetryCustomizer dlqCustomizer) { |
| 137 | + this.kafkaCustomizer = kafkaCustomizer; |
| 138 | + this.dlqCustomizer = dlqCustomizer; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void configure(AbstractMessageListenerContainer<?, ?> container, String destinationName, |
| 143 | + String group, ExtendedConsumerProperties<KafkaConsumerProperties> extendedConsumerProperties) { |
| 144 | + kafkaCustomizer.configure(container, destinationName, group, extendedConsumerProperties); |
| 145 | + dlqCustomizer.configure(container, destinationName, group, null, null, extendedConsumerProperties); |
| 146 | + } |
| 147 | + |
| 148 | + public KafkaListenerContainerCustomizer getKafkaCustomizer() { |
| 149 | + return kafkaCustomizer; |
| 150 | + } |
| 151 | + |
| 152 | + public ListenerContainerWithDlqAndRetryCustomizer getDlqCustomizer() { |
| 153 | + return dlqCustomizer; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void configure(AbstractMessageListenerContainer<?, ?> container, String destinationName, String group) { |
| 158 | + |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments