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