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 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;
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)
49 public class CounterApplicationIT
51 public final static String TOPIC_IN = "in";
52 public final static String TOPIC_OUT = "out";
53 static final int PARTITIONS = 2;
56 KafkaTemplate<String, Word> kafkaTemplate;
64 consumer.received.clear();
69 void testSendMessage()
71 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
73 await("Expexted converted data")
74 .atMost(Duration.ofSeconds(10))
75 .untilAsserted(() -> TestData.assertExpectedResult(consumer.getReceivedMessages()));
79 @RequiredArgsConstructor
82 private final List<KeyValue<Word, WordCounter>> received = new LinkedList<>();
84 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
85 public synchronized void receive(ConsumerRecord<Word, WordCounter> record)
88 "Received message: {} -> {}, key: {}, 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()));
96 synchronized List<KeyValue<Word, WordCounter>> getReceivedMessages()
103 static class Configuration
106 ProducerFactory<?, ?> producerFactory(Properties streamProcessorProperties)
108 Map<String, Object> propertyMap = convertToMap(streamProcessorProperties);
111 ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
112 JsonSerializer.class.getName());
114 ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
115 JsonSerializer.class.getName());
117 return new DefaultKafkaProducerFactory<>(propertyMap);
121 ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
122 Properties streamProcessorProperties)
124 Map<String, Object> propertyMap = convertToMap(streamProcessorProperties);
127 ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
128 JsonDeserializer.class.getName());
130 ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
131 JsonDeserializer.class.getName());
133 ConsumerFactory<? super Object, ? super Object> consumerFactory =
134 new DefaultKafkaConsumerFactory<>(propertyMap);
136 ConcurrentKafkaListenerContainerFactory<Object, Object> kafkaListenerContainerFactory =
137 new ConcurrentKafkaListenerContainerFactory<>();
139 kafkaListenerContainerFactory.setConsumerFactory(consumerFactory);
141 return kafkaListenerContainerFactory;
147 return new Consumer();
152 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
154 return Stores.inMemoryKeyValueStore("TEST-STORE");