76cfe42fba38a1e2ec3ff5e48edf54ba3c5cb8e4
[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.bootstrap-servers=${spring.embedded.kafka.brokers}",
30                                 "producer.topic=" + TOPIC})
31 @AutoConfigureMockMvc
32 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
33 @Slf4j
34 public class ApplicationTests
35 {
36         static final String TOPIC = "FOO";
37         static final int PARTITIONS = 10;
38
39         @Autowired
40         MockMvc mockMvc;
41         @Autowired
42         Consumer consumer;
43
44
45         @BeforeEach
46         public void clear()
47         {
48                 consumer.received.clear();
49         }
50
51
52         @Test
53         void testSendClientMessage() throws Exception
54         {
55                 mockMvc
56                                 .perform(post("/peter").content("Hallo Welt!"))
57                                 .andExpect(status().isOk());
58                 await("Message was send")
59                                 .atMost(Duration.ofSeconds(5))
60                                 .until(() -> consumer.received.size() == 1);
61         }
62
63         @Test
64         void testSendFooMessage() throws Exception
65         {
66                 mockMvc
67                                 .perform(put("/peter"))
68                                 .andExpect(status().isOk());
69                 await("Message was send")
70                                 .atMost(Duration.ofSeconds(5))
71                                 .until(() -> consumer.received.size() == 1);
72         }
73
74         @Test
75         void testSendGreeting() throws Exception
76         {
77                 mockMvc
78                                 .perform(post("/").content("peter"))
79                                 .andExpect(status().isOk());
80                 await("Message was send")
81                                 .atMost(Duration.ofSeconds(5))
82                                 .until(() -> consumer.received.size() == 1);
83         }
84
85
86         static class Consumer
87         {
88                 final List<ConsumerRecord<String, String>> received = new LinkedList<>();
89
90                 @KafkaListener(groupId = "TEST", topics = TOPIC)
91                 public void receive(ConsumerRecord<String, String> record)
92                 {
93                         log.debug("Received message: {}", record);
94                         received.add(record);
95                 }
96         }
97
98         @TestConfiguration
99         static class Configuration
100         {
101                 @Bean
102                 Consumer consumer()
103                 {
104                         return new Consumer();
105                 }
106         }
107 }