From: Kai Moritz <kai@juplo.de>
Date: Sun, 9 Jun 2024 17:48:33 +0000 (+0200)
Subject: query: 1.0.6 - Added IT, that queries the expected state
X-Git-Tag: query-with-kafkaproducer~9
X-Git-Url: https://juplo.de/gitweb/?a=commitdiff_plain;h=dcb383aef4f4f454e3a1aed9cf1f2a599c7b1bb9;p=demos%2Fkafka%2Fwordcount

query: 1.0.6 - Added IT, that queries the expected state
---

diff --git a/src/test/java/de/juplo/kafka/wordcount/query/QueryApplicationIT.java b/src/test/java/de/juplo/kafka/wordcount/query/QueryApplicationIT.java
index 914aeaf..e38871f 100644
--- a/src/test/java/de/juplo/kafka/wordcount/query/QueryApplicationIT.java
+++ b/src/test/java/de/juplo/kafka/wordcount/query/QueryApplicationIT.java
@@ -7,19 +7,25 @@ import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
 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.http.MediaType;
 import org.springframework.kafka.core.KafkaTemplate;
 import org.springframework.kafka.support.SendResult;
 import org.springframework.kafka.test.context.EmbeddedKafka;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
 
+import java.nio.charset.StandardCharsets;
 import java.time.Duration;
 import java.util.concurrent.CompletableFuture;
 
 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.STORE_NAME;
 import static org.awaitility.Awaitility.await;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
 
 @SpringBootTest(
@@ -34,6 +40,7 @@ import static org.awaitility.Awaitility.await;
 				"juplo.wordcount.query.bootstrap-server=${spring.embedded.kafka.brokers}",
 				"juplo.wordcount.query.users-input-topic=" + QueryApplicationIT.TOPIC_USERS,
 				"juplo.wordcount.query.ranking-input-topic=" + QueryApplicationIT.TOPIC_TOP10 })
+@AutoConfigureMockMvc
 @EmbeddedKafka(topics = { QueryApplicationIT.TOPIC_TOP10, QueryApplicationIT.TOPIC_USERS})
 @Slf4j
 public class QueryApplicationIT
@@ -41,6 +48,9 @@ public class QueryApplicationIT
 	public static final String TOPIC_TOP10 = "top10";
 	public static final String TOPIC_USERS = "users";
 
+
+	@Autowired
+	MockMvc mockMvc;
 	@Autowired
 	QueryStreamProcessor streamProcessor;
 
@@ -75,16 +85,42 @@ public class QueryApplicationIT
 		}
 	}
 
-	@DisplayName("Await the expected state in the state-store")
+	@DisplayName("Await, that the expected state is visible in the state-store")
 	@Test
-	public void testAwaitExpectedState()
+	public void testAwaitExpectedStateInStore()
 	{
-		await("Expected state")
+		await("The expected state is visible in the state-store")
 				.atMost(Duration.ofSeconds(5))
-				.catchUncaughtExceptions()
 				.untilAsserted(() -> TestData.assertExpectedState(user -> streamProcessor.getStore().get(user)));
 	}
 
+	@DisplayName("Await, that the expected state is queryable")
+	@Test
+	public void testAwaitExpectedStateIsQueryable()
+	{
+		await("The expected state is queryable")
+				.atMost(Duration.ofSeconds(5))
+				.untilAsserted(() -> TestData.assertExpectedState(user -> requestUserRankingFor(user)));
+	}
+
+	private String requestUserRankingFor(String user)
+	{
+		try
+		{
+			return mockMvc
+					.perform(MockMvcRequestBuilders.get("/{user}", user)
+							.contentType(MediaType.APPLICATION_JSON))
+					.andExpect(status().isOk())
+					.andReturn()
+					.getResponse()
+					.getContentAsString(StandardCharsets.UTF_8);
+		}
+		catch (Exception e)
+		{
+			throw new RuntimeException(e);
+		}
+	}
+
 	@TestConfiguration
 	static class Configuration
 	{