1 package de.juplo.kafka.wordcount.query;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
5 import org.apache.kafka.streams.state.Stores;
6 import org.junit.jupiter.api.BeforeAll;
7 import org.junit.jupiter.api.DisplayName;
8 import org.junit.jupiter.api.Test;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11 import org.springframework.boot.test.context.SpringBootTest;
12 import org.springframework.boot.test.context.TestConfiguration;
13 import org.springframework.context.annotation.Bean;
14 import org.springframework.context.annotation.Primary;
15 import org.springframework.http.MediaType;
16 import org.springframework.kafka.core.KafkaTemplate;
17 import org.springframework.kafka.support.SendResult;
18 import org.springframework.kafka.test.context.EmbeddedKafka;
19 import org.springframework.test.web.servlet.MockMvc;
20 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
22 import java.nio.charset.StandardCharsets;
23 import java.time.Duration;
24 import java.util.concurrent.CompletableFuture;
26 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.STORE_NAME;
27 import static org.awaitility.Awaitility.await;
28 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
33 "spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer",
34 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
35 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
36 "logging.level.root=WARN",
37 "logging.level.de.juplo=DEBUG",
38 "logging.level.org.apache.kafka.clients=INFO",
39 "logging.level.org.apache.kafka.streams=INFO",
40 "juplo.wordcount.query.bootstrap-server=${spring.embedded.kafka.brokers}",
41 "juplo.wordcount.query.users-input-topic=" + QueryApplicationIT.TOPIC_USERS,
42 "juplo.wordcount.query.ranking-input-topic=" + QueryApplicationIT.TOPIC_TOP10 })
44 @EmbeddedKafka(topics = { QueryApplicationIT.TOPIC_TOP10, QueryApplicationIT.TOPIC_USERS})
46 public class QueryApplicationIT
48 public static final String TOPIC_TOP10 = "top10";
49 public static final String TOPIC_USERS = "users";
55 QueryStreamProcessor streamProcessor;
59 public static void testSendMessage(
60 @Autowired KafkaTemplate<String, Object> kafkaTemplate)
64 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_USERS, kv.key, kv.value)));
67 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_TOP10, kv.key, kv.value)));
70 private static void flush(CompletableFuture<SendResult<String, Object>> future)
74 SendResult<String, Object> result = future.get();
76 "Sent: {}={}, partition={}, offset={}",
77 result.getProducerRecord().key(),
78 result.getProducerRecord().value(),
79 result.getRecordMetadata().partition(),
80 result.getRecordMetadata().offset());
84 throw new RuntimeException(e);
88 @DisplayName("Await, that the expected state is visible in the state-store")
90 public void testAwaitExpectedStateInStore()
92 await("The expected state is visible in the state-store")
93 .atMost(Duration.ofSeconds(5))
94 .untilAsserted(() -> TestData.assertExpectedState(user -> streamProcessor.getStore().get(user)));
97 @DisplayName("Await, that the expected state is queryable")
99 public void testAwaitExpectedStateIsQueryable()
101 await("The expected state is queryable")
102 .atMost(Duration.ofSeconds(5))
103 .untilAsserted(() -> TestData.assertExpectedState(user -> requestUserRankingFor(user)));
106 private String requestUserRankingFor(String user)
111 .perform(MockMvcRequestBuilders.get("/{user}", user)
112 .contentType(MediaType.APPLICATION_JSON))
113 .andExpect(status().isOk())
116 .getContentAsString(StandardCharsets.UTF_8);
120 throw new RuntimeException(e);
125 static class Configuration
129 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
131 return Stores.inMemoryKeyValueStore(STORE_NAME);