-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookControllerTest.java
More file actions
128 lines (114 loc) · 4.81 KB
/
WebhookControllerTest.java
File metadata and controls
128 lines (114 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package dev.vality.alerting.tg.bot;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.vality.alerting.tg.bot.config.PostgresqlSpringBootITest;
import dev.vality.alerting.tg.bot.config.properties.AlertmanagerWebhookProperties;
import dev.vality.alerting.tg.bot.controller.WebhookController;
import dev.vality.alerting.tg.bot.model.Webhook;
import dev.vality.alerting.tg.bot.pojo.ProviderThread;
import dev.vality.alerting.tg.bot.service.AlertBot;
import lombok.val;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.telegram.telegrambots.longpolling.starter.TelegramBotInitializer;
import org.telegram.telegrambots.meta.generics.TelegramClient;
import java.nio.charset.StandardCharsets;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.verify;
@PostgresqlSpringBootITest
@SpringBootTest
@TestPropertySource(properties = {
"spring.cloud.vault.enabled=false",
"spring.mvc.pathmatch.matching-strategy=ant_path_matcher",
"bot.token=test",
"bot.name=vality_alerting_bot",
"bot.chatId=1",
"bot.threads.commands=1",
"bot.threads.errors5xx=2",
"bot.threads.altpay-conversion=3",
"bot.threads.failed-machines=4",
"bot.threads.pending-payments=5"
})
public class WebhookControllerTest {
@MockitoBean
AlertmanagerWebhookProperties webhookProperties;
@MockitoBean
AlertBot alertBot;
@MockitoBean
TelegramClient telegramClient;
@MockitoBean
TelegramBotInitializer telegramBotInitializer;
@MockitoBean
RowMapper<ProviderThread> rowMapper;
String webhookJson = """
{
"status": "firing",
"receiver": "telegram",
"alerts": [
{
"status": "firing",
"labels": {
"alertname": "FailedMachines",
"provider_id": "323",
"provider_name": "provider test name",
"terminal_id": "32333",
"terminal_name": "terminal test name",
"severity": "critical",
"job": "payments",
"namespace": "prod",
"service": "payments-api",
"instance": "payments-api-1",
"pod": "payments-api-1-abc123"
},
"annotations": {
"summary": "HTTP 5xx rate is too high",
"description": "Payments API is returning >5% 5xx responses for 5m",
"runbook_url": "https://runbook.company/alerts/errors5xx"
}
},
{
"status": "resolved",
"labels": {
"alertname": "AltpayConversionLow",
"provider_id": "323",
"provider_name": "provider test name",
"terminal_id": "32333",
"terminal_name": "terminal test name",
"severity": "warning",
"job": "altpay",
"namespace": "prod",
"service": "altpay-conversion",
"pod": "altpay-0-xzy987"
},
"annotations": {
"summary": "Altpay conversion dropped",
"description": "Altpay conversion < 2% in last 10m"
}
}
]
}
""";
@Test
public void sendTgMessageTest() {
ObjectMapper objectMapper = new ObjectMapper();
WebhookController webhookController = new WebhookController(webhookProperties, objectMapper, alertBot);
MockHttpServletRequest req = new MockHttpServletRequest();
req.setMethod("POST");
req.setRequestURI("/alertmanager/webhook");
req.setContentType(MediaType.APPLICATION_JSON_VALUE);
req.setCharacterEncoding(StandardCharsets.UTF_8.name());
req.setContent(webhookJson.getBytes(StandardCharsets.UTF_8));
val response = webhookController.processWebhook(req);
assertThat(response.getStatusCode().value()).isEqualTo(200);
ArgumentCaptor<Webhook> webhookCaptor = ArgumentCaptor.forClass(Webhook.class);
verify(alertBot).sendAlertMessages(webhookCaptor.capture());
Webhook passed = webhookCaptor.getValue();
assertThat(passed).isNotNull();
assertThat(passed.getAlerts()).isNotNull();
}
}