-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlertingIntegrationTest.java
More file actions
159 lines (140 loc) · 7.06 KB
/
AlertingIntegrationTest.java
File metadata and controls
159 lines (140 loc) · 7.06 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package dev.vality.alerting.mayday.integration;
import dev.vality.alerting.mayday.Alert;
import dev.vality.alerting.mayday.AlertConfiguration;
import dev.vality.alerting.mayday.AlertingServiceSrv;
import dev.vality.alerting.mayday.UserAlert;
import dev.vality.alerting.mayday.alertmanager.client.k8s.AlertmanagerClient;
import dev.vality.alerting.mayday.alertmanager.client.k8s.model.AlertmanagerConfig;
import dev.vality.alerting.mayday.alertmanager.service.AlertmanagerService;
import dev.vality.alerting.mayday.alerttemplate.dao.DawayDao;
import dev.vality.alerting.mayday.common.constant.PrometheusRuleAnnotation;
import dev.vality.alerting.mayday.prometheus.client.k8s.PrometheusClient;
import dev.vality.alerting.mayday.prometheus.client.k8s.model.PrometheusRule;
import dev.vality.alerting.mayday.prometheus.service.PrometheusService;
import dev.vality.alerting.mayday.testutil.DawayObjectUtil;
import dev.vality.alerting.mayday.testutil.K8sObjectUtil;
import dev.vality.alerting.mayday.testutil.ThriftObjectUtil;
import org.apache.thrift.TException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
@SpringBootTest
public class AlertingIntegrationTest {
@Autowired
private AlertingServiceSrv.Iface thriftEndpoint;
@Autowired
private PrometheusService prometheusService;
@Autowired
private AlertmanagerService alertmanagerService;
@MockitoBean
private PrometheusClient prometheusClient;
@MockitoBean
private AlertmanagerClient alertmanagerClient;
@MockitoBean
private DawayDao dawayDao;
private AutoCloseable mocks;
private Object[] preparedMocks;
@BeforeEach
public void init() {
mocks = MockitoAnnotations.openMocks(this);
preparedMocks = new Object[]{prometheusClient, alertmanagerClient, dawayDao};
}
@AfterEach
public void clean() throws Exception {
verifyNoMoreInteractions(preparedMocks);
mocks.close();
}
@Test
void createAlert() throws TException {
when(prometheusClient.getPrometheusRule(prometheusService.getPrometheusRuleName()))
.thenReturn(Optional.of(new PrometheusRule()));
when(alertmanagerClient.getAlertmanagerConfig(alertmanagerService.getAlertmanagerConfigName()))
.thenReturn(Optional.of(new AlertmanagerConfig()));
when(dawayDao.getPaymentProviders()).thenReturn(DawayObjectUtil.getTestProviders());
when(dawayDao.getPaymentTerminals()).thenReturn(DawayObjectUtil.getTestTerminals());
when(dawayDao.getShops()).thenReturn(DawayObjectUtil.getTestShops());
when(dawayDao.getCurrencies()).thenReturn(DawayObjectUtil.getTestCurrencies());
var createAlertRequest =
ThriftObjectUtil.testCreatePaymentConversionAlertRequest(getPaymentConversionAlertConfiguration());
thriftEndpoint.createAlert(createAlertRequest);
verify(prometheusClient, times(1)).getPrometheusRule(prometheusService.getPrometheusRuleName());
verify(prometheusClient, times(1))
.addAlertToPrometheusRuleGroup(eq(prometheusService.getPrometheusRuleName()),
eq(createAlertRequest.getUserId()), any());
verify(alertmanagerClient, times(1))
.getAlertmanagerConfig(eq(alertmanagerService.getAlertmanagerConfigName()));
verify(alertmanagerClient, times(1))
.addRouteIfNotExists(eq(alertmanagerService.getAlertmanagerConfigName()), any());
verify(dawayDao, times(2)).getPaymentProviders();
verify(dawayDao, times(2)).getPaymentTerminals();
verify(dawayDao, times(2)).getShops();
verify(dawayDao, times(2)).getCurrencies();
}
@Test
void getUserAlertsEmpty() throws TException {
String userName = UUID.randomUUID().toString();
when(prometheusClient.getPrometheusRule(prometheusService.getPrometheusRuleName()))
.thenReturn(Optional.of(new PrometheusRule()));
when(prometheusClient.getPrometheusRuleGroupAlerts(prometheusService.getPrometheusRuleName(), userName))
.thenReturn(Set.of());
List<UserAlert> userAlerts = thriftEndpoint.getUserAlerts(userName);
assertNotNull(userAlerts);
assertTrue(userAlerts.isEmpty());
verify(prometheusClient, times(1))
.getPrometheusRule(prometheusService.getPrometheusRuleName());
verify(prometheusClient, times(1))
.getPrometheusRuleGroupAlerts(prometheusService.getPrometheusRuleName(), userName);
}
@Test
void getUserAlerts() throws TException {
String userName = UUID.randomUUID().toString();
var testRule = K8sObjectUtil.testPrometheusRule();
when(prometheusClient.getPrometheusRule(prometheusService.getPrometheusRuleName()))
.thenReturn(Optional.of(new PrometheusRule()));
when(prometheusClient.getPrometheusRuleGroupAlerts(prometheusService.getPrometheusRuleName(), userName))
.thenReturn(Set.of(testRule));
List<UserAlert> userAlerts = thriftEndpoint.getUserAlerts(userName);
assertNotNull(userAlerts);
assertEquals(1, userAlerts.size());
UserAlert userAlert = userAlerts.get(0);
assertEquals(testRule.getAlert(), userAlert.getId());
assertEquals(testRule.getAnnotations().get(PrometheusRuleAnnotation.ALERT_NAME), userAlert.getName());
verify(prometheusClient, times(1))
.getPrometheusRule(prometheusService.getPrometheusRuleName());
verify(prometheusClient, times(1))
.getPrometheusRuleGroupAlerts(prometheusService.getPrometheusRuleName(), userName);
}
AlertConfiguration getPaymentConversionAlertConfiguration() throws TException {
List<Alert> alertList = getSupportedAlerts();
AlertConfiguration alertConfiguration =
thriftEndpoint.getAlertConfiguration(alertList.stream()
.filter(alert -> alert.getId().equals("payment_conversion"))
.findFirst()
.orElseThrow().getId());
assertNotNull(alertConfiguration);
assertNotNull(alertConfiguration.getId());
assertNotNull(alertConfiguration.getParameters());
assertFalse(alertConfiguration.getParameters().isEmpty());
return alertConfiguration;
}
private List<Alert> getSupportedAlerts() throws TException {
List<Alert> alertList = thriftEndpoint.getSupportedAlerts();
assertNotNull(alertList);
assertFalse(alertList.isEmpty());
for (Alert alert : alertList) {
assertNotNull(alert.getId());
assertNotNull(alert.getName());
}
return alertList;
}
}