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