splitter: 1.2.0 - Separated test-data in class `TestData` -- ALIGN
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / splitter / SplitterApplicationIT.java
1 package de.juplo.kafka.wordcount.splitter;
2
3 import de.juplo.kafka.wordcount.counter.TestWord;
4 import lombok.extern.slf4j.Slf4j;
5 import org.junit.jupiter.api.BeforeEach;
6 import org.junit.jupiter.api.Test;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.boot.test.context.SpringBootTest;
9 import org.springframework.boot.test.context.TestConfiguration;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.kafka.annotation.KafkaListener;
12 import org.springframework.kafka.core.KafkaTemplate;
13 import org.springframework.kafka.support.KafkaHeaders;
14 import org.springframework.kafka.test.context.EmbeddedKafka;
15 import org.springframework.messaging.handler.annotation.Header;
16 import org.springframework.messaging.handler.annotation.Payload;
17 import org.springframework.util.LinkedMultiValueMap;
18 import org.springframework.util.MultiValueMap;
19
20 import java.time.Duration;
21
22 import static de.juplo.kafka.wordcount.splitter.SplitterApplicationIT.*;
23 import static org.awaitility.Awaitility.await;
24
25
26 @SpringBootTest(
27                 properties = {
28                                 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
29                                 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
30                                 "spring.kafka.consumer.auto-offset-reset=earliest",
31                                 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
32                                 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.TestWord",
33                                 "spring.kafka.consumer.properties.spring.json.trusted.packages=de.juplo.kafka.wordcount.splitter",
34                                 "logging.level.root=WARN",
35                                 "logging.level.de.juplo=DEBUG",
36                                 "juplo.wordcount.splitter.bootstrap-server=${spring.embedded.kafka.brokers}",
37                                 "juplo.wordcount.splitter.input-topic=" + TOPIC_IN,
38                                 "juplo.wordcount.splitter.output-topic=" + TOPIC_OUT })
39 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
40 @Slf4j
41 public class SplitterApplicationIT
42 {
43         public final static String TOPIC_IN = "in";
44         public final static String TOPIC_OUT = "out";
45
46         @Autowired
47         KafkaTemplate<String, Recording> kafkaTemplate;
48         @Autowired
49         Consumer consumer;
50
51
52         @BeforeEach
53         public void clear()
54         {
55                 consumer.received.clear();
56         }
57
58         @Test
59         void testSendMessage() throws Exception
60         {
61                 TestData.writeInputData((user, recording) -> kafkaTemplate.send(TOPIC_IN, user, recording));
62
63                 await("Expexted converted data")
64                                 .atMost(Duration.ofSeconds(5))
65                                 .untilAsserted(() ->
66                                                 TestData.assertExpectedMessages(consumer.getReceivedMessages()));
67         }
68
69
70         static class Consumer
71         {
72                 private final MultiValueMap<String, TestWord> received = new LinkedMultiValueMap<>();
73
74                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
75                 public synchronized void receive(
76                                 @Header(KafkaHeaders.RECEIVED_KEY) String key,
77                                 @Payload TestWord value)
78                 {
79                         log.debug("Received message: {}={}", key, value);
80                         received.add(key, value);
81                 }
82
83                 synchronized MultiValueMap<String, TestWord> getReceivedMessages()
84                 {
85                         return received;
86                 }
87         }
88
89
90         @TestConfiguration
91         static class Configuration
92         {
93                 @Bean
94                 Consumer consumer()
95                 {
96                         return new Consumer();
97                 }
98         }
99 }