|
| 1 | +/* |
| 2 | + * Copyright 2022 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.retrytopic; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.BDDMockito.given; |
| 21 | + |
| 22 | +import java.lang.reflect.Method; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | +import java.util.function.Predicate; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 31 | + |
| 32 | +import org.springframework.beans.factory.BeanFactory; |
| 33 | +import org.springframework.kafka.config.MethodKafkaListenerEndpoint; |
| 34 | +import org.springframework.kafka.support.EndpointHandlerMethod; |
| 35 | +import org.springframework.kafka.support.TopicPartitionOffset; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Tomaz Fernandes |
| 39 | + * @since 2.8.5 |
| 40 | + */ |
| 41 | +@ExtendWith(MockitoExtension.class) |
| 42 | +class EndpointCustomizerFactoryTests { |
| 43 | + |
| 44 | + @Mock |
| 45 | + private DestinationTopic.Properties properties; |
| 46 | + |
| 47 | + @Mock |
| 48 | + private EndpointHandlerMethod beanMethod; |
| 49 | + |
| 50 | + @Mock |
| 51 | + private BeanFactory beanFactory; |
| 52 | + |
| 53 | + @Mock |
| 54 | + private RetryTopicNamesProviderFactory retryTopicNamesProviderFactory; |
| 55 | + |
| 56 | + @Mock |
| 57 | + private MethodKafkaListenerEndpoint<?, ?> endpoint; |
| 58 | + |
| 59 | + private final String[] topics = {"myTopic1", "myTopic2"}; |
| 60 | + |
| 61 | + private final Method method = EndpointCustomizerFactory.class.getDeclaredMethods()[0]; |
| 62 | + |
| 63 | + @Test |
| 64 | + void shouldNotCustomizeEndpointForMainTopicWithTopics() { |
| 65 | + |
| 66 | + given(beanMethod.resolveBean(this.beanFactory)).willReturn(method); |
| 67 | + given(endpoint.getTopics()).willReturn(Arrays.asList(topics)); |
| 68 | + given(properties.suffix()).willReturn(""); |
| 69 | + RetryTopicNamesProviderFactory.RetryTopicNamesProvider provider = |
| 70 | + new SuffixingRetryTopicNamesProviderFactory().createRetryTopicNamesProvider(properties); |
| 71 | + given(retryTopicNamesProviderFactory.createRetryTopicNamesProvider(properties)).willReturn(provider); |
| 72 | + |
| 73 | + EndpointCustomizer endpointCustomizer = new EndpointCustomizerFactory(properties, beanMethod, |
| 74 | + beanFactory, retryTopicNamesProviderFactory).createEndpointCustomizer(); |
| 75 | + |
| 76 | + List<EndpointCustomizer.TopicNamesHolder> holders = |
| 77 | + (List<EndpointCustomizer.TopicNamesHolder>) endpointCustomizer.customizeEndpointAndCollectTopics(endpoint); |
| 78 | + |
| 79 | + assertThat(holders).hasSize(2).element(0) |
| 80 | + .matches(assertMainTopic(0)); |
| 81 | + assertThat(holders).element(1) |
| 82 | + .matches(assertMainTopic(1)); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void shouldNotCustomizeEndpointForMainTopicWithTPO() { |
| 88 | + |
| 89 | + given(beanMethod.resolveBean(this.beanFactory)).willReturn(method); |
| 90 | + given(properties.isMainEndpoint()).willReturn(true); |
| 91 | + given(properties.suffix()).willReturn(""); |
| 92 | + RetryTopicNamesProviderFactory.RetryTopicNamesProvider provider = |
| 93 | + new SuffixingRetryTopicNamesProviderFactory().createRetryTopicNamesProvider(properties); |
| 94 | + given(retryTopicNamesProviderFactory.createRetryTopicNamesProvider(properties)).willReturn(provider); |
| 95 | + |
| 96 | + String testString = "testString"; |
| 97 | + MethodKafkaListenerEndpoint<Object, Object> endpointTPO = new MethodKafkaListenerEndpoint<>(); |
| 98 | + endpointTPO.setTopicPartitions(new TopicPartitionOffset(topics[0], 0, 0L), |
| 99 | + new TopicPartitionOffset(topics[1], 1, 1L)); |
| 100 | + endpointTPO.setMethod(this.method); |
| 101 | + endpointTPO.setId(testString); |
| 102 | + endpointTPO.setClientIdPrefix(testString); |
| 103 | + endpointTPO.setGroup(testString); |
| 104 | + |
| 105 | + EndpointCustomizer endpointCustomizer = new EndpointCustomizerFactory(properties, beanMethod, |
| 106 | + beanFactory, retryTopicNamesProviderFactory).createEndpointCustomizer(); |
| 107 | + |
| 108 | + List<EndpointCustomizer.TopicNamesHolder> holders = |
| 109 | + (List<EndpointCustomizer.TopicNamesHolder>) endpointCustomizer.customizeEndpointAndCollectTopics(endpointTPO); |
| 110 | + |
| 111 | + assertThat(holders).hasSize(2).element(0) |
| 112 | + .matches(assertMainTopic(0)); |
| 113 | + assertThat(holders).element(1) |
| 114 | + .matches(assertMainTopic(1)); |
| 115 | + |
| 116 | + assertThat(endpointTPO.getTopics()) |
| 117 | + .isEmpty(); |
| 118 | + |
| 119 | + TopicPartitionOffset[] topicPartitionsToAssign = endpointTPO.getTopicPartitionsToAssign(); |
| 120 | + assertThat(topicPartitionsToAssign).hasSize(2); |
| 121 | + assertThat(equalsTopicPartitionOffset(topicPartitionsToAssign[0], |
| 122 | + new TopicPartitionOffset(topics[0], 0, 0L))).isTrue(); |
| 123 | + assertThat(equalsTopicPartitionOffset(topicPartitionsToAssign[1], |
| 124 | + new TopicPartitionOffset(topics[1], 1, 1L))).isTrue(); |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + private Predicate<EndpointCustomizer.TopicNamesHolder> assertMainTopic(int index) { |
| 129 | + return holder -> holder.getCustomizedTopic().equals(topics[index]) |
| 130 | + && holder.getMainTopic().equals(topics[index]); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void shouldCustomizeEndpointForRetryTopic() { |
| 135 | + |
| 136 | + MethodKafkaListenerEndpoint<Object, Object> endpoint = new MethodKafkaListenerEndpoint<>(); |
| 137 | + String testString = "testString"; |
| 138 | + endpoint.setTopics(this.topics); |
| 139 | + endpoint.setMethod(this.method); |
| 140 | + endpoint.setId(testString); |
| 141 | + endpoint.setClientIdPrefix(testString); |
| 142 | + endpoint.setGroup(testString); |
| 143 | + |
| 144 | + MethodKafkaListenerEndpoint<Object, Object> endpointTPO = new MethodKafkaListenerEndpoint<>(); |
| 145 | + endpointTPO.setTopicPartitions(new TopicPartitionOffset(topics[0], 0, 0L), |
| 146 | + new TopicPartitionOffset(topics[1], 1, 1L)); |
| 147 | + endpointTPO.setMethod(this.method); |
| 148 | + endpointTPO.setId(testString); |
| 149 | + endpointTPO.setClientIdPrefix(testString); |
| 150 | + endpointTPO.setGroup(testString); |
| 151 | + |
| 152 | + String suffix = "-retry"; |
| 153 | + given(beanMethod.resolveBean(this.beanFactory)).willReturn(method); |
| 154 | + given(properties.isMainEndpoint()).willReturn(false); |
| 155 | + given(properties.suffix()).willReturn(suffix); |
| 156 | + given(properties.numPartitions()).willReturn(2); |
| 157 | + |
| 158 | + RetryTopicNamesProviderFactory.RetryTopicNamesProvider provider = |
| 159 | + new SuffixingRetryTopicNamesProviderFactory().createRetryTopicNamesProvider(properties); |
| 160 | + given(retryTopicNamesProviderFactory.createRetryTopicNamesProvider(properties)).willReturn(provider); |
| 161 | + |
| 162 | + EndpointCustomizer endpointCustomizer = new EndpointCustomizerFactory(properties, beanMethod, |
| 163 | + beanFactory, retryTopicNamesProviderFactory).createEndpointCustomizer(); |
| 164 | + |
| 165 | + List<EndpointCustomizer.TopicNamesHolder> holders = |
| 166 | + (List<EndpointCustomizer.TopicNamesHolder>) endpointCustomizer.customizeEndpointAndCollectTopics(endpoint); |
| 167 | + |
| 168 | + String topic1WithSuffix = topics[0] + suffix; |
| 169 | + String topic2WithSuffix = topics[1] + suffix; |
| 170 | + assertThat(holders).hasSize(2).element(0) |
| 171 | + .matches(holder -> holder.getMainTopic().equals(topics[0]) |
| 172 | + && holder.getCustomizedTopic().equals(topic1WithSuffix)); |
| 173 | + assertThat(holders).hasSize(2).element(1) |
| 174 | + .matches(holder -> holder.getMainTopic().equals(topics[1]) |
| 175 | + && holder.getCustomizedTopic().equals(topic2WithSuffix)); |
| 176 | + |
| 177 | + String testStringSuffix = testString + suffix; |
| 178 | + |
| 179 | + assertThat(endpoint.getTopics()) |
| 180 | + .contains(topic1WithSuffix, topic2WithSuffix); |
| 181 | + assertThat(endpoint.getId()) |
| 182 | + .isEqualTo(testStringSuffix); |
| 183 | + assertThat(endpoint.getClientIdPrefix()) |
| 184 | + .isEqualTo(testStringSuffix); |
| 185 | + assertThat(endpoint.getGroup()) |
| 186 | + .isEqualTo(testStringSuffix); |
| 187 | + assertThat(endpoint.getTopicPartitionsToAssign()).isEmpty(); |
| 188 | + |
| 189 | + List<EndpointCustomizer.TopicNamesHolder> holdersTPO = |
| 190 | + (List<EndpointCustomizer.TopicNamesHolder>) endpointCustomizer.customizeEndpointAndCollectTopics(endpointTPO); |
| 191 | + |
| 192 | + assertThat(holdersTPO).hasSize(2).element(0) |
| 193 | + .matches(holder -> holder.getMainTopic().equals(topics[0]) |
| 194 | + && holder.getCustomizedTopic().equals(topic1WithSuffix)); |
| 195 | + assertThat(holdersTPO).hasSize(2).element(1) |
| 196 | + .matches(holder -> holder.getMainTopic().equals(topics[1]) |
| 197 | + && holder.getCustomizedTopic().equals(topic2WithSuffix)); |
| 198 | + |
| 199 | + assertThat(endpointTPO.getTopics()) |
| 200 | + .isEmpty(); |
| 201 | + |
| 202 | + TopicPartitionOffset[] topicPartitionsToAssign = endpointTPO.getTopicPartitionsToAssign(); |
| 203 | + assertThat(topicPartitionsToAssign).hasSize(2); |
| 204 | + assertThat(equalsTopicPartitionOffset(topicPartitionsToAssign[0], |
| 205 | + new TopicPartitionOffset(topic1WithSuffix, 0, (Long) null))).isTrue(); |
| 206 | + assertThat(equalsTopicPartitionOffset(topicPartitionsToAssign[1], |
| 207 | + new TopicPartitionOffset(topic2WithSuffix, 1, (Long) null))).isTrue(); |
| 208 | + |
| 209 | + assertThat(endpointTPO.getId()) |
| 210 | + .isEqualTo(testStringSuffix); |
| 211 | + assertThat(endpointTPO.getClientIdPrefix()) |
| 212 | + .isEqualTo(testStringSuffix); |
| 213 | + assertThat(endpointTPO.getGroup()) |
| 214 | + .isEqualTo(testStringSuffix); |
| 215 | + } |
| 216 | + |
| 217 | + private boolean equalsTopicPartitionOffset(TopicPartitionOffset tpo1, TopicPartitionOffset tpo2) { |
| 218 | + return tpo1.getTopicPartition().equals(tpo2.getTopicPartition()) && |
| 219 | + ((tpo1.getOffset() == null && tpo2.getOffset() == null) || |
| 220 | + (tpo1.getOffset() != null && tpo1.getOffset().equals(tpo2.getOffset()))); |
| 221 | + |
| 222 | + } |
| 223 | +} |
0 commit comments