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.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.RANKING_STORE_NAME;
27 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.USER_STORE_NAME;
28 import static org.awaitility.Awaitility.await;
29 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
34 "spring.main.allow-bean-definition-overriding=true",
35 "spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer",
36 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
37 "spring.kafka.producer.properties.spring.json.type.mapping=userdata:de.juplo.kafka.wordcount.users.TestUserData,ranking:de.juplo.kafka.wordcount.top10.TestRanking",
38 "logging.level.root=WARN",
39 "logging.level.de.juplo=DEBUG",
40 "logging.level.org.apache.kafka.clients=INFO",
41 "logging.level.org.apache.kafka.streams=INFO",
42 "juplo.wordcount.query.bootstrap-server=${spring.embedded.kafka.brokers}",
43 "juplo.wordcount.query.users-input-topic=" + QueryApplicationIT.TOPIC_USERS,
44 "juplo.wordcount.query.ranking-input-topic=" + QueryApplicationIT.TOPIC_TOP10 })
46 @EmbeddedKafka(topics = { QueryApplicationIT.TOPIC_TOP10, QueryApplicationIT.TOPIC_USERS})
48 public class QueryApplicationIT
50 public static final String TOPIC_TOP10 = "top10";
51 public static final String TOPIC_USERS = "users";
57 ObjectMapper objectMapper;
59 QueryStreamProcessor streamProcessor;
63 public static void testSendMessage(
64 @Autowired KafkaTemplate<String, Object> kafkaTemplate)
68 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_USERS, kv.key, kv.value)));
71 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_TOP10, kv.key, kv.value)));
74 private static void flush(CompletableFuture<SendResult<String, Object>> future)
78 SendResult<String, Object> result = future.get();
80 "Sent: {}={}, partition={}, offset={}",
81 result.getProducerRecord().key(),
82 result.getProducerRecord().value(),
83 result.getRecordMetadata().partition(),
84 result.getRecordMetadata().offset());
88 throw new RuntimeException(e);
92 @DisplayName("Await, that the expected state is visible in the state-store")
94 public void testAwaitExpectedStateInStore()
96 await("The expected state is visible in the state-store")
97 .atMost(Duration.ofSeconds(5))
98 .untilAsserted(() -> TestData.assertExpectedState(user -> streamProcessor.getStore().get(user)));
101 @DisplayName("Await, that the expected state is queryable")
103 public void testAwaitExpectedStateIsQueryable()
105 await("The expected state is queryable")
106 .atMost(Duration.ofSeconds(5))
107 .untilAsserted(() -> TestData.assertExpectedState(user -> requestUserRankingFor(user)));
110 private UserRanking requestUserRankingFor(String user)
114 return objectMapper.readValue(
116 .perform(MockMvcRequestBuilders.get("/{user}", user)
117 .contentType(MediaType.APPLICATION_JSON))
118 .andExpect(status().isOk())
121 .getContentAsString(StandardCharsets.UTF_8),
126 throw new RuntimeException(e);
131 static class Configuration
134 KeyValueBytesStoreSupplier userStoreSupplier()
136 return Stores.inMemoryKeyValueStore(USER_STORE_NAME);
140 KeyValueBytesStoreSupplier rankingStoreSupplier()
142 return Stores.inMemoryKeyValueStore(RANKING_STORE_NAME);