1 package de.juplo.kafka.wordcount.recorder;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import lombok.extern.slf4j.Slf4j;
5 import org.apache.kafka.clients.consumer.ConsumerRecord;
6 import org.junit.jupiter.api.DisplayName;
7 import org.junit.jupiter.api.Test;
8 import org.springframework.beans.factory.annotation.Autowired;
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.http.MediaType;
14 import org.springframework.kafka.annotation.KafkaListener;
15 import org.springframework.kafka.test.context.EmbeddedKafka;
16 import org.springframework.test.web.servlet.MockMvc;
17 import org.springframework.test.web.servlet.MvcResult;
18 import org.springframework.util.LinkedMultiValueMap;
19 import org.springframework.util.MultiValueMap;
21 import java.time.Duration;
23 import static de.juplo.kafka.wordcount.recorder.ApplicationTests.PARTITIONS;
24 import static de.juplo.kafka.wordcount.recorder.ApplicationTests.TOPIC_OUT;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.awaitility.Awaitility.await;
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
29 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
30 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
31 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
36 "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
37 "juplo.wordcount.recorder.bootstrap-server=${spring.embedded.kafka.brokers}",
38 "juplo.wordcount.recorder.topic=" + TOPIC_OUT })
40 @EmbeddedKafka(topics = { TOPIC_OUT }, partitions = PARTITIONS)
42 class ApplicationTests
44 static final String TOPIC_OUT = "out";
45 static final int PARTITIONS = 2;
46 static final String USER = "päter";
47 static final String SENTENCE = "Hall° Wält?¢*&%€!";
55 ObjectMapper objectMapper;
59 @DisplayName("The application context loads")
65 @DisplayName("Posted messages are excepted and sent to Kafka")
66 void userMessagesAreExceptedAndSentToKafka() throws Exception
68 MvcResult result = mockMvc
69 .perform(post("/{user}", USER)
70 .contentType(MediaType.TEXT_PLAIN)
74 mockMvc.perform(asyncDispatch(result))
76 .andExpect(status().isOk())
77 .andExpect(jsonPath("$.username").value(USER))
78 .andExpect(jsonPath("$.sentence").value(SENTENCE));
80 await("Expexted converted data")
81 .atMost(Duration.ofSeconds(10))
84 assertThat(consumer.received.get(USER)).hasSize(1);
85 RecordingResult recordingResult = objectMapper.readValue(
86 consumer.received.get(USER).get(0),
87 RecordingResult.class);
88 assertThat(recordingResult.getUsername()).isEqualTo(USER);
89 assertThat(recordingResult.getSentence()).isEqualTo(SENTENCE);
90 assertThat(recordingResult.getTopic()).isEqualTo(TOPIC_OUT);
91 assertThat(recordingResult.getPartition()).isBetween(0, PARTITIONS - 1);
92 assertThat(recordingResult.getStatus()).isNull();
93 assertThat(recordingResult.getError()).isNull();
99 final MultiValueMap<String, String> received = new LinkedMultiValueMap<>();
101 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
102 public void receive(ConsumerRecord<String, String> record)
104 log.debug("Received message: {}", record);
105 received.add(record.key(), record.value());
110 static class Configuration
115 return new Consumer();