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.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;
27 import java.time.Duration;
28 import java.util.stream.Stream;
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.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 })
56 public class Top10ApplicationIT
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";
65 Top10StreamProcessor streamProcessor;
69 public static void testSendMessage(
70 @Autowired KafkaTemplate<TestWord, TestCounter> kafkaTemplate)
73 .of(TestData.INPUT_MESSAGES)
78 SendResult<TestWord, TestCounter> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
80 "Sent: {}={}, partition={}, offset={}",
81 result.getProducerRecord().key(),
82 result.getProducerRecord().value(),
83 result.getRecordMetadata().partition(),
84 result.getRecordMetadata().offset());
88 throw new RuntimeException(e);
93 @DisplayName("Await the expected state in the state-store")
95 public void testAwaitExpectedState()
97 await("Expected state")
98 .atMost(Duration.ofSeconds(5))
99 .untilAsserted(() -> TestData.assertExpectedState(streamProcessor.getStore(STORE_NAME)));
102 @DisplayName("Await the expected output messages")
104 public void testAwaitExpectedMessages()
106 await("Expected messages")
107 .atMost(Duration.ofSeconds(5))
108 .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
112 static class Consumer
114 private final MultiValueMap<User, Ranking> received = new LinkedMultiValueMap<>();
116 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
117 public synchronized void receive(
118 @Header(KafkaHeaders.RECEIVED_KEY) User user,
119 @Payload TestRanking ranking)
121 log.debug("Received message: {} -> {}", user, ranking);
122 received.add(user, Ranking.of(ranking.getEntries()));
125 synchronized MultiValueMap<User, Ranking> getReceivedMessages()
132 static class Configuration
137 return new Consumer();
142 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
144 return Stores.inMemoryKeyValueStore(STORE_NAME);