counter: 1.2.15 - `TestData` only holds and asserts the test-data
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / counter / CounterApplicationIT.java
1 package de.juplo.kafka.wordcount.counter;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
5 import org.apache.kafka.streams.state.Stores;
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.context.annotation.Primary;
13 import org.springframework.kafka.annotation.KafkaListener;
14 import org.springframework.kafka.core.KafkaTemplate;
15 import org.springframework.kafka.support.KafkaHeaders;
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.counter.CounterApplicationIT.TOPIC_IN;
26 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.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.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
36                                 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
37                                 "spring.kafka.consumer.properties.spring.json.use.type.headers=false",
38                                 "spring.kafka.consumer.properties.spring.json.key.default.type=de.juplo.kafka.wordcount.counter.Word",
39                                 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.WordCounter",
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, Word> 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                 Stream
71                                 .of(TestData.INPUT_MESSAGES)
72                                 .forEach(word -> kafkaTemplate.send(TOPIC_IN, word.getUser(), word));
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<Word, WordCounter> received = new LinkedMultiValueMap<>();
83
84                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
85                 public synchronized void receive(
86                                 @Header(KafkaHeaders.RECEIVED_KEY) Word word,
87                                 @Payload WordCounter counter)
88                 {
89                         log.debug("Received message: {} -> {}", word, counter);
90                         received.add(word, counter);
91                 }
92
93                 synchronized MultiValueMap<Word, WordCounter> 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 }