1 package de.juplo.kafka.wordcount.counter;
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;
10 import java.util.function.Consumer;
11 import java.util.stream.Stream;
13 import static org.assertj.core.api.Assertions.assertThat;
18 static final String PETER = "peter";
19 static final String KLAUS = "klaus";
21 static final String WORD_HALLO = "Hallo";
22 static final String WORD_MÜSCH = "Müsch";
23 static final String WORD_WELT = "Welt";
24 static final String WORD_S = "s";
25 static final String WORD_BOÄH = "Boäh";
27 static final TestOutputWord PETER_HALLO = TestOutputWord.of(PETER, WORD_HALLO);
28 static final TestOutputWord PETER_WELT = TestOutputWord.of(PETER, WORD_WELT);
29 static final TestOutputWord PETER_BOÄH = TestOutputWord.of(PETER, WORD_BOÄH);
30 static final TestOutputWord KLAUS_MÜSCH = TestOutputWord.of(KLAUS, WORD_MÜSCH);
31 static final TestOutputWord KLAUS_S = TestOutputWord.of(KLAUS, WORD_S);
33 private static final KeyValue<String, TestInputWord>[] INPUT_MESSAGES = new KeyValue[]
37 TestInputWord.of(PETER, WORD_HALLO)),
40 TestInputWord.of(KLAUS, WORD_MÜSCH)),
43 TestInputWord.of(PETER, WORD_WELT)),
46 TestInputWord.of(KLAUS, WORD_MÜSCH)),
49 TestInputWord.of(KLAUS, WORD_S)),
52 TestInputWord.of(PETER, WORD_BOÄH)),
55 TestInputWord.of(PETER, WORD_WELT)),
58 TestInputWord.of(PETER, WORD_BOÄH)),
61 TestInputWord.of(KLAUS, WORD_S)),
64 TestInputWord.of(PETER, WORD_BOÄH)),
67 TestInputWord.of(KLAUS, WORD_S)),
70 static Stream<KeyValue<String, TestInputWord>> getInputMessages()
72 return Stream.of(TestData.INPUT_MESSAGES);
75 static Consumer<MultiValueMap<TestOutputWord, TestOutputWordCounter>> expectedMessagesAssertion()
77 return receivedMessages -> assertExpectedMessages(receivedMessages);
80 static void assertExpectedMessages(MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages)
82 expectedMessages().forEach(
84 assertThat(receivedMessages.get(word))
85 .containsExactlyElementsOf(counter));
88 static Consumer<MultiValueMap<TestOutputWord, TestOutputWordCounter>> expectedNumberOfMessagesForWordAssertion()
90 return receivedMessages -> assertExpectedNumberOfMessagesForWord(receivedMessages);
93 static void assertExpectedNumberOfMessagesForWord(MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages)
95 assertThat(countMessagesForWord(PETER_HALLO, receivedMessages));
96 assertThat(countMessagesForWord(PETER_WELT, receivedMessages));
97 assertThat(countMessagesForWord(PETER_BOÄH, receivedMessages));
98 assertThat(countMessagesForWord(KLAUS_MÜSCH, receivedMessages));
99 assertThat(countMessagesForWord(KLAUS_S, receivedMessages));
102 private static int countMessagesForWord(TestOutputWord word, MultiValueMap<TestOutputWord, TestOutputWordCounter> messagesForUsers)
104 return messagesForUsers.get(word).size();
107 static Consumer<MultiValueMap<TestOutputWord, TestOutputWordCounter>> expectedLastMessagesForWordAssertion()
109 return receivedMessages -> assertExpectedLastMessagesForWord(receivedMessages);
112 static void assertExpectedLastMessagesForWord(MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages)
114 assertWordCountEqualsWordCountFromLastMessage(PETER_HALLO, getLastMessageFor(PETER_HALLO, receivedMessages));
115 assertWordCountEqualsWordCountFromLastMessage(PETER_WELT, getLastMessageFor(PETER_WELT, receivedMessages));
116 assertWordCountEqualsWordCountFromLastMessage(PETER_BOÄH, getLastMessageFor(PETER_BOÄH, receivedMessages));
117 assertWordCountEqualsWordCountFromLastMessage(KLAUS_MÜSCH, getLastMessageFor(KLAUS_MÜSCH, receivedMessages));
118 assertWordCountEqualsWordCountFromLastMessage(KLAUS_S, getLastMessageFor(KLAUS_S, receivedMessages));
121 private static void assertWordCountEqualsWordCountFromLastMessage(
123 TestOutputWordCounter counter)
125 assertThat(counter).isEqualTo(getLastMessageFor(word));
128 private static TestOutputWordCounter getLastMessageFor(TestOutputWord word)
130 return getLastMessageFor(word, expectedMessages());
133 private static TestOutputWordCounter getLastMessageFor(
135 MultiValueMap<TestOutputWord, TestOutputWordCounter> messagesForWord)
137 return messagesForWord
140 .reduce(null, (left, right) -> right);
143 private static final KeyValue<TestOutputWord, TestOutputWordCounter>[] EXPECTED_MESSAGES = new KeyValue[]
147 TestOutputWordCounter.of(PETER, WORD_HALLO,1)),
150 TestOutputWordCounter.of(KLAUS, WORD_MÜSCH,1)),
153 TestOutputWordCounter.of(PETER, WORD_WELT,1)),
156 TestOutputWordCounter.of(KLAUS, WORD_MÜSCH,2)),
159 TestOutputWordCounter.of(KLAUS, WORD_S,1)),
162 TestOutputWordCounter.of(PETER, WORD_BOÄH,1)),
165 TestOutputWordCounter.of(PETER, WORD_WELT,2)),
168 TestOutputWordCounter.of(PETER, WORD_BOÄH,2)),
171 TestOutputWordCounter.of(KLAUS, WORD_S,2)),
174 TestOutputWordCounter.of(PETER, WORD_BOÄH,3)),
177 TestOutputWordCounter.of(KLAUS, WORD_S,3)),
180 static MultiValueMap<TestOutputWord, TestOutputWordCounter> expectedMessages()
182 MultiValueMap<TestOutputWord, TestOutputWordCounter> expectedMessages = new LinkedMultiValueMap<>();
184 .of(EXPECTED_MESSAGES)
185 .forEach(keyValue -> expectedMessages.add(keyValue.key, keyValue.value));
186 return expectedMessages;