top10: 1.2.1 - `Top10ApplicationIT` asserts type-mapping for output-data
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / top10 / Top10ApplicationIT.java
1 package de.juplo.kafka.wordcount.top10;
2
3 import de.juplo.kafka.wordcount.counter.TestWord;
4 import de.juplo.kafka.wordcount.counter.TestCounter;
5 import de.juplo.kafka.wordcount.query.TestRanking;
6 import de.juplo.kafka.wordcount.query.TestUser;
7 import lombok.extern.slf4j.Slf4j;
8 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
9 import org.apache.kafka.streams.state.Stores;
10 import org.junit.jupiter.api.*;
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.support.SendResult;
20 import org.springframework.kafka.test.context.EmbeddedKafka;
21 import org.springframework.messaging.handler.annotation.Header;
22 import org.springframework.messaging.handler.annotation.Payload;
23 import org.springframework.util.LinkedMultiValueMap;
24 import org.springframework.util.MultiValueMap;
25
26 import java.time.Duration;
27 import java.util.stream.Stream;
28
29 import static de.juplo.kafka.wordcount.top10.Top10StreamProcessor.STORE_NAME;
30 import static org.awaitility.Awaitility.await;
31
32
33 @SpringBootTest(
34                 properties = {
35                                 "spring.kafka.producer.key-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
36                                 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
37                                 "spring.kafka.producer.properties.spring.json.type.mapping=word:de.juplo.kafka.wordcount.counter.TestWord,counter:de.juplo.kafka.wordcount.counter.TestCounter",
38                                 "spring.kafka.consumer.auto-offset-reset=earliest",
39                                 "spring.kafka.consumer.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
40                                 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
41                                 "spring.kafka.consumer.properties.spring.json.type.mapping=user:de.juplo.kafka.wordcount.query.TestUser,ranking:de.juplo.kafka.wordcount.query.TestRanking",
42                                 "logging.level.root=WARN",
43                                 "logging.level.de.juplo=DEBUG",
44                                 "logging.level.org.apache.kafka.clients=INFO",
45                                 "logging.level.org.apache.kafka.streams=INFO",
46                                 "juplo.wordcount.top10.bootstrap-server=${spring.embedded.kafka.brokers}",
47                                 "juplo.wordcount.top10.commit-interval=100",
48                                 "juplo.wordcount.top10.cacheMaxBytes=0",
49                                 "juplo.wordcount.top10.input-topic=" + Top10ApplicationIT.TOPIC_IN,
50                                 "juplo.wordcount.top10.output-topic=" + Top10ApplicationIT.TOPIC_OUT })
51 @EmbeddedKafka(topics = { Top10ApplicationIT.TOPIC_IN, Top10ApplicationIT.TOPIC_OUT })
52 @Slf4j
53 public class Top10ApplicationIT
54 {
55         public static final String TOPIC_IN = "in";
56         public static final String TOPIC_OUT = "out";
57
58         @Autowired
59         Consumer consumer;
60         @Autowired
61         Top10StreamProcessor streamProcessor;
62
63
64         @BeforeAll
65         public static void testSendMessage(
66                         @Autowired KafkaTemplate<TestWord, TestCounter> kafkaTemplate)
67         {
68                 Stream
69                                 .of(TestData.INPUT_MESSAGES)
70                                 .forEach(kv ->
71                                 {
72                                         try
73                                         {
74                                                 SendResult<TestWord, TestCounter> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
75                                                 log.info(
76                                                                 "Sent: {}={}, partition={}, offset={}",
77                                                                 result.getProducerRecord().key(),
78                                                                 result.getProducerRecord().value(),
79                                                                 result.getRecordMetadata().partition(),
80                                                                 result.getRecordMetadata().offset());
81                                         }
82                                         catch (Exception e)
83                                         {
84                                                 throw new RuntimeException(e);
85                                         }
86                                 });
87         }
88
89         @DisplayName("Await the expected state in the state-store")
90         @Test
91         public void testAwaitExpectedState()
92         {
93                 await("Expected state")
94                                 .atMost(Duration.ofSeconds(5))
95                                 .untilAsserted(() -> TestData.assertExpectedState(streamProcessor.getStore()));
96         }
97
98         @DisplayName("Await the expected output messages")
99         @Test
100         @Disabled
101         public void testAwaitExpectedMessages()
102         {
103                 await("Expected messages")
104                                 .atMost(Duration.ofSeconds(5))
105                                 .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
106         }
107
108         @DisplayName("Await the expected number of messages")
109         @Test
110         public void testAwaitExpectedNumberOfMessagesForUsers()
111         {
112                 await("Expected number of messages")
113                                 .atMost(Duration.ofSeconds(5))
114                                 .untilAsserted(() -> TestData.assertExpectedNumberOfMessagesForUsers(consumer.getReceivedMessages()));
115         }
116
117         @DisplayName("Await the expected final output messages")
118         @Test
119         public void testAwaitExpectedLastMessagesForUsers()
120         {
121                 await("Expected final output messages")
122                                 .atMost(Duration.ofSeconds(5))
123                                 .untilAsserted(() -> TestData.assertExpectedLastMessagesForUsers(consumer.getReceivedMessages()));
124         }
125
126
127         static class Consumer
128         {
129                 private final MultiValueMap<TestUser, TestRanking> received = new LinkedMultiValueMap<>();
130
131                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
132                 public synchronized void receive(
133                                 @Header(KafkaHeaders.RECEIVED_KEY) TestUser user,
134                                 @Payload TestRanking ranking)
135                 {
136                         log.debug("Received message: {} -> {}", user, ranking);
137                         received.add(user, ranking);
138                 }
139
140                 synchronized MultiValueMap<TestUser, TestRanking> getReceivedMessages()
141                 {
142                         return received;
143                 }
144         }
145
146         @TestConfiguration
147         static class Configuration
148         {
149                 @Bean
150                 Consumer consumer()
151                 {
152                         return new Consumer();
153                 }
154
155                 @Primary
156                 @Bean
157                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
158                 {
159                         return Stores.inMemoryKeyValueStore(STORE_NAME);
160                 }
161         }
162 }