From 275a1f45456d1e4b881f72db1fa9a2a615bc3fd3 Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Thu, 13 Feb 2020 22:03:25 +0200 Subject: [PATCH] first review round changes --- .../configurationproperties/Connection.java | 23 --------- .../ConnectionFactory.java | 21 --------- .../configurationproperties/Credentials.java | 2 - .../CustomCredentialsConverter.java | 2 +- .../PropertyConversion.java | 10 ++-- .../configurationproperties/ServerConfig.java | 47 ++++--------------- .../ServerConfigFactory.java | 15 ++++++ ...indingPropertiesToBeanMethodsUnitTest.java | 34 ++++++++++++++ ...ingPropertiesToThirdpartyPOJOUnitTest.java | 25 ---------- ...ngPropertiesToUserDefinedPOJOUnitTest.java | 31 +++++------- .../BindingYMLPropertiesUnitTest.java | 36 +++++++------- .../PropertyValidationUnitTest.java | 9 ++-- .../SpringPropertiesConversionUnitTest.java | 19 +++++--- .../src/test/resources/application.yml | 24 ++++++---- .../resources/server-config-test.properties | 13 ++--- .../thirdparty-config-test.properties | 7 --- 16 files changed, 130 insertions(+), 188 deletions(-) delete mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Connection.java delete mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ConnectionFactory.java create mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java create mode 100644 spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java delete mode 100644 spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToThirdpartyPOJOUnitTest.java delete mode 100644 spring-boot-testing/src/test/resources/thirdparty-config-test.properties 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 deleted file mode 100644 index 769bd2e9a9..0000000000 --- a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Connection.java +++ /dev/null @@ -1,23 +0,0 @@ -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 deleted file mode 100644 index b93f4851d1..0000000000 --- a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ConnectionFactory.java +++ /dev/null @@ -1,21 +0,0 @@ -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 index e84a69b0d8..673adad41b 100644 --- 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 @@ -4,8 +4,6 @@ public class Credentials { private String username; private String password; - - public Credentials() {System.out.println("### INIT2 ###");} public Credentials(String username, String password) { this.username = username; 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 index 6c49117aa2..deb71dcdad 100644 --- 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 @@ -7,7 +7,7 @@ import org.springframework.stereotype.Component; @Component @ConfigurationPropertiesBinding public class CustomCredentialsConverter implements Converter { - + @Override public Credentials convert(String source) { String data[] = source.split(","); 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 index 0050d24863..9747858ed7 100644 --- 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 @@ -17,14 +17,14 @@ public class PropertyConversion { @DataSizeUnit(DataUnit.GIGABYTES) private DataSize downloadSpeed; - + private Duration backupDay; - + @DurationUnit(ChronoUnit.HOURS) private Duration backupHour; - + private Credentials credentials; - + public Duration getBackupDay() { return backupDay; } @@ -60,7 +60,7 @@ public class PropertyConversion { 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 index d92c0261f3..bfacc6e679 100644 --- 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 @@ -1,7 +1,7 @@ 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; @@ -9,39 +9,20 @@ import org.springframework.context.annotation.Configuration; @ConfigurationProperties(prefix = "server") public class ServerConfig { - private String name; private Address address; - private Map dirs; - private Set imgIds; - + private Map resourcesPath; + 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() { @@ -52,19 +33,11 @@ public class ServerConfig { this.address = address; } - public Set getImgIds() { - return imgIds; - } - - public void setImgIds(Set imgIds) { - this.imgIds = imgIds; + public Map getResourcesPath() { + return resourcesPath; } - public Map getDirs() { - return dirs; - } - - public void setDirs(Map dirs) { - this.dirs = dirs; + public void setResourcesPath(Map resourcesPath) { + this.resourcesPath = resourcesPath; } } diff --git a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java new file mode 100644 index 0000000000..8d74de1732 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java @@ -0,0 +1,15 @@ +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 ServerConfigFactory { + + @Bean + @ConfigurationProperties(prefix = "server.default") + public ServerConfig getDefaultConfigs() { + return new ServerConfig(); + } +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java new file mode 100644 index 0000000000..a5bc09e290 --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.boot.configurationproperties; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@EnableConfigurationProperties(value = ServerConfig.class) +@ContextConfiguration(classes = ServerConfigFactory.class) +@TestPropertySource("classpath:server-config-test.properties") +public class BindingPropertiesToBeanMethodsUnitTest { + + @Autowired + private ServerConfigFactory configFactory; + + @Test + void givenBeanAnnotatedMethod_whenBindingProperties_thenAllFieldsAreSet() { + + assertEquals("192.168.0.2", configFactory.getDefaultConfigs().getAddress().getIp()); + + Map expectedResourcesPath = new HashMap<>(); + expectedResourcesPath.put("imgs", "/root/def/imgs"); + assertEquals(expectedResourcesPath, configFactory.getDefaultConfigs().getResourcesPath()); + } +} \ 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 deleted file mode 100644 index 9daaf46ca7..0000000000 --- a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToThirdpartyPOJOUnitTest.java +++ /dev/null @@ -1,25 +0,0 @@ -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 index 44c4de01e4..96bbc6639a 100644 --- 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 @@ -3,15 +3,17 @@ package com.baeldung.boot.configurationproperties; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.HashMap; -import java.util.HashSet; 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 +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@EnableConfigurationProperties(value = ServerConfig.class) @TestPropertySource("classpath:server-config-test.properties") public class BindingPropertiesToUserDefinedPOJOUnitTest { @@ -21,19 +23,10 @@ public class BindingPropertiesToUserDefinedPOJOUnitTest { @Test void givenUserDefinedPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() { - assertEquals("node1", serverConfig.getName()); - - Set expectedImgs = new HashSet<>(); - expectedImgs.add("img1.jpg"); - expectedImgs.add("img2.jpg"); - assertEquals(expectedImgs, serverConfig.getImgIds()); - assertEquals("192.168.0.1", serverConfig.getAddress().getIp()); - assertEquals(8099, serverConfig.getAddress().getPort()); - Map expectedDirs = new HashMap<>(); - expectedDirs.put("imgs", "/root/imgs"); - expectedDirs.put("html", "/root/html"); - assertEquals(expectedDirs, serverConfig.getDirs()); + Map expectedResourcesPath = new HashMap<>(); + expectedResourcesPath.put("imgs", "/root/imgs"); + assertEquals(expectedResourcesPath, serverConfig.getResourcesPath()); } } \ 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 index 8f88eeca7e..eda8eb7f6b 100644 --- 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 @@ -1,37 +1,33 @@ package com.baeldung.boot.configurationproperties; import static org.junit.jupiter.api.Assertions.assertEquals; - import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.Set; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; -@SpringBootTest +@ExtendWith(SpringExtension.class) +@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class) +@EnableConfigurationProperties(value = ServerConfig.class) +@ActiveProfiles("test") public class BindingYMLPropertiesUnitTest { - + @Autowired private ServerConfig serverConfig; - + @Test void whenBindingYMLConfigFile_thenAllFieldsAreSet() { - - assertEquals("node2", serverConfig.getName()); - Set expectedImgs = new HashSet<>(); - expectedImgs.add("img1.png"); - expectedImgs.add("img2.png"); - assertEquals(expectedImgs, serverConfig.getImgIds()); + assertEquals("192.168.0.4", serverConfig.getAddress().getIp()); - assertEquals("192.168.0.2", serverConfig.getAddress().getIp()); - assertEquals(5000, serverConfig.getAddress().getPort()); - - Map expectedDirs = new HashMap<>(); - expectedDirs.put("imgs", "/etc/imgs"); - expectedDirs.put("html", "/etc/html"); - assertEquals(expectedDirs, serverConfig.getDirs()); + Map expectedResourcesPath = new HashMap<>(); + expectedResourcesPath.put("imgs", "/etc/test/imgs"); + assertEquals(expectedResourcesPath, serverConfig.getResourcesPath()); } } \ 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 index 08ff3536df..8833011425 100644 --- 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 @@ -7,11 +7,14 @@ import javax.validation.Validator; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; -@SpringBootTest +@ExtendWith(SpringExtension.class) +@EnableConfigurationProperties(value = MailServer.class) @TestPropertySource("classpath:property-validation-test.properties") public class PropertyValidationUnitTest { @@ -27,7 +30,7 @@ public class PropertyValidationUnitTest { @Test void whenBindingPropertiesToValidatedBeans_thenConstrainsAreChecked() { - + assertEquals(0, propertyValidator.validate(mailServer.getPropertiesMap()).size()); assertEquals(0, propertyValidator.validate(mailServer.getMailConfig()).size()); } 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 index 78a0e980a6..d24d563de4 100644 --- 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 @@ -1,14 +1,21 @@ 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.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.util.unit.DataSize; -@SpringBootTest +@ExtendWith(SpringExtension.class) +@EnableConfigurationProperties(value = PropertyConversion.class) +@ContextConfiguration(classes = CustomCredentialsConverter.class) @TestPropertySource("classpath:spring-conversion-test.properties") public class SpringPropertiesConversionUnitTest { @@ -17,21 +24,21 @@ public class SpringPropertiesConversionUnitTest { @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()); } diff --git a/spring-boot-testing/src/test/resources/application.yml b/spring-boot-testing/src/test/resources/application.yml index 7f22039eff..7dfd4d98a8 100644 --- a/spring-boot-testing/src/test/resources/application.yml +++ b/spring-boot-testing/src/test/resources/application.yml @@ -1,11 +1,15 @@ +spring: + profiles: test 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 + address: + ip: 192.168.0.4 + resources_path: + imgs: /etc/test/imgs +--- +spring: + profiles: dev +server: + address: + ip: 192.168.0.5 + resources_path: + imgs: /etc/dev/imgs \ 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 index 2ebdd206c8..62b23ed1d6 100644 --- a/spring-boot-testing/src/test/resources/server-config-test.properties +++ b/spring-boot-testing/src/test/resources/server-config-test.properties @@ -1,11 +1,6 @@ -server.NAME=node1 - server.address.ip=192.168.0.1 -server.address.port=8099 +server.resources_path.imgs=/root/imgs -# 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 +# default config +server.default.address.ip=192.168.0.2 +server.default.resources_path.imgs=/root/def/imgs \ 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 deleted file mode 100644 index 1412d56352..0000000000 --- a/spring-boot-testing/src/test/resources/thirdparty-config-test.properties +++ /dev/null @@ -1,7 +0,0 @@ -# testing -testing.authorization=foo -testing.timeout=50 - -# live -live.authorization=bar -live.timeout=100 \ No newline at end of file