Vorlage für die JSON-Version des Rest-Producers
[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.http.MediaType;
12 import org.springframework.kafka.annotation.KafkaListener;
13 import org.springframework.kafka.test.context.EmbeddedKafka;
14 import org.springframework.test.web.servlet.MockMvc;
15
16 import java.time.Duration;
17 import java.util.LinkedList;
18 import java.util.List;
19
20 import static de.juplo.kafka.ApplicationTests.PARTITIONS;
21 import static de.juplo.kafka.ApplicationTests.TOPIC;
22 import static org.awaitility.Awaitility.*;
23 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
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 testSendMessage() throws Exception
55         {
56                 mockMvc
57                                 .perform(
58                                                 post("/peter")
59                                                                 .header("X-id", 7)
60                                                                 .contentType(MediaType.APPLICATION_JSON)
61                                                                 .content("666"))
62                                 .andExpect(status().isOk());
63                 await("Message was send")
64                                 .atMost(Duration.ofSeconds(5))
65                                 .until(() -> consumer.received.size() == 1);
66         }
67
68
69         static class Consumer
70         {
71                 final List<ConsumerRecord<String, String>> received = new LinkedList<>();
72
73                 @KafkaListener(groupId = "TEST", topics = TOPIC)
74                 public void receive(ConsumerRecord<String, String> record)
75                 {
76                         log.debug("Received message: {}", record);
77                         received.add(record);
78                 }
79         }
80
81         @TestConfiguration
82         static class Configuration
83         {
84                 @Bean
85                 Consumer consumer()
86                 {
87                         return new Consumer();
88                 }
89         }
90 }