|
65 | 65 | import org.junit.jupiter.api.AfterAll;
|
66 | 66 | import org.junit.jupiter.api.BeforeAll;
|
67 | 67 | import org.junit.jupiter.api.Test;
|
| 68 | +import org.junit.jupiter.params.ParameterizedTest; |
| 69 | +import org.junit.jupiter.params.provider.NullSource; |
| 70 | +import org.junit.jupiter.params.provider.ValueSource; |
68 | 71 |
|
69 | 72 | import org.springframework.aop.framework.ProxyFactory;
|
70 | 73 | import org.springframework.kafka.KafkaException;
|
|
94 | 97 | * @author Biju Kunjummen
|
95 | 98 | * @author Endika Gutierrez
|
96 | 99 | * @author Thomas Strauß
|
| 100 | + * @author Gurps Bassi |
97 | 101 | */
|
98 | 102 | @EmbeddedKafka(topics = { KafkaTemplateTests.INT_KEY_TOPIC, KafkaTemplateTests.STRING_KEY_TOPIC })
|
99 | 103 | public class KafkaTemplateTests {
|
@@ -153,6 +157,12 @@ void testTemplate() {
|
153 | 157 |
|
154 | 158 | template.setDefaultTopic(INT_KEY_TOPIC);
|
155 | 159 |
|
| 160 | + template.setConsumerFactory( |
| 161 | + new DefaultKafkaConsumerFactory<>(KafkaTestUtils.consumerProps("xx", "false", embeddedKafka))); |
| 162 | + ConsumerRecords<Integer, String> initialRecords = |
| 163 | + template.receive(Collections.singleton(new TopicPartitionOffset(INT_KEY_TOPIC, 1, 1L))); |
| 164 | + assertThat(initialRecords).isEmpty(); |
| 165 | + |
156 | 166 | template.sendDefault("foo");
|
157 | 167 | assertThat(KafkaTestUtils.getSingleRecord(consumer, INT_KEY_TOPIC)).has(value("foo"));
|
158 | 168 |
|
@@ -191,8 +201,7 @@ void testTemplate() {
|
191 | 201 | assertThat(partitions).isNotNull();
|
192 | 202 | assertThat(partitions).hasSize(2);
|
193 | 203 | assertThat(KafkaTestUtils.getPropertyValue(pf.createProducer(), "delegate")).isSameAs(wrapped.get());
|
194 |
| - template.setConsumerFactory( |
195 |
| - new DefaultKafkaConsumerFactory<>(KafkaTestUtils.consumerProps("xx", "false", embeddedKafka))); |
| 204 | + |
196 | 205 | ConsumerRecord<Integer, String> receive = template.receive(INT_KEY_TOPIC, 1, received.offset());
|
197 | 206 | assertThat(receive).has(allOf(keyValue(2, "buz"), partition(1)))
|
198 | 207 | .extracting(ConsumerRecord::offset)
|
@@ -554,4 +563,21 @@ void testFutureFailureOnSend() {
|
554 | 563 | pf.destroy();
|
555 | 564 | }
|
556 | 565 |
|
| 566 | + @ParameterizedTest(name = "{0} is invalid") |
| 567 | + @NullSource |
| 568 | + @ValueSource(longs = -1) |
| 569 | + void testReceiveWhenOffsetIsInvalid(Long offset) { |
| 570 | + Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); |
| 571 | + DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); |
| 572 | + KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); |
| 573 | + |
| 574 | + template.setConsumerFactory( |
| 575 | + new DefaultKafkaConsumerFactory<>(KafkaTestUtils.consumerProps("xx", "false", embeddedKafka))); |
| 576 | + TopicPartitionOffset tpoWithNullOffset = new TopicPartitionOffset(INT_KEY_TOPIC, 1, offset); |
| 577 | + |
| 578 | + assertThatExceptionOfType(KafkaException.class) |
| 579 | + .isThrownBy(() -> template.receive(Collections.singleton(tpoWithNullOffset))) |
| 580 | + .withMessage("Offset supplied in TopicPartitionOffset is invalid: " + tpoWithNullOffset); |
| 581 | + } |
| 582 | + |
557 | 583 | }
|
0 commit comments