1 package de.juplo.kafka.wordcount.popular;
3 import de.juplo.kafka.wordcount.splitter.InputUser;
4 import de.juplo.kafka.wordcount.splitter.InputWord;
5 import de.juplo.kafka.wordcount.stats.OutputWindowedWord;
6 import de.juplo.kafka.wordcount.stats.OutputWordCounter;
7 import org.apache.kafka.streams.KeyValue;
8 import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
9 import org.springframework.util.LinkedMultiValueMap;
10 import org.springframework.util.MultiValueMap;
12 import java.util.stream.Stream;
14 import static org.assertj.core.api.Assertions.assertThat;
19 static final String PETER = "peter";
20 static final String KLAUS = "klaus";
22 static final String WORD_HALLO = "Hallo";
23 static final String WORD_MÜSCH = "Müsch";
24 static final String WORD_WELT = "Welt";
25 static final String WORD_S = "s";
26 static final String WORD_BOÄH = "Boäh";
28 static final OutputWindowedWord PETER_HALLO = OutputWindowedWord.of(PETER, WORD_HALLO);
29 static final OutputWindowedWord PETER_WELT = OutputWindowedWord.of(PETER, WORD_WELT);
30 static final OutputWindowedWord PETER_BOÄH = OutputWindowedWord.of(PETER, WORD_BOÄH);
31 static final OutputWindowedWord KLAUS_MÜSCH = OutputWindowedWord.of(KLAUS, WORD_MÜSCH);
32 static final OutputWindowedWord KLAUS_S = OutputWindowedWord.of(KLAUS, WORD_S);
34 private static final KeyValue<InputUser, InputWord>[] INPUT_MESSAGES = new KeyValue[]
38 InputWord.of(PETER, WORD_HALLO)),
41 InputWord.of(KLAUS, WORD_MÜSCH)),
44 InputWord.of(PETER, WORD_WELT)),
47 InputWord.of(KLAUS, WORD_MÜSCH)),
50 InputWord.of(KLAUS, WORD_S)),
53 InputWord.of(PETER, WORD_BOÄH)),
56 InputWord.of(PETER, WORD_WELT)),
59 InputWord.of(PETER, WORD_BOÄH)),
62 InputWord.of(KLAUS, WORD_S)),
65 InputWord.of(PETER, WORD_BOÄH)),
68 InputWord.of(KLAUS, WORD_S)),
71 static Stream<KeyValue<InputUser, InputWord>> getInputMessages()
73 return Stream.of(TestData.INPUT_MESSAGES);
76 static void assertExpectedMessages(MultiValueMap<OutputWindowedWord, OutputWordCounter> receivedMessages)
78 expectedMessages().forEach(
80 assertThat(receivedMessages.get(word))
81 .containsExactlyElementsOf(counter));
84 static void assertExpectedNumberOfMessagesForWord(MultiValueMap<OutputWindowedWord, OutputWordCounter> receivedMessages)
86 assertThat(countMessagesForWord(PETER_HALLO, receivedMessages));
87 assertThat(countMessagesForWord(PETER_WELT, receivedMessages));
88 assertThat(countMessagesForWord(PETER_BOÄH, receivedMessages));
89 assertThat(countMessagesForWord(KLAUS_MÜSCH, receivedMessages));
90 assertThat(countMessagesForWord(KLAUS_S, receivedMessages));
93 private static int countMessagesForWord(OutputWindowedWord word, MultiValueMap<OutputWindowedWord, OutputWordCounter> messagesForUsers)
95 return messagesForUsers.get(word) == null
97 : messagesForUsers.get(word).size();
100 static void assertExpectedState(ReadOnlyKeyValueStore<Word, Long> store)
102 assertWordCountEqualsWordCountFromLastMessage(PETER_HALLO, store.get(wordOf(PETER_HALLO)));
103 assertWordCountEqualsWordCountFromLastMessage(PETER_WELT, store.get(wordOf(PETER_WELT)));
104 assertWordCountEqualsWordCountFromLastMessage(PETER_BOÄH, store.get(wordOf(PETER_BOÄH)));
105 assertWordCountEqualsWordCountFromLastMessage(KLAUS_MÜSCH, store.get(wordOf(KLAUS_MÜSCH)));
106 assertWordCountEqualsWordCountFromLastMessage(KLAUS_S, store.get(wordOf(KLAUS_S)));
109 private static Word wordOf(OutputWindowedWord testOutputWindowedWord)
111 Word word = new Word();
113 word.setUser(testOutputWindowedWord.getUser());
114 word.setWord(testOutputWindowedWord.getWord());
119 static void assertExpectedLastMessagesForWord(MultiValueMap<OutputWindowedWord, OutputWordCounter> receivedMessages)
121 assertWordCountEqualsWordCountFromLastMessage(PETER_HALLO, getLastMessageFor(PETER_HALLO, receivedMessages));
122 assertWordCountEqualsWordCountFromLastMessage(PETER_WELT, getLastMessageFor(PETER_WELT, receivedMessages));
123 assertWordCountEqualsWordCountFromLastMessage(PETER_BOÄH, getLastMessageFor(PETER_BOÄH, receivedMessages));
124 assertWordCountEqualsWordCountFromLastMessage(KLAUS_MÜSCH, getLastMessageFor(KLAUS_MÜSCH, receivedMessages));
125 assertWordCountEqualsWordCountFromLastMessage(KLAUS_S, getLastMessageFor(KLAUS_S, receivedMessages));
128 private static void assertWordCountEqualsWordCountFromLastMessage(
129 OutputWindowedWord word,
132 OutputWordCounter testOutputWordCounter = OutputWordCounter.of(
136 assertWordCountEqualsWordCountFromLastMessage(word, testOutputWordCounter);
139 private static void assertWordCountEqualsWordCountFromLastMessage(
140 OutputWindowedWord word,
141 OutputWordCounter counter)
143 assertThat(counter).isEqualTo(getLastMessageFor(word));
146 private static OutputWordCounter getLastMessageFor(OutputWindowedWord word)
148 return getLastMessageFor(word, expectedMessages());
151 private static OutputWordCounter getLastMessageFor(
152 OutputWindowedWord user,
153 MultiValueMap<OutputWindowedWord, OutputWordCounter> messagesForWord)
155 return messagesForWord
158 .reduce(null, (left, right) -> right);
161 private static final KeyValue<OutputWindowedWord, OutputWordCounter>[] EXPECTED_MESSAGES = new KeyValue[]
165 OutputWordCounter.of(PETER, WORD_HALLO,1)),
168 OutputWordCounter.of(KLAUS, WORD_MÜSCH,1)),
171 OutputWordCounter.of(PETER, WORD_WELT,1)),
174 OutputWordCounter.of(KLAUS, WORD_MÜSCH,2)),
177 OutputWordCounter.of(KLAUS, WORD_S,1)),
180 OutputWordCounter.of(PETER, WORD_BOÄH,1)),
183 OutputWordCounter.of(PETER, WORD_WELT,2)),
186 OutputWordCounter.of(PETER, WORD_BOÄH,2)),
189 OutputWordCounter.of(KLAUS, WORD_S,2)),
192 OutputWordCounter.of(PETER, WORD_BOÄH,3)),
195 OutputWordCounter.of(KLAUS, WORD_S,3)),
198 static MultiValueMap<OutputWindowedWord, OutputWordCounter> expectedMessages()
200 MultiValueMap<OutputWindowedWord, OutputWordCounter> expectedMessages = new LinkedMultiValueMap<>();
202 .of(EXPECTED_MESSAGES)
203 .forEach(keyValue -> expectedMessages.add(keyValue.key, keyValue.value));
204 return expectedMessages;