counter: 1.1.7 - Fixed a bug in the assertion-logic for the expected state
[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.streams.state.KeyValueBytesStoreSupplier;
7 import org.apache.kafka.streams.state.Stores;
8 import org.junit.jupiter.api.BeforeEach;
9 import org.junit.jupiter.api.Test;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.test.context.SpringBootTest;
12 import org.springframework.boot.test.context.TestConfiguration;
13 import org.springframework.context.annotation.Bean;
14 import org.springframework.context.annotation.Primary;
15 import org.springframework.kafka.annotation.KafkaListener;
16 import org.springframework.kafka.core.KafkaTemplate;
17 import org.springframework.kafka.test.context.EmbeddedKafka;
18
19 import java.time.Duration;
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.*;
24 import static org.awaitility.Awaitility.*;
25
26
27 @SpringBootTest(
28                 properties = {
29                                 "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
30                                 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
31                                 "juplo.wordcount.counter.commit-interval=0",
32                                 "juplo.wordcount.counter.cacheMaxBytes=0",
33                                 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
34                                 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
35 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT }, partitions = PARTITIONS)
36 @Slf4j
37 public class CounterApplicationIT
38 {
39         public final static String TOPIC_IN = "in";
40         public final static String TOPIC_OUT = "out";
41         static final int PARTITIONS = 2;
42
43         @Autowired
44         KafkaTemplate<String, String> kafkaTemplate;
45         @Autowired
46         Consumer consumer;
47
48
49         @BeforeEach
50         public void clear()
51         {
52                 consumer.received.clear();
53         }
54
55
56         @Test
57         void testSendMessage()
58         {
59                 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
60
61                 await("Expexted converted data")
62                                 .atMost(Duration.ofSeconds(10))
63                                 .untilAsserted(() -> TestData.assertExpectedResult(consumer.received));
64         }
65
66
67         @RequiredArgsConstructor
68         static class Consumer
69         {
70                 private final List<Message> received = new LinkedList<>();
71
72                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
73                 public void receive(ConsumerRecord<String, String> record)
74                 {
75                         log.debug("Received message: {}", record);
76                         received.add(Message.of(record.key(),record.value()));
77                 }
78         }
79
80         @TestConfiguration
81         static class Configuration
82         {
83                 @Bean
84                 Consumer consumer()
85                 {
86                         return new Consumer();
87                 }
88
89                 @Primary
90                 @Bean
91                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
92                 {
93                         return Stores.inMemoryKeyValueStore("TEST-STORE");
94                 }
95         }
96 }