1 package de.juplo.kafka.wordcount.counter;
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;
25 import java.time.Duration;
26 import java.util.LinkedList;
27 import java.util.List;
29 import java.util.Properties;
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.*;
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)
46 public class CounterApplicationIT
48 public final static String TOPIC_IN = "in";
49 public final static String TOPIC_OUT = "out";
50 static final int PARTITIONS = 2;
53 KafkaTemplate<String, Word> kafkaTemplate;
61 consumer.received.clear();
66 void testSendMessage()
68 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
70 await("Expexted converted data")
71 .atMost(Duration.ofSeconds(10))
72 .untilAsserted(() -> TestData.assertExpectedResult(consumer.getReceivedMessages()));
76 @RequiredArgsConstructor
79 private final List<KeyValue<Word, WordCount>> received = new LinkedList<>();
81 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
82 public synchronized void receive(ConsumerRecord<Word, WordCount> record)
84 log.debug("Received message: {}", record);
85 received.add(KeyValue.pair(record.key(),record.value()));
88 synchronized List<KeyValue<Word, WordCount>> getReceivedMessages()
95 static class Configuration
98 ProducerFactory<?, ?> producerFactory(Properties streamProcessorProperties)
100 Map<String, Object> propertyMap = convertToMap(streamProcessorProperties);
103 ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
104 JsonSerializer.class.getName());
106 ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
107 JsonSerializer.class.getName());
109 return new DefaultKafkaProducerFactory<>(propertyMap);
113 ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
114 Properties streamProcessorProperties)
116 Map<String, Object> propertyMap = convertToMap(streamProcessorProperties);
119 ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
120 JsonDeserializer.class.getName());
122 ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
123 JsonDeserializer.class.getName());
125 ConsumerFactory<? super Object, ? super Object> consumerFactory =
126 new DefaultKafkaConsumerFactory<>(propertyMap);
128 ConcurrentKafkaListenerContainerFactory<Object, Object> kafkaListenerContainerFactory =
129 new ConcurrentKafkaListenerContainerFactory<>();
131 kafkaListenerContainerFactory.setConsumerFactory(consumerFactory);
133 return kafkaListenerContainerFactory;
139 return new Consumer();
144 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
146 return Stores.inMemoryKeyValueStore("TEST-STORE");