counter: 1.2.15 - Refined `TestData` (explicit key in input-data)
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / counter / CounterApplicationIT.java
1 package de.juplo.kafka.wordcount.counter;
2
3 import de.juplo.kafka.wordcount.splitter.TestInputWord;
4 import de.juplo.kafka.wordcount.top10.TestOutputWord;
5 import de.juplo.kafka.wordcount.top10.TestOutputWordCounter;
6 import lombok.extern.slf4j.Slf4j;
7 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
8 import org.apache.kafka.streams.state.Stores;
9 import org.junit.jupiter.api.BeforeEach;
10 import org.junit.jupiter.api.Test;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.boot.test.context.SpringBootTest;
13 import org.springframework.boot.test.context.TestConfiguration;
14 import org.springframework.context.annotation.Bean;
15 import org.springframework.context.annotation.Primary;
16 import org.springframework.kafka.annotation.KafkaListener;
17 import org.springframework.kafka.core.KafkaTemplate;
18 import org.springframework.kafka.support.KafkaHeaders;
19 import org.springframework.kafka.test.context.EmbeddedKafka;
20 import org.springframework.messaging.handler.annotation.Header;
21 import org.springframework.messaging.handler.annotation.Payload;
22 import org.springframework.util.LinkedMultiValueMap;
23 import org.springframework.util.MultiValueMap;
24
25 import java.time.Duration;
26
27 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_IN;
28 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_OUT;
29 import static org.awaitility.Awaitility.await;
30
31
32 @SpringBootTest(
33                 properties = {
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.type.mapping=word:de.juplo.kafka.wordcount.top10.TestOutputWord,counter:de.juplo.kafka.wordcount.top10.TestOutputWordCounter",
40                                 "logging.level.root=WARN",
41                                 "logging.level.de.juplo=DEBUG",
42                                 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
43                                 "juplo.wordcount.counter.commit-interval=0",
44                                 "juplo.wordcount.counter.cacheMaxBytes=0",
45                                 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
46                                 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
47 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
48 @Slf4j
49 public class CounterApplicationIT
50 {
51         public static final String TOPIC_IN = "in";
52         public static final String TOPIC_OUT = "out";
53
54         @Autowired
55         KafkaTemplate<String, TestInputWord> kafkaTemplate;
56         @Autowired
57         Consumer consumer;
58
59
60         @BeforeEach
61         public void clear()
62         {
63                 consumer.received.clear();
64         }
65
66
67         @Test
68         void testSendMessage()
69         {
70                 TestData
71                                 .getInputMessages()
72                                 .forEach(kv -> kafkaTemplate.send(TOPIC_IN, kv.key, kv.value));
73
74                 await("Expected messages")
75                                 .atMost(Duration.ofSeconds(10))
76                                 .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
77         }
78
79
80         static class Consumer
81         {
82                 private final MultiValueMap<TestOutputWord, TestOutputWordCounter> received = new LinkedMultiValueMap<>();
83
84                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
85                 public synchronized void receive(
86                                 @Header(KafkaHeaders.RECEIVED_KEY) TestOutputWord word,
87                                 @Payload TestOutputWordCounter counter)
88                 {
89                         log.debug("Received message: {} -> {}", word, counter);
90                         received.add(word, counter);
91                 }
92
93                 synchronized MultiValueMap<TestOutputWord, TestOutputWordCounter> getReceivedMessages()
94                 {
95                         return received;
96                 }
97         }
98
99         @TestConfiguration
100         static class Configuration
101         {
102                 @Bean
103                 Consumer consumer()
104                 {
105                         return new Consumer();
106                 }
107
108                 @Primary
109                 @Bean
110                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
111                 {
112                         return Stores.inMemoryKeyValueStore("TEST-STORE");
113                 }
114         }
115 }