-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Input Validation for Monitor Fields #771
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.commons.utils | ||
|
||
import org.junit.jupiter.api.Assertions.assertFalse | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
|
||
class ValidationHelpers { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: Could you rename the test suite and it file to something like |
||
@Test | ||
fun `test valid names`() { | ||
/* Start with letter or underscore | ||
Followed by letters, numbers, underscore or hyphen | ||
Total length between 4 and 50 characters */ | ||
|
||
assertTrue(isValidName("valid")) | ||
assertTrue(isValidName("Valid_name")) | ||
assertTrue(isValidName("valid_name_123")) | ||
assertTrue(isValidName("_validName")) | ||
assertTrue(isValidName("valid_name-123")) | ||
assertTrue(isValidName("_123_valid_name")) | ||
|
||
// Boundary value tests | ||
assertTrue(isValidName("qwer")) | ||
assertTrue(isValidName("q".repeat(50))) | ||
assertTrue(isValidName("____")) | ||
assertTrue(isValidName("a-b-")) | ||
} | ||
|
||
@Test | ||
fun `test invalid names`() { | ||
// Invalid starting characters | ||
assertFalse(isValidName("123name")) | ||
assertFalse(isValidName("-name")) | ||
assertFalse(isValidName("1name")) | ||
|
||
// Should not have invalid characters | ||
assertFalse(isValidName("invalid@name")) | ||
assertFalse(isValidName("invalid name")) | ||
assertFalse(isValidName("invalid#name")) | ||
assertFalse(isValidName("invalid.name")) | ||
assertFalse(isValidName(" ")) | ||
|
||
// Should not allow special characters other than _ and - | ||
assertFalse(isValidName("name!")) | ||
assertFalse(isValidName("name*")) | ||
assertFalse(isValidName("name$")) | ||
assertFalse(isValidName("name%")) | ||
|
||
// Boundary value tests | ||
assertFalse(isValidName("abc")) | ||
assertFalse(isValidName("a".repeat(51))) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a short doc comment that explains this function validates the doc level monitor DocLevelQuery name specifically? We use the term "query" in many places, so a quick comment can help prevent confusion.
https://github.com/opensearch-project/common-utils/blob/main/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt#L16