-
Notifications
You must be signed in to change notification settings - Fork 93
/
AlertingPluginInterfaceTests.kt
309 lines (274 loc) · 14.3 KB
/
AlertingPluginInterfaceTests.kt
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package org.opensearch.commons.alerting
import com.nhaarman.mockitokotlin2.whenever
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Answers
import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.mock
import org.mockito.junit.jupiter.MockitoExtension
import org.opensearch.action.ActionType
import org.opensearch.action.search.SearchResponse
import org.opensearch.client.node.NodeClient
import org.opensearch.common.settings.Settings
import org.opensearch.commons.alerting.action.AcknowledgeAlertRequest
import org.opensearch.commons.alerting.action.AcknowledgeAlertResponse
import org.opensearch.commons.alerting.action.AcknowledgeChainedAlertRequest
import org.opensearch.commons.alerting.action.DeleteMonitorRequest
import org.opensearch.commons.alerting.action.DeleteMonitorResponse
import org.opensearch.commons.alerting.action.DeleteWorkflowRequest
import org.opensearch.commons.alerting.action.DeleteWorkflowResponse
import org.opensearch.commons.alerting.action.GetAlertsRequest
import org.opensearch.commons.alerting.action.GetAlertsResponse
import org.opensearch.commons.alerting.action.GetFindingsRequest
import org.opensearch.commons.alerting.action.GetFindingsResponse
import org.opensearch.commons.alerting.action.GetMonitorRequest
import org.opensearch.commons.alerting.action.GetMonitorResponse
import org.opensearch.commons.alerting.action.GetWorkflowAlertsRequest
import org.opensearch.commons.alerting.action.GetWorkflowAlertsResponse
import org.opensearch.commons.alerting.action.GetWorkflowRequest
import org.opensearch.commons.alerting.action.GetWorkflowResponse
import org.opensearch.commons.alerting.action.IndexMonitorRequest
import org.opensearch.commons.alerting.action.IndexMonitorResponse
import org.opensearch.commons.alerting.action.IndexWorkflowRequest
import org.opensearch.commons.alerting.action.IndexWorkflowResponse
import org.opensearch.commons.alerting.action.PublishFindingsRequest
import org.opensearch.commons.alerting.action.SearchMonitorRequest
import org.opensearch.commons.alerting.action.SubscribeFindingsResponse
import org.opensearch.commons.alerting.model.FindingDocument
import org.opensearch.commons.alerting.model.FindingWithDocs
import org.opensearch.commons.alerting.model.Monitor
import org.opensearch.commons.alerting.model.Workflow
import org.opensearch.core.action.ActionListener
import org.opensearch.core.common.io.stream.NamedWriteableRegistry
import org.opensearch.core.rest.RestStatus
import org.opensearch.index.seqno.SequenceNumbers
import org.opensearch.search.SearchModule
@Suppress("UNCHECKED_CAST")
@ExtendWith(MockitoExtension::class)
internal class AlertingPluginInterfaceTests {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private lateinit var client: NodeClient
@Test
fun indexMonitor() {
val monitor = randomQueryLevelMonitor()
val request = mock(IndexMonitorRequest::class.java)
val response = IndexMonitorResponse(
Monitor.NO_ID,
Monitor.NO_VERSION,
SequenceNumbers.UNASSIGNED_SEQ_NO,
SequenceNumbers.UNASSIGNED_PRIMARY_TERM,
monitor
)
val listener: ActionListener<IndexMonitorResponse> =
mock(ActionListener::class.java) as ActionListener<IndexMonitorResponse>
val namedWriteableRegistry = NamedWriteableRegistry(SearchModule(Settings.EMPTY, emptyList()).namedWriteables)
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<IndexMonitorResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.indexMonitor(client, request, namedWriteableRegistry, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
@Test
fun indexWorkflow() {
val workflow = randomWorkflow()
val request = mock(IndexWorkflowRequest::class.java)
val response = IndexWorkflowResponse(
Workflow.NO_ID,
Workflow.NO_VERSION,
SequenceNumbers.UNASSIGNED_SEQ_NO,
SequenceNumbers.UNASSIGNED_PRIMARY_TERM,
workflow
)
val listener: ActionListener<IndexWorkflowResponse> =
mock(ActionListener::class.java) as ActionListener<IndexWorkflowResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<IndexWorkflowResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.indexWorkflow(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
@Test
fun indexBucketMonitor() {
val monitor = randomBucketLevelMonitor()
val request = mock(IndexMonitorRequest::class.java)
val response = IndexMonitorResponse(
Monitor.NO_ID,
Monitor.NO_VERSION,
SequenceNumbers.UNASSIGNED_SEQ_NO,
SequenceNumbers.UNASSIGNED_PRIMARY_TERM,
monitor
)
val listener: ActionListener<IndexMonitorResponse> =
mock(ActionListener::class.java) as ActionListener<IndexMonitorResponse>
val namedWriteableRegistry = NamedWriteableRegistry(SearchModule(Settings.EMPTY, emptyList()).namedWriteables)
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<IndexMonitorResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.indexMonitor(client, request, namedWriteableRegistry, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
@Test
fun deleteMonitor() {
val request = mock(DeleteMonitorRequest::class.java)
val response = DeleteMonitorResponse(Monitor.NO_ID, Monitor.NO_VERSION)
val listener: ActionListener<DeleteMonitorResponse> =
mock(ActionListener::class.java) as ActionListener<DeleteMonitorResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<DeleteMonitorResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.deleteMonitor(client, request, listener)
}
@Test
fun deleteWorkflow() {
val request = mock(DeleteWorkflowRequest::class.java)
val response = DeleteWorkflowResponse(Workflow.NO_ID, Workflow.NO_VERSION)
val listener: ActionListener<DeleteWorkflowResponse> =
mock(ActionListener::class.java) as ActionListener<DeleteWorkflowResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<DeleteWorkflowResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.deleteWorkflow(client, request, listener)
}
@Test
fun getWorkflow() {
val request = mock(GetWorkflowRequest::class.java)
val response = GetWorkflowResponse(
id = "id",
version = 1,
seqNo = 1,
primaryTerm = 1,
status = RestStatus.OK,
workflow = randomWorkflow()
)
val listener: ActionListener<GetWorkflowResponse> =
mock(ActionListener::class.java) as ActionListener<GetWorkflowResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<GetWorkflowResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.getWorkflow(client, request, listener)
}
@Test
fun getAlerts() {
val monitor = randomQueryLevelMonitor()
val alert = randomAlert(monitor)
val request = mock(GetAlertsRequest::class.java)
val response = GetAlertsResponse(listOf(alert), 1)
val listener: ActionListener<GetAlertsResponse> =
mock(ActionListener::class.java) as ActionListener<GetAlertsResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<GetAlertsResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.getAlerts(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
@Test
fun getWorkflowAlerts() {
val request = mock(GetWorkflowAlertsRequest::class.java)
val response = GetWorkflowAlertsResponse(listOf(randomChainedAlert()), emptyList(), 1)
val listener: ActionListener<GetWorkflowAlertsResponse> =
mock(ActionListener::class.java) as ActionListener<GetWorkflowAlertsResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<GetWorkflowAlertsResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.getWorkflowAlerts(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
@Test
fun getFindings() {
val finding = randomFinding()
val documentIds = finding.relatedDocIds
val relatedDocs = mutableListOf<FindingDocument>()
val request = mock(GetFindingsRequest::class.java)
val documents: Map<String, FindingDocument> = mutableMapOf()
for (docId in documentIds) {
val key = "${finding.index}|$docId"
documents[key]?.let { document -> relatedDocs.add(document) }
}
val findingWithDocs = FindingWithDocs(finding, relatedDocs)
val response = GetFindingsResponse(RestStatus.OK, 1, listOf(findingWithDocs))
val listener: ActionListener<GetFindingsResponse> =
mock(ActionListener::class.java) as ActionListener<GetFindingsResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<GetFindingsResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.getFindings(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
@Test
fun publishFindings() {
val request = mock(PublishFindingsRequest::class.java)
val response = SubscribeFindingsResponse(status = RestStatus.OK)
val listener: ActionListener<SubscribeFindingsResponse> =
mock(ActionListener::class.java) as ActionListener<SubscribeFindingsResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<SubscribeFindingsResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.publishFinding(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
@Test
fun acknowledgeAlerts() {
val request = mock(AcknowledgeAlertRequest::class.java)
val response = AcknowledgeAlertResponse(acknowledged = listOf(), failed = listOf(), missing = listOf())
val listener: ActionListener<AcknowledgeAlertResponse> =
mock(ActionListener::class.java) as ActionListener<AcknowledgeAlertResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<AcknowledgeAlertResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.acknowledgeAlerts(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
@Test
fun acknowledgeChainedAlerts() {
val request = mock(AcknowledgeChainedAlertRequest::class.java)
val response = AcknowledgeAlertResponse(acknowledged = listOf(), failed = listOf(), missing = listOf())
val listener: ActionListener<AcknowledgeAlertResponse> =
mock(ActionListener::class.java) as ActionListener<AcknowledgeAlertResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<AcknowledgeAlertResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.acknowledgeChainedAlerts(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
@Test
fun getMonitor() {
val request = mock(GetMonitorRequest::class.java)
val response = GetMonitorResponse("test-id", 1, 1, 1, null, null)
val listener: ActionListener<GetMonitorResponse> =
mock(ActionListener::class.java) as ActionListener<GetMonitorResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<GetMonitorResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.getMonitor(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
@Test
fun searchMonitors() {
val request = mock(SearchMonitorRequest::class.java)
val response = mock(SearchResponse::class.java)
val listener: ActionListener<SearchResponse> =
mock(ActionListener::class.java) as ActionListener<SearchResponse>
Mockito.doAnswer {
(it.getArgument(2) as ActionListener<SearchResponse>)
.onResponse(response)
}.whenever(client).execute(Mockito.any(ActionType::class.java), Mockito.any(), Mockito.any())
AlertingPluginInterface.searchMonitors(client, request, listener)
Mockito.verify(listener, Mockito.times(1)).onResponse(ArgumentMatchers.eq(response))
}
}