1 package de.juplo.kafka.wordcount.recorder;
3 import de.juplo.kafka.wordcount.splitter.TestRecording;
4 import de.juplo.kafka.wordcount.splitter.TestUser;
5 import lombok.extern.slf4j.Slf4j;
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.support.KafkaHeaders;
16 import org.springframework.kafka.test.context.EmbeddedKafka;
17 import org.springframework.messaging.handler.annotation.Header;
18 import org.springframework.messaging.handler.annotation.Payload;
19 import org.springframework.test.web.servlet.MockMvc;
20 import org.springframework.test.web.servlet.MvcResult;
21 import org.springframework.util.LinkedMultiValueMap;
22 import org.springframework.util.MultiValueMap;
24 import java.time.Duration;
25 import java.util.List;
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.key-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
41 "spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer",
42 "spring.kafka.consumer.properties.spring.json.key.default.type=de.juplo.kafka.wordcount.splitter.TestUser",
43 "spring.kafka.consumer.properties.spring.json.value.default.type=de.juplo.kafka.wordcount.splitter.TestRecording",
44 "logging.level.root=WARN",
45 "logging.level.de.juplo=DEBUG",
46 "juplo.wordcount.recorder.bootstrap-server=${spring.embedded.kafka.brokers}",
47 "juplo.wordcount.recorder.topic=" + TOPIC_OUT })
49 @EmbeddedKafka(topics = { TOPIC_OUT })
51 class RecorderApplicationIT
53 static final String TOPIC_OUT = "out";
62 @DisplayName("Posted messages are accepted and sent to Kafka")
63 void userMessagesAreAcceptedAndSentToKafka()
65 MultiValueMap<TestUser, TestRecording> recordings = new LinkedMultiValueMap<>();
71 sendRedording(kv.key, kv.value);
72 recordings.add(TestUser.of(kv.key), TestRecording.of(kv.key, kv.value));
76 await("Received expected messages")
77 .atMost(Duration.ofSeconds(5))
78 .untilAsserted(() -> recordings.forEach((user, userRecordings) ->
79 assertThat(consumer.receivedFor(user)).containsExactlyElementsOf(userRecordings)));
82 void sendRedording(String user, String sentence)
86 MvcResult result = mockMvc
87 .perform(post("/{user}", user)
88 .contentType(MediaType.TEXT_PLAIN)
92 mockMvc.perform(asyncDispatch(result))
94 .andExpect(status().isOk())
95 .andExpect(jsonPath("$.username").value(user))
96 .andExpect(jsonPath("$.sentence").value(sentence));
100 throw new RuntimeException(e);
105 static class Consumer
107 private final MultiValueMap<TestUser, TestRecording> received = new LinkedMultiValueMap<>();
109 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
110 public synchronized void receive(
111 @Header(KafkaHeaders.RECEIVED_KEY) TestUser user,
112 @Payload TestRecording recording)
114 log.debug("Received message: {}={}", user, recording);
115 received.add(user, recording);
118 synchronized List<TestRecording> receivedFor(TestUser user)
120 List<TestRecording> recordings = received.get(user);
121 return recordings == null
123 : received.get(user).stream().toList();
128 static class Configuration
133 return new Consumer();