Skip to content

Commit a418f21

Browse files
committed
add third-party component properties example and update the tests
1 parent 735d80d commit a418f21

File tree

6 files changed

+66
-9
lines changed

6 files changed

+66
-9
lines changed

spring-boot/configuration/src/main/java/io/reflectoring/validation/AppConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package io.reflectoring.validation;
22

3+
import io.reflectoring.validation.thirdparty.ThirdPartyComponentProperties;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
35
import org.springframework.boot.context.properties.EnableConfigurationProperties;
46
import org.springframework.context.annotation.Bean;
57
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.validation.annotation.Validated;
69

710
@Configuration
811
@EnableConfigurationProperties(AppProperties.class)
@@ -13,4 +16,11 @@ public static ReportEmailAddressValidator configurationPropertiesValidator() {
1316
return new ReportEmailAddressValidator();
1417
}
1518

19+
@Bean
20+
@Validated
21+
@ConfigurationProperties(prefix = "app.third-party.properties")
22+
public ThirdPartyComponentProperties thirdPartyComponentProperties() {
23+
return new ThirdPartyComponentProperties();
24+
}
25+
1626
}

spring-boot/configuration/src/main/java/io/reflectoring/validation/AppProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import org.springframework.validation.annotation.Validated;
55

66
import javax.validation.Valid;
7-
import javax.validation.constraints.NotEmpty;
7+
import javax.validation.constraints.NotBlank;
88

99
@Validated
1010
@ConfigurationProperties(prefix = "app.properties")
1111
class AppProperties {
1212

13-
@NotEmpty
13+
@NotBlank
1414
private String name;
1515

1616
@Valid
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.reflectoring.validation.thirdparty;
2+
3+
import javax.validation.constraints.NotBlank;
4+
5+
/**
6+
* We assume that this bean comes from another jar file
7+
*/
8+
public class ThirdPartyComponentProperties {
9+
10+
@NotBlank
11+
private String name;
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
}

spring-boot/configuration/src/main/resources/application-validation.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ app.properties.name=Analysis Application
22
app.properties.report.send-emails=true
33
app.properties.report.type=PLAIN_TEXT
44
app.properties.report.interval-in-days=14
5-
app.properties.report.email-address[email protected]
5+
app.properties.report.email-address=[email protected]
6+
# third-party component properties
7+
app.third-party.properties.name=Third Party!
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* We create Spring Application dynamically to catch and test application context startup exceptions
1919
*/
20-
class AppPropertiesInvalidInputTest {
20+
class PropertiesInvalidInputTest {
2121

2222
SpringApplication application;
2323
Properties properties;
@@ -45,7 +45,7 @@ void whenGivenNameEmpty_thenNotEmptyValidationFails() {
4545
.isInstanceOf(ConfigurationPropertiesBindException.class)
4646
.hasRootCauseInstanceOf(BindValidationException.class)
4747
.hasStackTraceContaining("Field error in object 'app.properties' on field 'name'")
48-
.hasStackTraceContaining("[must not be empty]");
48+
.hasStackTraceContaining("[must not be blank]");
4949

5050
}
5151

@@ -101,4 +101,17 @@ void whenGivenReportEmailAddressDoesNotContainAnalysisappDomain_thenCustomEmailV
101101

102102
}
103103

104+
@Test
105+
void whenGivenThirdPartyComponentNameIsEmpty_thenNotEmptyValidationFails() {
106+
107+
properties.put("app.third-party.properties.name", "");
108+
109+
assertThatThrownBy(application::run)
110+
.isInstanceOf(ConfigurationPropertiesBindException.class)
111+
.hasRootCauseInstanceOf(BindValidationException.class)
112+
.hasStackTraceContaining("Field error in object 'app.third-party.properties' on field 'name'")
113+
.hasStackTraceContaining("[must not be blank]");
114+
115+
}
116+
104117
}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.reflectoring.validation;
22

3+
import io.reflectoring.validation.thirdparty.ThirdPartyComponentProperties;
34
import org.junit.jupiter.api.Test;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.boot.test.context.SpringBootTest;
@@ -11,15 +12,19 @@
1112
"app.properties.report.send-emails=true",
1213
"app.properties.report.type=PLAIN_TEXT",
1314
"app.properties.report.interval-in-days=14",
14-
15-
}, classes = {AppConfiguration.class, AppProperties.class, ReportProperties.class})
16-
class AppPropertiesValidInputTest {
15+
16+
"app.third-party.properties.name=Third Party!"
17+
}, classes = {AppConfiguration.class})
18+
class PropertiesValidInputTest {
1719

1820
@Autowired
1921
AppProperties appProperties;
2022

23+
@Autowired
24+
ThirdPartyComponentProperties thirdPartyComponentProperties;
25+
2126
@Test
22-
void propertiesAreLoaded() {
27+
void appPropertiesAreLoaded() {
2328
assertThat(appProperties).isNotNull();
2429
assertThat(appProperties.getName()).isEqualTo("My Test App");
2530
assertThat(appProperties.getReport()).isNotNull();
@@ -29,4 +34,10 @@ void propertiesAreLoaded() {
2934
assertThat(appProperties.getReport().getEmailAddress()).isEqualTo("[email protected]");
3035
}
3136

37+
@Test
38+
void thirdPartyComponentPropertiesAreLoaded() {
39+
assertThat(thirdPartyComponentProperties).isNotNull();
40+
assertThat(thirdPartyComponentProperties.getName()).isEqualTo("Third Party!");
41+
}
42+
3243
}

0 commit comments

Comments
 (0)