1 package de.juplo.kafka.wordcount.query;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import de.juplo.kafka.wordcount.top10.TestRanking;
5 import de.juplo.kafka.wordcount.top10.TestUser;
6 import lombok.extern.slf4j.Slf4j;
7 import org.apache.kafka.clients.producer.ProducerConfig;
8 import org.apache.kafka.common.serialization.StringSerializer;
9 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
10 import org.apache.kafka.streams.state.Stores;
11 import org.junit.jupiter.api.BeforeAll;
12 import org.junit.jupiter.api.DisplayName;
13 import org.junit.jupiter.api.Test;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
16 import org.springframework.boot.test.context.SpringBootTest;
17 import org.springframework.boot.test.context.TestConfiguration;
18 import org.springframework.context.annotation.Bean;
19 import org.springframework.http.MediaType;
20 import org.springframework.kafka.core.KafkaTemplate;
21 import org.springframework.kafka.core.ProducerFactory;
22 import org.springframework.kafka.support.SendResult;
23 import org.springframework.kafka.support.serializer.JsonSerializer;
24 import org.springframework.kafka.test.context.EmbeddedKafka;
25 import org.springframework.test.web.servlet.MockMvc;
26 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
28 import java.nio.charset.StandardCharsets;
29 import java.time.Duration;
31 import java.util.concurrent.CompletableFuture;
33 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.RANKING_STORE_NAME;
34 import static de.juplo.kafka.wordcount.query.QueryStreamProcessor.USER_STORE_NAME;
35 import static org.awaitility.Awaitility.await;
36 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
41 "spring.main.allow-bean-definition-overriding=true",
42 "logging.level.root=WARN",
43 "logging.level.de.juplo=DEBUG",
44 "logging.level.org.apache.kafka.clients=INFO",
45 "logging.level.org.apache.kafka.streams=INFO",
46 "juplo.wordcount.query.bootstrap-server=${spring.embedded.kafka.brokers}",
47 "juplo.wordcount.query.commit-interval=100",
48 "juplo.wordcount.query.cache-max-bytes=0",
49 "juplo.wordcount.query.users-input-topic=" + QueryApplicationIT.TOPIC_USERS,
50 "juplo.wordcount.query.ranking-input-topic=" + QueryApplicationIT.TOPIC_TOP10 })
52 @EmbeddedKafka(topics = { QueryApplicationIT.TOPIC_TOP10, QueryApplicationIT.TOPIC_USERS})
54 public class QueryApplicationIT
56 public static final String TOPIC_TOP10 = "top10";
57 public static final String TOPIC_USERS = "users";
63 ObjectMapper objectMapper;
65 QueryStreamProcessor streamProcessor;
69 public static void testSendMessage(
70 @Autowired KafkaTemplate usersKafkaTemplate,
71 @Autowired KafkaTemplate top10KafkaTemplate)
75 .forEach(kv -> flush(usersKafkaTemplate.send(TOPIC_USERS, kv.key, kv.value)));
78 .forEach(kv -> flush(top10KafkaTemplate.send(TOPIC_TOP10, kv.key, kv.value)));
81 private static void flush(CompletableFuture<SendResult> future)
85 SendResult result = future.get();
87 "Sent: {}={}, partition={}, offset={}",
88 result.getProducerRecord().key(),
89 result.getProducerRecord().value(),
90 result.getRecordMetadata().partition(),
91 result.getRecordMetadata().offset());
95 throw new RuntimeException(e);
99 @DisplayName("Await, that the expected state is visible in the state-store")
101 public void testAwaitExpectedStateInStore()
103 await("The expected state is visible in the state-store")
104 .atMost(Duration.ofSeconds(5))
105 .untilAsserted(() -> TestData.assertExpectedState(user -> streamProcessor.getStore().get(user)));
108 @DisplayName("Await, that the expected state is queryable")
110 public void testAwaitExpectedStateIsQueryable()
112 await("The expected state is queryable")
113 .atMost(Duration.ofSeconds(5))
114 .untilAsserted(() -> TestData.assertExpectedState(user -> requestUserRankingFor(user)));
117 private UserRanking requestUserRankingFor(String user)
121 return objectMapper.readValue(
123 .perform(MockMvcRequestBuilders.get("/{user}", user)
124 .contentType(MediaType.APPLICATION_JSON))
125 .andExpect(status().isOk())
128 .getContentAsString(StandardCharsets.UTF_8),
133 throw new RuntimeException(e);
138 static class Configuration
141 KafkaTemplate usersKafkaTemplate(ProducerFactory producerFactory)
143 Map<String, Object> properties = Map.of(
144 ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName(),
145 ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class.getName(),
146 JsonSerializer.ADD_TYPE_INFO_HEADERS, false);
147 return new KafkaTemplate(producerFactory, properties);
151 KafkaTemplate top10KafkaTemplate(ProducerFactory producerFactory)
153 Map<String, Object> properties = Map.of(
154 ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, JsonSerializer.class.getName(),
155 ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class.getName(),
156 JsonSerializer.TYPE_MAPPINGS, "user:" + TestUser.class.getName() + ",ranking:" + TestRanking.class.getName());
157 return new KafkaTemplate(producerFactory, properties);
161 KeyValueBytesStoreSupplier userStoreSupplier()
163 return Stores.inMemoryKeyValueStore(USER_STORE_NAME);
167 KeyValueBytesStoreSupplier rankingStoreSupplier()
169 return Stores.inMemoryKeyValueStore(RANKING_STORE_NAME);