@@ -21,6 +21,7 @@ import org.opensearch.script.Script
21
21
import java.time.ZonedDateTime
22
22
import java.time.format.DateTimeFormatter
23
23
import java.time.temporal.ChronoUnit.MILLIS
24
+ import java.util.Locale
24
25
25
26
class DocumentMonitorRunnerIT : AlertingRestTestCase () {
26
27
@@ -343,20 +344,67 @@ class DocumentMonitorRunnerIT : AlertingRestTestCase() {
343
344
assertTrue(" Findings saved for test monitor" , findings[1 ].relatedDocIds.contains(" 5" ))
344
345
}
345
346
346
- fun `test execute monitor with wildcard index that generates alerts and findings` () {
347
- val testIndex = createTestIndex(" test1" )
348
- val testIndex2 = createTestIndex(" test2" )
347
+ fun `test execute monitor with wildcard index that generates alerts and findings for EQUALS query operator` () {
348
+ val testIndexPrefix = " test-index-${randomAlphaOfLength(10 ).lowercase(Locale .ROOT )} "
349
+ val testQueryName = " wildcard-test-query"
350
+ val testIndex = createTestIndex(" ${testIndexPrefix} 1" )
351
+ val testIndex2 = createTestIndex(" ${testIndexPrefix} 2" )
352
+
349
353
val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
350
354
val testDoc = """ {
351
355
"message" : "This is an error from IAD region",
352
356
"test_strict_date_time" : "$testTime ",
353
357
"test_field" : "us-west-2"
354
358
}"""
355
359
356
- val docQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = " 3 " )
357
- val docLevelInput = DocLevelMonitorInput (" description" , listOf (" test *" ), listOf (docQuery))
360
+ val docQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = testQueryName )
361
+ val docLevelInput = DocLevelMonitorInput (" description" , listOf (" $testIndexPrefix *" ), listOf (docQuery))
358
362
359
- val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN )
363
+ val trigger = randomDocumentLevelTrigger(condition = Script (" query[name=$testQueryName ]" ))
364
+ val monitor = createMonitor(randomDocumentLevelMonitor(inputs = listOf (docLevelInput), triggers = listOf (trigger)))
365
+ assertNotNull(monitor.id)
366
+
367
+ indexDoc(testIndex, " 1" , testDoc)
368
+ indexDoc(testIndex2, " 5" , testDoc)
369
+
370
+ val response = executeMonitor(monitor.id)
371
+
372
+ val output = entityAsMap(response)
373
+
374
+ assertEquals(monitor.name, output[" monitor_name" ])
375
+ @Suppress(" UNCHECKED_CAST" )
376
+ val searchResult = (output.objectMap(" input_results" )[" results" ] as List <Map <String , Any >>).first()
377
+ @Suppress(" UNCHECKED_CAST" )
378
+ val matchingDocsToQuery = searchResult[docQuery.id] as List <String >
379
+ assertEquals(" Incorrect search result" , 2 , matchingDocsToQuery.size)
380
+ assertTrue(" Incorrect search result" , matchingDocsToQuery.containsAll(listOf (" 1|$testIndex " , " 5|$testIndex2 " )))
381
+
382
+ val alerts = searchAlertsWithFilter(monitor)
383
+ assertEquals(" Alert saved for test monitor" , 2 , alerts.size)
384
+
385
+ val findings = searchFindings(monitor)
386
+ assertEquals(" Findings saved for test monitor" , 2 , findings.size)
387
+ val foundFindings = findings.filter { it.relatedDocIds.contains(" 1" ) || it.relatedDocIds.contains(" 5" ) }
388
+ assertEquals(" Didn't find findings for docs 1 and 5" , 2 , foundFindings.size)
389
+ }
390
+
391
+ fun `test execute monitor with wildcard index that generates alerts and findings for NOT EQUALS query operator` () {
392
+ val testIndexPrefix = " test-index-${randomAlphaOfLength(10 ).lowercase(Locale .ROOT )} "
393
+ val testQueryName = " wildcard-test-query"
394
+ val testIndex = createTestIndex(" ${testIndexPrefix} 1" )
395
+ val testIndex2 = createTestIndex(" ${testIndexPrefix} 2" )
396
+
397
+ val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
398
+ val testDoc = """ {
399
+ "message" : "This is an error from IAD region",
400
+ "test_strict_date_time" : "$testTime ",
401
+ "test_field" : "us-west-2"
402
+ }"""
403
+
404
+ val docQuery = DocLevelQuery (query = " NOT (test_field:\" us-west-1\" )" , name = testQueryName)
405
+ val docLevelInput = DocLevelMonitorInput (" description" , listOf (" $testIndexPrefix *" ), listOf (docQuery))
406
+
407
+ val trigger = randomDocumentLevelTrigger(condition = Script (" query[name=$testQueryName ]" ))
360
408
val monitor = createMonitor(randomDocumentLevelMonitor(inputs = listOf (docLevelInput), triggers = listOf (trigger)))
361
409
assertNotNull(monitor.id)
362
410
0 commit comments