diff --git a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Connection.java b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Connection.java new file mode 100644 index 0000000000..769bd2e9a9 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Connection.java @@ -0,0 +1,23 @@ +package com.baeldung.boot.configurationproperties; + +public class Connection { + + private String authorization; + private int timeout; + + public String getAuthorization() { + return authorization; + } + + public void setAuthorization(String authorization) { + this.authorization = authorization; + } + + public int getTimeout() { + return timeout; + } + + public void setTimeout(int timeout) { + this.timeout = timeout; + } +} diff --git a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ConnectionFactory.java b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ConnectionFactory.java new file mode 100644 index 0000000000..b93f4851d1 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ConnectionFactory.java @@ -0,0 +1,21 @@ +package com.baeldung.boot.configurationproperties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ConnectionFactory { + + @Bean + @ConfigurationProperties(prefix = "testing") + public Connection getTestingConnection() { + return new Connection(); + } + + @Bean + @ConfigurationProperties(prefix = "live") + public Connection getLiveConnection() { + return new Connection(); + } +} diff --git a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java new file mode 100644 index 0000000000..e84a69b0d8 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java @@ -0,0 +1,30 @@ +package com.baeldung.boot.configurationproperties; + +public class Credentials { + + private String username; + private String password; + + public Credentials() {System.out.println("### INIT2 ###");} + + public Credentials(String username, String password) { + this.username = username; + this.password = password; + } + + 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-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java new file mode 100644 index 0000000000..6c49117aa2 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java @@ -0,0 +1,16 @@ +package com.baeldung.boot.configurationproperties; + +import org.springframework.boot.context.properties.ConfigurationPropertiesBinding; +import org.springframework.core.convert.converter.Converter; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationPropertiesBinding +public class CustomCredentialsConverter implements Converter { + + @Override + public Credentials convert(String source) { + String data[] = source.split(","); + return new Credentials(data[0], data[1]); + } +} diff --git a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java new file mode 100644 index 0000000000..8ba42d76f8 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java @@ -0,0 +1,59 @@ +package com.baeldung.boot.configurationproperties; + +import java.util.Map; + +import javax.validation.Valid; +import javax.validation.constraints.Email; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.validation.annotation.Validated; + +@Configuration +@ConfigurationProperties(prefix = "validate") +@PropertySource("classpath:property-validation.properties") +@Validated +public class MailServer { + + @NotNull + @NotEmpty + private Map propertiesMap; + + @Valid + private MailConfig mailConfig = new MailConfig(); + + public static class MailConfig { + + @NotBlank + @Email + private String address; + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + } + + public Map getPropertiesMap() { + return propertiesMap; + } + + public void setPropertiesMap(Map propertiesMap) { + this.propertiesMap = propertiesMap; + } + + public MailConfig getMailConfig() { + return mailConfig; + } + + public void setMailConfig(MailConfig mailConfig) { + this.mailConfig = mailConfig; + } +} \ No newline at end of file diff --git a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java new file mode 100644 index 0000000000..0050d24863 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java @@ -0,0 +1,67 @@ +package com.baeldung.boot.configurationproperties; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.convert.DataSizeUnit; +import org.springframework.boot.convert.DurationUnit; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.unit.DataSize; +import org.springframework.util.unit.DataUnit; + +@Configuration +@ConfigurationProperties(prefix = "server") +public class PropertyConversion { + + private DataSize uploadSpeed; + + @DataSizeUnit(DataUnit.GIGABYTES) + private DataSize downloadSpeed; + + private Duration backupDay; + + @DurationUnit(ChronoUnit.HOURS) + private Duration backupHour; + + private Credentials credentials; + + public Duration getBackupDay() { + return backupDay; + } + + public void setBackupDay(Duration backupDay) { + this.backupDay = backupDay; + } + + public Duration getBackupHour() { + return backupHour; + } + + public void setBackupHour(Duration backupHour) { + this.backupHour = backupHour; + } + + public DataSize getUploadSpeed() { + return uploadSpeed; + } + + public void setUploadSpeed(DataSize uploadSpeed) { + this.uploadSpeed = uploadSpeed; + } + + public DataSize getDownloadSpeed() { + return downloadSpeed; + } + + public void setDownloadSpeed(DataSize downloadSpeed) { + this.downloadSpeed = downloadSpeed; + } + + public Credentials getCredentials() { + return credentials; + } + + public void setCredentials(Credentials credentials) { + this.credentials = credentials; + } +} diff --git a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java new file mode 100644 index 0000000000..d92c0261f3 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java @@ -0,0 +1,70 @@ +package com.baeldung.boot.configurationproperties; + +import java.util.Map; +import java.util.Set; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "server") +public class ServerConfig { + + private String name; + private Address address; + private Map dirs; + private Set imgIds; + + public static class Address { + + private String ip; + private int port; + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + public Set getImgIds() { + return imgIds; + } + + public void setImgIds(Set imgIds) { + this.imgIds = imgIds; + } + + public Map getDirs() { + return dirs; + } + + public void setDirs(Map dirs) { + this.dirs = dirs; + } +} diff --git a/spring-boot-testing/src/main/resources/property-validation.properties b/spring-boot-testing/src/main/resources/property-validation.properties new file mode 100644 index 0000000000..6b4c881dc0 --- /dev/null +++ b/spring-boot-testing/src/main/resources/property-validation.properties @@ -0,0 +1,4 @@ +validate.propertiesMap.first=prop1 +validate.propertiesMap.second=prop2 + +validate.mail_config.address=user1@test \ No newline at end of file diff --git a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToThirdpartyPOJOUnitTest.java b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToThirdpartyPOJOUnitTest.java new file mode 100644 index 0000000000..9daaf46ca7 --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToThirdpartyPOJOUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.boot.configurationproperties; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; + +@SpringBootTest +@TestPropertySource("classpath:thirdparty-config-test.properties") +public class BindingPropertiesToThirdpartyPOJOUnitTest { + + @Autowired + private ConnectionFactory connectionfactory; + + @Test + void givenThirdPartyPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() { + + assertEquals("foo", connectionfactory.getTestingConnection().getAuthorization()); + assertEquals(50, connectionfactory.getTestingConnection().getTimeout()); + + assertEquals("bar", connectionfactory.getLiveConnection().getAuthorization()); + assertEquals(100, connectionfactory.getLiveConnection().getTimeout()); + } +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java new file mode 100644 index 0000000000..0c74d37bb6 --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.boot.configurationproperties; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.Map; +import java.util.Set; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; + +@SpringBootTest +@TestPropertySource("classpath:server-config-test.properties") +public class BindingPropertiesToUserDefinedPOJOUnitTest { + + @Autowired + private ServerConfig serverConfig; + + @Test + void givenUserDefinedPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() { + + assertEquals("node1", serverConfig.getName()); + + assertEquals(Set.of("img1.jpg", "img2.jpg"), + serverConfig.getImgIds()); + + assertEquals("192.168.0.1", serverConfig.getAddress().getIp()); + assertEquals(8099, serverConfig.getAddress().getPort()); + + assertEquals(Map.of("imgs", "/root/imgs", "html", "/root/html"), + serverConfig.getDirs()); + } +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java new file mode 100644 index 0000000000..6d2d1d21e3 --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.boot.configurationproperties; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.Map; +import java.util.Set; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class BindingYMLPropertiesUnitTest { + + @Autowired + private ServerConfig serverConfig; + + @Test + void whenBindingYMLConfigFile_thenAllFieldsAreSet() { + + assertEquals("node2", serverConfig.getName()); + + assertEquals(Set.of("img1.png", "img2.png"), + serverConfig.getImgIds()); + + assertEquals("192.168.0.2", serverConfig.getAddress().getIp()); + assertEquals(5000, serverConfig.getAddress().getPort()); + + assertEquals(Map.of("imgs", "/etc/imgs", "html", "/etc/html"), + serverConfig.getDirs()); + } +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java new file mode 100644 index 0000000000..08ff3536df --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.boot.configurationproperties; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import javax.validation.Validation; +import javax.validation.Validator; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; + +@SpringBootTest +@TestPropertySource("classpath:property-validation-test.properties") +public class PropertyValidationUnitTest { + + @Autowired + private MailServer mailServer; + + private static Validator propertyValidator; + + @BeforeAll + public static void setup() { + propertyValidator = Validation.buildDefaultValidatorFactory().getValidator(); + } + + @Test + void whenBindingPropertiesToValidatedBeans_thenConstrainsAreChecked() { + + assertEquals(0, propertyValidator.validate(mailServer.getPropertiesMap()).size()); + assertEquals(0, propertyValidator.validate(mailServer.getMailConfig()).size()); + } +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java new file mode 100644 index 0000000000..78a0e980a6 --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.boot.configurationproperties; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.time.Duration; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; +import org.springframework.util.unit.DataSize; + +@SpringBootTest +@TestPropertySource("classpath:spring-conversion-test.properties") +public class SpringPropertiesConversionUnitTest { + + @Autowired + private PropertyConversion propertyConversion; + + @Test + void whenUsingSpringDefaultSizeConversion_thenDataSizeObjectIsSet() { + + assertEquals(DataSize.ofMegabytes(500), propertyConversion.getUploadSpeed()); + assertEquals(DataSize.ofGigabytes(10), propertyConversion.getDownloadSpeed()); + } + + @Test + void whenUsingSpringDefaultDurationConversion_thenDurationObjectIsSet() { + + assertEquals(Duration.ofDays(1), propertyConversion.getBackupDay()); + assertEquals(Duration.ofHours(8), propertyConversion.getBackupHour()); + } + + @Test + void whenRegisteringCustomCredentialsConverter_thenCredentialsAreParsed() { + + assertEquals("user", propertyConversion.getCredentials().getUsername()); + assertEquals("123", propertyConversion.getCredentials().getPassword()); + } +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/application.yml b/spring-boot-testing/src/test/resources/application.yml new file mode 100644 index 0000000000..7f22039eff --- /dev/null +++ b/spring-boot-testing/src/test/resources/application.yml @@ -0,0 +1,11 @@ +server: + name: node2 + address: + ip: 192.168.0.2 + port: 5000 + dirs: + imgs: /etc/imgs + html: /etc/html + imgIds: + - img1.png + - img2.png \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/property-validation-test.properties b/spring-boot-testing/src/test/resources/property-validation-test.properties new file mode 100644 index 0000000000..f85f4770c9 --- /dev/null +++ b/spring-boot-testing/src/test/resources/property-validation-test.properties @@ -0,0 +1,4 @@ +validate.propertiesMap.first=prop1 +validate.propertiesMap.second= + +validate.mail_config.address=user1.test \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/server-config-test.properties b/spring-boot-testing/src/test/resources/server-config-test.properties new file mode 100644 index 0000000000..2ebdd206c8 --- /dev/null +++ b/spring-boot-testing/src/test/resources/server-config-test.properties @@ -0,0 +1,11 @@ +server.NAME=node1 + +server.address.ip=192.168.0.1 +server.address.port=8099 + +# directories +server.dirs.imgs=/root/imgs +server.dirs.html=/root/html + +server.img_ids[0]=img1.jpg +server.img_ids[1]=img2.jpg \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/spring-conversion-test.properties b/spring-boot-testing/src/test/resources/spring-conversion-test.properties new file mode 100644 index 0000000000..87444cee10 --- /dev/null +++ b/spring-boot-testing/src/test/resources/spring-conversion-test.properties @@ -0,0 +1,10 @@ +# bandwidth +server.upload_speed=500MB +server.download_speed=10 + +# backup date +server.backup_day=1d +server.backup_hour=8 + +# custom converter +server.credentials=user,123 \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/thirdparty-config-test.properties b/spring-boot-testing/src/test/resources/thirdparty-config-test.properties new file mode 100644 index 0000000000..1412d56352 --- /dev/null +++ b/spring-boot-testing/src/test/resources/thirdparty-config-test.properties @@ -0,0 +1,7 @@ +# testing +testing.authorization=foo +testing.timeout=50 + +# live +live.authorization=bar +live.timeout=100 \ No newline at end of file