counter: 1.2.8 - Reconfigured tests to send data as domain-instances
[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.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;
22
23 import java.time.Duration;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.stream.Collectors;
29
30 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.*;
31 import static org.awaitility.Awaitility.*;
32
33
34 @SpringBootTest(
35                 properties = {
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)
43 @Slf4j
44 public class CounterApplicationIT
45 {
46         public final static String TOPIC_IN = "in";
47         public final static String TOPIC_OUT = "out";
48         static final int PARTITIONS = 2;
49
50         @Autowired
51         KafkaTemplate<String, Word> kafkaTemplate;
52         @Autowired
53         Consumer consumer;
54
55
56         @BeforeEach
57         public void clear()
58         {
59                 consumer.received.clear();
60         }
61
62
63         @Test
64         void testSendMessage()
65         {
66                 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
67
68                 await("Expexted converted data")
69                                 .atMost(Duration.ofSeconds(10))
70                                 .untilAsserted(() -> TestData.assertExpectedResult(consumer.getReceivedMessages()));
71         }
72
73
74         @RequiredArgsConstructor
75         static class Consumer
76         {
77                 private final List<Message> received = new LinkedList<>();
78
79                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
80                 public synchronized void receive(ConsumerRecord<String, String> record)
81                 {
82                         log.debug("Received message: {}", record);
83                         received.add(Message.of(record.key(),record.value()));
84                 }
85
86                 synchronized List<Message> getReceivedMessages()
87                 {
88                         return received;
89                 }
90         }
91
92         @TestConfiguration
93         static class Configuration
94         {
95                 @Bean
96                 ProducerFactory<?, ?> producerFactory(Properties streamProcessorProperties)
97                 {
98                         Map<String, Object> propertyMap = streamProcessorProperties
99                                         .entrySet()
100                                         .stream()
101                                         .collect(
102                                                         Collectors.toMap(
103                                                                         entry -> (String)entry.getKey(),
104                                                                         entry -> entry.getValue()
105                                                         ));
106
107                         propertyMap.put(
108                                         ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
109                                         JsonSerializer.class.getName());
110                         propertyMap.put(
111                                         ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
112                                         JsonSerializer.class.getName());
113
114                         return new DefaultKafkaProducerFactory<>(propertyMap);
115                 }
116
117                 @Bean
118                 Consumer consumer()
119                 {
120                         return new Consumer();
121                 }
122
123                 @Primary
124                 @Bean
125                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
126                 {
127                         return Stores.inMemoryKeyValueStore("TEST-STORE");
128                 }
129         }
130 }