counter: 1.2.10 - Replaced helper-class `Message` with `KeyValue`
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / counter / CounterApplicationIT.java
1 package de.juplo.kafka.wordcount.counter;
2
3 import lombok.RequiredArgsConstructor;
4 import lombok.extern.slf4j.Slf4j;
5 import org.apache.kafka.clients.consumer.ConsumerConfig;
6 import org.apache.kafka.clients.consumer.ConsumerRecord;
7 import org.apache.kafka.clients.producer.ProducerConfig;
8 import org.apache.kafka.streams.KeyValue;
9 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
10 import org.apache.kafka.streams.state.Stores;
11 import org.junit.jupiter.api.BeforeEach;
12 import org.junit.jupiter.api.Test;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.boot.test.context.SpringBootTest;
15 import org.springframework.boot.test.context.TestConfiguration;
16 import org.springframework.context.annotation.Bean;
17 import org.springframework.context.annotation.Primary;
18 import org.springframework.kafka.annotation.KafkaListener;
19 import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
20 import org.springframework.kafka.core.*;
21 import org.springframework.kafka.support.serializer.JsonDeserializer;
22 import org.springframework.kafka.support.serializer.JsonSerializer;
23 import org.springframework.kafka.test.context.EmbeddedKafka;
24
25 import java.time.Duration;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.stream.Collectors;
31
32 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.*;
33 import static org.awaitility.Awaitility.*;
34
35
36 @SpringBootTest(
37                 properties = {
38                                 "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
39                                 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
40                                 "juplo.wordcount.counter.commit-interval=0",
41                                 "juplo.wordcount.counter.cacheMaxBytes=0",
42                                 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
43                                 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
44 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT }, partitions = PARTITIONS)
45 @Slf4j
46 public class CounterApplicationIT
47 {
48         public final static String TOPIC_IN = "in";
49         public final static String TOPIC_OUT = "out";
50         static final int PARTITIONS = 2;
51
52         @Autowired
53         KafkaTemplate<String, Word> kafkaTemplate;
54         @Autowired
55         Consumer consumer;
56
57
58         @BeforeEach
59         public void clear()
60         {
61                 consumer.received.clear();
62         }
63
64
65         @Test
66         void testSendMessage()
67         {
68                 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
69
70                 await("Expexted converted data")
71                                 .atMost(Duration.ofSeconds(10))
72                                 .untilAsserted(() -> TestData.assertExpectedResult(consumer.getReceivedMessages()));
73         }
74
75
76         @RequiredArgsConstructor
77         static class Consumer
78         {
79                 private final List<KeyValue<Word, WordCount>> received = new LinkedList<>();
80
81                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
82                 public synchronized void receive(ConsumerRecord<Word, WordCount> record)
83                 {
84                         log.debug("Received message: {}", record);
85                         received.add(KeyValue.pair(record.key(),record.value()));
86                 }
87
88                 synchronized List<KeyValue<Word, WordCount>> getReceivedMessages()
89                 {
90                         return received;
91                 }
92         }
93
94         @TestConfiguration
95         static class Configuration
96         {
97                 @Bean
98                 ProducerFactory<?, ?> producerFactory(Properties streamProcessorProperties)
99                 {
100                         Map<String, Object> propertyMap = streamProcessorProperties
101                                         .entrySet()
102                                         .stream()
103                                         .collect(
104                                                         Collectors.toMap(
105                                                                         entry -> (String)entry.getKey(),
106                                                                         entry -> entry.getValue()
107                                                         ));
108
109                         propertyMap.put(
110                                         ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
111                                         JsonSerializer.class.getName());
112                         propertyMap.put(
113                                         ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
114                                         JsonSerializer.class.getName());
115
116                         return new DefaultKafkaProducerFactory<>(propertyMap);
117                 }
118
119                 @Bean
120                 ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
121                                 Properties streamProcessorProperties)
122                 {
123                         Map<String, Object> propertyMap = streamProcessorProperties
124                                         .entrySet()
125                                         .stream()
126                                         .collect(
127                                                         Collectors.toMap(
128                                                                         entry -> (String)entry.getKey(),
129                                                                         entry -> entry.getValue()
130                                                         ));
131
132                         propertyMap.put(
133                                         ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
134                                         JsonDeserializer.class.getName());
135                         propertyMap.put(
136                                         ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
137                                         JsonDeserializer.class.getName());
138
139                         ConsumerFactory<? super Object, ? super Object> consumerFactory =
140                                         new DefaultKafkaConsumerFactory<>(propertyMap);
141
142                         ConcurrentKafkaListenerContainerFactory<Object, Object> kafkaListenerContainerFactory =
143                                         new ConcurrentKafkaListenerContainerFactory<>();
144
145                         kafkaListenerContainerFactory.setConsumerFactory(consumerFactory);
146
147                         return kafkaListenerContainerFactory;
148                 }
149
150                 @Bean
151                 Consumer consumer()
152                 {
153                         return new Consumer();
154                 }
155
156                 @Primary
157                 @Bean
158                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
159                 {
160                         return Stores.inMemoryKeyValueStore("TEST-STORE");
161                 }
162         }
163 }