counter: 1.2.15 - Refined `TestData` (explicit key in input-data)
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / counter / TestData.java
index ab265a9..777537b 100644 (file)
@@ -1,15 +1,12 @@
 package de.juplo.kafka.wordcount.counter;
 
-import org.apache.kafka.common.header.Header;
-import org.apache.kafka.common.header.Headers;
+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.Map;
-import java.util.Properties;
-import java.util.function.BiConsumer;
-import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -17,104 +14,102 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 class TestData
 {
-       static void injectInputMessages(BiConsumer<String, Word> consumer)
-       {
-               Stream
-                               .of(inputMessagesArray)
-                               .forEach(word -> consumer.accept(word.getUser(), word));
-       }
+       static final String PETER = "peter";
+       static final String KLAUS = "klaus";
 
-       static Word[] inputMessagesArray = new Word[]
+       private static final KeyValue<String, TestInputWord>[] INPUT_MESSAGES = new KeyValue[]
        {
-                       Word.of("peter","Hallo"),
-                       Word.of("klaus","Müsch"),
-                       Word.of("peter","Welt"),
-                       Word.of("klaus","Müsch"),
-                       Word.of("klaus","s"),
-                       Word.of("peter","Boäh"),
-                       Word.of("peter","Welt"),
-                       Word.of("peter","Boäh"),
-                       Word.of("klaus","s"),
-                       Word.of("peter","Boäh"),
-                       Word.of("klaus","s"),
+                       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")),
        };
 
-       static void assertExpectedMessages(MultiValueMap<Word, WordCounter> receivedMessages)
+       static Stream<KeyValue<String, TestInputWord>> getInputMessages()
+       {
+               return Stream.of(TestData.INPUT_MESSAGES);
+       }
+
+       static void assertExpectedMessages(MultiValueMap<TestOutputWord, TestOutputWordCounter> receivedMessages)
        {
-               expectedMessages.forEach(
+               expectedMessages().forEach(
                                (word, counter) ->
                                                assertThat(receivedMessages.get(word))
                                                                .containsExactlyElementsOf(counter));
        }
 
-       static KeyValue<Word, WordCounter>[] expectedMessagesArray = new KeyValue[]
+       private static final KeyValue<TestOutputWord, TestOutputWordCounter>[] EXPECTED_MESSAGES = new KeyValue[]
        {
                        KeyValue.pair(
-                                       Word.of("peter","Hallo"),
-                                       WordCounter.of("peter","Hallo",1)),
+                                       TestOutputWord.of(PETER, "Hallo"),
+                                       TestOutputWordCounter.of(PETER, "Hallo",1)),
                        KeyValue.pair(
-                                       Word.of("klaus","Müsch"),
-                                       WordCounter.of("klaus","Müsch",1)),
+                                       TestOutputWord.of(KLAUS, "Müsch"),
+                                       TestOutputWordCounter.of(KLAUS, "Müsch",1)),
                        KeyValue.pair(
-                                       Word.of("peter","Welt"),
-                                       WordCounter.of("peter","Welt",1)),
+                                       TestOutputWord.of(PETER, "Welt"),
+                                       TestOutputWordCounter.of(PETER, "Welt",1)),
                        KeyValue.pair(
-                                       Word.of("klaus","Müsch"),
-                                       WordCounter.of("klaus","Müsch",2)),
+                                       TestOutputWord.of(KLAUS, "Müsch"),
+                                       TestOutputWordCounter.of(KLAUS, "Müsch",2)),
                        KeyValue.pair(
-                                       Word.of("klaus","s"),
-                                       WordCounter.of("klaus","s",1)),
+                                       TestOutputWord.of(KLAUS, "s"),
+                                       TestOutputWordCounter.of(KLAUS, "s",1)),
                        KeyValue.pair(
-                                       Word.of("peter","Boäh"),
-                                       WordCounter.of("peter","Boäh",1)),
+                                       TestOutputWord.of(PETER, "Boäh"),
+                                       TestOutputWordCounter.of(PETER, "Boäh",1)),
                        KeyValue.pair(
-                                       Word.of("peter","Welt"),
-                                       WordCounter.of("peter","Welt",2)),
+                                       TestOutputWord.of(PETER, "Welt"),
+                                       TestOutputWordCounter.of(PETER, "Welt",2)),
                        KeyValue.pair(
-                                       Word.of("peter","Boäh"),
-                                       WordCounter.of("peter","Boäh",2)),
+                                       TestOutputWord.of(PETER, "Boäh"),
+                                       TestOutputWordCounter.of(PETER, "Boäh",2)),
                        KeyValue.pair(
-                                       Word.of("klaus","s"),
-                                       WordCounter.of("klaus","s",2)),
+                                       TestOutputWord.of(KLAUS, "s"),
+                                       TestOutputWordCounter.of(KLAUS, "s",2)),
                        KeyValue.pair(
-                                       Word.of("peter","Boäh"),
-                                       WordCounter.of("peter","Boäh",3)),
+                                       TestOutputWord.of(PETER, "Boäh"),
+                                       TestOutputWordCounter.of(PETER, "Boäh",3)),
                        KeyValue.pair(
-                                       Word.of("klaus","s"),
-                                       WordCounter.of("klaus","s",3)),
+                                       TestOutputWord.of(KLAUS, "s"),
+                                       TestOutputWordCounter.of(KLAUS, "s",3)),
        };
 
-       static MultiValueMap<Word, WordCounter> expectedMessages;
-       static
+       static MultiValueMap<TestOutputWord, TestOutputWordCounter> expectedMessages()
        {
-               expectedMessages = new LinkedMultiValueMap<>();
+               MultiValueMap<TestOutputWord, TestOutputWordCounter> expectedMessages = new LinkedMultiValueMap<>();
                Stream
-                               .of(expectedMessagesArray)
+                               .of(EXPECTED_MESSAGES)
                                .forEach(keyValue -> expectedMessages.add(keyValue.key, keyValue.value));
-       }
-
-       static Map<String, Object> convertToMap(Properties properties)
-       {
-               return properties
-                               .entrySet()
-                               .stream()
-                               .collect(
-                                               Collectors.toMap(
-                                                               entry -> (String)entry.getKey(),
-                                                               entry -> entry.getValue()
-                                               ));
-       }
-
-       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());
-               }
+               return expectedMessages;
        }
 }