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 "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";
63 public static void testSendMessage(
64 @Autowired KafkaTemplate<TestWord, TestCounter> kafkaTemplate)
67 .of(TestData.INPUT_MESSAGES)
72 SendResult<TestWord, TestCounter> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
74 "Sent: {}={}, partition={}, offset={}",
75 result.getProducerRecord().key(),
76 result.getProducerRecord().value(),
77 result.getRecordMetadata().partition(),
78 result.getRecordMetadata().offset());
82 throw new RuntimeException(e);
87 @DisplayName("Await the expected output messages")
89 public void testAwaitExpectedMessages()
91 await("Expected messages")
92 .atMost(Duration.ofSeconds(5))
93 .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
99 private final MultiValueMap<User, Ranking> received = new LinkedMultiValueMap<>();
101 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
102 public synchronized void receive(
103 @Header(KafkaHeaders.RECEIVED_KEY) User user,
104 @Payload TestRanking ranking)
106 log.debug("Received message: {} -> {}", user, ranking);
107 received.add(user, Ranking.of(ranking.getEntries()));
110 synchronized MultiValueMap<User, Ranking> getReceivedMessages()
117 static class Configuration
122 return new Consumer();