|
| 1 | +package dev.vality.alerting.tg.bot; |
| 2 | + |
| 3 | +import dev.vality.alerting.tg.bot.config.properties.AlertmanagerWebhookProperties; |
| 4 | +import dev.vality.alerting.tg.bot.controller.WebhookController; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 8 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 9 | +import org.springframework.boot.test.context.TestConfiguration; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.context.annotation.Import; |
| 12 | +import org.springframework.http.MediaType; |
| 13 | +import org.springframework.lang.Nullable; |
| 14 | +import org.springframework.test.context.TestPropertySource; |
| 15 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 16 | +import org.springframework.test.web.servlet.MockMvc; |
| 17 | +import org.springframework.web.servlet.HandlerInterceptor; |
| 18 | +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| 19 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 20 | + |
| 21 | +import jakarta.servlet.http.HttpServletRequest; |
| 22 | +import jakarta.servlet.http.HttpServletResponse; |
| 23 | +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
| 24 | + |
| 25 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 26 | +import static org.mockito.ArgumentMatchers.argThat; |
| 27 | +import static org.mockito.Mockito.times; |
| 28 | +import static org.mockito.Mockito.verify; |
| 29 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 30 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 31 | + |
| 32 | +@WebMvcTest(WebhookController.class) |
| 33 | +@Import(WebhookControllerWithInterceptorTest.TestMvcConfig.class) |
| 34 | +@AutoConfigureMockMvc(addFilters = false) |
| 35 | +@TestPropertySource(properties = { |
| 36 | + "spring.mvc.pathmatch.matching-strategy=ant_path_matcher", |
| 37 | + "bot.token=test", |
| 38 | + "bot.name=vality_alerting_bot", |
| 39 | + "bot.chatId=1", |
| 40 | + "bot.topics.commands=1", |
| 41 | + "bot.topics.errors5xx=2", |
| 42 | + "bot.topics.altpay-conversion=3", |
| 43 | + "bot.topics.failed-machines=4", |
| 44 | + "bot.topics.pending-payments=5" |
| 45 | +}) |
| 46 | +class WebhookControllerWithInterceptorTest { |
| 47 | + |
| 48 | + @Autowired MockMvc mvc; |
| 49 | + |
| 50 | + @MockitoBean |
| 51 | + TelegramSender telegramSender; |
| 52 | + |
| 53 | + @MockitoBean |
| 54 | + AlertmanagerWebhookProperties alertmanagerWebhookProperties; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + RequestMappingHandlerMapping mappings; |
| 58 | + |
| 59 | + @Test |
| 60 | + void mappingExists_sanityCheck() { |
| 61 | + var exists = mappings.getHandlerMethods().keySet().stream() |
| 62 | + .anyMatch(i -> i.getPatternsCondition().getPatterns().stream() |
| 63 | + .anyMatch(p -> p.equals("/alertmanager/webhook"))); |
| 64 | + assertThat(exists) |
| 65 | + .as("Контроллер смапил /alertmanager/webhook") |
| 66 | + .isTrue(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void whenWebhookPosted_thenTestInterceptorInvokesTelegramSender() throws Exception { |
| 71 | + String json = """ |
| 72 | + { "status":"firing", "alerts":[ { "status":"firing" } ] } |
| 73 | + """; |
| 74 | + |
| 75 | + mvc.perform(post("/alertmanager/webhook") |
| 76 | + .contentType(MediaType.APPLICATION_JSON) |
| 77 | + .content(json)) |
| 78 | + .andExpect(status().isOk()); |
| 79 | + |
| 80 | + verify(telegramSender, times(1)) |
| 81 | + .send(argThat(s -> s.contains("webhook"))); |
| 82 | + } |
| 83 | + |
| 84 | + @TestConfiguration |
| 85 | + static class TestMvcConfig implements WebMvcConfigurer { |
| 86 | + |
| 87 | + @Autowired TelegramSender telegramSender; |
| 88 | + |
| 89 | + @Bean |
| 90 | + HandlerInterceptor webhookNotifyInterceptor() { |
| 91 | + return new HandlerInterceptor() { |
| 92 | + @Override |
| 93 | + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) { |
| 94 | + if (request.getRequestURI().equals("/alertmanager/webhook") && response.getStatus() == 200) { |
| 95 | + telegramSender.send("alertmanager webhook handled"); |
| 96 | + } |
| 97 | + } |
| 98 | + }; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void addInterceptors(InterceptorRegistry registry) { |
| 103 | + registry.addInterceptor(webhookNotifyInterceptor()) |
| 104 | + .addPathPatterns("/alertmanager/webhook"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + interface TelegramSender { |
| 109 | + void send(String text); |
| 110 | + } |
| 111 | +} |
0 commit comments