19ada51fc53d75cc572efc9fb77a56d3b0580ffb
[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 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;
26
27 import java.nio.charset.StandardCharsets;
28 import java.time.Duration;
29 import java.util.Map;
30 import java.util.concurrent.CompletableFuture;
31
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;
36
37
38 @SpringBootTest(
39                 properties = {
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 })
50 @AutoConfigureMockMvc
51 @EmbeddedKafka(topics = { QueryApplicationIT.TOPIC_TOP10, QueryApplicationIT.TOPIC_USERS})
52 @Slf4j
53 public class QueryApplicationIT
54 {
55         public static final String TOPIC_TOP10 = "top10";
56         public static final String TOPIC_USERS = "users";
57
58
59         @Autowired
60         MockMvc mockMvc;
61         @Autowired
62         ObjectMapper objectMapper;
63         @Autowired
64         QueryStreamProcessor streamProcessor;
65
66
67         @BeforeAll
68         public static void testSendMessage(
69                         @Autowired KafkaTemplate usersKafkaTemplate,
70                         @Autowired KafkaTemplate top10KafkaTemplate)
71         {
72                 TestData
73                                 .getUsersMessages()
74                                 .forEach(kv -> flush(usersKafkaTemplate.send(TOPIC_USERS, kv.key, kv.value)));
75                 TestData
76                                 .getTop10Messages()
77                                 .forEach(kv -> flush(top10KafkaTemplate.send(TOPIC_TOP10, kv.key, kv.value)));
78         }
79
80         private static void flush(CompletableFuture<SendResult> future)
81         {
82                 try
83                 {
84                         SendResult result = future.get();
85                         log.info(
86                                         "Sent: {}={}, partition={}, offset={}",
87                                         result.getProducerRecord().key(),
88                                         result.getProducerRecord().value(),
89                                         result.getRecordMetadata().partition(),
90                                         result.getRecordMetadata().offset());
91                 }
92                 catch (Exception e)
93                 {
94                         throw new RuntimeException(e);
95                 }
96         }
97
98         @DisplayName("Await, that the expected state is visible in the state-store")
99         @Test
100         public void testAwaitExpectedStateInStore()
101         {
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)));
105         }
106
107         @DisplayName("Await, that the expected state is queryable")
108         @Test
109         public void testAwaitExpectedStateIsQueryable()
110         {
111                 await("The expected state is queryable")
112                                 .atMost(Duration.ofSeconds(5))
113                                 .untilAsserted(() -> TestData.assertExpectedState(user -> requestUserRankingFor(user)));
114         }
115
116         private UserRanking requestUserRankingFor(String user)
117         {
118                 try
119                 {
120                         return objectMapper.readValue(
121                                         mockMvc
122                                                         .perform(MockMvcRequestBuilders.get("/{user}", user)
123                                                                         .contentType(MediaType.APPLICATION_JSON))
124                                                         .andExpect(status().isOk())
125                                                         .andReturn()
126                                                         .getResponse()
127                                                         .getContentAsString(StandardCharsets.UTF_8),
128                                         UserRanking.class);
129                 }
130                 catch (Exception e)
131                 {
132                         throw new RuntimeException(e);
133                 }
134         }
135
136         @TestConfiguration
137         static class Configuration
138         {
139                 @Bean
140                 KafkaTemplate usersKafkaTemplate(ProducerFactory producerFactory)
141                 {
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);
147                 }
148
149                 @Bean
150                 KafkaTemplate top10KafkaTemplate(ProducerFactory producerFactory)
151                 {
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);
157                 }
158
159                 @Bean
160                 KeyValueBytesStoreSupplier userStoreSupplier()
161                 {
162                         return Stores.inMemoryKeyValueStore(USER_STORE_NAME);
163                 }
164
165                 @Bean
166                 KeyValueBytesStoreSupplier rankingStoreSupplier()
167                 {
168                         return Stores.inMemoryKeyValueStore(RANKING_STORE_NAME);
169                 }
170         }
171 }