0a70993e47774f420ff37d66c8f0b536b94a9217
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / recorder / ApplicationTests.java
1 package de.juplo.kafka.wordcount.recorder;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerRecord;
5 import org.junit.jupiter.api.DisplayName;
6 import org.junit.jupiter.api.Test;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.boot.test.context.TestConfiguration;
11 import org.springframework.context.annotation.Bean;
12 import org.springframework.http.MediaType;
13 import org.springframework.kafka.annotation.KafkaListener;
14 import org.springframework.kafka.test.context.EmbeddedKafka;
15 import org.springframework.test.web.servlet.MockMvc;
16 import org.springframework.util.LinkedMultiValueMap;
17 import org.springframework.util.MultiValueMap;
18
19 import static de.juplo.kafka.wordcount.recorder.ApplicationTests.PARTITIONS;
20 import static de.juplo.kafka.wordcount.recorder.ApplicationTests.TOPIC_OUT;
21 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
22 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
23 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
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                                 "juplo.wordcount.recorder.bootstrap-server=${spring.embedded.kafka.brokers}",
31                                 "juplo.wordcount.recorder.topic=" + TOPIC_OUT })
32 @AutoConfigureMockMvc
33 @EmbeddedKafka(topics = { TOPIC_OUT }, partitions = PARTITIONS)
34 @Slf4j
35 class ApplicationTests
36 {
37         static final String TOPIC_OUT = "out";
38         static final int PARTITIONS = 2;
39         static final String USER = "päter";
40         static final String SENTENCE = "Hall° Wält?¢*&%€!";
41
42
43         @Autowired
44         private MockMvc mockMvc;
45
46         @Test
47         @DisplayName("The application context loads")
48         void contextLoads()
49         {
50         }
51
52         @Test
53         void userMessagesAreExceptedAndSentToKafka() throws Exception
54         {
55                 mockMvc
56                                 .perform(post("/{user}", USER)
57                                                 .contentType(MediaType.TEXT_PLAIN)
58                                                 .content(SENTENCE))
59                                 .andDo(print())
60                                 .andExpect(status().isOk())
61                                 .andExpect(jsonPath("$.username").value(USER))
62                                 .andExpect(jsonPath("$.sentence").value(SENTENCE));
63         }
64
65         static class Consumer
66         {
67                 final MultiValueMap<String, String> received = new LinkedMultiValueMap<>();
68
69                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
70                 public void receive(ConsumerRecord<String, String> record)
71                 {
72                         log.debug("Received message: {}", record);
73                         received.add(record.key(), record.value());
74                 }
75         }
76
77         @TestConfiguration
78         static class Configuration
79         {
80                 @Bean
81                 Consumer consumer()
82                 {
83                         return new Consumer();
84                 }
85         }
86 }