counter: 1.2.11 - Refactored test-classes (DRY for test-config)
[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
31 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.*;
32 import static de.juplo.kafka.wordcount.counter.TestData.convertToMap;
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 = convertToMap(streamProcessorProperties);
101
102                         propertyMap.put(
103                                         ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
104                                         JsonSerializer.class.getName());
105                         propertyMap.put(
106                                         ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
107                                         JsonSerializer.class.getName());
108
109                         return new DefaultKafkaProducerFactory<>(propertyMap);
110                 }
111
112                 @Bean
113                 ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
114                                 Properties streamProcessorProperties)
115                 {
116                         Map<String, Object> propertyMap = convertToMap(streamProcessorProperties);
117
118                         propertyMap.put(
119                                         ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
120                                         JsonDeserializer.class.getName());
121                         propertyMap.put(
122                                         ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
123                                         JsonDeserializer.class.getName());
124
125                         ConsumerFactory<? super Object, ? super Object> consumerFactory =
126                                         new DefaultKafkaConsumerFactory<>(propertyMap);
127
128                         ConcurrentKafkaListenerContainerFactory<Object, Object> kafkaListenerContainerFactory =
129                                         new ConcurrentKafkaListenerContainerFactory<>();
130
131                         kafkaListenerContainerFactory.setConsumerFactory(consumerFactory);
132
133                         return kafkaListenerContainerFactory;
134                 }
135
136                 @Bean
137                 Consumer consumer()
138                 {
139                         return new Consumer();
140                 }
141
142                 @Primary
143                 @Bean
144                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
145                 {
146                         return Stores.inMemoryKeyValueStore("TEST-STORE");
147                 }
148         }
149 }