counter: 1.2.15 - Separated serialization-config into a static method
[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.common.header.Header;
7 import org.apache.kafka.common.header.Headers;
8 import org.apache.kafka.streams.KeyValue;
9 import org.springframework.util.LinkedMultiValueMap;
10 import org.springframework.util.MultiValueMap;
11
12 import java.util.stream.Stream;
13
14 import static org.assertj.core.api.Assertions.assertThat;
15
16
17 class TestData
18 {
19         private static final TestInputWord[] INPUT_MESSAGES = new TestInputWord[]
20         {
21                         TestInputWord.of("peter","Hallo"),
22                         TestInputWord.of("klaus","Müsch"),
23                         TestInputWord.of("peter","Welt"),
24                         TestInputWord.of("klaus","Müsch"),
25                         TestInputWord.of("klaus","s"),
26                         TestInputWord.of("peter","Boäh"),
27                         TestInputWord.of("peter","Welt"),
28                         TestInputWord.of("peter","Boäh"),
29                         TestInputWord.of("klaus","s"),
30                         TestInputWord.of("peter","Boäh"),
31                         TestInputWord.of("klaus","s"),
32         };
33
34         static Stream<TestInputWord> getInputMessages()
35         {
36                 return Stream.of(TestData.INPUT_MESSAGES);
37         }
38
39         static void assertExpectedMessages(MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages)
40         {
41                 expectedMessages().forEach(
42                                 (word, counter) ->
43                                                 assertThat(receivedMessages.get(word))
44                                                                 .containsExactlyElementsOf(counter));
45         }
46
47         private static final KeyValue<TestOutputWord, TestOutputWordCounter>[] EXPECTED_MESSAGES = new KeyValue[]
48         {
49                         KeyValue.pair(
50                                         TestOutputWord.of("peter","Hallo"),
51                                         TestOutputWordCounter.of("peter","Hallo",1)),
52                         KeyValue.pair(
53                                         TestOutputWord.of("klaus","Müsch"),
54                                         TestOutputWordCounter.of("klaus","Müsch",1)),
55                         KeyValue.pair(
56                                         TestOutputWord.of("peter","Welt"),
57                                         TestOutputWordCounter.of("peter","Welt",1)),
58                         KeyValue.pair(
59                                         TestOutputWord.of("klaus","Müsch"),
60                                         TestOutputWordCounter.of("klaus","Müsch",2)),
61                         KeyValue.pair(
62                                         TestOutputWord.of("klaus","s"),
63                                         TestOutputWordCounter.of("klaus","s",1)),
64                         KeyValue.pair(
65                                         TestOutputWord.of("peter","Boäh"),
66                                         TestOutputWordCounter.of("peter","Boäh",1)),
67                         KeyValue.pair(
68                                         TestOutputWord.of("peter","Welt"),
69                                         TestOutputWordCounter.of("peter","Welt",2)),
70                         KeyValue.pair(
71                                         TestOutputWord.of("peter","Boäh"),
72                                         TestOutputWordCounter.of("peter","Boäh",2)),
73                         KeyValue.pair(
74                                         TestOutputWord.of("klaus","s"),
75                                         TestOutputWordCounter.of("klaus","s",2)),
76                         KeyValue.pair(
77                                         TestOutputWord.of("peter","Boäh"),
78                                         TestOutputWordCounter.of("peter","Boäh",3)),
79                         KeyValue.pair(
80                                         TestOutputWord.of("klaus","s"),
81                                         TestOutputWordCounter.of("klaus","s",3)),
82         };
83
84         static MultiValueMap<TestOutputWord, TestOutputWordCounter> expectedMessages()
85         {
86                 MultiValueMap<TestOutputWord, TestOutputWordCounter> expectedMessages = new LinkedMultiValueMap<>();
87                 Stream
88                                 .of(EXPECTED_MESSAGES)
89                                 .forEach(keyValue -> expectedMessages.add(keyValue.key, keyValue.value));
90                 return expectedMessages;
91         }
92
93         static String parseHeader(Headers headers, String key)
94         {
95                 Header header = headers.lastHeader(key);
96                 if (header == null)
97                 {
98                         return key + "=null";
99                 }
100                 else
101                 {
102                         return key + "=" + new String(header.value());
103                 }
104         }
105 }