Skip to content

Commit 058fc66

Browse files
committed
Enhancements in Kafka binder FunctionBatchingConversionTests
1 parent fac9eb1 commit 058fc66

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

binders/kafka-binder/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/FunctionBatchingConversionTests.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.charset.StandardCharsets;
2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.Map;
2223
import java.util.concurrent.CountDownLatch;
2324
import java.util.concurrent.TimeUnit;
2425
import java.util.function.Consumer;
@@ -33,6 +34,7 @@
3334
import org.springframework.context.annotation.Configuration;
3435
import org.springframework.kafka.test.context.EmbeddedKafka;
3536
import org.springframework.messaging.Message;
37+
import org.springframework.messaging.support.MessageBuilder;
3638
import org.springframework.test.annotation.DirtiesContext;
3739
import org.springframework.util.Assert;
3840

@@ -56,40 +58,47 @@ public class FunctionBatchingConversionTests {
5658

5759
static CountDownLatch latch = new CountDownLatch(3);
5860

59-
static List<Person> persons = new ArrayList<>();
61+
static List<Map<String, Object>> batchConvertedHeaders = new ArrayList<>();
6062

6163
@Test
6264
void conversionFailuresRemoveTheHeadersProperly() throws Exception {
63-
streamBridge.send("cfrthp-topic", "hello".getBytes(StandardCharsets.UTF_8));
64-
streamBridge.send("cfrthp-topic", "hello".getBytes(StandardCharsets.UTF_8));
65-
streamBridge.send("cfrthp-topic", "{\"name\":\"Ricky\"}".getBytes(StandardCharsets.UTF_8));
66-
streamBridge.send("cfrthp-topic", "{\"name\":\"Julian\"}".getBytes(StandardCharsets.UTF_8));
67-
streamBridge.send("cfrthp-topic", "{\"name\":\"Bubbles\"}".getBytes(StandardCharsets.UTF_8));
65+
streamBridge.send("cfrthp-topic", MessageBuilder.withPayload("hello".getBytes(StandardCharsets.UTF_8))
66+
.setHeader("index", 0).build());
67+
streamBridge.send("cfrthp-topic", MessageBuilder.withPayload("{\"name\":\"Ricky\"}".getBytes(StandardCharsets.UTF_8))
68+
.setHeader("index", 1).build());
69+
streamBridge.send("cfrthp-topic", MessageBuilder.withPayload("{\"name\":\"Julian\"}".getBytes(StandardCharsets.UTF_8))
70+
.setHeader("index", 2).build());
71+
streamBridge.send("cfrthp-topic", MessageBuilder.withPayload("hello".getBytes(StandardCharsets.UTF_8))
72+
.setHeader("index", 3).build());
73+
streamBridge.send("cfrthp-topic", MessageBuilder.withPayload("{\"name\":\"Bubbles\"}".getBytes(StandardCharsets.UTF_8))
74+
.setHeader("index", 4).build());
6875

6976
Assert.isTrue(latch.await(10, TimeUnit.SECONDS), "Failed to receive message");
7077

71-
assertThat(persons.size()).isEqualTo(3);
72-
assertThat(persons.get(0).toString().contains("Ricky")).isTrue();
73-
assertThat(persons.get(1).toString().contains("Julian")).isTrue();
74-
assertThat(persons.get(2).toString().contains("Bubbles")).isTrue();
78+
assertThat(batchConvertedHeaders.size()).isEqualTo(3);
79+
80+
assertThat(batchConvertedHeaders.get(0).get("index")).isEqualTo(1);
81+
assertThat(batchConvertedHeaders.get(1).get("index")).isEqualTo(2);
82+
assertThat(batchConvertedHeaders.get(2).get("index")).isEqualTo(4);
7583
}
7684

7785
@EnableAutoConfiguration
7886
@Configuration
7987
public static class Config {
8088

8189
@Bean
90+
@SuppressWarnings("unchecked")
8291
Consumer<Message<List<Person>>> batchConsumer() {
8392
return message -> {
8493
if (!message.getPayload().isEmpty()) {
94+
List<Map<String, Object>> o = (List<Map<String, Object>>) message.getHeaders().get("kafka_batchConvertedHeaders");
95+
batchConvertedHeaders.addAll(o);
8596
message.getPayload().forEach(c -> {
86-
persons.add(c);
8797
latch.countDown();
8898
});
8999
}
90100
};
91101
}
92-
93102
}
94103

95104
record Person(String name) {

0 commit comments

Comments
 (0)