1 package de.juplo.kafka.wordcount.counter;
3 import lombok.RequiredArgsConstructor;
4 import lombok.extern.slf4j.Slf4j;
5 import org.apache.kafka.clients.consumer.ConsumerRecord;
6 import org.apache.kafka.clients.producer.ProducerConfig;
7 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
8 import org.apache.kafka.streams.state.Stores;
9 import org.junit.jupiter.api.BeforeEach;
10 import org.junit.jupiter.api.Test;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.boot.test.context.SpringBootTest;
13 import org.springframework.boot.test.context.TestConfiguration;
14 import org.springframework.context.annotation.Bean;
15 import org.springframework.context.annotation.Primary;
16 import org.springframework.kafka.annotation.KafkaListener;
17 import org.springframework.kafka.core.DefaultKafkaProducerFactory;
18 import org.springframework.kafka.core.KafkaTemplate;
19 import org.springframework.kafka.core.ProducerFactory;
20 import org.springframework.kafka.support.serializer.JsonSerializer;
21 import org.springframework.kafka.test.context.EmbeddedKafka;
23 import java.time.Duration;
24 import java.util.LinkedList;
25 import java.util.List;
27 import java.util.Properties;
28 import java.util.stream.Collectors;
30 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.*;
31 import static org.awaitility.Awaitility.*;
36 "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
37 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
38 "juplo.wordcount.counter.commit-interval=0",
39 "juplo.wordcount.counter.cacheMaxBytes=0",
40 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
41 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
42 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT }, partitions = PARTITIONS)
44 public class CounterApplicationIT
46 public final static String TOPIC_IN = "in";
47 public final static String TOPIC_OUT = "out";
48 static final int PARTITIONS = 2;
51 KafkaTemplate<String, Word> kafkaTemplate;
59 consumer.received.clear();
64 void testSendMessage()
66 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
68 await("Expexted converted data")
69 .atMost(Duration.ofSeconds(10))
70 .untilAsserted(() -> TestData.assertExpectedResult(consumer.getReceivedMessages()));
74 @RequiredArgsConstructor
77 private final List<Message> received = new LinkedList<>();
79 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
80 public synchronized void receive(ConsumerRecord<String, String> record)
82 log.debug("Received message: {}", record);
83 received.add(Message.of(record.key(),record.value()));
86 synchronized List<Message> getReceivedMessages()
93 static class Configuration
96 ProducerFactory<?, ?> producerFactory(Properties streamProcessorProperties)
98 Map<String, Object> propertyMap = streamProcessorProperties
103 entry -> (String)entry.getKey(),
104 entry -> entry.getValue()
108 ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
109 JsonSerializer.class.getName());
111 ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
112 JsonSerializer.class.getName());
114 return new DefaultKafkaProducerFactory<>(propertyMap);
120 return new Consumer();
125 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
127 return Stores.inMemoryKeyValueStore("TEST-STORE");