X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fwordcount%2Fcounter%2FTestData.java;h=b5c6c46fcdda396cba69e4e2c20aaf4c4427e434;hb=237133719b1d06d542302fb948d9cf3aff80a8a4;hp=6419059f8e9d76546ab63eac1fe08ef9900731d5;hpb=3c23bfd42005211ac9812fba698ab74c8a6b7aa0;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 6419059..b5c6c46 100644 --- a/src/test/java/de/juplo/kafka/wordcount/counter/TestData.java +++ b/src/test/java/de/juplo/kafka/wordcount/counter/TestData.java @@ -3,12 +3,11 @@ package de.juplo.kafka.wordcount.counter; 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.common.header.Header; -import org.apache.kafka.common.header.Headers; import org.apache.kafka.streams.KeyValue; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import java.util.function.Consumer; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -16,26 +15,68 @@ import static org.assertj.core.api.Assertions.assertThat; class TestData { - private static final TestInputWord[] INPUT_MESSAGES = new TestInputWord[] + static final String PETER = "peter"; + static final String KLAUS = "klaus"; + + 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(PETER, WORD_HALLO); + static final TestOutputWord PETER_WELT = TestOutputWord.of(PETER, WORD_WELT); + static final TestOutputWord PETER_BOÄH = TestOutputWord.of(PETER, WORD_BOÄH); + static final TestOutputWord KLAUS_MÜSCH = TestOutputWord.of(KLAUS, WORD_MÜSCH); + static final TestOutputWord KLAUS_S = TestOutputWord.of(KLAUS, WORD_S); + + private static final KeyValue[] INPUT_MESSAGES = new KeyValue[] { - TestInputWord.of("peter","Hallo"), - TestInputWord.of("klaus","Müsch"), - TestInputWord.of("peter","Welt"), - TestInputWord.of("klaus","Müsch"), - TestInputWord.of("klaus","s"), - TestInputWord.of("peter","Boäh"), - TestInputWord.of("peter","Welt"), - TestInputWord.of("peter","Boäh"), - TestInputWord.of("klaus","s"), - TestInputWord.of("peter","Boäh"), - TestInputWord.of("klaus","s"), + new KeyValue<>( + PETER, + TestInputWord.of(PETER, WORD_HALLO)), + new KeyValue<>( + KLAUS, + TestInputWord.of(KLAUS, WORD_MÜSCH)), + new KeyValue<>( + PETER, + TestInputWord.of(PETER, WORD_WELT)), + new KeyValue<>( + KLAUS, + TestInputWord.of(KLAUS, WORD_MÜSCH)), + new KeyValue<>( + KLAUS, + TestInputWord.of(KLAUS, WORD_S)), + new KeyValue<>( + PETER, + TestInputWord.of(PETER, WORD_BOÄH)), + new KeyValue<>( + PETER, + TestInputWord.of(PETER, WORD_WELT)), + new KeyValue<>( + PETER, + TestInputWord.of(PETER, WORD_BOÄH)), + new KeyValue<>( + KLAUS, + TestInputWord.of(KLAUS, WORD_S)), + new KeyValue<>( + PETER, + TestInputWord.of(PETER, WORD_BOÄH)), + new KeyValue<>( + KLAUS, + TestInputWord.of(KLAUS, WORD_S)), }; - static Stream getInputMessages() + static Stream> getInputMessages() { return Stream.of(TestData.INPUT_MESSAGES); } + static Consumer> expectedMessagesAssertion() + { + return receivedMessages -> assertExpectedMessages(receivedMessages); + } + static void assertExpectedMessages(MultiValueMap receivedMessages) { expectedMessages().forEach( @@ -44,41 +85,96 @@ class TestData .containsExactlyElementsOf(counter)); } + static Consumer> expectedNumberOfMessagesForWordAssertion() + { + return receivedMessages -> assertExpectedNumberOfMessagesForWord(receivedMessages); + } + + 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).size(); + } + + static Consumer> expectedLastMessagesForWordAssertion() + { + return receivedMessages -> assertExpectedLastMessagesForWord(receivedMessages); + } + + 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, + 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() @@ -89,17 +185,4 @@ class TestData .forEach(keyValue -> expectedMessages.add(keyValue.key, keyValue.value)); return expectedMessages; } - - static String parseHeader(Headers headers, String key) - { - Header header = headers.lastHeader(key); - if (header == null) - { - return key + "=null"; - } - else - { - return key + "=" + new String(header.value()); - } - } }