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