19
19
import java .nio .charset .StandardCharsets ;
20
20
import java .util .ArrayList ;
21
21
import java .util .List ;
22
+ import java .util .Map ;
22
23
import java .util .concurrent .CountDownLatch ;
23
24
import java .util .concurrent .TimeUnit ;
24
25
import java .util .function .Consumer ;
33
34
import org .springframework .context .annotation .Configuration ;
34
35
import org .springframework .kafka .test .context .EmbeddedKafka ;
35
36
import org .springframework .messaging .Message ;
37
+ import org .springframework .messaging .support .MessageBuilder ;
36
38
import org .springframework .test .annotation .DirtiesContext ;
37
39
import org .springframework .util .Assert ;
38
40
@@ -56,40 +58,47 @@ public class FunctionBatchingConversionTests {
56
58
57
59
static CountDownLatch latch = new CountDownLatch (3 );
58
60
59
- static List <Person > persons = new ArrayList <>();
61
+ static List <Map < String , Object >> batchConvertedHeaders = new ArrayList <>();
60
62
61
63
@ Test
62
64
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 ());
68
75
69
76
Assert .isTrue (latch .await (10 , TimeUnit .SECONDS ), "Failed to receive message" );
70
77
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 );
75
83
}
76
84
77
85
@ EnableAutoConfiguration
78
86
@ Configuration
79
87
public static class Config {
80
88
81
89
@ Bean
90
+ @ SuppressWarnings ("unchecked" )
82
91
Consumer <Message <List <Person >>> batchConsumer () {
83
92
return message -> {
84
93
if (!message .getPayload ().isEmpty ()) {
94
+ List <Map <String , Object >> o = (List <Map <String , Object >>) message .getHeaders ().get ("kafka_batchConvertedHeaders" );
95
+ batchConvertedHeaders .addAll (o );
85
96
message .getPayload ().forEach (c -> {
86
- persons .add (c );
87
97
latch .countDown ();
88
98
});
89
99
}
90
100
};
91
101
}
92
-
93
102
}
94
103
95
104
record Person (String name ) {
0 commit comments