a702e1d1be7d711b425c60020b7f24af412df818
[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.BeforeEach;
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.test.context.EmbeddedKafka;
16 import org.springframework.messaging.handler.annotation.Header;
17 import org.springframework.messaging.handler.annotation.Payload;
18 import org.springframework.util.LinkedMultiValueMap;
19 import org.springframework.util.MultiValueMap;
20
21 import java.time.Duration;
22
23 import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.*;
24 import static org.awaitility.Awaitility.await;
25
26
27 @SpringBootTest(
28                 properties = {
29                                 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
30                                 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
31                                 "spring.kafka.consumer.auto-offset-reset=earliest",
32                                 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
33                                 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.TestWord",
34                                 "spring.kafka.consumer.properties.spring.json.trusted.packages=de.juplo.kafka.wordcount.splitter",
35                                 "logging.level.root=WARN",
36                                 "logging.level.de.juplo=DEBUG",
37                                 "juplo.wordcount.splitter.bootstrap-server=${spring.embedded.kafka.brokers}",
38                                 "juplo.wordcount.splitter.input-topic=" + TOPIC_IN,
39                                 "juplo.wordcount.splitter.output-topic=" + TOPIC_OUT })
40 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
41 @Slf4j
42 public class SplitterApplicationIT
43 {
44         public final static String TOPIC_IN = "in";
45         public final static String TOPIC_OUT = "out";
46
47         @Autowired
48         KafkaTemplate<String, TestRecording> kafkaTemplate;
49         @Autowired
50         Consumer consumer;
51
52
53         @BeforeEach
54         public void clear()
55         {
56                 consumer.received.clear();
57         }
58
59         @Test
60         void testSendMessage() throws Exception
61         {
62                 TestData.writeInputData((user, recording) -> kafkaTemplate.send(TOPIC_IN, user, recording));
63
64                 await("Expexted converted data")
65                                 .atMost(Duration.ofSeconds(5))
66                                 .untilAsserted(() ->
67                                                 TestData.assertExpectedMessages(consumer.getReceivedMessages()));
68         }
69
70
71         static class Consumer
72         {
73                 private final MultiValueMap<String, TestWord> received = new LinkedMultiValueMap<>();
74
75                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
76                 public synchronized void receive(
77                                 @Header(KafkaHeaders.RECEIVED_KEY) String key,
78                                 @Payload TestWord value)
79                 {
80                         log.debug("Received message: {}={}", key, value);
81                         received.add(key, value);
82                 }
83
84                 synchronized MultiValueMap<String, TestWord> getReceivedMessages()
85                 {
86                         return received;
87                 }
88         }
89
90
91         @TestConfiguration
92         static class Configuration
93         {
94                 @Bean
95                 Consumer consumer()
96                 {
97                         return new Consumer();
98                 }
99         }
100 }