@@ -26,6 +26,8 @@ import org.opensearch.alerting.randomAnomalyDetector
26
26
import org.opensearch.alerting.randomAnomalyDetectorWithUser
27
27
import org.opensearch.alerting.randomBucketLevelMonitor
28
28
import org.opensearch.alerting.randomBucketLevelTrigger
29
+ import org.opensearch.alerting.randomDocLevelMonitorInput
30
+ import org.opensearch.alerting.randomDocLevelQuery
29
31
import org.opensearch.alerting.randomDocumentLevelMonitor
30
32
import org.opensearch.alerting.randomDocumentLevelTrigger
31
33
import org.opensearch.alerting.randomQueryLevelMonitor
@@ -1285,6 +1287,48 @@ class MonitorRestApiIT : AlertingRestTestCase() {
1285
1287
}
1286
1288
}
1287
1289
1290
+ fun `test creating and updating a document monitor with invalid query name` () {
1291
+ // creating a monitor with an invalid query name
1292
+ val invalidQueryName = " Invalid @ query ! name"
1293
+ val queries = listOf (randomDocLevelQuery(name = invalidQueryName))
1294
+ val randomDocLevelMonitorInput = randomDocLevelMonitorInput(queries = queries)
1295
+ val inputs = listOf (randomDocLevelMonitorInput)
1296
+ val trigger = randomDocumentLevelTrigger()
1297
+ var monitor = randomDocumentLevelMonitor(inputs = inputs, triggers = listOf (trigger))
1298
+
1299
+ try {
1300
+ client().makeRequest(" POST" , ALERTING_BASE_URI , emptyMap(), monitor.toHttpEntity())
1301
+ fail(" Doc level monitor with invalid query name should be rejected" )
1302
+ } catch (e: ResponseException ) {
1303
+ assertEquals(" Unexpected status" , RestStatus .BAD_REQUEST , e.response.restStatus())
1304
+ val expectedMessage = " Doc level query name, $invalidQueryName , may only contain alphanumeric values"
1305
+ e.message?.let { assertTrue(it.contains(expectedMessage)) }
1306
+ }
1307
+
1308
+ // successfully creating monitor with valid query name
1309
+ val testIndex = createTestIndex()
1310
+ val docQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = " valid name" , fields = listOf ())
1311
+ val docLevelInput = DocLevelMonitorInput (" description" , listOf (testIndex), listOf (docQuery))
1312
+
1313
+ monitor = createMonitor(randomDocumentLevelMonitor(inputs = listOf (docLevelInput), triggers = listOf (trigger)))
1314
+
1315
+ // updating monitor with invalid query name
1316
+ val updatedDocQuery = DocLevelQuery (query = " test_field:\" us-west-2\" " , name = invalidQueryName, fields = listOf ())
1317
+ val updatedDocLevelInput = DocLevelMonitorInput (" description" , listOf (testIndex), listOf (updatedDocQuery))
1318
+
1319
+ try {
1320
+ client().makeRequest(
1321
+ " PUT" , monitor.relativeUrl(),
1322
+ emptyMap(), monitor.copy(inputs = listOf (updatedDocLevelInput)).toHttpEntity()
1323
+ )
1324
+ fail(" Doc level monitor with invalid query name should be rejected" )
1325
+ } catch (e: ResponseException ) {
1326
+ assertEquals(" Unexpected status" , RestStatus .BAD_REQUEST , e.response.restStatus())
1327
+ val expectedMessage = " Doc level query name, $invalidQueryName , may only contain alphanumeric values"
1328
+ e.message?.let { assertTrue(it.contains(expectedMessage)) }
1329
+ }
1330
+ }
1331
+
1288
1332
/* *
1289
1333
* This use case is needed by the frontend plugin for displaying alert counts on the Monitors list page.
1290
1334
* https://github.com/opensearch-project/alerting-dashboards-plugin/blob/main/server/services/MonitorService.js#L235
0 commit comments