top10: 1.1.2 - (RED) Added test, that asserts the expectated processing
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / top10 / TestData.java
diff --git a/src/test/java/de/juplo/kafka/wordcount/top10/TestData.java b/src/test/java/de/juplo/kafka/wordcount/top10/TestData.java
new file mode 100644 (file)
index 0000000..73a405e
--- /dev/null
@@ -0,0 +1,159 @@
+package de.juplo.kafka.wordcount.top10;
+
+import de.juplo.kafka.wordcount.counter.TestCounter;
+import de.juplo.kafka.wordcount.counter.TestWord;
+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.Map;
+import java.util.Properties;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+class TestData
+{
+       static final KeyValue<TestWord, TestCounter>[] INPUT_MESSAGES = new KeyValue[]
+       {
+                       new KeyValue<>(
+                                       TestWord.of("peter","Hallo"),
+                                       TestCounter.of("peter","Hallo",1)),
+                       new KeyValue<>(
+                                       TestWord.of("klaus","Müsch"),
+                                       TestCounter.of("klaus","Müsch",1)),
+                       new KeyValue<>(
+                                       TestWord.of("peter","Welt"),
+                                       TestCounter.of("peter","Welt",1)),
+                       new KeyValue<>(
+                                       TestWord.of("klaus","Müsch"),
+                                       TestCounter.of("klaus","Müsch",2)),
+                       new KeyValue<>(
+                                       TestWord.of("klaus","s"),
+                                       TestCounter.of("klaus","s",1)),
+                       new KeyValue<>(
+                                       TestWord.of("peter","Boäh"),
+                                       TestCounter.of("peter","Boäh",1)),
+                       new KeyValue<>(
+                                       TestWord.of("peter","Welt"),
+                                       TestCounter.of("peter","Welt",2)),
+                       new KeyValue<>(
+                                       TestWord.of("peter","Boäh"),
+                                       TestCounter.of("peter","Boäh",2)),
+                       new KeyValue<>(
+                                       TestWord.of("klaus","s"),
+                                       TestCounter.of("klaus","s",2)),
+                       new KeyValue<>(
+                                       TestWord.of("peter","Boäh"),
+                                       TestCounter.of("peter","Boäh",3)),
+                       new KeyValue<>(
+                                       TestWord.of("klaus","s"),
+                                       TestCounter.of("klaus","s",3)),
+       };
+
+       static void assertExpectedMessages(MultiValueMap<String, Ranking> receivedMessages)
+       {
+               expectedMessages().forEach(
+                               (user, rankings) ->
+                                               assertThat(receivedMessages.get(user))
+                                                               .containsExactlyElementsOf(rankings));
+       }
+
+       static KeyValue<String, Ranking>[] EXPECTED_MESSAGES = new KeyValue[]
+       {
+                       KeyValue.pair( // 0
+                                       "peter",
+                                       Ranking.of(
+                                                       Entry.of("Hallo", 1l))),
+                       KeyValue.pair( // 1
+                                       "klaus",
+                                       Ranking.of(
+                                                       Entry.of("Müsch", 1l))),
+                       KeyValue.pair( // 2
+                                       "peter",
+                                       Ranking.of(
+                                                       Entry.of("Hallo", 1l),
+                                                       Entry.of("Welt", 1l))),
+                       KeyValue.pair( // 3
+                                       "klaus",
+                                       Ranking.of(
+                                                       Entry.of("Müsch", 2l))),
+                       KeyValue.pair( // 4
+                                       "klaus",
+                                       Ranking.of(
+                                                       Entry.of("Müsch", 2l),
+                                                       Entry.of("s", 1l))),
+                       KeyValue.pair( // 5
+                                       "peter",
+                                       Ranking.of(
+                                                       Entry.of("Hallo", 1l),
+                                                       Entry.of("Welt", 1l),
+                                                       Entry.of("Boäh", 1l))),
+                       KeyValue.pair( // 6
+                                       "peter",
+                                       Ranking.of(
+                                                       Entry.of("Welt", 2l),
+                                                       Entry.of("Hallo", 1l),
+                                                       Entry.of("Boäh", 1l))),
+                       KeyValue.pair( // 7
+                                       "peter",
+                                       Ranking.of(
+                                                       Entry.of("Welt", 2l),
+                                                       Entry.of("Boäh", 2l),
+                                                       Entry.of("Hallo", 1l))),
+                       KeyValue.pair( // 8
+                                       "klaus",
+                                       Ranking.of(
+                                                       Entry.of("Müsch", 2l),
+                                                       Entry.of("s", 2l))),
+                       KeyValue.pair( // 9
+                                       "peter",
+                                       Ranking.of(
+                                                       Entry.of("Boäh", 3l),
+                                                       Entry.of("Welt", 2l),
+                                                       Entry.of("Hallo", 1l))),
+                       KeyValue.pair( // 10
+                                       "klaus",
+                                       Ranking.of(
+                                                       Entry.of("s", 3l),
+                                                       Entry.of("Müsch", 2l))),
+       };
+
+       static MultiValueMap<String, Ranking> expectedMessages()
+       {
+               MultiValueMap<String, Ranking> expectedMessages = new LinkedMultiValueMap<>();
+               Stream
+                               .of(EXPECTED_MESSAGES)
+                               .forEach(keyValue -> expectedMessages.add(keyValue.key, keyValue.value));
+               return expectedMessages;
+       }
+
+       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());
+               }
+       }
+}