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.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;
24 import java.time.Duration;
25 import java.util.LinkedList;
26 import java.util.List;
28 import java.util.Properties;
29 import java.util.stream.Collectors;
31 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.*;
32 import static org.awaitility.Awaitility.*;
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)
45 public class CounterApplicationIT
47 public final static String TOPIC_IN = "in";
48 public final static String TOPIC_OUT = "out";
49 static final int PARTITIONS = 2;
52 KafkaTemplate<String, Word> kafkaTemplate;
60 consumer.received.clear();
65 void testSendMessage()
67 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
69 await("Expexted converted data")
70 .atMost(Duration.ofSeconds(10))
71 .untilAsserted(() -> TestData.assertExpectedResult(consumer.getReceivedMessages()));
75 @RequiredArgsConstructor
78 private final List<Message> received = new LinkedList<>();
80 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
81 public synchronized void receive(ConsumerRecord<Word, WordCount> record)
83 log.debug("Received message: {}", record);
84 received.add(Message.of(record.key(),record.value()));
87 synchronized List<Message> getReceivedMessages()
94 static class Configuration
97 ProducerFactory<?, ?> producerFactory(Properties streamProcessorProperties)
99 Map<String, Object> propertyMap = streamProcessorProperties
104 entry -> (String)entry.getKey(),
105 entry -> entry.getValue()
109 ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
110 JsonSerializer.class.getName());
112 ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
113 JsonSerializer.class.getName());
115 return new DefaultKafkaProducerFactory<>(propertyMap);
119 ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
120 Properties streamProcessorProperties)
122 Map<String, Object> propertyMap = streamProcessorProperties
127 entry -> (String)entry.getKey(),
128 entry -> entry.getValue()
132 ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
133 JsonDeserializer.class.getName());
135 ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
136 JsonDeserializer.class.getName());
138 ConsumerFactory<? super Object, ? super Object> consumerFactory =
139 new DefaultKafkaConsumerFactory<>(propertyMap);
141 ConcurrentKafkaListenerContainerFactory<Object, Object> kafkaListenerContainerFactory =
142 new ConcurrentKafkaListenerContainerFactory<>();
144 kafkaListenerContainerFactory.setConsumerFactory(consumerFactory);
146 return kafkaListenerContainerFactory;
152 return new Consumer();
157 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
159 return Stores.inMemoryKeyValueStore("TEST-STORE");