1 package de.juplo.kafka.wordcount.top10;
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;
26 import java.time.Duration;
27 import java.util.stream.Stream;
29 import static de.juplo.kafka.wordcount.top10.Top10StreamProcessor.STORE_NAME;
30 import static org.awaitility.Awaitility.await;
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 })
53 public class Top10ApplicationIT
55 public static final String TOPIC_IN = "in";
56 public static final String TOPIC_OUT = "out";
61 Top10StreamProcessor streamProcessor;
65 public static void testSendMessage(
66 @Autowired KafkaTemplate<TestWord, TestCounter> kafkaTemplate)
69 .of(TestData.INPUT_MESSAGES)
74 SendResult<TestWord, TestCounter> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
76 "Sent: {}={}, partition={}, offset={}",
77 result.getProducerRecord().key(),
78 result.getProducerRecord().value(),
79 result.getRecordMetadata().partition(),
80 result.getRecordMetadata().offset());
84 throw new RuntimeException(e);
89 @DisplayName("Await the expected state in the state-store")
91 public void testAwaitExpectedState()
93 await("Expected state")
94 .atMost(Duration.ofSeconds(5))
95 .untilAsserted(() -> TestData.assertExpectedState(streamProcessor.getStore()));
98 @DisplayName("Await the expected output messages")
101 public void testAwaitExpectedMessages()
103 await("Expected messages")
104 .atMost(Duration.ofSeconds(5))
105 .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
108 @DisplayName("Await the expected number of messages")
110 public void testAwaitExpectedNumberOfMessagesForUsers()
112 await("Expected number of messages")
113 .atMost(Duration.ofSeconds(5))
114 .untilAsserted(() -> TestData.assertExpectedNumberOfMessagesForUsers(consumer.getReceivedMessages()));
117 @DisplayName("Await the expected final output messages")
119 public void testAwaitExpectedLastMessagesForUsers()
121 await("Expected final output messages")
122 .atMost(Duration.ofSeconds(5))
123 .untilAsserted(() -> TestData.assertExpectedLastMessagesForUsers(consumer.getReceivedMessages()));
127 static class Consumer
129 private final MultiValueMap<TestUser, TestRanking> received = new LinkedMultiValueMap<>();
131 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
132 public synchronized void receive(
133 @Header(KafkaHeaders.RECEIVED_KEY) TestUser user,
134 @Payload TestRanking ranking)
136 log.debug("Received message: {} -> {}", user, ranking);
137 received.add(user, ranking);
140 synchronized MultiValueMap<TestUser, TestRanking> getReceivedMessages()
147 static class Configuration
152 return new Consumer();
157 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
159 return Stores.inMemoryKeyValueStore(STORE_NAME);