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.streams.KeyValue;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.function.BiConsumer;
-import java.util.stream.Collectors;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
class TestData
{
- static void writeInputData(BiConsumer<String, Word> consumer)
+ 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<String, TestInputWord>[] INPUT_MESSAGES = new KeyValue[]
+ {
+ 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<KeyValue<String, TestInputWord>> getInputMessages()
+ {
+ return Stream.of(TestData.INPUT_MESSAGES);
+ }
+
+ static Consumer<MultiValueMap<TestOutputWord, TestOutputWordCounter>> expectedMessagesAssertion()
{
- consumer.accept(
- "peter",
- Word.of("peter","Hallo"));
- consumer.accept(
- "klaus",
- Word.of("klaus","Müsch"));
- consumer.accept(
- "peter",
- Word.of("peter","Welt"));
- consumer.accept(
- "klaus",
- Word.of("klaus","Müsch"));
- consumer.accept(
- "klaus",
- Word.of("klaus","s"));
- consumer.accept(
- "peter",
- Word.of("peter","Boäh"));
- consumer.accept(
- "peter",
- Word.of("peter","Welt"));
- consumer.accept(
- "peter",
- Word.of("peter","Boäh"));
- consumer.accept(
- "klaus",
- Word.of("klaus","s"));
- consumer.accept(
- "peter",
- Word.of("peter","Boäh"));
- consumer.accept(
- "klaus",
- Word.of("klaus","s"));
+ return receivedMessages -> assertExpectedMessages(receivedMessages);
}
- static void assertExpectedResult(List<KeyValue<Word, WordCounter>> receivedMessages)
+ static void assertExpectedMessages(MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages)
{
- assertThat(receivedMessages).hasSize(11);
- assertThat(receivedMessages).containsSubsequence(
- expectedMessages[0]); // Hallo
- assertThat(receivedMessages).containsSubsequence(
- expectedMessages[1],
- expectedMessages[3]); // Müsch
- assertThat(receivedMessages).containsSubsequence(
- expectedMessages[2],
- expectedMessages[6]);
- assertThat(receivedMessages).containsSubsequence(
- expectedMessages[4],
- expectedMessages[8],
- expectedMessages[10]); // s
- assertThat(receivedMessages).containsSubsequence(
- expectedMessages[5],
- expectedMessages[7],
- expectedMessages[9]); // Boäh
+ expectedMessages().forEach(
+ (word, counter) ->
+ assertThat(receivedMessages.get(word))
+ .containsExactlyElementsOf(counter));
}
- static KeyValue<Word, WordCounter>[] expectedMessages = new KeyValue[]
+ private static final KeyValue<TestOutputWord, TestOutputWordCounter>[] EXPECTED_MESSAGES = new KeyValue[]
{
KeyValue.pair(
- Word.of("peter","Hallo"),
- WordCounter.of("peter","Hallo",1)),
+ PETER_HALLO,
+ TestOutputWordCounter.of(PETER, WORD_HALLO,1)),
KeyValue.pair(
- Word.of("klaus","Müsch"),
- WordCounter.of("klaus","Müsch",1)),
+ KLAUS_MÜSCH,
+ TestOutputWordCounter.of(KLAUS, WORD_MÜSCH,1)),
KeyValue.pair(
- Word.of("peter","Welt"),
- WordCounter.of("peter","Welt",1)),
+ PETER_WELT,
+ TestOutputWordCounter.of(PETER, WORD_WELT,1)),
KeyValue.pair(
- Word.of("klaus","Müsch"),
- WordCounter.of("klaus","Müsch",2)),
+ KLAUS_MÜSCH,
+ TestOutputWordCounter.of(KLAUS, WORD_MÜSCH,2)),
KeyValue.pair(
- Word.of("klaus","s"),
- WordCounter.of("klaus","s",1)),
+ KLAUS_S,
+ TestOutputWordCounter.of(KLAUS, WORD_S,1)),
KeyValue.pair(
- Word.of("peter","Boäh"),
- WordCounter.of("peter","Boäh",1)),
+ PETER_BOÄH,
+ TestOutputWordCounter.of(PETER, WORD_BOÄH,1)),
KeyValue.pair(
- Word.of("peter","Welt"),
- WordCounter.of("peter","Welt",2)),
+ PETER_WELT,
+ TestOutputWordCounter.of(PETER, WORD_WELT,2)),
KeyValue.pair(
- Word.of("peter","Boäh"),
- WordCounter.of("peter","Boäh",2)),
+ PETER_BOÄH,
+ TestOutputWordCounter.of(PETER, WORD_BOÄH,2)),
KeyValue.pair(
- Word.of("klaus","s"),
- WordCounter.of("klaus","s",2)),
+ KLAUS_S,
+ TestOutputWordCounter.of(KLAUS, WORD_S,2)),
KeyValue.pair(
- Word.of("peter","Boäh"),
- WordCounter.of("peter","Boäh",3)),
+ PETER_BOÄH,
+ TestOutputWordCounter.of(PETER, WORD_BOÄH,3)),
KeyValue.pair(
- Word.of("klaus","s"),
- WordCounter.of("klaus","s",3)),
+ KLAUS_S,
+ TestOutputWordCounter.of(KLAUS, WORD_S,3)),
};
- static Map<String, Object> convertToMap(Properties properties)
+ static MultiValueMap<TestOutputWord, TestOutputWordCounter> expectedMessages()
{
- return properties
- .entrySet()
- .stream()
- .collect(
- Collectors.toMap(
- entry -> (String)entry.getKey(),
- entry -> entry.getValue()
- ));
+ MultiValueMap<TestOutputWord, TestOutputWordCounter> expectedMessages = new LinkedMultiValueMap<>();
+ Stream
+ .of(EXPECTED_MESSAGES)
+ .forEach(keyValue -> expectedMessages.add(keyValue.key, keyValue.value));
+ return expectedMessages;
}
}