add a sample custom validator for AppProperties and add tests
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package io.reflectoring.validation;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.Valid;
|
||||
@@ -8,7 +10,7 @@ import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Validated
|
||||
@ConfigurationProperties(prefix = "app.properties")
|
||||
class AppProperties {
|
||||
class AppProperties implements Validator {
|
||||
|
||||
@NotBlank
|
||||
private String name;
|
||||
@@ -16,6 +18,23 @@ class AppProperties {
|
||||
@Valid
|
||||
private ReportProperties report;
|
||||
|
||||
private static final String APP_BASE_NAME = "Application";
|
||||
|
||||
public boolean supports(Class clazz) {
|
||||
return AppProperties.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
public void validate(Object target, Errors errors) {
|
||||
|
||||
AppProperties appProperties = (AppProperties) target;
|
||||
if (!appProperties.getName().endsWith(APP_BASE_NAME)) {
|
||||
errors.rejectValue("name", "field.name.malformed",
|
||||
new Object[]{APP_BASE_NAME},
|
||||
"The application name must contain [" + APP_BASE_NAME + "] base name");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ class PropertiesInvalidInputTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGivenNameEmpty_thenNotEmptyValidationFails() {
|
||||
void whenGivenNameEmpty_thenNotBlankValidationFails() {
|
||||
|
||||
properties.put("app.properties.name", "");
|
||||
|
||||
@@ -49,6 +49,19 @@ class PropertiesInvalidInputTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGivenNameDoesNotContainBaseName_thenCustomAppPropertiesValidatorFails() {
|
||||
|
||||
properties.put("app.properties.name", "My App");
|
||||
|
||||
assertThatThrownBy(application::run)
|
||||
.isInstanceOf(ConfigurationPropertiesBindException.class)
|
||||
.hasRootCauseInstanceOf(BindValidationException.class)
|
||||
.hasStackTraceContaining("Field error in object 'app.properties' on field 'name'")
|
||||
.hasStackTraceContaining("[The application name must contain [Application] base name]");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGivenReportIntervalInDaysMoreThan30_thenMaxValidationFails() {
|
||||
|
||||
@@ -102,7 +115,7 @@ class PropertiesInvalidInputTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGivenThirdPartyComponentNameIsEmpty_thenNotEmptyValidationFails() {
|
||||
void whenGivenThirdPartyComponentNameIsEmpty_thenNotBlankValidationFails() {
|
||||
|
||||
properties.put("app.third-party.properties.name", "");
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest(properties = {
|
||||
"app.properties.name=My Test App",
|
||||
"app.properties.name=My Test Application",
|
||||
"app.properties.report.send-emails=true",
|
||||
"app.properties.report.type=PLAIN_TEXT",
|
||||
"app.properties.report.interval-in-days=14",
|
||||
@@ -26,7 +26,7 @@ class PropertiesValidInputTest {
|
||||
@Test
|
||||
void appPropertiesAreLoaded() {
|
||||
assertThat(appProperties).isNotNull();
|
||||
assertThat(appProperties.getName()).isEqualTo("My Test App");
|
||||
assertThat(appProperties.getName()).isEqualTo("My Test Application");
|
||||
assertThat(appProperties.getReport()).isNotNull();
|
||||
assertThat(appProperties.getReport().getSendEmails()).isTrue();
|
||||
assertThat(appProperties.getReport().getType()).isEqualTo(ReportType.PLAIN_TEXT);
|
||||
|
||||
Reference in New Issue
Block a user