counter: 1.3.0 - (RED) Made `CounterApplicationIT` fail too
[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.TestInputUser;
4 import de.juplo.kafka.wordcount.splitter.TestInputWord;
5 import de.juplo.kafka.wordcount.top10.TestOutputWord;
6 import de.juplo.kafka.wordcount.top10.TestOutputWordCounter;
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.BeforeAll;
11 import org.junit.jupiter.api.DisplayName;
12 import org.junit.jupiter.api.Test;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.boot.test.context.SpringBootTest;
15 import org.springframework.boot.test.context.TestConfiguration;
16 import org.springframework.context.annotation.Bean;
17 import org.springframework.context.annotation.Primary;
18 import org.springframework.kafka.annotation.KafkaListener;
19 import org.springframework.kafka.core.KafkaTemplate;
20 import org.springframework.kafka.support.KafkaHeaders;
21 import org.springframework.kafka.support.SendResult;
22 import org.springframework.kafka.test.context.EmbeddedKafka;
23 import org.springframework.messaging.handler.annotation.Header;
24 import org.springframework.messaging.handler.annotation.Payload;
25 import org.springframework.util.LinkedMultiValueMap;
26 import org.springframework.util.MultiValueMap;
27
28 import java.time.Duration;
29
30 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_IN;
31 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_OUT;
32 import static de.juplo.kafka.wordcount.counter.CounterStreamProcessor.STORE_NAME;
33 import static org.awaitility.Awaitility.await;
34
35
36 @SpringBootTest(
37                 properties = {
38                                 "spring.kafka.producer.key-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
39                                 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
40                                 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
41                                 "spring.kafka.consumer.auto-offset-reset=earliest",
42                                 "spring.kafka.consumer.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
43                                 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
44                                 "spring.kafka.consumer.properties.spring.json.type.mapping=word:de.juplo.kafka.wordcount.top10.TestOutputWord,counter:de.juplo.kafka.wordcount.top10.TestOutputWordCounter",
45                                 "logging.level.root=WARN",
46                                 "logging.level.de.juplo=DEBUG",
47                                 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
48                                 "juplo.wordcount.counter.commit-interval=0",
49                                 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
50                                 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
51 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
52 @Slf4j
53 public class CounterApplicationIT
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         CounterStreamProcessor streamProcessor;
62
63
64         @BeforeAll
65         public static void testSendMessage(
66                         @Autowired KafkaTemplate<TestInputUser, TestInputWord> kafkaTemplate)
67         {
68                 TestData
69                                 .getInputMessages()
70                                 .forEach(kv ->
71                                 {
72                                         try
73                                         {
74                                                 SendResult<TestInputUser, TestInputWord> 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 number of messages")
90         @Test
91         public void testAwaitExpectedNumberOfMessagesForUsers()
92         {
93                 await("Expected number of messages")
94                                 .atMost(Duration.ofSeconds(5))
95                                 .untilAsserted(() -> consumer.enforceAssertion(
96                                                 receivedMessages -> TestData.assertExpectedNumberOfMessagesForWord(receivedMessages)));
97         }
98
99         @DisplayName("Await the expected output messages")
100         @Test
101         void testSendMessage()
102         {
103                 await("Expected messages")
104                                 .atMost(Duration.ofSeconds(10))
105                                 .untilAsserted(() -> consumer.enforceAssertion(
106                                                 receivedMessages -> TestData.assertExpectedMessages(receivedMessages)));
107         }
108
109         @DisplayName("Await the expected final output messages")
110         @Test
111         public void testAwaitExpectedLastMessagesForUsers()
112         {
113                 await("Expected final output messages")
114                                 .atMost(Duration.ofSeconds(5))
115                                 .untilAsserted(() -> consumer.enforceAssertion(
116                                                 receivedMessages -> TestData.assertExpectedLastMessagesForWord(receivedMessages)));
117         }
118
119         @DisplayName("Await the expected state in the state-store")
120         @Test
121         public void testAwaitExpectedState()
122         {
123                 await("Expected state")
124                                 .atMost(Duration.ofSeconds(5))
125                                 .untilAsserted(() -> TestData.assertExpectedState(streamProcessor.getStore()));
126         }
127
128
129         static class Consumer
130         {
131                 private final MultiValueMap<TestOutputWord, TestOutputWordCounter> received = new LinkedMultiValueMap<>();
132
133                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
134                 public synchronized void receive(
135                                 @Header(KafkaHeaders.RECEIVED_KEY) TestOutputWord word,
136                                 @Payload TestOutputWordCounter counter)
137                 {
138                         log.debug("Received message: {} -> {}", word, counter);
139                         received.add(word, counter);
140                 }
141
142                 synchronized void enforceAssertion(
143                                 java.util.function.Consumer<MultiValueMap<TestOutputWord, TestOutputWordCounter>> assertion)
144                 {
145                         assertion.accept(received);
146                 }
147         }
148
149         @TestConfiguration
150         static class Configuration
151         {
152                 @Bean
153                 Consumer consumer()
154                 {
155                         return new Consumer();
156                 }
157
158                 @Primary
159                 @Bean
160                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
161                 {
162                         return Stores.inMemoryKeyValueStore(STORE_NAME);
163                 }
164         }
165 }