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.junit.jupiter.api.BeforeAll;
8 import org.junit.jupiter.api.DisplayName;
9 import org.junit.jupiter.api.Test;
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.kafka.annotation.KafkaListener;
15 import org.springframework.kafka.core.KafkaTemplate;
16 import org.springframework.kafka.support.KafkaHeaders;
17 import org.springframework.kafka.support.SendResult;
18 import org.springframework.kafka.test.context.EmbeddedKafka;
19 import org.springframework.messaging.handler.annotation.Header;
20 import org.springframework.messaging.handler.annotation.Payload;
21 import org.springframework.util.LinkedMultiValueMap;
22 import org.springframework.util.MultiValueMap;
24 import java.time.Duration;
25 import java.util.stream.Stream;
27 import static org.awaitility.Awaitility.await;
32 "spring.kafka.producer.key-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
33 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
34 "spring.kafka.producer.properties.spring.json.type.mapping=word:de.juplo.kafka.wordcount.counter.TestWord,counter:de.juplo.kafka.wordcount.counter.TestCounter",
35 "spring.kafka.consumer.auto-offset-reset=earliest",
36 "spring.kafka.consumer.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
37 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
38 "spring.kafka.consumer.properties.spring.json.use.type.headers=false",
39 "spring.kafka.consumer.properties.spring.json.key.default.type=de.juplo.kafka.wordcount.top10.User",
40 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.query.TestRanking",
41 "spring.kafka.consumer.properties.spring.json.trusted.packages=de.juplo.kafka.wordcount.top10 ",
42 "logging.level.root=WARN",
43 "logging.level.de.juplo=DEBUG",
44 "juplo.wordcount.top10.bootstrap-server=${spring.embedded.kafka.brokers}",
45 "juplo.wordcount.top10.input-topic=" + Top10ApplicationIT.TOPIC_IN,
46 "juplo.wordcount.top10.output-topic=" + Top10ApplicationIT.TOPIC_OUT })
47 @EmbeddedKafka(topics = { Top10ApplicationIT.TOPIC_IN, Top10ApplicationIT.TOPIC_OUT })
49 public class Top10ApplicationIT
51 public static final String TOPIC_IN = "in";
52 public static final String TOPIC_OUT = "out";
59 public static void testSendMessage(
60 @Autowired KafkaTemplate<TestWord, TestCounter> kafkaTemplate)
63 .of(TestData.INPUT_MESSAGES)
68 SendResult<TestWord, TestCounter> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
70 "Sent: {}={}, partition={}, offset={}",
71 result.getProducerRecord().key(),
72 result.getProducerRecord().value(),
73 result.getRecordMetadata().partition(),
74 result.getRecordMetadata().offset());
78 throw new RuntimeException(e);
83 @DisplayName("Await the expected output messages")
85 public void testAwaitExpectedMessages()
87 await("Expected messages")
88 .atMost(Duration.ofSeconds(5))
89 .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
95 private final MultiValueMap<User, Ranking> received = new LinkedMultiValueMap<>();
97 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
98 public synchronized void receive(
99 @Header(KafkaHeaders.RECEIVED_KEY) User user,
100 @Payload TestRanking ranking)
102 log.debug("Received message: {} -> {}", user, ranking);
103 received.add(user, Ranking.of(ranking.getEntries()));
106 synchronized MultiValueMap<User, Ranking> getReceivedMessages()
113 static class Configuration
118 return new Consumer();