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.commit-interval=100",
44 "juplo.wordcount.query.cache-max-bytes=0",
45 "juplo.wordcount.query.users-input-topic=" + QueryApplicationIT.TOPIC_USERS,
46 "juplo.wordcount.query.ranking-input-topic=" + QueryApplicationIT.TOPIC_TOP10 })
48 @EmbeddedKafka(topics = { QueryApplicationIT.TOPIC_TOP10, QueryApplicationIT.TOPIC_USERS})
50 public class QueryApplicationIT
52 public static final String TOPIC_TOP10 = "top10";
53 public static final String TOPIC_USERS = "users";
59 ObjectMapper objectMapper;
61 QueryStreamProcessor streamProcessor;
65 public static void testSendMessage(
66 @Autowired KafkaTemplate<String, Object> kafkaTemplate)
70 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_USERS, kv.key, kv.value)));
73 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_TOP10, kv.key, kv.value)));
76 private static void flush(CompletableFuture<SendResult<String, Object>> future)
80 SendResult<String, Object> result = future.get();
82 "Sent: {}={}, partition={}, offset={}",
83 result.getProducerRecord().key(),
84 result.getProducerRecord().value(),
85 result.getRecordMetadata().partition(),
86 result.getRecordMetadata().offset());
90 throw new RuntimeException(e);
94 @DisplayName("Await, that the expected state is visible in the state-store")
96 public void testAwaitExpectedStateInStore()
98 await("The expected state is visible in the state-store")
99 .atMost(Duration.ofSeconds(5))
100 .untilAsserted(() -> TestData.assertExpectedState(user -> streamProcessor.getStore().get(user)));
103 @DisplayName("Await, that the expected state is queryable")
105 public void testAwaitExpectedStateIsQueryable()
107 await("The expected state is queryable")
108 .atMost(Duration.ofSeconds(5))
109 .untilAsserted(() -> TestData.assertExpectedState(user -> requestUserRankingFor(user)));
112 private UserRanking requestUserRankingFor(String user)
116 return objectMapper.readValue(
118 .perform(MockMvcRequestBuilders.get("/{user}", user)
119 .contentType(MediaType.APPLICATION_JSON))
120 .andExpect(status().isOk())
123 .getContentAsString(StandardCharsets.UTF_8),
128 throw new RuntimeException(e);
133 static class Configuration
136 KeyValueBytesStoreSupplier userStoreSupplier()
138 return Stores.inMemoryKeyValueStore(USER_STORE_NAME);
142 KeyValueBytesStoreSupplier rankingStoreSupplier()
144 return Stores.inMemoryKeyValueStore(RANKING_STORE_NAME);