WIP
[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.junit.jupiter.api.extension.ExtendWith;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.beans.factory.annotation.Value;
9 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
10 import org.springframework.boot.test.context.SpringBootTest;
11 import org.springframework.boot.test.context.TestConfiguration;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.kafka.annotation.KafkaListener;
14 import org.springframework.kafka.test.context.EmbeddedKafka;
15 import org.springframework.test.context.junit.jupiter.SpringExtension;
16 import org.springframework.test.web.servlet.MockMvc;
17
18 import java.time.Duration;
19 import java.util.LinkedList;
20 import java.util.List;
21
22 import static de.juplo.kafka.ApplicationTests.PARTITIONS;
23 import static de.juplo.kafka.ApplicationTests.TOPIC;
24 import static org.awaitility.Awaitility.*;
25 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
28
29
30 @ExtendWith(SpringExtension.class)
31 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
32 @Slf4j
33 public class ApplicationTests
34 {
35         static final String TOPIC = "FOO";
36         static final int PARTITIONS = 10;
37
38   @Value("${spring.embedded.kafka.brokers}")
39   String bootstrapServers;
40
41         @Test
42         void testConcurrentProducers() throws Exception
43         {
44     SimpleProducer instanceA = new SimpleProducer(
45       bootstrapServers,
46       TOPIC,
47       "A");
48     SimpleProducer instanceB = new SimpleProducer(
49       bootstrapServers,
50       TOPIC,
51       "B");
52
53
54                 mockMvc
55                                 .perform(post("/peter").content("Hallo Welt!"))
56                                 .andExpect(status().isOk());
57                 await("Message was send")
58                                 .atMost(Duration.ofSeconds(5))
59                                 .until(() -> consumer.received.size() == 1);
60         }
61
62         @Test
63         void testSendFooMessage() throws Exception
64         {
65                 mockMvc
66                                 .perform(put("/peter"))
67                                 .andExpect(status().isOk());
68                 await("Message was send")
69                                 .atMost(Duration.ofSeconds(5))
70                                 .until(() -> consumer.received.size() == 1);
71         }
72
73         @Test
74         void testSendGreeting() throws Exception
75         {
76                 mockMvc
77                                 .perform(post("/").content("peter"))
78                                 .andExpect(status().isOk());
79                 await("Message was send")
80                                 .atMost(Duration.ofSeconds(5))
81                                 .until(() -> consumer.received.size() == 1);
82         }
83 }