counter: 1.1.6 - Refactored the test-data into a separated class
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / counter / CounterApplicationIT.java
1 package de.juplo.kafka.wordcount.counter;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import lombok.RequiredArgsConstructor;
5 import lombok.extern.slf4j.Slf4j;
6 import org.apache.kafka.clients.consumer.ConsumerRecord;
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.KafkaTemplate;
18 import org.springframework.kafka.test.context.EmbeddedKafka;
19
20 import java.time.Duration;
21 import java.util.LinkedList;
22 import java.util.List;
23
24 import static de.juplo.kafka.wordcount.counter.CounterApplicationIT.*;
25 import static org.awaitility.Awaitility.*;
26
27
28 @SpringBootTest(
29                 properties = {
30                                 "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
31                                 "juplo.wordcount.counter.bootstrap-server=${spring.embedded.kafka.brokers}",
32                                 "juplo.wordcount.counter.commit-interval=0",
33                                 "juplo.wordcount.counter.cacheMaxBytes=0",
34                                 "juplo.wordcount.counter.input-topic=" + TOPIC_IN,
35                                 "juplo.wordcount.counter.output-topic=" + TOPIC_OUT })
36 @EmbeddedKafka(topics = { TOPIC_IN, TOPIC_OUT }, partitions = PARTITIONS)
37 @Slf4j
38 public class CounterApplicationIT
39 {
40         public final static String TOPIC_IN = "in";
41         public final static String TOPIC_OUT = "out";
42         static final int PARTITIONS = 2;
43
44         @Autowired
45         KafkaTemplate<String, String> kafkaTemplate;
46         @Autowired
47         ObjectMapper mapper;
48         @Autowired
49         Consumer consumer;
50
51
52         @BeforeEach
53         public void clear()
54         {
55                 consumer.received.clear();
56         }
57
58
59         @Test
60         void testSendMessage()
61         {
62                 TestData.writeInputData((key, value) -> kafkaTemplate.send(TOPIC_IN, key, value));
63
64                 await("Expexted converted data")
65                                 .atMost(Duration.ofSeconds(10))
66                                 .untilAsserted(() -> TestData.assertExpectedResult(consumer.received, mapper));
67         }
68
69
70         @RequiredArgsConstructor
71         static class Consumer
72         {
73                 private final List<Message> received = new LinkedList<>();
74
75                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
76                 public void receive(ConsumerRecord<String, String> record)
77                 {
78                         log.debug("Received message: {}", record);
79                         received.add(Message.of(record.key(),record.value()));
80                 }
81         }
82
83         @TestConfiguration
84         static class Configuration
85         {
86                 @Bean
87                 Consumer consumer()
88                 {
89                         return new Consumer();
90                 }
91
92                 @Primary
93                 @Bean
94                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
95                 {
96                         return Stores.inMemoryKeyValueStore("TEST-STORE");
97                 }
98         }
99 }