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