counter: 1.2.15 - Refined method-naming in `TestData`
[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
24 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_IN;
25 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.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.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
35                                 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
36                                 "spring.kafka.consumer.properties.spring.json.use.type.headers=false",
37                                 "spring.kafka.consumer.properties.spring.json.key.default.type=de.juplo.kafka.wordcount.counter.Word",
38                                 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.WordCounter",
39                                 "logging.level.root=WARN",
40                                 "logging.level.de.juplo=DEBUG",
41                                 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
42                                 "juplo.wordcount.counter.commit-interval=0",
43                                 "juplo.wordcount.counter.cacheMaxBytes=0",
44                                 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
45                                 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
46 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
47 @Slf4j
48 public class CounterApplicationIT
49 {
50         public static final String TOPIC_IN = "in";
51         public static final String TOPIC_OUT = "out";
52
53         @Autowired
54         KafkaTemplate<String, Word> kafkaTemplate;
55         @Autowired
56         Consumer consumer;
57
58
59         @BeforeEach
60         public void clear()
61         {
62                 consumer.received.clear();
63         }
64
65
66         @Test
67         void testSendMessage()
68         {
69                 TestData.injectInputMessages((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
70
71                 await("Expected messages")
72                                 .atMost(Duration.ofSeconds(10))
73                                 .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
74         }
75
76
77         static class Consumer
78         {
79                 private final MultiValueMap<Word, WordCounter> received = new LinkedMultiValueMap<>();
80
81                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
82                 public synchronized void receive(
83                                 @Header(KafkaHeaders.RECEIVED_KEY) Word word,
84                                 @Payload WordCounter counter)
85                 {
86                         log.debug("Received message: {} -> {}", word, counter);
87                         received.add(word, counter);
88                 }
89
90                 synchronized MultiValueMap<Word, WordCounter> getReceivedMessages()
91                 {
92                         return received;
93                 }
94         }
95
96         @TestConfiguration
97         static class Configuration
98         {
99                 @Bean
100                 Consumer consumer()
101                 {
102                         return new Consumer();
103                 }
104
105                 @Primary
106                 @Bean
107                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
108                 {
109                         return Stores.inMemoryKeyValueStore("TEST-STORE");
110                 }
111         }
112 }