X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fwordcount%2Fcounter%2FTestData.java;h=862eb2bc67463437168bc94db062c62d8d3cdb5f;hb=refs%2Fheads%2Fcounter;hp=777537ba4d3411c9efc366aa5a569eb6ddc969c9;hpb=44f1ad5dcd50851ef5d93b1be759481d5a38f63a;p=demos%2Fkafka%2Fwordcount diff --git a/src/test/java/de/juplo/kafka/wordcount/counter/TestData.java b/src/test/java/de/juplo/kafka/wordcount/counter/TestData.java index 777537b..9b38dbc 100644 --- a/src/test/java/de/juplo/kafka/wordcount/counter/TestData.java +++ b/src/test/java/de/juplo/kafka/wordcount/counter/TestData.java @@ -1,14 +1,17 @@ package de.juplo.kafka.wordcount.counter; +import de.juplo.kafka.wordcount.splitter.TestInputUser; import de.juplo.kafka.wordcount.splitter.TestInputWord; import de.juplo.kafka.wordcount.top10.TestOutputWord; import de.juplo.kafka.wordcount.top10.TestOutputWordCounter; import org.apache.kafka.streams.KeyValue; +import org.apache.kafka.streams.state.ReadOnlyKeyValueStore; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import java.util.stream.Stream; +import static de.juplo.kafka.wordcount.counter.CounterStreamProcessor.TYPE; import static org.assertj.core.api.Assertions.assertThat; @@ -17,44 +20,56 @@ class TestData static final String PETER = "peter"; static final String KLAUS = "klaus"; - private static final KeyValue[] INPUT_MESSAGES = new KeyValue[] + static final String WORD_HALLO = "Hallo"; + static final String WORD_MÜSCH = "Müsch"; + static final String WORD_WELT = "Welt"; + static final String WORD_S = "s"; + static final String WORD_BOÄH = "Boäh"; + + static final TestOutputWord PETER_HALLO = TestOutputWord.of(TYPE, PETER, WORD_HALLO); + static final TestOutputWord PETER_WELT = TestOutputWord.of(TYPE, PETER, WORD_WELT); + static final TestOutputWord PETER_BOÄH = TestOutputWord.of(TYPE, PETER, WORD_BOÄH); + static final TestOutputWord KLAUS_MÜSCH = TestOutputWord.of(TYPE, KLAUS, WORD_MÜSCH); + static final TestOutputWord KLAUS_S = TestOutputWord.of(TYPE, KLAUS, WORD_S); + + private static final KeyValue[] INPUT_MESSAGES = new KeyValue[] { - new KeyValue<>( - PETER, - TestInputWord.of(PETER, "Hallo")), - new KeyValue<>( - KLAUS, - TestInputWord.of(KLAUS, "Müsch")), - new KeyValue<>( - PETER, - TestInputWord.of(PETER, "Welt")), - new KeyValue<>( - KLAUS, - TestInputWord.of(KLAUS, "Müsch")), - new KeyValue<>( - KLAUS, - TestInputWord.of(KLAUS, "s")), - new KeyValue<>( - PETER, - TestInputWord.of(PETER, "Boäh")), - new KeyValue<>( - PETER, - TestInputWord.of(PETER, "Welt")), - new KeyValue<>( - PETER, - TestInputWord.of(PETER, "Boäh")), - new KeyValue<>( - KLAUS, - TestInputWord.of(KLAUS, "s")), - new KeyValue<>( - PETER, - TestInputWord.of(PETER, "Boäh")), - new KeyValue<>( - KLAUS, - TestInputWord.of(KLAUS, "s")), + KeyValue.pair( + TestInputUser.of(PETER), + TestInputWord.of(PETER, WORD_HALLO)), + KeyValue.pair( + TestInputUser.of(KLAUS), + TestInputWord.of(KLAUS, WORD_MÜSCH)), + KeyValue.pair( + TestInputUser.of(PETER), + TestInputWord.of(PETER, WORD_WELT)), + KeyValue.pair( + TestInputUser.of(KLAUS), + TestInputWord.of(KLAUS, WORD_MÜSCH)), + KeyValue.pair( + TestInputUser.of(KLAUS), + TestInputWord.of(KLAUS, WORD_S)), + KeyValue.pair( + TestInputUser.of(PETER), + TestInputWord.of(PETER, WORD_BOÄH)), + KeyValue.pair( + TestInputUser.of(PETER), + TestInputWord.of(PETER, WORD_WELT)), + KeyValue.pair( + TestInputUser.of(PETER), + TestInputWord.of(PETER, WORD_BOÄH)), + KeyValue.pair( + TestInputUser.of(KLAUS), + TestInputWord.of(KLAUS, WORD_S)), + KeyValue.pair( + TestInputUser.of(PETER), + TestInputWord.of(PETER, WORD_BOÄH)), + KeyValue.pair( + TestInputUser.of(KLAUS), + TestInputWord.of(KLAUS, WORD_S)), }; - static Stream> getInputMessages() + static Stream> getInputMessages() { return Stream.of(TestData.INPUT_MESSAGES); } @@ -67,41 +82,115 @@ class TestData .containsExactlyElementsOf(counter)); } + static void assertExpectedNumberOfMessagesForWord(MultiValueMap receivedMessages) + { + assertThat(countMessagesForWord(PETER_HALLO, receivedMessages)); + assertThat(countMessagesForWord(PETER_WELT, receivedMessages)); + assertThat(countMessagesForWord(PETER_BOÄH, receivedMessages)); + assertThat(countMessagesForWord(KLAUS_MÜSCH, receivedMessages)); + assertThat(countMessagesForWord(KLAUS_S, receivedMessages)); + } + + private static int countMessagesForWord(TestOutputWord word, MultiValueMap messagesForUsers) + { + return messagesForUsers.get(word) == null + ? 0 + : messagesForUsers.get(word).size(); + } + + static void assertExpectedState(ReadOnlyKeyValueStore store) + { + assertWordCountEqualsWordCountFromLastMessage(PETER_HALLO, store.get(wordOf(PETER_HALLO))); + assertWordCountEqualsWordCountFromLastMessage(PETER_WELT, store.get(wordOf(PETER_WELT))); + assertWordCountEqualsWordCountFromLastMessage(PETER_BOÄH, store.get(wordOf(PETER_BOÄH))); + assertWordCountEqualsWordCountFromLastMessage(KLAUS_MÜSCH, store.get(wordOf(KLAUS_MÜSCH))); + assertWordCountEqualsWordCountFromLastMessage(KLAUS_S, store.get(wordOf(KLAUS_S))); + } + + private static Word wordOf(TestOutputWord testOutputWord) + { + return Word.of( + testOutputWord.getChannel(), + testOutputWord.getKey()); + } + + static void assertExpectedLastMessagesForWord(MultiValueMap receivedMessages) + { + assertWordCountEqualsWordCountFromLastMessage(PETER_HALLO, getLastMessageFor(PETER_HALLO, receivedMessages)); + assertWordCountEqualsWordCountFromLastMessage(PETER_WELT, getLastMessageFor(PETER_WELT, receivedMessages)); + assertWordCountEqualsWordCountFromLastMessage(PETER_BOÄH, getLastMessageFor(PETER_BOÄH, receivedMessages)); + assertWordCountEqualsWordCountFromLastMessage(KLAUS_MÜSCH, getLastMessageFor(KLAUS_MÜSCH, receivedMessages)); + assertWordCountEqualsWordCountFromLastMessage(KLAUS_S, getLastMessageFor(KLAUS_S, receivedMessages)); + } + + private static void assertWordCountEqualsWordCountFromLastMessage( + TestOutputWord word, + Long counter) + { + TestOutputWordCounter testOutputWordCounter = TestOutputWordCounter.of( + word.getChannel(), + word.getKey(), + counter); + assertWordCountEqualsWordCountFromLastMessage(word, testOutputWordCounter); + } + + private static void assertWordCountEqualsWordCountFromLastMessage( + TestOutputWord word, + TestOutputWordCounter counter) + { + assertThat(counter).isEqualTo(getLastMessageFor(word)); + } + + private static TestOutputWordCounter getLastMessageFor(TestOutputWord word) + { + return getLastMessageFor(word, expectedMessages()); + } + + private static TestOutputWordCounter getLastMessageFor( + TestOutputWord user, + MultiValueMap messagesForWord) + { + return messagesForWord + .get(user) + .stream() + .reduce(null, (left, right) -> right); + } + private static final KeyValue[] EXPECTED_MESSAGES = new KeyValue[] { KeyValue.pair( - TestOutputWord.of(PETER, "Hallo"), - TestOutputWordCounter.of(PETER, "Hallo",1)), + PETER_HALLO, + TestOutputWordCounter.of(PETER, WORD_HALLO,1)), KeyValue.pair( - TestOutputWord.of(KLAUS, "Müsch"), - TestOutputWordCounter.of(KLAUS, "Müsch",1)), + KLAUS_MÜSCH, + TestOutputWordCounter.of(KLAUS, WORD_MÜSCH,1)), KeyValue.pair( - TestOutputWord.of(PETER, "Welt"), - TestOutputWordCounter.of(PETER, "Welt",1)), + PETER_WELT, + TestOutputWordCounter.of(PETER, WORD_WELT,1)), KeyValue.pair( - TestOutputWord.of(KLAUS, "Müsch"), - TestOutputWordCounter.of(KLAUS, "Müsch",2)), + KLAUS_MÜSCH, + TestOutputWordCounter.of(KLAUS, WORD_MÜSCH,2)), KeyValue.pair( - TestOutputWord.of(KLAUS, "s"), - TestOutputWordCounter.of(KLAUS, "s",1)), + KLAUS_S, + TestOutputWordCounter.of(KLAUS, WORD_S,1)), KeyValue.pair( - TestOutputWord.of(PETER, "Boäh"), - TestOutputWordCounter.of(PETER, "Boäh",1)), + PETER_BOÄH, + TestOutputWordCounter.of(PETER, WORD_BOÄH,1)), KeyValue.pair( - TestOutputWord.of(PETER, "Welt"), - TestOutputWordCounter.of(PETER, "Welt",2)), + PETER_WELT, + TestOutputWordCounter.of(PETER, WORD_WELT,2)), KeyValue.pair( - TestOutputWord.of(PETER, "Boäh"), - TestOutputWordCounter.of(PETER, "Boäh",2)), + PETER_BOÄH, + TestOutputWordCounter.of(PETER, WORD_BOÄH,2)), KeyValue.pair( - TestOutputWord.of(KLAUS, "s"), - TestOutputWordCounter.of(KLAUS, "s",2)), + KLAUS_S, + TestOutputWordCounter.of(KLAUS, WORD_S,2)), KeyValue.pair( - TestOutputWord.of(PETER, "Boäh"), - TestOutputWordCounter.of(PETER, "Boäh",3)), + PETER_BOÄH, + TestOutputWordCounter.of(PETER, WORD_BOÄH,3)), KeyValue.pair( - TestOutputWord.of(KLAUS, "s"), - TestOutputWordCounter.of(KLAUS, "s",3)), + KLAUS_S, + TestOutputWordCounter.of(KLAUS, WORD_S,3)), }; static MultiValueMap expectedMessages()