@@ -99,6 +99,96 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() {
99
99
assertTrue(" Incorrect search result" , matchingDocsToQuery.contains(" 5|$testIndex " ))
100
100
}
101
101
102
+ fun `test execute monitor yields results in alert action context` () {
103
+
104
+ val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
105
+ val testDoc = """ {
106
+ "message" : "This is an error from IAD region",
107
+ "test_strict_date_time" : "$testTime ",
108
+ "test_field" : "us-west-2"
109
+ }"""
110
+
111
+ val index = createTestIndex()
112
+
113
+ val docQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = " 3" )
114
+ val docLevelInput = DocLevelMonitorInput (" description" , listOf (index), listOf (docQuery))
115
+
116
+ val action = randomAction(template = randomTemplateScript(" {{ctx.results.0}}" ), destinationId = createDestination().id)
117
+ val monitor = randomDocumentLevelMonitor(
118
+ inputs = listOf (docLevelInput),
119
+ triggers = listOf (randomDocumentLevelTrigger(condition = ALWAYS_RUN , actions = listOf (action)))
120
+ )
121
+
122
+ indexDoc(index, " 1" , testDoc)
123
+
124
+ val response = executeMonitor(monitor, params = DRYRUN_MONITOR )
125
+
126
+ val output = entityAsMap(response)
127
+ assertEquals(monitor.name, output[" monitor_name" ])
128
+
129
+ assertEquals(1 , output.objectMap(" trigger_results" ).values.size)
130
+
131
+ for (triggerResult in output.objectMap(" trigger_results" ).values) {
132
+ assertEquals(1 , triggerResult.objectMap(" action_results" ).values.size)
133
+ for (alertActionResult in triggerResult.objectMap(" action_results" ).values) {
134
+ for (actionResult in alertActionResult.values) {
135
+ @Suppress(" UNCHECKED_CAST" ) val actionOutput = (actionResult as Map <String , Map <String , String >>)[" output" ]
136
+ as Map <String , String >
137
+ assertTrue(actionOutput[" subject" ].toString().contains(docQuery.id))
138
+ assertTrue(actionOutput[" message" ].toString().contains(docQuery.id))
139
+ }
140
+ }
141
+ }
142
+ val alerts = searchAlerts(monitor)
143
+ assertEquals(" Alert saved for test monitor" , 0 , alerts.size)
144
+ }
145
+
146
+ fun `test execute monitor yields triggeredDocs in alert action context` () {
147
+
148
+ val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
149
+ val testDoc = """ {
150
+ "message" : "This is an error from IAD region",
151
+ "test_strict_date_time" : "$testTime ",
152
+ "test_field" : "us-west-2"
153
+ }"""
154
+
155
+ val index = createTestIndex()
156
+
157
+ val docQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = " 3" )
158
+ val docLevelInput = DocLevelMonitorInput (" description" , listOf (index), listOf (docQuery))
159
+
160
+ val action = randomAction(template = randomTemplateScript(" {{ctx.triggeredDocs}}" ), destinationId = createDestination().id)
161
+ val monitor = randomDocumentLevelMonitor(
162
+ inputs = listOf (docLevelInput),
163
+ triggers = listOf (randomDocumentLevelTrigger(condition = ALWAYS_RUN , actions = listOf (action)))
164
+ )
165
+
166
+ val id = " 1"
167
+ indexDoc(index, id, testDoc)
168
+
169
+ val response = executeMonitor(monitor, params = DRYRUN_MONITOR )
170
+
171
+ val output = entityAsMap(response)
172
+ assertEquals(monitor.name, output[" monitor_name" ])
173
+
174
+ assertEquals(1 , output.objectMap(" trigger_results" ).values.size)
175
+
176
+ for (triggerResult in output.objectMap(" trigger_results" ).values) {
177
+ assertEquals(1 , triggerResult.objectMap(" action_results" ).values.size)
178
+ for (alertActionResult in triggerResult.objectMap(" action_results" ).values) {
179
+ assertEquals(alertActionResult.values.size, 1 )
180
+ for (actionResult in alertActionResult.values) {
181
+ @Suppress(" UNCHECKED_CAST" ) val actionOutput = (actionResult as Map <String , Map <String , String >>)[" output" ]
182
+ as Map <String , String >
183
+ assertTrue(actionOutput[" subject" ].toString().contains(" 0=$id |$index " ))
184
+ assertTrue(actionOutput[" message" ].toString().contains(" 0=$id |$index " ))
185
+ }
186
+ }
187
+ }
188
+ val alerts = searchAlerts(monitor)
189
+ assertEquals(" Alert saved for test monitor" , 0 , alerts.size)
190
+ }
191
+
102
192
fun `test execute monitor generates alerts and findings` () {
103
193
val testIndex = createTestIndex()
104
194
val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
0 commit comments