WIP
[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.Test;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.boot.test.context.SpringBootTest;
8 import org.springframework.boot.test.context.TestConfiguration;
9 import org.springframework.context.annotation.Bean;
10 import org.springframework.http.HttpHeaders;
11 import org.springframework.http.MediaType;
12 import org.springframework.kafka.annotation.KafkaListener;
13 import org.springframework.kafka.test.context.EmbeddedKafka;
14 import org.springframework.test.web.reactive.server.WebTestClient;
15 import org.springframework.util.LinkedMultiValueMap;
16 import org.springframework.util.MimeType;
17 import org.springframework.util.MimeTypeUtils;
18 import org.springframework.util.MultiValueMap;
19
20 import static de.juplo.kafka.wordcount.recorder.ApplicationTests.PARTITIONS;
21 import static de.juplo.kafka.wordcount.recorder.ApplicationTests.TOPIC_OUT;
22
23
24 @SpringBootTest(
25                 properties = {
26                                 "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
27                                 "juplo.wordcount.recorder.bootstrap-server=${spring.embedded.kafka.brokers}",
28                                 "juplo.wordcount.recorder.topic=" + TOPIC_OUT })
29 @EmbeddedKafka(topics = { TOPIC_OUT }, partitions = PARTITIONS)
30 @Slf4j
31 class ApplicationTests
32 {
33         public final static String TOPIC_OUT = "out";
34         static final int PARTITIONS = 2;
35
36         @Autowired
37         WebTestClient client;
38
39         @Test
40         void contextLoads()
41         {
42         }
43
44         @Test
45         void userEventsAreSent()
46         {
47                 client
48                                 .post()
49                                 .uri("peter")
50                                 .contentType(MediaType.TEXT_PLAIN)
51                                 .bodyValue("Hallö Wält*&%€!")
52                                 .
53         }
54
55         static class Consumer
56         {
57                 final MultiValueMap<String, String> received = new LinkedMultiValueMap<>();
58
59                 @KafkaListener(groupId = "TEST", topics = TOPIC_OUT)
60                 public void receive(ConsumerRecord<String, String> record)
61                 {
62                         log.debug("Received message: {}", record);
63                         received.add(record.key(), record.value());
64                 }
65         }
66
67         @TestConfiguration
68         static class Configuration
69         {
70                 @Bean
71                 Consumer consumer()
72                 {
73                         return new Consumer();
74                 }
75         }
76 }