|
| 1 | +package dev.vality.alerting.tg.bot; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import dev.vality.alerting.tg.bot.config.AlertBotConfig; |
| 5 | +import dev.vality.alerting.tg.bot.config.properties.AlertmanagerWebhookProperties; |
| 6 | +import dev.vality.alerting.tg.bot.controller.WebhookController; |
| 7 | +import dev.vality.alerting.tg.bot.model.Webhook; |
| 8 | +import dev.vality.alerting.tg.bot.service.AlertBot; |
| 9 | +import lombok.val; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.mockito.ArgumentCaptor; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | +import org.springframework.http.MediaType; |
| 14 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 15 | +import org.springframework.test.context.TestPropertySource; |
| 16 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 17 | + |
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | + |
| 20 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | + |
| 23 | +@SpringBootTest |
| 24 | +@TestPropertySource(properties = { |
| 25 | + "spring.mvc.pathmatch.matching-strategy=ant_path_matcher", |
| 26 | + "bot.token=test", |
| 27 | + "bot.name=vality_alerting_bot", |
| 28 | + "bot.chatId=1", |
| 29 | + "bot.topics.commands=1", |
| 30 | + "bot.topics.errors5xx=2", |
| 31 | + "bot.topics.altpay-conversion=3", |
| 32 | + "bot.topics.failed-machines=4", |
| 33 | + "bot.topics.pending-payments=5" |
| 34 | +}) |
| 35 | +public class WebhookControllerTest { |
| 36 | + |
| 37 | + @MockitoBean |
| 38 | + AlertmanagerWebhookProperties webhookProperties; |
| 39 | + |
| 40 | + @MockitoBean |
| 41 | + AlertBot alertBot; |
| 42 | + |
| 43 | + @MockitoBean |
| 44 | + AlertBotConfig alertBotConfig; |
| 45 | + |
| 46 | + String webhookJson = """ |
| 47 | + { |
| 48 | + "status": "firing", |
| 49 | + "receiver": "telegram", |
| 50 | + "alerts": [ |
| 51 | + { |
| 52 | + "status": "firing", |
| 53 | + "labels": { |
| 54 | + "alertname": "Errors5xxHigh", |
| 55 | + "severity": "critical", |
| 56 | + "job": "payments", |
| 57 | + "namespace": "prod", |
| 58 | + "service": "payments-api", |
| 59 | + "instance": "payments-api-1", |
| 60 | + "pod": "payments-api-1-abc123" |
| 61 | + }, |
| 62 | + "annotations": { |
| 63 | + "summary": "HTTP 5xx rate is too high", |
| 64 | + "description": "Payments API is returning >5% 5xx responses for 5m", |
| 65 | + "runbook_url": "https://runbook.company/alerts/errors5xx" |
| 66 | + } |
| 67 | + }, |
| 68 | + { |
| 69 | + "status": "resolved", |
| 70 | + "labels": { |
| 71 | + "alertname": "AltpayConversionLow", |
| 72 | + "severity": "warning", |
| 73 | + "job": "altpay", |
| 74 | + "namespace": "prod", |
| 75 | + "service": "altpay-conversion", |
| 76 | + "pod": "altpay-0-xzy987" |
| 77 | + }, |
| 78 | + "annotations": { |
| 79 | + "summary": "Altpay conversion dropped", |
| 80 | + "description": "Altpay conversion < 2% in last 10m" |
| 81 | + } |
| 82 | + } |
| 83 | + ] |
| 84 | + } |
| 85 | + """; |
| 86 | + |
| 87 | + @Test |
| 88 | + public void sendTgMessageTest() { |
| 89 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 90 | + WebhookController webhookController = new WebhookController(webhookProperties, objectMapper, alertBot); |
| 91 | + |
| 92 | + MockHttpServletRequest req = new MockHttpServletRequest(); |
| 93 | + req.setMethod("POST"); |
| 94 | + req.setRequestURI("/alertmanager/webhook"); |
| 95 | + req.setContentType(MediaType.APPLICATION_JSON_VALUE); |
| 96 | + req.setCharacterEncoding(StandardCharsets.UTF_8.name()); |
| 97 | + req.setContent(webhookJson.getBytes(StandardCharsets.UTF_8)); |
| 98 | + |
| 99 | + val response = webhookController.processWebhook(req); |
| 100 | + assertThat(response.getStatusCode().value()).isEqualTo(200); |
| 101 | + |
| 102 | + ArgumentCaptor<Webhook> webhookCaptor = ArgumentCaptor.forClass(Webhook.class); |
| 103 | + verify(alertBot).sendAlertMessage(webhookCaptor.capture()); |
| 104 | + Webhook passed = webhookCaptor.getValue(); |
| 105 | + assertThat(passed).isNotNull(); |
| 106 | + assertThat(passed.getAlerts()).isNotNull(); |
| 107 | + } |
| 108 | +} |
0 commit comments