e945b31e5c48dc5a1ebb85eb9d59891f259d37e0
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / splitter / SplitterApplicationIT.java
1 package de.juplo.kafka.wordcount.splitter;
2
3 import de.juplo.kafka.wordcount.counter.TestOutputUser;
4 import de.juplo.kafka.wordcount.counter.TestOutputWord;
5 import de.juplo.kafka.wordcount.recorder.TestInputRecording;
6 import de.juplo.kafka.wordcount.recorder.TestInputUser;
7 import lombok.extern.slf4j.Slf4j;
8 import org.junit.jupiter.api.BeforeAll;
9 import org.junit.jupiter.api.Test;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.test.context.SpringBootTest;
12 import org.springframework.boot.test.context.TestConfiguration;
13 import org.springframework.context.annotation.Bean;
14 import org.springframework.kafka.annotation.KafkaListener;
15 import org.springframework.kafka.core.KafkaTemplate;
16 import org.springframework.kafka.support.KafkaHeaders;
17 import org.springframework.kafka.support.SendResult;
18 import org.springframework.kafka.test.context.EmbeddedKafka;
19 import org.springframework.messaging.handler.annotation.Header;
20 import org.springframework.messaging.handler.annotation.Payload;
21 import org.springframework.util.LinkedMultiValueMap;
22 import org.springframework.util.MultiValueMap;
23
24 import java.time.Duration;
25
26 import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.TOPIC_IN;
27 import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.TOPIC_OUT;
28 import static org.awaitility.Awaitility.await;
29
30
31 @SpringBootTest(
32                 properties = {
33                                 "spring.kafka.producer.key-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
34                                 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
35                                 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
36                                 "spring.kafka.consumer.auto-offset-reset=earliest",
37                                 "spring.kafka.consumer.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
38                                 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
39                                 "spring.kafka.consumer.properties.spring.json.key.default.type=de.juplo.kafka.wordcount.counter.TestOutputUser",
40                                 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.TestOutputWord",
41                                 "logging.level.root=WARN",
42                                 "logging.level.de.juplo=DEBUG",
43                                 "juplo.wordcount.splitter.bootstrap-server=${spring.embedded.kafka.brokers}",
44                                 "juplo.wordcount.splitter.input-topic=" + TOPIC_IN,
45                                 "juplo.wordcount.splitter.output-topic=" + TOPIC_OUT })
46 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
47 @Slf4j
48 public class SplitterApplicationIT
49 {
50         public final static String TOPIC_IN = "in";
51         public final static String TOPIC_OUT = "out";
52
53         @Autowired
54         Consumer consumer;
55
56         @BeforeAll
57         public static void testSendMessage(
58                         @Autowired KafkaTemplate<TestInputUser, TestInputRecording> kafkaTemplate)
59         {
60                 TestData
61                                 .getInputMessages()
62                                 .forEach(kv ->
63                                 {
64                                         try
65                                         {
66                                                 SendResult<TestInputUser, TestInputRecording> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
67                                                 log.info(
68                                                                 "Sent: {}={}, partition={}, offset={}",
69                                                                 result.getProducerRecord().key(),
70                                                                 result.getProducerRecord().value(),
71                                                                 result.getRecordMetadata().partition(),
72                                                                 result.getRecordMetadata().offset());
73                                         }
74                                         catch (Exception e)
75                                         {
76                                                 throw new RuntimeException(e);
77                                         }
78                                 });
79         }
80
81
82         @Test
83         void testSendMessage() throws Exception
84         {
85                 await("Expected converted data")
86                                 .atMost(Duration.ofSeconds(5))
87                                 .untilAsserted(() ->
88                                                 TestData.assertExpectedMessages(consumer.getReceivedMessages()));
89         }
90
91
92         static class Consumer
93         {
94                 private final MultiValueMap<TestOutputUser, TestOutputWord> received = new LinkedMultiValueMap<>();
95
96                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
97                 public synchronized void receive(
98                                 @Header(KafkaHeaders.RECEIVED_KEY) TestOutputUser key,
99                                 @Payload TestOutputWord value)
100                 {
101                         log.debug("Received message: {}={}", key, value);
102                         received.add(key, value);
103                 }
104
105                 synchronized MultiValueMap<TestOutputUser, TestOutputWord> getReceivedMessages()
106                 {
107                         return received;
108                 }
109         }
110
111
112         @TestConfiguration
113         static class Configuration
114         {
115                 @Bean
116                 Consumer consumer()
117                 {
118                         return new Consumer();
119                 }
120         }
121 }