8c343d5e6d00136de545568b5d2705b24a7a3a55
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / splitter / TestData.java
1 package de.juplo.kafka.wordcount.splitter;
2
3 import de.juplo.kafka.wordcount.counter.TestWord;
4 import de.juplo.kafka.wordcount.recorder.TestRecording;
5 import org.apache.kafka.streams.KeyValue;
6 import org.springframework.util.LinkedMultiValueMap;
7 import org.springframework.util.MultiValueMap;
8
9 import java.time.Duration;
10 import java.util.stream.Stream;
11
12 import static org.assertj.core.api.Assertions.assertThat;
13 import static org.awaitility.Awaitility.await;
14
15
16 public class TestData
17 {
18         static final String PETER = "peter";
19         static final String KLAUS = "klaus";
20
21
22         static final KeyValue<String, TestRecording>[] INPUT_MESSAGES = new KeyValue[]
23         {
24                         new KeyValue<>(
25                                         PETER,
26                                         TestRecording.of(PETER, "Hallo Welt!")),
27                         new KeyValue<>(
28                                         KLAUS,
29                                         TestRecording.of(KLAUS, "Müsch gäb's auch!")),
30                         new KeyValue<>(
31                                         PETER,
32                                         TestRecording.of(PETER, "Boäh, echt! ß mal nä Nümmäh!")),
33         };
34
35         static void assertExpectedMessages(MultiValueMap<String, TestWord> receivedMessages)
36         {
37                 await("Received expected messages")
38                                 .atMost(Duration.ofSeconds(5))
39                                 .untilAsserted(() -> expectedMessages().forEach((user, word) ->
40                                                 assertThat(receivedMessages.get(user)).containsExactlyElementsOf(word)));
41         }
42
43         static final KeyValue<String, TestWord>[] EXPECTED_MESSAGES = new KeyValue[]
44         {
45                         KeyValue.pair(
46                                         "peter",
47                                         TestWord.of("peter", "Hallo")),
48                         KeyValue.pair(
49                                         "peter",
50                                         TestWord.of("peter", "Welt")),
51                         KeyValue.pair(
52                                         "klaus",
53                                         TestWord.of("klaus", "Müsch")),
54                         KeyValue.pair(
55                                         "klaus",
56                                         TestWord.of("klaus", "gäb")),
57                         KeyValue.pair(
58                                         "klaus",
59                                         TestWord.of("klaus", "s")),
60                         KeyValue.pair(
61                                         "klaus",
62                                         TestWord.of("klaus", "auch")),
63                         KeyValue.pair(
64                                         "peter",
65                                         TestWord.of("peter", "Boäh")),
66                         KeyValue.pair(
67                                         "peter",
68                                         TestWord.of("peter", "echt")),
69                         KeyValue.pair(
70                                         "peter",
71                                         TestWord.of("peter", "ß")),
72                         KeyValue.pair(
73                                         "peter",
74                                         TestWord.of("peter", "mal")),
75                         KeyValue.pair(
76                                         "peter",
77                                         TestWord.of("peter", "nä")),
78                         KeyValue.pair(
79                                         "peter",
80                                         TestWord.of("peter", "Nümmäh")),
81         };
82
83         static MultiValueMap<String, TestWord> expectedMessages()
84         {
85                 MultiValueMap<String, TestWord> expectedMessages = new LinkedMultiValueMap<>();
86                 Stream
87                                 .of(EXPECTED_MESSAGES)
88                                 .forEach(keyValue -> expectedMessages.add(keyValue.key, keyValue.value));
89                 return expectedMessages;
90         }
91 }