diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt index 63a004dd..8bdb4bbb 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt @@ -80,7 +80,7 @@ data class DocLevelQuery( const val QUERY_FIELD_NAMES_FIELD = "query_field_names" const val NO_ID = "" val INVALID_CHARACTERS: List = listOf(" ", "[", "]", "{", "}", "(", ")") - val QUERY_NAME_REGEX = "^.{0,256}$".toRegex() // regex to restrict string length 256 chars + val QUERY_NAME_REGEX = "^.{1,256}$".toRegex() // regex to restrict string length between 1 - 256 chars @JvmStatic @Throws(IOException::class) diff --git a/src/test/kotlin/org/opensearch/commons/alerting/model/DocLevelMonitorInputTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/model/DocLevelMonitorInputTests.kt index 58082798..2df672a6 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/model/DocLevelMonitorInputTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/model/DocLevelMonitorInputTests.kt @@ -44,6 +44,20 @@ class DocLevelMonitorInputTests { @Test fun `test create Doc Level Query with invalid name length`() { val stringBuilder = StringBuilder() + + // test empty string + val emptyString = stringBuilder.toString() + try { + randomDocLevelQuery(name = emptyString) + Assertions.fail("Expecting an illegal argument exception") + } catch (e: IllegalArgumentException) { + Assertions.assertEquals( + "The query name, $emptyString, can be max 256 characters.", + e.message + ) + } + + // test string with 257 chars repeat(257) { stringBuilder.append("a") }