|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 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.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeAll; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.ObjectProvider; |
| 27 | +import org.springframework.boot.autoconfigure.kafka.KafkaProperties; |
| 28 | +import org.springframework.cloud.stream.binder.ExtendedProducerProperties; |
| 29 | +import org.springframework.cloud.stream.binder.TestUtils; |
| 30 | +import org.springframework.cloud.stream.binder.kafka.config.ClientFactoryCustomizer; |
| 31 | +import org.springframework.cloud.stream.binder.kafka.properties.KafkaBinderConfigurationProperties; |
| 32 | +import org.springframework.cloud.stream.binder.kafka.provisioning.KafkaTopicProvisioner; |
| 33 | +import org.springframework.context.support.GenericApplicationContext; |
| 34 | +import org.springframework.kafka.core.ProducerFactory; |
| 35 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 36 | +import org.springframework.kafka.test.condition.EmbeddedKafkaCondition; |
| 37 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 38 | +import org.springframework.kafka.transaction.KafkaTransactionManager; |
| 39 | +import org.springframework.retry.support.RetryTemplate; |
| 40 | + |
| 41 | +import static org.assertj.core.api.Assertions.assertThat; |
| 42 | +import static org.mockito.ArgumentMatchers.any; |
| 43 | +import static org.mockito.ArgumentMatchers.eq; |
| 44 | +import static org.mockito.ArgumentMatchers.isNull; |
| 45 | +import static org.mockito.Mockito.mock; |
| 46 | +import static org.mockito.Mockito.spy; |
| 47 | +import static org.mockito.Mockito.verify; |
| 48 | + |
| 49 | +/** |
| 50 | + * @author Soby Chacko |
| 51 | + */ |
| 52 | +@EmbeddedKafka(count = 1, controlledShutdown = true, brokerProperties = {"transaction.state.log.replication.factor=1", |
| 53 | + "transaction.state.log.min.isr=1"}) |
| 54 | +class KafkaBinderTransactionCustomizerTest { |
| 55 | + |
| 56 | + private static EmbeddedKafkaBroker embeddedKafka; |
| 57 | + |
| 58 | + @BeforeAll |
| 59 | + public static void setup() { |
| 60 | + embeddedKafka = EmbeddedKafkaCondition.getBroker(); |
| 61 | + } |
| 62 | + |
| 63 | + @SuppressWarnings("unchecked") |
| 64 | + @Test |
| 65 | + void clientFactoryCustomizerAppliedBeforeTransactionManager() throws Exception { |
| 66 | + KafkaProperties kafkaProperties = new TestKafkaProperties(); |
| 67 | + kafkaProperties.setBootstrapServers(Collections |
| 68 | + .singletonList(embeddedKafka.getBrokersAsString())); |
| 69 | + |
| 70 | + KafkaBinderConfigurationProperties configurationProperties = new KafkaBinderConfigurationProperties( |
| 71 | + kafkaProperties, mock(ObjectProvider.class)); |
| 72 | + configurationProperties.getTransaction().setTransactionIdPrefix("custom-tx-"); |
| 73 | + |
| 74 | + KafkaTopicProvisioner provisioningProvider = new KafkaTopicProvisioner( |
| 75 | + configurationProperties, kafkaProperties, prop -> { |
| 76 | + }); |
| 77 | + provisioningProvider.setMetadataRetryOperations(new RetryTemplate()); |
| 78 | + |
| 79 | + // Create a tracking list for customized factories |
| 80 | + List<ProducerFactory<?, ?>> customizedFactories = new ArrayList<>(); |
| 81 | + |
| 82 | + // Create a customizer that we'll register after the binder is created |
| 83 | + ClientFactoryCustomizer customizer = new ClientFactoryCustomizer() { |
| 84 | + @Override |
| 85 | + public void configure(ProducerFactory<?, ?> pf) { |
| 86 | + customizedFactories.add(pf); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + KafkaMessageChannelBinder binder = spy(new KafkaMessageChannelBinder( |
| 91 | + configurationProperties, provisioningProvider)); |
| 92 | + |
| 93 | + GenericApplicationContext applicationContext = new GenericApplicationContext(); |
| 94 | + applicationContext.refresh(); |
| 95 | + binder.setApplicationContext(applicationContext); |
| 96 | + |
| 97 | + // Add the customizer AFTER binder creation but BEFORE afterPropertiesSet |
| 98 | + binder.addClientFactoryCustomizer(customizer); |
| 99 | + |
| 100 | + // Now initialize the binder (this triggers onInit) |
| 101 | + binder.afterPropertiesSet(); |
| 102 | + |
| 103 | + // Verify KafkaMessageChannelBinder.getProducerFactory was called from onInit |
| 104 | + verify(binder).getProducerFactory( |
| 105 | + eq("custom-tx-"), |
| 106 | + any(ExtendedProducerProperties.class), |
| 107 | + eq("custom-tx-.producer"), |
| 108 | + isNull()); |
| 109 | + |
| 110 | + // Verify customizer was applied |
| 111 | + assertThat(customizedFactories).isNotEmpty(); |
| 112 | + |
| 113 | + // Verify that the producer factory from the transaction manager is in our list of customized factories |
| 114 | + KafkaTransactionManager<?, ?> txManager = (KafkaTransactionManager<?, ?>) |
| 115 | + TestUtils.getPropertyValue(binder, "transactionManager"); |
| 116 | + assertThat(txManager).isNotNull(); |
| 117 | + ProducerFactory<?, ?> producerFactory = txManager.getProducerFactory(); |
| 118 | + // This verifies that the same producer factory that was customized is used for the transaction manager |
| 119 | + assertThat(customizedFactories).contains(producerFactory); |
| 120 | + } |
| 121 | + |
| 122 | + @SuppressWarnings("unchecked") |
| 123 | + @Test |
| 124 | + void multipleCustomizersAppliedInOrder() throws Exception { |
| 125 | + KafkaProperties kafkaProperties = new TestKafkaProperties(); |
| 126 | + kafkaProperties.setBootstrapServers(Collections |
| 127 | + .singletonList(embeddedKafka.getBrokersAsString())); |
| 128 | + |
| 129 | + KafkaBinderConfigurationProperties configurationProperties = new KafkaBinderConfigurationProperties( |
| 130 | + kafkaProperties, mock(ObjectProvider.class)); |
| 131 | + configurationProperties.getTransaction().setTransactionIdPrefix("multi-tx-"); |
| 132 | + |
| 133 | + KafkaTopicProvisioner provisioningProvider = new KafkaTopicProvisioner( |
| 134 | + configurationProperties, kafkaProperties, prop -> { |
| 135 | + }); |
| 136 | + provisioningProvider.setMetadataRetryOperations(new RetryTemplate()); |
| 137 | + |
| 138 | + // Track order of customizers and customized factories |
| 139 | + List<String> customizationOrder = new ArrayList<>(); |
| 140 | + List<ProducerFactory<?, ?>> customizedFactories = new ArrayList<>(); |
| 141 | + |
| 142 | + KafkaMessageChannelBinder binder = spy(new KafkaMessageChannelBinder( |
| 143 | + configurationProperties, provisioningProvider)); |
| 144 | + |
| 145 | + GenericApplicationContext applicationContext = new GenericApplicationContext(); |
| 146 | + applicationContext.refresh(); |
| 147 | + binder.setApplicationContext(applicationContext); |
| 148 | + |
| 149 | + // Add multiple customizers |
| 150 | + binder.addClientFactoryCustomizer(new ClientFactoryCustomizer() { |
| 151 | + @Override |
| 152 | + public void configure(ProducerFactory<?, ?> pf) { |
| 153 | + customizationOrder.add("customizer1"); |
| 154 | + customizedFactories.add(pf); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + binder.addClientFactoryCustomizer(new ClientFactoryCustomizer() { |
| 159 | + @Override |
| 160 | + public void configure(ProducerFactory<?, ?> pf) { |
| 161 | + customizationOrder.add("customizer2"); |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + binder.addClientFactoryCustomizer(new ClientFactoryCustomizer() { |
| 166 | + @Override |
| 167 | + public void configure(ProducerFactory<?, ?> pf) { |
| 168 | + customizationOrder.add("customizer3"); |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + binder.afterPropertiesSet(); |
| 173 | + |
| 174 | + assertThat(customizationOrder).containsExactly("customizer1", "customizer2", "customizer3"); |
| 175 | + |
| 176 | + KafkaTransactionManager<?, ?> txManager = (KafkaTransactionManager<?, ?>) |
| 177 | + TestUtils.getPropertyValue(binder, "transactionManager"); |
| 178 | + assertThat(txManager).isNotNull(); |
| 179 | + ProducerFactory<?, ?> producerFactory = txManager.getProducerFactory(); |
| 180 | + // Verify that the producer factory used in transaction manager is one that was customized |
| 181 | + assertThat(customizedFactories).contains(producerFactory); |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments