1 package de.juplo.kafka.wordcount.query;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import de.juplo.kafka.wordcount.top10.TestRanking;
5 import lombok.extern.slf4j.Slf4j;
6 import org.apache.kafka.clients.producer.ProducerConfig;
7 import org.apache.kafka.common.serialization.StringSerializer;
8 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
9 import org.apache.kafka.streams.state.Stores;
10 import org.junit.jupiter.api.BeforeAll;
11 import org.junit.jupiter.api.DisplayName;
12 import org.junit.jupiter.api.Test;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
15 import org.springframework.boot.test.context.SpringBootTest;
16 import org.springframework.boot.test.context.TestConfiguration;
17 import org.springframework.context.annotation.Bean;
18 import org.springframework.http.MediaType;
19 import org.springframework.kafka.core.KafkaTemplate;
20 import org.springframework.kafka.core.ProducerFactory;
21 import org.springframework.kafka.support.SendResult;
22 import org.springframework.kafka.support.serializer.JsonSerializer;
23 import org.springframework.kafka.test.context.EmbeddedKafka;
24 import org.springframework.test.web.servlet.MockMvc;
25 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
27 import java.nio.charset.StandardCharsets;
28 import java.time.Duration;
30 import java.util.concurrent.CompletableFuture;
32 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.RANKING_STORE_NAME;
33 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.USER_STORE_NAME;
34 import static org.awaitility.Awaitility.await;
35 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
40 "spring.main.allow-bean-definition-overriding=true",
41 "logging.level.root=WARN",
42 "logging.level.de.juplo=DEBUG",
43 "logging.level.org.apache.kafka.clients=INFO",
44 "logging.level.org.apache.kafka.streams=INFO",
45 "juplo.wordcount.query.bootstrap-server=${spring.embedded.kafka.brokers}",
46 "juplo.wordcount.query.commit-interval=100",
47 "juplo.wordcount.query.cache-max-bytes=0",
48 "juplo.wordcount.query.users-input-topic=" + QueryApplicationIT.TOPIC_USERS,
49 "juplo.wordcount.query.ranking-input-topic=" + QueryApplicationIT.TOPIC_TOP10 })
51 @EmbeddedKafka(topics = { QueryApplicationIT.TOPIC_TOP10, QueryApplicationIT.TOPIC_USERS})
53 public class QueryApplicationIT
55 public static final String TOPIC_TOP10 = "top10";
56 public static final String TOPIC_USERS = "users";
62 ObjectMapper objectMapper;
64 QueryStreamProcessor streamProcessor;
68 public static void testSendMessage(
69 @Autowired KafkaTemplate usersKafkaTemplate,
70 @Autowired KafkaTemplate top10KafkaTemplate)
74 .forEach(kv -> flush(usersKafkaTemplate.send(TOPIC_USERS, kv.key, kv.value)));
77 .forEach(kv -> flush(top10KafkaTemplate.send(TOPIC_TOP10, kv.key, kv.value)));
80 private static void flush(CompletableFuture<SendResult> future)
84 SendResult result = future.get();
86 "Sent: {}={}, partition={}, offset={}",
87 result.getProducerRecord().key(),
88 result.getProducerRecord().value(),
89 result.getRecordMetadata().partition(),
90 result.getRecordMetadata().offset());
94 throw new RuntimeException(e);
98 @DisplayName("Await, that the expected state is visible in the state-store")
100 public void testAwaitExpectedStateInStore()
102 await("The expected state is visible in the state-store")
103 .atMost(Duration.ofSeconds(5))
104 .untilAsserted(() -> TestData.assertExpectedState(user -> streamProcessor.getStore().get(user)));
107 @DisplayName("Await, that the expected state is queryable")
109 public void testAwaitExpectedStateIsQueryable()
111 await("The expected state is queryable")
112 .atMost(Duration.ofSeconds(5))
113 .untilAsserted(() -> TestData.assertExpectedState(user -> requestUserRankingFor(user)));
116 private UserRanking requestUserRankingFor(String user)
120 return objectMapper.readValue(
122 .perform(MockMvcRequestBuilders.get("/{user}", user)
123 .contentType(MediaType.APPLICATION_JSON))
124 .andExpect(status().isOk())
127 .getContentAsString(StandardCharsets.UTF_8),
132 throw new RuntimeException(e);
137 static class Configuration
140 KafkaTemplate usersKafkaTemplate(ProducerFactory producerFactory)
142 Map<String, Object> properties = Map.of(
143 ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName(),
144 ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class.getName(),
145 JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
146 return new KafkaTemplate(producerFactory, properties);
150 KafkaTemplate top10KafkaTemplate(ProducerFactory producerFactory)
152 Map<String, Object> properties = Map.of(
153 ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName(),
154 ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class.getName(),
155 JsonSerializer.TYPE_MAPPINGS, "ranking:" + TestRanking.class.getName());
156 return new KafkaTemplate(producerFactory, properties);
160 KeyValueBytesStoreSupplier userStoreSupplier()
162 return Stores.inMemoryKeyValueStore(USER_STORE_NAME);
166 KeyValueBytesStoreSupplier rankingStoreSupplier()
168 return Stores.inMemoryKeyValueStore(RANKING_STORE_NAME);