2d45f03bd0438bbe1c895f2d4b8e831875d400cb
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / top10 / Top10ApplicationIT.java
1 package de.juplo.kafka.wordcount.top10;
2
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;
23
24 import java.time.Duration;
25 import java.util.stream.Stream;
26
27 import static org.awaitility.Awaitility.await;
28
29
30 @SpringBootTest(
31                 properties = {
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 })
48 @Slf4j
49 public class Top10ApplicationIT
50 {
51         public static final String TOPIC_IN = "in";
52         public static final String TOPIC_OUT = "out";
53
54         @Autowired
55         Consumer consumer;
56
57
58         @BeforeAll
59         public static void testSendMessage(
60                         @Autowired KafkaTemplate<TestWord, TestCounter> kafkaTemplate)
61         {
62                 Stream
63                                 .of(TestData.INPUT_MESSAGES)
64                                 .forEach(kv ->
65                                 {
66                                         try
67                                         {
68                                                 SendResult<TestWord, TestCounter> result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get();
69                                                 log.info(
70                                                                 "Sent: {}={}, partition={}, offset={}",
71                                                                 result.getProducerRecord().key(),
72                                                                 result.getProducerRecord().value(),
73                                                                 result.getRecordMetadata().partition(),
74                                                                 result.getRecordMetadata().offset());
75                                         }
76                                         catch (Exception e)
77                                         {
78                                                 throw new RuntimeException(e);
79                                         }
80                                 });
81         }
82
83         @DisplayName("Await the expected output messages")
84         @Test
85         public void testAwaitExpectedMessages()
86         {
87                 await("Expected messages")
88                                 .atMost(Duration.ofSeconds(5))
89                                 .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
90         }
91
92
93         static class Consumer
94         {
95                 private final MultiValueMap<User, Ranking> received = new LinkedMultiValueMap<>();
96
97                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
98                 public synchronized void receive(
99                                 @Header(KafkaHeaders.RECEIVED_KEY) User user,
100                                 @Payload TestRanking ranking)
101                 {
102                         log.debug("Received message: {} -> {}", user, ranking);
103                         received.add(user, Ranking.of(ranking.getEntries()));
104                 }
105
106                 synchronized MultiValueMap<User, Ranking> getReceivedMessages()
107                 {
108                         return received;
109                 }
110         }
111
112         @TestConfiguration
113         static class Configuration
114         {
115                 @Bean
116                 Consumer consumer()
117                 {
118                         return new Consumer();
119                 }
120         }
121 }