1 package de.juplo.kafka.wordcount.query;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import lombok.extern.slf4j.Slf4j;
5 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
6 import org.apache.kafka.streams.state.Stores;
7 import org.junit.jupiter.api.BeforeAll;
8 import org.junit.jupiter.api.DisplayName;
9 import org.junit.jupiter.api.Test;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
12 import org.springframework.boot.test.context.SpringBootTest;
13 import org.springframework.boot.test.context.TestConfiguration;
14 import org.springframework.context.annotation.Bean;
15 import org.springframework.context.annotation.Primary;
16 import org.springframework.http.MediaType;
17 import org.springframework.kafka.core.KafkaTemplate;
18 import org.springframework.kafka.support.SendResult;
19 import org.springframework.kafka.test.context.EmbeddedKafka;
20 import org.springframework.test.web.servlet.MockMvc;
21 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
23 import java.nio.charset.StandardCharsets;
24 import java.time.Duration;
25 import java.util.concurrent.CompletableFuture;
27 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.STORE_NAME;
28 import static org.awaitility.Awaitility.await;
29 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
34 "spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer",
35 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
36 "spring.kafka.producer.properties.spring.json.type.mapping=userdata:de.juplo.kafka.wordcount.users.TestUserData,ranking:de.juplo.kafka.wordcount.top10.TestRanking",
37 "logging.level.root=WARN",
38 "logging.level.de.juplo=DEBUG",
39 "logging.level.org.apache.kafka.clients=INFO",
40 "logging.level.org.apache.kafka.streams=INFO",
41 "juplo.wordcount.query.bootstrap-server=${spring.embedded.kafka.brokers}",
42 "juplo.wordcount.query.users-input-topic=" + QueryApplicationIT.TOPIC_USERS,
43 "juplo.wordcount.query.ranking-input-topic=" + QueryApplicationIT.TOPIC_TOP10 })
45 @EmbeddedKafka(topics = { QueryApplicationIT.TOPIC_TOP10, QueryApplicationIT.TOPIC_USERS})
47 public class QueryApplicationIT
49 public static final String TOPIC_TOP10 = "top10";
50 public static final String TOPIC_USERS = "users";
56 ObjectMapper objectMapper;
58 QueryStreamProcessor streamProcessor;
62 public static void testSendMessage(
63 @Autowired KafkaTemplate<String, Object> kafkaTemplate)
67 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_USERS, kv.key, kv.value)));
70 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_TOP10, kv.key, kv.value)));
73 private static void flush(CompletableFuture<SendResult<String, Object>> future)
77 SendResult<String, Object> result = future.get();
79 "Sent: {}={}, partition={}, offset={}",
80 result.getProducerRecord().key(),
81 result.getProducerRecord().value(),
82 result.getRecordMetadata().partition(),
83 result.getRecordMetadata().offset());
87 throw new RuntimeException(e);
91 @DisplayName("Await, that the expected state is visible in the state-store")
93 public void testAwaitExpectedStateInStore()
95 await("The expected state is visible in the state-store")
96 .atMost(Duration.ofSeconds(5))
97 .untilAsserted(() -> TestData.assertExpectedState(user -> streamProcessor.getStore().get(user)));
100 @DisplayName("Await, that the expected state is queryable")
102 public void testAwaitExpectedStateIsQueryable()
104 await("The expected state is queryable")
105 .atMost(Duration.ofSeconds(5))
106 .untilAsserted(() -> TestData.assertExpectedState(user -> requestUserRankingFor(user)));
109 private UserRanking requestUserRankingFor(String user)
113 return objectMapper.readValue(
115 .perform(MockMvcRequestBuilders.get("/{user}", user)
116 .contentType(MediaType.APPLICATION_JSON))
117 .andExpect(status().isOk())
120 .getContentAsString(StandardCharsets.UTF_8),
125 throw new RuntimeException(e);
130 static class Configuration
134 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
136 return Stores.inMemoryKeyValueStore(STORE_NAME);