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