cd7d9283f0a879307930b159b6522e5f3ad69e29
[demos/kafka/training] / src / test / java / de / juplo / kafka / ApplicationTests.java
1 package de.juplo.kafka;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerRecord;
5 import org.junit.jupiter.api.*;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8 import org.springframework.boot.test.context.SpringBootTest;
9 import org.springframework.boot.test.context.TestConfiguration;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.kafka.annotation.KafkaListener;
12 import org.springframework.kafka.test.context.EmbeddedKafka;
13 import org.springframework.test.web.servlet.MockMvc;
14
15 import java.time.Duration;
16 import java.util.LinkedList;
17 import java.util.List;
18
19 import static de.juplo.kafka.ApplicationTests.PARTITIONS;
20 import static de.juplo.kafka.ApplicationTests.TOPIC;
21 import static org.awaitility.Awaitility.*;
22 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
23 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
24 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
25
26
27 @SpringBootTest(
28                 properties = {
29                                 "spring.kafka.consumer.bootstrap-servers=${spring.embedded.kafka.brokers}",
30                                 "producer.bootstrap-server=${spring.embedded.kafka.brokers}",
31                                 "producer.topic=" + TOPIC})
32 @AutoConfigureMockMvc
33 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
34 @Slf4j
35 public class ApplicationTests
36 {
37         static final String TOPIC = "FOO";
38         static final int PARTITIONS = 10;
39
40         @Autowired
41         MockMvc mockMvc;
42         @Autowired
43         Consumer consumer;
44
45
46         @BeforeEach
47         public void clear()
48         {
49                 consumer.received.clear();
50         }
51
52
53         @Test
54         void testSendClientMessage() throws Exception
55         {
56                 mockMvc
57                                 .perform(post("/peter").content("Hallo Welt!"))
58                                 .andExpect(status().isOk());
59                 await("Message was send")
60                                 .atMost(Duration.ofSeconds(5))
61                                 .until(() -> consumer.received.size() == 1);
62         }
63
64         @Test
65         void testSendFooMessage() throws Exception
66         {
67                 mockMvc
68                                 .perform(put("/peter"))
69                                 .andExpect(status().isOk());
70                 await("Message was send")
71                                 .atMost(Duration.ofSeconds(5))
72                                 .until(() -> consumer.received.size() == 1);
73         }
74
75         @Test
76         void testSendGreeting() throws Exception
77         {
78                 mockMvc
79                                 .perform(post("/").content("peter"))
80                                 .andExpect(status().isOk());
81                 await("Message was send")
82                                 .atMost(Duration.ofSeconds(5))
83                                 .until(() -> consumer.received.size() == 1);
84         }
85
86
87         static class Consumer
88         {
89                 final List<ConsumerRecord<String, String>> received = new LinkedList<>();
90
91                 @KafkaListener(groupId = "TEST", topics = TOPIC)
92                 public void receive(ConsumerRecord<String, String> record)
93                 {
94                         log.debug("Received message: {}", record);
95                         received.add(record);
96                 }
97         }
98
99         @TestConfiguration
100         static class Configuration
101         {
102                 @Bean
103                 Consumer consumer()
104                 {
105                         return new Consumer();
106                 }
107         }
108 }