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 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.*;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.test.context.SpringBootTest;
12 import org.springframework.boot.test.context.TestConfiguration;
13 import org.springframework.context.annotation.Bean;
14 import org.springframework.context.annotation.Primary;
15 import org.springframework.kafka.annotation.KafkaListener;
16 import org.springframework.kafka.core.KafkaTemplate;
17 import org.springframework.kafka.support.KafkaHeaders;
18 import org.springframework.kafka.support.SendResult;
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;
25 import java.time.Duration;
26 import java.util.stream.Stream;
28 import static org.awaitility.Awaitility.await;
33 "spring.kafka.producer.key-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
34 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
35 "spring.kafka.producer.properties.spring.json.type.mapping=word:de.juplo.kafka.wordcount.counter.TestWord,counter:de.juplo.kafka.wordcount.counter.TestCounter",
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.User",
41 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.query.TestRanking",
42 "spring.kafka.consumer.properties.spring.json.trusted.packages=de.juplo.kafka.wordcount.top10 ",
43 "logging.level.root=WARN",
44 "logging.level.de.juplo=DEBUG",
45 "logging.level.org.apache.kafka.clients=INFO",
46 "logging.level.org.apache.kafka.streams=INFO",
47 "juplo.wordcount.top10.bootstrap-server=${spring.embedded.kafka.brokers}",
48 "juplo.wordcount.top10.commit-interval=100",
49 "juplo.wordcount.top10.cacheMaxBytes=0",
50 "juplo.wordcount.top10.input-topic=" + Top10ApplicationIT.TOPIC_IN,
51 "juplo.wordcount.top10.output-topic=" + Top10ApplicationIT.TOPIC_OUT })
52 @EmbeddedKafka(topics = { Top10ApplicationIT.TOPIC_IN, Top10ApplicationIT.TOPIC_OUT })
54 public class Top10ApplicationIT
56 public static final String TOPIC_IN = "in";
57 public static final String TOPIC_OUT = "out";
58 public static final String STORE_NAME = "TEST-STORE";
63 Top10StreamProcessor streamProcessor;
67 public static void testSendMessage(
68 @Autowired KafkaTemplate<TestWord, TestCounter> kafkaTemplate)
71 .of(TestData.INPUT_MESSAGES)
76 SendResult<TestWord, TestCounter> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
78 "Sent: {}={}, partition={}, offset={}",
79 result.getProducerRecord().key(),
80 result.getProducerRecord().value(),
81 result.getRecordMetadata().partition(),
82 result.getRecordMetadata().offset());
86 throw new RuntimeException(e);
91 @DisplayName("Await the expected state in the state-store")
93 public void testAwaitExpectedState()
95 await("Expected state")
96 .atMost(Duration.ofSeconds(5))
97 .untilAsserted(() -> TestData.assertExpectedState(streamProcessor.getStore(STORE_NAME)));
100 @DisplayName("Await the expected output messages")
103 public void testAwaitExpectedMessages()
105 await("Expected messages")
106 .atMost(Duration.ofSeconds(5))
107 .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
111 static class Consumer
113 private final MultiValueMap<User, Ranking> received = new LinkedMultiValueMap<>();
115 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
116 public synchronized void receive(
117 @Header(KafkaHeaders.RECEIVED_KEY) User user,
118 @Payload TestRanking ranking)
120 log.debug("Received message: {} -> {}", user, ranking);
121 received.add(user, Ranking.of(ranking.getEntries()));
124 synchronized MultiValueMap<User, Ranking> getReceivedMessages()
131 static class Configuration
136 return new Consumer();
141 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
143 return Stores.inMemoryKeyValueStore(STORE_NAME);