counter: 1.3.1 - Splitted up test in `CounterStreamProcessorTopologyTest`
[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 de.juplo.kafka.wordcount.counter.TestData.parseHeader;
34 import static org.awaitility.Awaitility.await;
35 import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.*;
36 import static org.springframework.kafka.support.mapping.AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME;
37
38
39 @SpringBootTest(
40                 properties = {
41                                 "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
42                                 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
43                                 "juplo.wordcount.counter.commit-interval=0",
44                                 "juplo.wordcount.counter.cacheMaxBytes=0",
45                                 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
46                                 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
47 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT }, partitions = PARTITIONS)
48 @Slf4j
49 public class CounterApplicationIT
50 {
51         public final static String TOPIC_IN = "in";
52         public final static String TOPIC_OUT = "out";
53         static final int PARTITIONS = 2;
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         @RequiredArgsConstructor
80         static class Consumer
81         {
82                 private final List<KeyValue<Word, WordCounter>> received = new LinkedList<>();
83
84                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
85                 public synchronized void receive(ConsumerRecord<Word, WordCounter> record)
86                 {
87                         log.debug(
88                                         "Received message: {} -> {}, key: {}, value: {}",
89                                         record.key(),
90                                         record.value(),
91                                         parseHeader(record.headers(), KEY_DEFAULT_CLASSID_FIELD_NAME),
92                                         parseHeader(record.headers(), DEFAULT_CLASSID_FIELD_NAME));
93                         received.add(KeyValue.pair(record.key(),record.value()));
94                 }
95
96                 synchronized List<KeyValue<Word, WordCounter>> getReceivedMessages()
97                 {
98                         return received;
99                 }
100         }
101
102         @TestConfiguration
103         static class Configuration
104         {
105                 @Bean
106                 ProducerFactory<?, ?> producerFactory(Properties streamProcessorProperties)
107                 {
108                         Map<String, Object> propertyMap = convertToMap(streamProcessorProperties);
109
110                         propertyMap.put(
111                                         ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
112                                         JsonSerializer.class.getName());
113                         propertyMap.put(
114                                         ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
115                                         JsonSerializer.class.getName());
116
117                         return new DefaultKafkaProducerFactory<>(propertyMap);
118                 }
119
120                 @Bean
121                 ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
122                                 Properties streamProcessorProperties)
123                 {
124                         Map<String, Object> propertyMap = convertToMap(streamProcessorProperties);
125
126                         propertyMap.put(
127                                         ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
128                                         JsonDeserializer.class.getName());
129                         propertyMap.put(
130                                         ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
131                                         JsonDeserializer.class.getName());
132
133                         ConsumerFactory<? super Object, ? super Object> consumerFactory =
134                                         new DefaultKafkaConsumerFactory<>(propertyMap);
135
136                         ConcurrentKafkaListenerContainerFactory<Object, Object> kafkaListenerContainerFactory =
137                                         new ConcurrentKafkaListenerContainerFactory<>();
138
139                         kafkaListenerContainerFactory.setConsumerFactory(consumerFactory);
140
141                         return kafkaListenerContainerFactory;
142                 }
143
144                 @Bean
145                 Consumer consumer()
146                 {
147                         return new Consumer();
148                 }
149
150                 @Primary
151                 @Bean
152                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
153                 {
154                         return Stores.inMemoryKeyValueStore("TEST-STORE");
155                 }
156         }
157 }