e38871ff4488bdc69e351af093aabeb86237e0da
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / query / QueryApplicationIT.java
1 package de.juplo.kafka.wordcount.query;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
5 import org.apache.kafka.streams.state.Stores;
6 import org.junit.jupiter.api.BeforeAll;
7 import org.junit.jupiter.api.DisplayName;
8 import org.junit.jupiter.api.Test;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11 import org.springframework.boot.test.context.SpringBootTest;
12 import org.springframework.boot.test.context.TestConfiguration;
13 import org.springframework.context.annotation.Bean;
14 import org.springframework.context.annotation.Primary;
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.STORE_NAME;
27 import static org.awaitility.Awaitility.await;
28 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29
30
31 @SpringBootTest(
32                 properties = {
33                                 "spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer",
34                                 "spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
35                                 "spring.kafka.producer.properties.spring.json.add.type.headers=false",
36                                 "logging.level.root=WARN",
37                                 "logging.level.de.juplo=DEBUG",
38                                 "logging.level.org.apache.kafka.clients=INFO",
39                                 "logging.level.org.apache.kafka.streams=INFO",
40                                 "juplo.wordcount.query.bootstrap-server=${spring.embedded.kafka.brokers}",
41                                 "juplo.wordcount.query.users-input-topic=" + QueryApplicationIT.TOPIC_USERS,
42                                 "juplo.wordcount.query.ranking-input-topic=" + QueryApplicationIT.TOPIC_TOP10 })
43 @AutoConfigureMockMvc
44 @EmbeddedKafka(topics = { QueryApplicationIT.TOPIC_TOP10, QueryApplicationIT.TOPIC_USERS})
45 @Slf4j
46 public class QueryApplicationIT
47 {
48         public static final String TOPIC_TOP10 = "top10";
49         public static final String TOPIC_USERS = "users";
50
51
52         @Autowired
53         MockMvc mockMvc;
54         @Autowired
55         QueryStreamProcessor streamProcessor;
56
57
58         @BeforeAll
59         public static void testSendMessage(
60                         @Autowired KafkaTemplate<String, Object> kafkaTemplate)
61         {
62                 TestData
63                                 .getUsersMessages()
64                                 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_USERS, kv.key, kv.value)));
65                 TestData
66                                 .getTop10Messages()
67                                 .forEach(kv -> flush(kafkaTemplate.send(TOPIC_TOP10, kv.key, kv.value)));
68         }
69
70         private static void flush(CompletableFuture<SendResult<String, Object>> future)
71         {
72                 try
73                 {
74                         SendResult<String, Object> result = future.get();
75                         log.info(
76                                         "Sent: {}={}, partition={}, offset={}",
77                                         result.getProducerRecord().key(),
78                                         result.getProducerRecord().value(),
79                                         result.getRecordMetadata().partition(),
80                                         result.getRecordMetadata().offset());
81                 }
82                 catch (Exception e)
83                 {
84                         throw new RuntimeException(e);
85                 }
86         }
87
88         @DisplayName("Await, that the expected state is visible in the state-store")
89         @Test
90         public void testAwaitExpectedStateInStore()
91         {
92                 await("The expected state is visible in the state-store")
93                                 .atMost(Duration.ofSeconds(5))
94                                 .untilAsserted(() -> TestData.assertExpectedState(user -> streamProcessor.getStore().get(user)));
95         }
96
97         @DisplayName("Await, that the expected state is queryable")
98         @Test
99         public void testAwaitExpectedStateIsQueryable()
100         {
101                 await("The expected state is queryable")
102                                 .atMost(Duration.ofSeconds(5))
103                                 .untilAsserted(() -> TestData.assertExpectedState(user -> requestUserRankingFor(user)));
104         }
105
106         private String requestUserRankingFor(String user)
107         {
108                 try
109                 {
110                         return mockMvc
111                                         .perform(MockMvcRequestBuilders.get("/{user}", user)
112                                                         .contentType(MediaType.APPLICATION_JSON))
113                                         .andExpect(status().isOk())
114                                         .andReturn()
115                                         .getResponse()
116                                         .getContentAsString(StandardCharsets.UTF_8);
117                 }
118                 catch (Exception e)
119                 {
120                         throw new RuntimeException(e);
121                 }
122         }
123
124         @TestConfiguration
125         static class Configuration
126         {
127                 @Primary
128                 @Bean
129                 KeyValueBytesStoreSupplier inMemoryStoreSupplier()
130                 {
131                         return Stores.inMemoryKeyValueStore(STORE_NAME);
132                 }
133         }
134 }