1 package de.juplo.kafka.wordcount.recorder;
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;
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;
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 })
33 @EmbeddedKafka(topics = { TOPIC_OUT }, partitions = PARTITIONS)
35 class ApplicationTests
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?¢*&%€!";
44 private MockMvc mockMvc;
47 @DisplayName("The application context loads")
53 void userMessagesAreExceptedAndSentToKafka() throws Exception
56 .perform(post("/{user}", USER)
57 .contentType(MediaType.TEXT_PLAIN)
60 .andExpect(status().isOk())
61 .andExpect(jsonPath("$.username").value(USER))
62 .andExpect(jsonPath("$.sentence").value(SENTENCE));
67 final MultiValueMap<String, String> received = new LinkedMultiValueMap<>();
69 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
70 public void receive(ConsumerRecord<String, String> record)
72 log.debug("Received message: {}", record);
73 received.add(record.key(), record.value());
78 static class Configuration
83 return new Consumer();