counter: 1.2.15 - Refined `TestData` (explicit key in input-data)
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / counter / TestData.java
1 package de.juplo.kafka.wordcount.counter;
2
3 import de.juplo.kafka.wordcount.splitter.TestInputWord;
4 import de.juplo.kafka.wordcount.top10.TestOutputWord;
5 import de.juplo.kafka.wordcount.top10.TestOutputWordCounter;
6 import org.apache.kafka.streams.KeyValue;
7 import org.springframework.util.LinkedMultiValueMap;
8 import org.springframework.util.MultiValueMap;
9
10 import java.util.stream.Stream;
11
12 import static org.assertj.core.api.Assertions.assertThat;
13
14
15 class TestData
16 {
17         static final String PETER = "peter";
18         static final String KLAUS = "klaus";
19
20         private static final KeyValue<String, TestInputWord>[] INPUT_MESSAGES = new KeyValue[]
21         {
22                         new KeyValue<>(
23                                         PETER,
24                                         TestInputWord.of(PETER, "Hallo")),
25                         new KeyValue<>(
26                                         KLAUS,
27                                         TestInputWord.of(KLAUS, "Müsch")),
28                         new KeyValue<>(
29                                         PETER,
30                                         TestInputWord.of(PETER, "Welt")),
31                         new KeyValue<>(
32                                         KLAUS,
33                                         TestInputWord.of(KLAUS, "Müsch")),
34                         new KeyValue<>(
35                                         KLAUS,
36                                         TestInputWord.of(KLAUS, "s")),
37                         new KeyValue<>(
38                                         PETER,
39                                         TestInputWord.of(PETER, "Boäh")),
40                         new KeyValue<>(
41                                         PETER,
42                                         TestInputWord.of(PETER, "Welt")),
43                         new KeyValue<>(
44                                         PETER,
45                                         TestInputWord.of(PETER, "Boäh")),
46                         new KeyValue<>(
47                                         KLAUS,
48                                         TestInputWord.of(KLAUS, "s")),
49                         new KeyValue<>(
50                                         PETER,
51                                         TestInputWord.of(PETER, "Boäh")),
52                         new KeyValue<>(
53                                         KLAUS,
54                                         TestInputWord.of(KLAUS, "s")),
55         };
56
57         static Stream<KeyValue<String, TestInputWord>> getInputMessages()
58         {
59                 return Stream.of(TestData.INPUT_MESSAGES);
60         }
61
62         static void assertExpectedMessages(MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages)
63         {
64                 expectedMessages().forEach(
65                                 (word, counter) ->
66                                                 assertThat(receivedMessages.get(word))
67                                                                 .containsExactlyElementsOf(counter));
68         }
69
70         private static final KeyValue<TestOutputWord, TestOutputWordCounter>[] EXPECTED_MESSAGES = new KeyValue[]
71         {
72                         KeyValue.pair(
73                                         TestOutputWord.of(PETER, "Hallo"),
74                                         TestOutputWordCounter.of(PETER, "Hallo",1)),
75                         KeyValue.pair(
76                                         TestOutputWord.of(KLAUS, "Müsch"),
77                                         TestOutputWordCounter.of(KLAUS, "Müsch",1)),
78                         KeyValue.pair(
79                                         TestOutputWord.of(PETER, "Welt"),
80                                         TestOutputWordCounter.of(PETER, "Welt",1)),
81                         KeyValue.pair(
82                                         TestOutputWord.of(KLAUS, "Müsch"),
83                                         TestOutputWordCounter.of(KLAUS, "Müsch",2)),
84                         KeyValue.pair(
85                                         TestOutputWord.of(KLAUS, "s"),
86                                         TestOutputWordCounter.of(KLAUS, "s",1)),
87                         KeyValue.pair(
88                                         TestOutputWord.of(PETER, "Boäh"),
89                                         TestOutputWordCounter.of(PETER, "Boäh",1)),
90                         KeyValue.pair(
91                                         TestOutputWord.of(PETER, "Welt"),
92                                         TestOutputWordCounter.of(PETER, "Welt",2)),
93                         KeyValue.pair(
94                                         TestOutputWord.of(PETER, "Boäh"),
95                                         TestOutputWordCounter.of(PETER, "Boäh",2)),
96                         KeyValue.pair(
97                                         TestOutputWord.of(KLAUS, "s"),
98                                         TestOutputWordCounter.of(KLAUS, "s",2)),
99                         KeyValue.pair(
100                                         TestOutputWord.of(PETER, "Boäh"),
101                                         TestOutputWordCounter.of(PETER, "Boäh",3)),
102                         KeyValue.pair(
103                                         TestOutputWord.of(KLAUS, "s"),
104                                         TestOutputWordCounter.of(KLAUS, "s",3)),
105         };
106
107         static MultiValueMap<TestOutputWord, TestOutputWordCounter> expectedMessages()
108         {
109                 MultiValueMap<TestOutputWord, TestOutputWordCounter> expectedMessages = new LinkedMultiValueMap<>();
110                 Stream
111                                 .of(EXPECTED_MESSAGES)
112                                 .forEach(keyValue -> expectedMessages.add(keyValue.key, keyValue.value));
113                 return expectedMessages;
114         }
115 }