top10: 1.2.1 - Refined assertion of expected state
authorKai Moritz <kai@juplo.de>
Sun, 2 Jun 2024 09:00:36 +0000 (11:00 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 8 Jun 2024 11:15:05 +0000 (13:15 +0200)
src/test/java/de/juplo/kafka/wordcount/top10/TestData.java

index d8c603a..eb30298 100644 (file)
@@ -69,8 +69,26 @@ class TestData
 
        static void assertExpectedState(ReadOnlyKeyValueStore<User, Ranking> store)
        {
-               assertThat(store.get(EXPECTED_MESSAGES[9].key)).isEqualTo(EXPECTED_MESSAGES[9].value);
-               assertThat(store.get(EXPECTED_MESSAGES[10].key)).isEqualTo(EXPECTED_MESSAGES[10].value);
+               assertRankingEqualsRankingFromLastMessage(PETER, store.get(PETER));
+               assertRankingEqualsRankingFromLastMessage(KLAUS, store.get(KLAUS));
+       }
+
+       static void assertRankingEqualsRankingFromLastMessage(User user, Ranking ranking)
+       {
+               assertThat(ranking).isEqualTo(getLastMessageFor(user));
+       }
+
+       static Ranking getLastMessageFor(User user)
+       {
+               return getLastMessageFor(user, expectedMessages());
+       }
+
+       static Ranking getLastMessageFor(User user, MultiValueMap<User, Ranking> messagesForUsers)
+       {
+               return messagesForUsers
+                               .get(user)
+                               .stream()
+                               .reduce(null, (left, right) -> right);
        }
 
        static KeyValue<User, Ranking>[] EXPECTED_MESSAGES = new KeyValue[]