top10: 1.2.1 - Added expectations for the number of received messages
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / top10 / Top10ApplicationIT.java
index 2d45f03..1097310 100644 (file)
@@ -4,13 +4,14 @@ import de.juplo.kafka.wordcount.counter.TestWord;
 import de.juplo.kafka.wordcount.counter.TestCounter;
 import de.juplo.kafka.wordcount.query.TestRanking;
 import lombok.extern.slf4j.Slf4j;
-import org.junit.jupiter.api.BeforeAll;
-import org.junit.jupiter.api.DisplayName;
-import org.junit.jupiter.api.Test;
+import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
+import org.apache.kafka.streams.state.Stores;
+import org.junit.jupiter.api.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.context.TestConfiguration;
 import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Primary;
 import org.springframework.kafka.annotation.KafkaListener;
 import org.springframework.kafka.core.KafkaTemplate;
 import org.springframework.kafka.support.KafkaHeaders;
@@ -41,7 +42,11 @@ import static org.awaitility.Awaitility.await;
                                "spring.kafka.consumer.properties.spring.json.trusted.packages=de.juplo.kafka.wordcount.top10   ",
                                "logging.level.root=WARN",
                                "logging.level.de.juplo=DEBUG",
+                               "logging.level.org.apache.kafka.clients=INFO",
+                               "logging.level.org.apache.kafka.streams=INFO",
                                "juplo.wordcount.top10.bootstrap-server=${spring.embedded.kafka.brokers}",
+                               "juplo.wordcount.top10.commit-interval=100",
+                               "juplo.wordcount.top10.cacheMaxBytes=0",
                                "juplo.wordcount.top10.input-topic=" + Top10ApplicationIT.TOPIC_IN,
                                "juplo.wordcount.top10.output-topic=" + Top10ApplicationIT.TOPIC_OUT })
 @EmbeddedKafka(topics = { Top10ApplicationIT.TOPIC_IN, Top10ApplicationIT.TOPIC_OUT })
@@ -50,9 +55,12 @@ public class Top10ApplicationIT
 {
        public static final String TOPIC_IN = "in";
        public static final String TOPIC_OUT = "out";
+       public static final String STORE_NAME = "TEST-STORE";
 
        @Autowired
        Consumer consumer;
+       @Autowired
+       Top10StreamProcessor streamProcessor;
 
 
        @BeforeAll
@@ -80,8 +88,18 @@ public class Top10ApplicationIT
                                });
        }
 
+       @DisplayName("Await the expected state in the state-store")
+       @Test
+       public void testAwaitExpectedState()
+       {
+               await("Expected state")
+                               .atMost(Duration.ofSeconds(5))
+                               .untilAsserted(() -> TestData.assertExpectedState(streamProcessor.getStore(STORE_NAME)));
+       }
+
        @DisplayName("Await the expected output messages")
        @Test
+       @Disabled
        public void testAwaitExpectedMessages()
        {
                await("Expected messages")
@@ -89,6 +107,24 @@ public class Top10ApplicationIT
                                .untilAsserted(() -> TestData.assertExpectedMessages(consumer.getReceivedMessages()));
        }
 
+       @DisplayName("Await the expected number of messages")
+       @Test
+       public void testAwaitExpectedNumberOfMessagesForUsers()
+       {
+               await("Expected number of messages")
+                               .atMost(Duration.ofSeconds(5))
+                               .untilAsserted(() -> TestData.assertExpectedNumberOfMessagesForUsers(consumer.getReceivedMessages()));
+       }
+
+       @DisplayName("Await the expected final output messages")
+       @Test
+       public void testAwaitExpectedLastMessagesForUsers()
+       {
+               await("Expected final output messages")
+                               .atMost(Duration.ofSeconds(5))
+                               .untilAsserted(() -> TestData.assertExpectedLastMessagesForUsers(consumer.getReceivedMessages()));
+       }
+
 
        static class Consumer
        {
@@ -117,5 +153,12 @@ public class Top10ApplicationIT
                {
                        return new Consumer();
                }
+
+               @Primary
+               @Bean
+               KeyValueBytesStoreSupplier inMemoryStoreSupplier()
+               {
+                       return Stores.inMemoryKeyValueStore(STORE_NAME);
+               }
        }
 }