query: 2.0.0 - Configured caching & commit-interval in integration-test
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / query / QueryApplicationIT.java
1 package de.juplo.kafka.wordcount.query;
2
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;
21
22 import java.nio.charset.StandardCharsets;
23 import java.time.Duration;
24 import java.util.concurrent.CompletableFuture;
25
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;
30
31
32 @SpringBootTest(
33                 properties = {
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 })
47 @AutoConfigureMockMvc
48 @EmbeddedKafka(topics = { QueryApplicationIT.TOPIC_TOP10, QueryApplicationIT.TOPIC_USERS})
49 @Slf4j
50 public class QueryApplicationIT
51 {
52         public static final String TOPIC_TOP10 = "top10";
53         public static final String TOPIC_USERS = "users";
54
55
56         @Autowired
57         MockMvc mockMvc;
58         @Autowired
59         ObjectMapper objectMapper;
60         @Autowired
61         QueryStreamProcessor streamProcessor;
62
63
64         @BeforeAll
65         public static void testSendMessage(
66                         @Autowired KafkaTemplate<String, Object> kafkaTemplate)
67         {
68                 TestData
69                                 .getUsersMessages()
70                                 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_USERS, kv.key, kv.value)));
71                 TestData
72                                 .getTop10Messages()
73                                 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_TOP10, kv.key, kv.value)));
74         }
75
76         private static void flush(CompletableFuture<SendResult<String, Object>> future)
77         {
78                 try
79                 {
80                         SendResult<String, Object> result = future.get();
81                         log.info(
82                                         "Sent: {}={}, partition={}, offset={}",
83                                         result.getProducerRecord().key(),
84                                         result.getProducerRecord().value(),
85                                         result.getRecordMetadata().partition(),
86                                         result.getRecordMetadata().offset());
87                 }
88                 catch (Exception e)
89                 {
90                         throw new RuntimeException(e);
91                 }
92         }
93
94         @DisplayName("Await, that the expected state is visible in the state-store")
95         @Test
96         public void testAwaitExpectedStateInStore()
97         {
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)));
101         }
102
103         @DisplayName("Await, that the expected state is queryable")
104         @Test
105         public void testAwaitExpectedStateIsQueryable()
106         {
107                 await("The expected state is queryable")
108                                 .atMost(Duration.ofSeconds(5))
109                                 .untilAsserted(() -> TestData.assertExpectedState(user -> requestUserRankingFor(user)));
110         }
111
112         private UserRanking requestUserRankingFor(String user)
113         {
114                 try
115                 {
116                         return objectMapper.readValue(
117                                         mockMvc
118                                                         .perform(MockMvcRequestBuilders.get("/{user}", user)
119                                                                         .contentType(MediaType.APPLICATION_JSON))
120                                                         .andExpect(status().isOk())
121                                                         .andReturn()
122                                                         .getResponse()
123                                                         .getContentAsString(StandardCharsets.UTF_8),
124                                         UserRanking.class);
125                 }
126                 catch (Exception e)
127                 {
128                         throw new RuntimeException(e);
129                 }
130         }
131
132         @TestConfiguration
133         static class Configuration
134         {
135                 @Bean
136                 KeyValueBytesStoreSupplier userStoreSupplier()
137                 {
138                         return Stores.inMemoryKeyValueStore(USER_STORE_NAME);
139                 }
140
141                 @Bean
142                 KeyValueBytesStoreSupplier rankingStoreSupplier()
143                 {
144                         return Stores.inMemoryKeyValueStore(RANKING_STORE_NAME);
145                 }
146         }
147 }