|
1 | 1 | /*
|
2 |
| - * Copyright 2020-2022 the original author or authors. |
| 2 | + * Copyright 2020-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
68 | 68 | import org.springframework.kafka.core.KafkaOperations.OperationsCallback;
|
69 | 69 | import org.springframework.kafka.core.ProducerFactory;
|
70 | 70 | import org.springframework.kafka.listener.DeadLetterPublishingRecoverer.HeaderNames;
|
| 71 | +import org.springframework.kafka.listener.DeadLetterPublishingRecoverer.SingleRecordHeader; |
71 | 72 | import org.springframework.kafka.support.KafkaHeaders;
|
72 | 73 | import org.springframework.kafka.support.SendResult;
|
73 | 74 | import org.springframework.kafka.support.converter.ConversionException;
|
@@ -878,6 +879,33 @@ void immutableHeaders() {
|
878 | 879 | assertThat(KafkaTestUtils.getPropertyValue(headers, "headers", List.class)).hasSize(12);
|
879 | 880 | }
|
880 | 881 |
|
| 882 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 883 | + @Test |
| 884 | + void replaceNotAppendHeader() { |
| 885 | + KafkaOperations<?, ?> template = mock(KafkaOperations.class); |
| 886 | + CompletableFuture future = mock(CompletableFuture.class); |
| 887 | + given(template.send(any(ProducerRecord.class))).willReturn(future); |
| 888 | + Headers headers = new RecordHeaders().add(new RecordHeader("foo", "orig".getBytes())); |
| 889 | + ConsumerRecord<String, String> record = new ConsumerRecord<>("foo", 0, 0L, 0L, TimestampType.NO_TIMESTAMP_TYPE, |
| 890 | + -1, -1, null, "bar", headers, Optional.empty()); |
| 891 | + DeadLetterPublishingRecoverer recoverer = new DeadLetterPublishingRecoverer(template); |
| 892 | + recoverer.setHeadersFunction((rec, ex) -> { |
| 893 | + RecordHeaders toReplace = new RecordHeaders( |
| 894 | + new RecordHeader[] { new SingleRecordHeader("foo", "one".getBytes()) }); |
| 895 | + return toReplace; |
| 896 | + }); |
| 897 | + recoverer.accept(record, new ListenerExecutionFailedException("test", "group", new RuntimeException())); |
| 898 | + ArgumentCaptor<ProducerRecord> producerRecordCaptor = ArgumentCaptor.forClass(ProducerRecord.class); |
| 899 | + verify(template).send(producerRecordCaptor.capture()); |
| 900 | + ProducerRecord outRecord = producerRecordCaptor.getValue(); |
| 901 | + Headers outHeaders = outRecord.headers(); |
| 902 | + assertThat(KafkaTestUtils.getPropertyValue(outHeaders, "headers", List.class)).hasSize(11); |
| 903 | + Iterator<Header> iterator = outHeaders.headers("foo").iterator(); |
| 904 | + assertThat(iterator.hasNext()).isTrue(); |
| 905 | + assertThat(iterator.next().value()).isEqualTo("one".getBytes()); |
| 906 | + assertThat(iterator.hasNext()).isFalse(); |
| 907 | + } |
| 908 | + |
881 | 909 | @SuppressWarnings("unchecked")
|
882 | 910 | @Test
|
883 | 911 | void nonCompliantProducerFactory() throws Exception {
|
|
0 commit comments