1 package de.juplo.kafka.wordcount.recorder;
3 import de.juplo.kafka.wordcount.splitter.TestRecording;
4 import lombok.extern.slf4j.Slf4j;
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.support.KafkaHeaders;
15 import org.springframework.kafka.test.context.EmbeddedKafka;
16 import org.springframework.messaging.handler.annotation.Header;
17 import org.springframework.messaging.handler.annotation.Payload;
18 import org.springframework.test.web.servlet.MockMvc;
19 import org.springframework.test.web.servlet.MvcResult;
20 import org.springframework.util.LinkedMultiValueMap;
21 import org.springframework.util.MultiValueMap;
23 import java.time.Duration;
24 import java.util.List;
25 import java.util.stream.Stream;
27 import static de.juplo.kafka.wordcount.recorder.RecorderApplicationIT.TOPIC_OUT;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.awaitility.Awaitility.await;
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
32 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
33 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
34 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
39 "spring.kafka.consumer.auto-offset-reset=earliest",
40 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
41 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.splitter.TestRecording",
42 "spring.kafka.consumer.properties.spring.json.trusted.packages=de.juplo.kafka.wordcount.recorder",
43 "logging.level.root=WARN",
44 "logging.level.de.juplo=DEBUG",
45 "juplo.wordcount.recorder.bootstrap-server=${spring.embedded.kafka.brokers}",
46 "juplo.wordcount.recorder.topic=" + TOPIC_OUT })
48 @EmbeddedKafka(topics = { TOPIC_OUT })
50 class RecorderApplicationIT
52 static final String TOPIC_OUT = "out";
61 @DisplayName("Posted messages are accepted and sent to Kafka")
62 void userMessagesAreAcceptedAndSentToKafka()
64 MultiValueMap<String, TestRecording> recordings = new LinkedMultiValueMap<>();
67 .of(TestData.INPUT_MESSAGES)
70 sendRedording(kv.key, kv.value);
71 recordings.add(kv.key, TestRecording.of(kv.key, kv.value));
75 await("Received expected messages")
76 .atMost(Duration.ofSeconds(5))
77 .untilAsserted(() -> recordings.forEach((user, userRecordings) ->
78 assertThat(consumer.receivedFor(user)).containsExactlyElementsOf(userRecordings)));
81 void sendRedording(String user, String sentence)
85 MvcResult result = mockMvc
86 .perform(post("/{user}", user)
87 .contentType(MediaType.TEXT_PLAIN)
91 mockMvc.perform(asyncDispatch(result))
93 .andExpect(status().isOk())
94 .andExpect(jsonPath("$.username").value(user))
95 .andExpect(jsonPath("$.sentence").value(sentence));
99 throw new RuntimeException(e);
104 static class Consumer
106 private final MultiValueMap<String, TestRecording> received = new LinkedMultiValueMap<>();
108 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
109 public synchronized void receive(
110 @Header(KafkaHeaders.RECEIVED_KEY) String user,
111 @Payload TestRecording recording)
113 log.debug("Received message: {}={}", user, recording);
114 received.add(user, recording);
117 synchronized List<TestRecording> receivedFor(String user)
119 List<TestRecording> recordings = received.get(user);
120 return recordings == null
122 : received.get(user).stream().toList();
127 static class Configuration
132 return new Consumer();