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