counter: 1.2.15 - Added assertion for the expected number of messages
authorKai Moritz <kai@juplo.de>
Sat, 8 Jun 2024 08:35:34 +0000 (10:35 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 8 Jun 2024 11:33:30 +0000 (13:33 +0200)
src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java
src/test/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessorTopologyTest.java
src/test/java/de/juplo/kafka/wordcount/counter/TestData.java

index 41e5b62..a771904 100644 (file)
@@ -82,6 +82,15 @@ public class CounterApplicationIT
                                });
        }
 
+       @DisplayName("Await the expected number of messages")
+       @Test
+       public void testAwaitExpectedNumberOfMessagesForUsers()
+       {
+               await("Expected number of messages")
+                               .atMost(Duration.ofSeconds(5))
+                               .untilAsserted(() -> consumer.enforceAssertion(TestData.expectedNumberOfMessagesForWordAssertion()));
+       }
+
        @DisplayName("Await the expected output messages")
        @Test
        void testSendMessage()
index a1b4c7f..e761679 100644 (file)
@@ -72,6 +72,8 @@ public class CounterStreamProcessorTopologyTest
         .forEach(record -> receivedMessages.add(record.key(), record.value()));
 
     TestData.assertExpectedMessages(receivedMessages);
+
+    TestData.assertExpectedNumberOfMessagesForWord(receivedMessages);
   }
 
   @AfterEach
index 714e585..c9d871a 100644 (file)
@@ -85,6 +85,25 @@ class TestData
                                                                .containsExactlyElementsOf(counter));
        }
 
+       static Consumer<MultiValueMap<TestOutputWord, TestOutputWordCounter>> expectedNumberOfMessagesForWordAssertion()
+       {
+               return receivedMessages -> assertExpectedNumberOfMessagesForWord(receivedMessages);
+       }
+
+       static void assertExpectedNumberOfMessagesForWord(MultiValueMap<TestOutputWord, TestOutputWordCounter> 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<TestOutputWord, TestOutputWordCounter> messagesForUsers)
+       {
+               return messagesForUsers.get(word).size();
+       }
+
        private static final KeyValue<TestOutputWord, TestOutputWordCounter>[] EXPECTED_MESSAGES = new KeyValue[]
        {
                        KeyValue.pair(