From 8ccec9a413ee3e36eb73d0d1e2294596b2b3681f Mon Sep 17 00:00:00 2001 From: pcoates33 Date: Sun, 27 Jan 2019 06:55:25 +0000 Subject: [PATCH] BAEL-2510 Add nested properties section (#6212) Add nested properties section to ConfigurationProperties article. --- .../baeldung/properties/ConfigProperties.java | 62 +++++-------------- .../org/baeldung/properties/Credentials.java | 37 +++++++++++ .../src/main/resources/configprops.properties | 2 +- .../ConfigPropertiesIntegrationTest.java | 31 +++++++--- .../resources/configprops-test.properties | 2 +- 5 files changed, 75 insertions(+), 59 deletions(-) create mode 100644 spring-boot/src/main/java/org/baeldung/properties/Credentials.java diff --git a/spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java b/spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java index 2d3e56100c..3698d8ef30 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java +++ b/spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java @@ -8,7 +8,6 @@ import javax.validation.constraints.Min; import javax.validation.constraints.NotBlank; import javax.validation.constraints.Pattern; -import org.hibernate.validator.constraints.Length; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @@ -20,41 +19,8 @@ import org.springframework.validation.annotation.Validated; @Validated public class ConfigProperties { - @Validated - public static class Credentials { - - @Length(max = 4, min = 1) - private String authMethod; - private String username; - private String password; - - public String getAuthMethod() { - return authMethod; - } - - public void setAuthMethod(String authMethod) { - this.authMethod = authMethod; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - } - @NotBlank - private String host; + private String hostName; @Min(1025) @Max(65536) @@ -63,16 +29,16 @@ public class ConfigProperties { @Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$") private String from; - private Credentials credentials; private List defaultRecipients; private Map additionalHeaders; + private Credentials credentials; - public String getHost() { - return host; + public String getHostName() { + return hostName; } - public void setHost(String host) { - this.host = host; + public void setHostName(String hostName) { + this.hostName = hostName; } public int getPort() { @@ -91,14 +57,6 @@ public class ConfigProperties { this.from = from; } - public Credentials getCredentials() { - return credentials; - } - - public void setCredentials(Credentials credentials) { - this.credentials = credentials; - } - public List getDefaultRecipients() { return defaultRecipients; } @@ -114,4 +72,12 @@ public class ConfigProperties { public void setAdditionalHeaders(Map additionalHeaders) { this.additionalHeaders = additionalHeaders; } + + public Credentials getCredentials() { + return credentials; + } + + public void setCredentials(Credentials credentials) { + this.credentials = credentials; + } } diff --git a/spring-boot/src/main/java/org/baeldung/properties/Credentials.java b/spring-boot/src/main/java/org/baeldung/properties/Credentials.java new file mode 100644 index 0000000000..2d8ac76e62 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/Credentials.java @@ -0,0 +1,37 @@ +package org.baeldung.properties; + +import org.hibernate.validator.constraints.Length; +import org.springframework.validation.annotation.Validated; + +@Validated +public class Credentials { + + @Length(max = 4, min = 1) + private String authMethod; + private String username; + private String password; + + public String getAuthMethod() { + return authMethod; + } + + public void setAuthMethod(String authMethod) { + this.authMethod = authMethod; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/spring-boot/src/main/resources/configprops.properties b/spring-boot/src/main/resources/configprops.properties index e5d9ae621d..2dad11f9cc 100644 --- a/spring-boot/src/main/resources/configprops.properties +++ b/spring-boot/src/main/resources/configprops.properties @@ -1,5 +1,5 @@ #Simple properties -mail.host=mailer@mail.com +mail.hostname=host@mail.com mail.port=9000 mail.from=mailer@mail.com diff --git a/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java index 3f3b558db9..4ba6bf29d8 100644 --- a/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java @@ -1,5 +1,8 @@ package org.baeldung.properties; +import java.util.List; +import java.util.Map; + import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -18,26 +21,36 @@ public class ConfigPropertiesIntegrationTest { @Test public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception { - Assert.assertTrue("From address is read as null!", properties.getFrom() != null); + Assert.assertEquals("Incorrectly bound hostName property", "host@mail.com", properties.getHostName()); + Assert.assertEquals("Incorrectly bound port property", 9000, properties.getPort()); + Assert.assertEquals("Incorrectly bound from property", "mailer@mail.com", properties.getFrom()); } @Test public void whenListPropertyQueriedthenReturnsProperty() throws Exception { - Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients().size() == 2); - Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients().size() == 2); + List defaultRecipients = properties.getDefaultRecipients(); + Assert.assertTrue("Couldn't bind list property!", defaultRecipients.size() == 2); + Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", defaultRecipients.size() == 2); + Assert.assertEquals("Incorrectly bound list[0] property", "admin@mail.com", defaultRecipients.get(0)); + Assert.assertEquals("Incorrectly bound list[1] property", "owner@mail.com", defaultRecipients.get(1)); } @Test public void whenMapPropertyQueriedthenReturnsProperty() throws Exception { - Assert.assertTrue("Couldn't bind map property!", properties.getAdditionalHeaders() != null); - Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders().size() == 3); + Map additionalHeaders = properties.getAdditionalHeaders(); + Assert.assertTrue("Couldn't bind map property!", additionalHeaders != null); + Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", additionalHeaders.size() == 3); + Assert.assertEquals("Incorrectly bound map[redelivery] property", "true", additionalHeaders.get("redelivery")); + Assert.assertEquals("Incorrectly bound map[secure] property", "true", additionalHeaders.get("secure")); + Assert.assertEquals("Incorrectly bound map[p3] property", "value", additionalHeaders.get("p3")); } @Test public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception { - Assert.assertTrue("Couldn't bind map property!", properties.getCredentials() != null); - Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getAuthMethod().equals("SHA1")); - Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getUsername().equals("john")); - Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getPassword().equals("password")); + Credentials credentials = properties.getCredentials(); + Assert.assertTrue("Couldn't bind map property!", credentials != null); + Assert.assertEquals("Incorrectly bound object property, authMethod", "SHA1", credentials.getAuthMethod()); + Assert.assertEquals("Incorrectly bound object property, username", "john", credentials.getUsername()); + Assert.assertEquals("Incorrectly bound object property, password", "password", credentials.getPassword()); } } diff --git a/spring-boot/src/test/resources/configprops-test.properties b/spring-boot/src/test/resources/configprops-test.properties index b27cf2107a..697771ae6e 100644 --- a/spring-boot/src/test/resources/configprops-test.properties +++ b/spring-boot/src/test/resources/configprops-test.properties @@ -1,5 +1,5 @@ #Simple properties -mail.host=mailer@mail.com +mail.hostname=host@mail.com mail.port=9000 mail.from=mailer@mail.com