Skip to content
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

Changed the test for RegexValidator class #608

Merged
merged 13 commits into from
Apr 7, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
* {@link ExampleCustomSettingConfig} contains the custom settings value and their static declarations.
*/
public class ExampleCustomSettingConfig {
private static final String FORBIDDEN_REGEX = "forbidden";

private static final String FORBIDDEN_VALUE = "forbidden";
/**
* A string setting. If the string setting matches the FORBIDDEN_REGEX string, the validation will fail.
* A string setting. If the string setting matches the FORBIDDEN_REGEX string and parameter isMatching is true the validation will fail.
*/
public static final Setting<String> VALIDATED_SETTING = Setting.simpleString(
"custom.validate",
new RegexValidator(FORBIDDEN_REGEX),
new RegexValidator(FORBIDDEN_VALUE, false),
Property.NodeScope,
Property.Dynamic
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,17 @@ public void onFailure(Exception e) {
assertEquals("failed to find action [" + UnregisteredAction.INSTANCE + "] to execute", ex.getMessage());
}

@Test
public void testValidatedSettings() {
final String expected = randomAlphaOfLengthBetween(1, 5);
final String expected = "foo";
final String actual = VALIDATED_SETTING.get(Settings.builder().put(VALIDATED_SETTING.getKey(), expected).build());
assertEquals(expected, actual);
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved

final IllegalArgumentException exception = expectThrows(
final IllegalArgumentException exceptionTrue = expectThrows(
IllegalArgumentException.class,
() -> VALIDATED_SETTING.get(Settings.builder().put("custom.validated", "it's forbidden").build())
() -> VALIDATED_SETTING.get(Settings.builder().put(VALIDATED_SETTING.getKey(), "it's forbidden").build())
);
assertEquals("Setting must not contain [forbidden]", exception.getMessage());

assertEquals("Setting [it's forbidden] must match regex [forbidden]", exceptionTrue.getMessage());
}
}