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