counter: 1.2.15 - Refined `CounterApplicationIT` (simplified setup)
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / counter / CounterApplicationIT.java
1 package de.juplo.kafka.wordcount.counter;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerRecord;
5 import org.apache.kafka.streams.KeyValue;
6 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
7 import org.apache.kafka.streams.state.Stores;
8 import org.junit.jupiter.api.BeforeEach;
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.context.annotation.Primary;
15 import org.springframework.kafka.annotation.KafkaListener;
16 import org.springframework.kafka.core.KafkaTemplate;
17 import org.springframework.kafka.test.context.EmbeddedKafka;
18
19 import java.time.Duration;
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_IN;
24 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.TOPIC_OUT;
25 import static de.juplo.kafka.wordcount.counter.TestData.parseHeader;
26 import static org.awaitility.Awaitility.await;
27 import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME;
28 import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.KEY_DEFAULT_CLASSID_FIELD_NAME;
29
30
31 @SpringBootTest(
32                 properties = {
33                                 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
34                                 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
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.counter.Word",
40                                 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.counter.WordCounter",
41                                 "logging.level.root=WARN",
42                                 "logging.level.de.juplo=DEBUG",
43                                 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
44                                 "juplo.wordcount.counter.commit-interval=0",
45                                 "juplo.wordcount.counter.cacheMaxBytes=0",
46                                 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
47                                 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
48 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT })
49 @Slf4j
50 public class CounterApplicationIT
51 {
52         public static final String TOPIC_IN = "in";
53         public static final String TOPIC_OUT = "out";
54
55         @Autowired
56         KafkaTemplate<String, Word> kafkaTemplate;
57         @Autowired
58         Consumer consumer;
59
60
61         @BeforeEach
62         public void clear()
63         {
64                 consumer.received.clear();
65         }
66
67
68         @Test
69         void testSendMessage()
70         {
71                 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
72
73                 await("Expexted converted data")
74                                 .atMost(Duration.ofSeconds(10))
75                                 .untilAsserted(() -> TestData.assertExpectedResult(consumer.getReceivedMessages()));
76         }
77
78
79         static class Consumer
80         {
81                 private final List<KeyValue<Word, WordCounter>> received = new LinkedList<>();
82
83                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
84                 public synchronized void receive(ConsumerRecord<Word, WordCounter> record)
85                 {
86                         log.debug(
87                                         "Received message: {} -> {}, key: {}, value: {}",
88                                         record.key(),
89                                         record.value(),
90                                         parseHeader(record.headers(), KEY_DEFAULT_CLASSID_FIELD_NAME),
91                                         parseHeader(record.headers(), DEFAULT_CLASSID_FIELD_NAME));
92                         received.add(KeyValue.pair(record.key(),record.value()));
93                 }
94
95                 synchronized List<KeyValue<Word, WordCounter>> getReceivedMessages()
96                 {
97                         return received;
98                 }
99         }
100
101         @TestConfiguration
102         static class Configuration
103         {
104                 @Bean
105                 Consumer consumer()
106                 {
107                         return new Consumer();
108                 }
109
110                 @Primary
111                 @Bean
112                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
113                 {
114                         return Stores.inMemoryKeyValueStore("TEST-STORE");
115                 }
116         }
117 }