|
| 1 | +/* |
| 2 | + * Copyright 2023 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.kafka.listener; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThatNoException; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.ArgumentMatchers.anyString; |
| 22 | +import static org.mockito.Mockito.never; |
| 23 | +import static org.mockito.Mockito.spy; |
| 24 | +import static org.mockito.Mockito.verify; |
| 25 | + |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Properties; |
| 28 | + |
| 29 | +import org.apache.commons.logging.LogFactory; |
| 30 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +import org.springframework.beans.DirectFieldAccessor; |
| 34 | +import org.springframework.core.log.LogAccessor; |
| 35 | +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| 36 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 37 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 38 | +import org.springframework.kafka.test.utils.KafkaTestUtils; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Gary Russell |
| 42 | + * @since 3.0 |
| 43 | + * |
| 44 | + */ |
| 45 | +@EmbeddedKafka(topics = "mtccac") |
| 46 | +public class MissingTopicCheckOverrideAdminConfigTests { |
| 47 | + |
| 48 | + @Test |
| 49 | + void configOverride(EmbeddedKafkaBroker broker) { |
| 50 | + Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("grp", "false", broker); |
| 51 | + consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "junkjunk"); |
| 52 | + DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps); |
| 53 | + ContainerProperties props = new ContainerProperties("mtccac"); |
| 54 | + props.setMissingTopicsFatal(true); |
| 55 | + props.getKafkaConsumerProperties().setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, |
| 56 | + broker.getBrokersAsString()); |
| 57 | + KafkaMessageListenerContainer<Integer, String> container = new KafkaMessageListenerContainer<>(cf, props) { |
| 58 | + |
| 59 | + @Override |
| 60 | + public void checkTopics() { |
| 61 | + super.checkTopics(); |
| 62 | + } |
| 63 | + |
| 64 | + }; |
| 65 | + LogAccessor logger = spy(new LogAccessor(LogFactory.getLog(getClass()))); |
| 66 | + new DirectFieldAccessor(container).setPropertyValue("logger", logger); |
| 67 | + assertThatNoException().isThrownBy(() -> container.checkTopics()); |
| 68 | + verify(logger, never()).error(any(), anyString()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void configOverrideDefault(EmbeddedKafkaBroker broker) { |
| 73 | + Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("grp", "false", broker); |
| 74 | + consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "junkjunk"); |
| 75 | + DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps); |
| 76 | + ContainerProperties props = new ContainerProperties("mtccac"); |
| 77 | + props.setMissingTopicsFatal(true); |
| 78 | + /* |
| 79 | + * Ensure this works if there are property defaults. |
| 80 | + * We have to iterate over the hash table because the user might have |
| 81 | + * used put() instead of setProperty(). |
| 82 | + */ |
| 83 | + Properties defaultProperties = new Properties(); |
| 84 | + defaultProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, broker.getBrokersAsString()); |
| 85 | + Properties properties = new Properties(defaultProperties); |
| 86 | + props.setKafkaConsumerProperties(properties); |
| 87 | + KafkaMessageListenerContainer<Integer, String> container = new KafkaMessageListenerContainer<>(cf, props) { |
| 88 | + |
| 89 | + @Override |
| 90 | + public void checkTopics() { |
| 91 | + super.checkTopics(); |
| 92 | + } |
| 93 | + |
| 94 | + }; |
| 95 | + LogAccessor logger = spy(new LogAccessor(LogFactory.getLog(getClass()))); |
| 96 | + new DirectFieldAccessor(container).setPropertyValue("logger", logger); |
| 97 | + assertThatNoException().isThrownBy(() -> container.checkTopics()); |
| 98 | + verify(logger, never()).error(any(), anyString()); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments