top10: 1.2.1 - (RED) Added an assertion regarding the expected state
[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 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.BeforeAll;
10 import org.junit.jupiter.api.DisplayName;
11 import org.junit.jupiter.api.Test;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.boot.test.context.SpringBootTest;
14 import org.springframework.boot.test.context.TestConfiguration;
15 import org.springframework.context.annotation.Bean;
16 import org.springframework.context.annotation.Primary;
17 import org.springframework.kafka.annotation.KafkaListener;
18 import org.springframework.kafka.core.KafkaTemplate;
19 import org.springframework.kafka.support.KafkaHeaders;
20 import org.springframework.kafka.support.SendResult;
21 import org.springframework.kafka.test.context.EmbeddedKafka;
22 import org.springframework.messaging.handler.annotation.Header;
23 import org.springframework.messaging.handler.annotation.Payload;
24 import org.springframework.util.LinkedMultiValueMap;
25 import org.springframework.util.MultiValueMap;
26
27 import java.time.Duration;
28 import java.util.stream.Stream;
29
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.use.type.headers=false",
42                                 "spring.kafka.consumer.properties.spring.json.key.default.type=de.juplo.kafka.wordcount.top10.User",
43                                 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.query.TestRanking",
44                                 "spring.kafka.consumer.properties.spring.json.trusted.packages=de.juplo.kafka.wordcount.top10   ",
45                                 "logging.level.root=WARN",
46                                 "logging.level.de.juplo=DEBUG",
47                                 "logging.level.org.apache.kafka.clients=INFO",
48                                 "logging.level.org.apache.kafka.streams=INFO",
49                                 "juplo.wordcount.top10.bootstrap-server=${spring.embedded.kafka.brokers}",
50                                 "juplo.wordcount.top10.commit-interval=100",
51                                 "juplo.wordcount.top10.cacheMaxBytes=0",
52                                 "juplo.wordcount.top10.input-topic=" + Top10ApplicationIT.TOPIC_IN,
53                                 "juplo.wordcount.top10.output-topic=" + Top10ApplicationIT.TOPIC_OUT })
54 @EmbeddedKafka(topics = { Top10ApplicationIT.TOPIC_IN, Top10ApplicationIT.TOPIC_OUT })
55 @Slf4j
56 public class Top10ApplicationIT
57 {
58         public static final String TOPIC_IN = "in";
59         public static final String TOPIC_OUT = "out";
60         public static final String STORE_NAME = "TEST-STORE";
61
62         @Autowired
63         Consumer consumer;
64         @Autowired
65         Top10StreamProcessor streamProcessor;
66
67
68         @BeforeAll
69         public static void testSendMessage(
70                         @Autowired KafkaTemplate<TestWord, TestCounter> kafkaTemplate)
71         {
72                 Stream
73                                 .of(TestData.INPUT_MESSAGES)
74                                 .forEach(kv ->
75                                 {
76                                         try
77                                         {
78                                                 SendResult<TestWord, TestCounter> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
79                                                 log.info(
80                                                                 "Sent: {}={}, partition={}, offset={}",
81                                                                 result.getProducerRecord().key(),
82                                                                 result.getProducerRecord().value(),
83                                                                 result.getRecordMetadata().partition(),
84                                                                 result.getRecordMetadata().offset());
85                                         }
86                                         catch (Exception e)
87                                         {
88                                                 throw new RuntimeException(e);
89                                         }
90                                 });
91         }
92
93         @DisplayName("Await the expected state in the state-store")
94         @Test
95         public void testAwaitExpectedState()
96         {
97                 await("Expected state")
98                                 .atMost(Duration.ofSeconds(5))
99                                 .untilAsserted(() -> TestData.assertExpectedState(streamProcessor.getStore(STORE_NAME)));
100         }
101
102         @DisplayName("Await the expected output messages")
103         @Test
104         public void testAwaitExpectedMessages()
105         {
106                 await("Expected messages")
107                                 .atMost(Duration.ofSeconds(5))
108                                 .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
109         }
110
111
112         static class Consumer
113         {
114                 private final MultiValueMap<User, Ranking> received = new LinkedMultiValueMap<>();
115
116                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
117                 public synchronized void receive(
118                                 @Header(KafkaHeaders.RECEIVED_KEY) User user,
119                                 @Payload TestRanking ranking)
120                 {
121                         log.debug("Received message: {} -> {}", user, ranking);
122                         received.add(user, Ranking.of(ranking.getEntries()));
123                 }
124
125                 synchronized MultiValueMap<User, Ranking> getReceivedMessages()
126                 {
127                         return received;
128                 }
129         }
130
131         @TestConfiguration
132         static class Configuration
133         {
134                 @Bean
135                 Consumer consumer()
136                 {
137                         return new Consumer();
138                 }
139
140                 @Primary
141                 @Bean
142                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
143                 {
144                         return Stores.inMemoryKeyValueStore(STORE_NAME);
145                 }
146         }
147 }