@@ -24,6 +24,7 @@ import org.opensearch.alerting.action.SearchMonitorAction
24
24
import org.opensearch.alerting.action.SearchMonitorRequest
25
25
import org.opensearch.alerting.alerts.AlertIndices
26
26
import org.opensearch.alerting.core.ScheduledJobIndices
27
+ import org.opensearch.alerting.model.DocumentLevelTriggerRunResult
27
28
import org.opensearch.alerting.transport.AlertingSingleNodeTestCase
28
29
import org.opensearch.alerting.util.DocLevelMonitorQueries
29
30
import org.opensearch.alerting.util.DocLevelMonitorQueries.Companion.INDEX_PATTERN_SUFFIX
@@ -808,6 +809,47 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
808
809
}
809
810
}
810
811
812
+ fun `test execute monitor without create when no monitors exists` () {
813
+ val docQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = " 3" )
814
+ val docLevelInput = DocLevelMonitorInput (" description" , listOf (index), listOf (docQuery))
815
+ val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN )
816
+ val customQueryIndex = " custom_alerts_index"
817
+ val analyzer = " whitespace"
818
+ var monitor = randomDocumentLevelMonitor(
819
+ inputs = listOf (docLevelInput),
820
+ triggers = listOf (trigger),
821
+ dataSources = DataSources (
822
+ queryIndex = customQueryIndex,
823
+ queryIndexMappingsByType = mapOf (Pair (" text" , mapOf (Pair (" analyzer" , analyzer)))),
824
+ )
825
+ )
826
+ var executeMonitorResponse = executeMonitor(monitor, null )
827
+ val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
828
+ val testDoc = """ {
829
+ "message" : "This is an error from IAD region",
830
+ "test_strict_date_time" : "$testTime ",
831
+ "test_field" : "us-west-2"
832
+ }"""
833
+
834
+ assertIndexNotExists(SCHEDULED_JOBS_INDEX )
835
+
836
+ val createMonitorResponse = createMonitor(monitor)
837
+
838
+ assertIndexExists(SCHEDULED_JOBS_INDEX )
839
+
840
+ indexDoc(index, " 1" , testDoc)
841
+
842
+ executeMonitorResponse = executeMonitor(monitor, createMonitorResponse?.id, dryRun = false )
843
+
844
+ Assert .assertEquals(executeMonitorResponse!! .monitorRunResult.monitorName, monitor.name)
845
+ Assert .assertEquals(executeMonitorResponse.monitorRunResult.triggerResults.size, 1 )
846
+ Assert .assertEquals(
847
+ (executeMonitorResponse.monitorRunResult.triggerResults.iterator().next().value as DocumentLevelTriggerRunResult )
848
+ .triggeredDocs.size,
849
+ 1
850
+ )
851
+ }
852
+
811
853
fun `test execute monitor with custom query index and custom field mappings` () {
812
854
val docQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = " 3" )
813
855
val docLevelInput = DocLevelMonitorInput (" description" , listOf (index), listOf (docQuery))
0 commit comments