forked from opensearch-project/common-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlertTests.kt
84 lines (73 loc) · 4.59 KB
/
AlertTests.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
package org.opensearch.commons.alerting
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.opensearch.commons.alerting.model.Alert
import java.time.Instant
import java.time.temporal.ChronoUnit
class AlertTests {
@Test
fun `test alert as template args`() {
val alert = randomAlert().copy(acknowledgedTime = null, lastNotificationTime = null)
val templateArgs = alert.asTemplateArg()
assertEquals(templateArgs[Alert.ALERT_ID_FIELD], alert.id, "Template args id does not match")
assertEquals(templateArgs[Alert.ALERT_VERSION_FIELD], alert.version, "Template args version does not match")
assertEquals(templateArgs[Alert.STATE_FIELD], alert.state.toString(), "Template args state does not match")
assertEquals(templateArgs[Alert.ERROR_MESSAGE_FIELD], alert.errorMessage, "Template args error message does not match")
assertEquals(templateArgs[Alert.ACKNOWLEDGED_TIME_FIELD], null, "Template args acknowledged time does not match")
assertEquals(templateArgs[Alert.END_TIME_FIELD], alert.endTime?.toEpochMilli(), "Template args end time does not")
assertEquals(templateArgs[Alert.START_TIME_FIELD], alert.startTime.toEpochMilli(), "Template args start time does not")
assertEquals(templateArgs[Alert.LAST_NOTIFICATION_TIME_FIELD], null, "Template args last notification time does not match")
assertEquals(templateArgs[Alert.SEVERITY_FIELD], alert.severity, "Template args severity does not match")
assertEquals(templateArgs[Alert.CLUSTERS_FIELD], alert.clusters?.joinToString(","), "Template args clusters does not match")
}
@Test
fun `test agg alert as template args`() {
val alert = randomAlertWithAggregationResultBucket().copy(acknowledgedTime = null, lastNotificationTime = null)
val templateArgs = alert.asTemplateArg()
assertEquals(templateArgs[Alert.ALERT_ID_FIELD], alert.id, "Template args id does not match")
assertEquals(templateArgs[Alert.ALERT_VERSION_FIELD], alert.version, "Template args version does not match")
assertEquals(templateArgs[Alert.STATE_FIELD], alert.state.toString(), "Template args state does not match")
assertEquals(templateArgs[Alert.ERROR_MESSAGE_FIELD], alert.errorMessage, "Template args error message does not match")
assertEquals(templateArgs[Alert.ACKNOWLEDGED_TIME_FIELD], null, "Template args acknowledged time does not match")
assertEquals(templateArgs[Alert.END_TIME_FIELD], alert.endTime?.toEpochMilli(), "Template args end time does not")
assertEquals(templateArgs[Alert.START_TIME_FIELD], alert.startTime.toEpochMilli(), "Template args start time does not")
assertEquals(templateArgs[Alert.LAST_NOTIFICATION_TIME_FIELD], null, "Template args last notification time does not match")
assertEquals(templateArgs[Alert.SEVERITY_FIELD], alert.severity, "Template args severity does not match")
assertEquals(templateArgs[Alert.CLUSTERS_FIELD], alert.clusters?.joinToString(","), "Template args clusters does not match")
assertEquals(
templateArgs[Alert.BUCKET_KEYS],
alert.aggregationResultBucket?.bucketKeys?.joinToString(","),
"Template args bucketKeys do not match"
)
assertEquals(
templateArgs[Alert.PARENTS_BUCKET_PATH],
alert.aggregationResultBucket?.parentBucketPath,
"Template args parentBucketPath does not match",
)
}
@Test
fun `test alert acknowledged`() {
val ackAlert = randomAlert().copy(state = Alert.State.ACKNOWLEDGED)
Assertions.assertTrue(ackAlert.isAcknowledged(), "Alert is not acknowledged")
val activeAlert = randomAlert().copy(state = Alert.State.ACTIVE)
Assertions.assertFalse(activeAlert.isAcknowledged(), "Alert is acknowledged")
}
@Test
fun `test alert in audit state`() {
val auditAlert = Alert(
randomQueryLevelMonitor(), randomQueryLevelTrigger(), Instant.now().truncatedTo(ChronoUnit.MILLIS),
null, actionExecutionResults = listOf(randomActionExecutionResult())
)
Assertions.assertFalse(auditAlert.isAcknowledged(), "Alert should not be in acknowledged state")
}
@Test
fun `test chained alert`() {
val workflow = randomWorkflow()
val trigger = randomChainedAlertTrigger()
val alert = randomChainedAlert(workflow = workflow, trigger = trigger)
assertEquals(alert.monitorId, "")
assertEquals(alert.id, "")
assertEquals(workflow.id, alert.workflowId)
}
}