From 2d1aca64e14fc2b762c0cbdd41cd4cc4551e2a33 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Tue, 21 Jan 2020 20:41:44 +0100 Subject: [PATCH 01/21] BAEL-3694: Refactor Mockito unit tests --- .../MockitoVoidMethodsUnitTest.java | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/testing-modules/mockito/src/test/java/com/baeldung/mockito/voidmethods/MockitoVoidMethodsUnitTest.java b/testing-modules/mockito/src/test/java/com/baeldung/mockito/voidmethods/MockitoVoidMethodsUnitTest.java index dd41d98b57..9dd98f184b 100644 --- a/testing-modules/mockito/src/test/java/com/baeldung/mockito/voidmethods/MockitoVoidMethodsUnitTest.java +++ b/testing-modules/mockito/src/test/java/com/baeldung/mockito/voidmethods/MockitoVoidMethodsUnitTest.java @@ -16,48 +16,51 @@ public class MockitoVoidMethodsUnitTest { @Test public void whenAddCalledVerified() { - MyList mockVoid = mock(MyList.class); - mockVoid.add(0, ""); - verify(mockVoid, times(1)).add(0, ""); + MyList myList = mock(MyList.class); + myList.add(0, ""); + + verify(myList, times(1)).add(0, ""); } @Test(expected = Exception.class) public void givenNull_addThrows() { - MyList mockVoid = mock(MyList.class); - doThrow().when(mockVoid).add(isA(Integer.class), isNull()); - mockVoid.add(0, null); + MyList myList = mock(MyList.class); + doThrow().when(myList).add(isA(Integer.class), isNull()); + + myList.add(0, null); } @Test public void whenAddCalledValueCaptured() { - MyList mockVoid = mock(MyList.class); + MyList myList = mock(MyList.class); ArgumentCaptor valueCapture = ArgumentCaptor.forClass(String.class); - doNothing().when(mockVoid).add(any(Integer.class), valueCapture.capture()); - mockVoid.add(0, "captured"); + doNothing().when(myList).add(any(Integer.class), valueCapture.capture()); + myList.add(0, "captured"); + assertEquals("captured", valueCapture.getValue()); } @Test public void whenAddCalledAnswered() { - MyList mockVoid = mock(MyList.class); - doAnswer((Answer) invocation -> { + MyList myList = mock(MyList.class); + doAnswer(invocation -> { Object arg0 = invocation.getArgument(0); Object arg1 = invocation.getArgument(1); //do something with the arguments here assertEquals(3, arg0); assertEquals("answer me", arg1); - return null; - }).when(mockVoid).add(any(Integer.class), any(String.class)); - mockVoid.add(3, "answer me"); + }).when(myList).add(any(Integer.class), any(String.class)); + myList.add(3, "answer me"); } @Test public void whenAddCalledRealMethodCalled() { - MyList mockVoid = mock(MyList.class); - doCallRealMethod().when(mockVoid).add(any(Integer.class), any(String.class)); - mockVoid.add(1, "real"); - verify(mockVoid, times(1)).add(1, "real"); + MyList myList = mock(MyList.class); + doCallRealMethod().when(myList).add(any(Integer.class), any(String.class)); + myList.add(1, "real"); + + verify(myList, times(1)).add(1, "real"); } } From 0104ff9c8207dae18cdfaadb10090cbe3e34e986 Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Thu, 23 Jan 2020 17:26:30 +0200 Subject: [PATCH 02/21] init --- .../configurationproperties/Connection.java | 23 ++++++ .../ConnectionFactory.java | 21 ++++++ .../configurationproperties/Credentials.java | 30 ++++++++ .../CustomCredentialsConverter.java | 16 +++++ .../configurationproperties/MailServer.java | 59 ++++++++++++++++ .../PropertyConversion.java | 67 ++++++++++++++++++ .../configurationproperties/ServerConfig.java | 70 +++++++++++++++++++ .../resources/property-validation.properties | 4 ++ ...ingPropertiesToThirdpartyPOJOUnitTest.java | 25 +++++++ ...ngPropertiesToUserDefinedPOJOUnitTest.java | 32 +++++++++ .../BindingYMLPropertiesUnitTest.java | 30 ++++++++ .../PropertyValidationUnitTest.java | 34 +++++++++ .../SpringPropertiesConversionUnitTest.java | 38 ++++++++++ .../src/test/resources/application.yml | 11 +++ .../property-validation-test.properties | 4 ++ .../resources/server-config-test.properties | 11 +++ .../spring-conversion-test.properties | 10 +++ .../thirdparty-config-test.properties | 7 ++ 18 files changed, 492 insertions(+) create mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Connection.java create 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/Credentials.java create mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java create mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java create mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java create mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java create mode 100644 spring-boot-testing/src/main/resources/property-validation.properties create mode 100644 spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToThirdpartyPOJOUnitTest.java create mode 100644 spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java create mode 100644 spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java create mode 100644 spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java create mode 100644 spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java create mode 100644 spring-boot-testing/src/test/resources/application.yml create mode 100644 spring-boot-testing/src/test/resources/property-validation-test.properties create mode 100644 spring-boot-testing/src/test/resources/server-config-test.properties create mode 100644 spring-boot-testing/src/test/resources/spring-conversion-test.properties create 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 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 From cc283b42332d70f6268f820a91556d02f46ed967 Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Thu, 23 Jan 2020 18:34:05 +0200 Subject: [PATCH 03/21] fixing configs properties --- .../src/test/resources/property-validation-test.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-testing/src/test/resources/property-validation-test.properties b/spring-boot-testing/src/test/resources/property-validation-test.properties index f85f4770c9..6b4c881dc0 100644 --- a/spring-boot-testing/src/test/resources/property-validation-test.properties +++ b/spring-boot-testing/src/test/resources/property-validation-test.properties @@ -1,4 +1,4 @@ validate.propertiesMap.first=prop1 -validate.propertiesMap.second= +validate.propertiesMap.second=prop2 -validate.mail_config.address=user1.test \ No newline at end of file +validate.mail_config.address=user1@test \ No newline at end of file From 5b245cde28d427c3b15c08d02dd181f606dea561 Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Thu, 23 Jan 2020 21:42:44 +0200 Subject: [PATCH 04/21] Using java7 syntax for Map and Set --- ...indingPropertiesToUserDefinedPOJOUnitTest.java | 15 +++++++++++---- .../BindingYMLPropertiesUnitTest.java | 15 +++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) 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 0c74d37bb6..44c4de01e4 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 @@ -1,6 +1,9 @@ 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; @@ -20,13 +23,17 @@ public class BindingPropertiesToUserDefinedPOJOUnitTest { assertEquals("node1", serverConfig.getName()); - assertEquals(Set.of("img1.jpg", "img2.jpg"), - serverConfig.getImgIds()); + 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()); - assertEquals(Map.of("imgs", "/root/imgs", "html", "/root/html"), - serverConfig.getDirs()); + Map expectedDirs = new HashMap<>(); + expectedDirs.put("imgs", "/root/imgs"); + expectedDirs.put("html", "/root/html"); + assertEquals(expectedDirs, 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 index 6d2d1d21e3..8f88eeca7e 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,6 +1,9 @@ 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; @@ -18,13 +21,17 @@ public class BindingYMLPropertiesUnitTest { assertEquals("node2", serverConfig.getName()); - assertEquals(Set.of("img1.png", "img2.png"), - serverConfig.getImgIds()); + Set expectedImgs = new HashSet<>(); + expectedImgs.add("img1.png"); + expectedImgs.add("img2.png"); + assertEquals(expectedImgs, 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()); + Map expectedDirs = new HashMap<>(); + expectedDirs.put("imgs", "/etc/imgs"); + expectedDirs.put("html", "/etc/html"); + assertEquals(expectedDirs, serverConfig.getDirs()); } } \ No newline at end of file From 275a1f45456d1e4b881f72db1fa9a2a615bc3fd3 Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Thu, 13 Feb 2020 22:03:25 +0200 Subject: [PATCH 05/21] 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 From 086adbd0c271314cf88a61f3f504c9ea79913e58 Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Sun, 16 Feb 2020 14:57:46 +0200 Subject: [PATCH 06/21] overriding properties section and changing article module --- .../configurationproperties/Credentials.java | 28 ++++++++ .../CustomCredentialsConverter.java | 16 +++++ .../configurationproperties/MailServer.java | 59 ++++++++++++++++ .../PropertyConversion.java | 67 +++++++++++++++++++ .../configurationproperties/ServerConfig.java | 43 ++++++++++++ .../ServerConfigFactory.java | 15 +++++ .../resources/property-validation.properties | 4 ++ ...indingPropertiesToBeanMethodsUnitTest.java | 34 ++++++++++ ...ngPropertiesToUserDefinedPOJOUnitTest.java | 32 +++++++++ .../BindingYMLPropertiesUnitTest.java | 33 +++++++++ ...ridingConfigurationPropertiesUnitTest.java | 33 +++++++++ .../PropertyValidationUnitTest.java | 37 ++++++++++ .../SpringPropertiesConversionUnitTest.java | 45 +++++++++++++ .../src/test/resources/application.yml | 15 +++++ .../property-validation-test.properties | 4 ++ .../resources/server-config-test.properties | 6 ++ .../spring-conversion-test.properties | 10 +++ 17 files changed, 481 insertions(+) create mode 100644 spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java create mode 100644 spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java create mode 100644 spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java create mode 100644 spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java create mode 100644 spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java create mode 100644 spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java create mode 100644 spring-boot-modules/spring-boot-testing/src/main/resources/property-validation.properties create mode 100644 spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java create mode 100644 spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java create mode 100644 spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java create mode 100644 spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/OverridingConfigurationPropertiesUnitTest.java create mode 100644 spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java create mode 100644 spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java create mode 100644 spring-boot-modules/spring-boot-testing/src/test/resources/application.yml create mode 100644 spring-boot-modules/spring-boot-testing/src/test/resources/property-validation-test.properties create mode 100644 spring-boot-modules/spring-boot-testing/src/test/resources/server-config-test.properties create mode 100644 spring-boot-modules/spring-boot-testing/src/test/resources/spring-conversion-test.properties diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java new file mode 100644 index 0000000000..673adad41b --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java @@ -0,0 +1,28 @@ +package com.baeldung.boot.configurationproperties; + +public class Credentials { + + private String username; + private String password; + + 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-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java new file mode 100644 index 0000000000..deb71dcdad --- /dev/null +++ b/spring-boot-modules/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-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java new file mode 100644 index 0000000000..8ba42d76f8 --- /dev/null +++ b/spring-boot-modules/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-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java new file mode 100644 index 0000000000..9747858ed7 --- /dev/null +++ b/spring-boot-modules/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-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java new file mode 100644 index 0000000000..bfacc6e679 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java @@ -0,0 +1,43 @@ +package com.baeldung.boot.configurationproperties; + +import java.util.Map; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "server") +public class ServerConfig { + + private Address address; + private Map resourcesPath; + + public static class Address { + + private String ip; + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + public Map getResourcesPath() { + return resourcesPath; + } + + public void setResourcesPath(Map resourcesPath) { + this.resourcesPath = resourcesPath; + } +} diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java new file mode 100644 index 0000000000..8d74de1732 --- /dev/null +++ b/spring-boot-modules/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-modules/spring-boot-testing/src/main/resources/property-validation.properties b/spring-boot-modules/spring-boot-testing/src/main/resources/property-validation.properties new file mode 100644 index 0000000000..6b4c881dc0 --- /dev/null +++ b/spring-boot-modules/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-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java new file mode 100644 index 0000000000..a5bc09e290 --- /dev/null +++ b/spring-boot-modules/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-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java new file mode 100644 index 0000000000..96bbc6639a --- /dev/null +++ b/spring-boot-modules/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.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.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 { + + @Autowired + private ServerConfig serverConfig; + + @Test + void givenUserDefinedPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() { + + assertEquals("192.168.0.1", serverConfig.getAddress().getIp()); + + Map expectedResourcesPath = new HashMap<>(); + expectedResourcesPath.put("imgs", "/root/imgs"); + assertEquals(expectedResourcesPath, serverConfig.getResourcesPath()); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java new file mode 100644 index 0000000000..eda8eb7f6b --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java @@ -0,0 +1,33 @@ +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.boot.test.context.ConfigFileApplicationContextInitializer; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@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("192.168.0.4", serverConfig.getAddress().getIp()); + + 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-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/OverridingConfigurationPropertiesUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/OverridingConfigurationPropertiesUnitTest.java new file mode 100644 index 0000000000..8631308296 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/OverridingConfigurationPropertiesUnitTest.java @@ -0,0 +1,33 @@ +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.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@EnableConfigurationProperties(value = MailServer.class) +@TestPropertySource(properties = { "validate.mail_config.address=new_user@test" }) +public class OverridingConfigurationPropertiesUnitTest { + + @Autowired + private MailServer mailServer; + + @Test + void givenUsingPropertiesAttribute_whenAssiginingNewValueToProprty_thenSpringUsesNewValue() { + + assertEquals("new_user@test", mailServer.getMailConfig().getAddress()); + + Map expectedMap = new HashMap<>(); + expectedMap.put("first", "prop1"); + expectedMap.put("second", "prop2"); + assertEquals(expectedMap, mailServer.getPropertiesMap()); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java new file mode 100644 index 0000000000..8833011425 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java @@ -0,0 +1,37 @@ +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.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 = MailServer.class) +@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-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java new file mode 100644 index 0000000000..d24d563de4 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java @@ -0,0 +1,45 @@ +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.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; + +@ExtendWith(SpringExtension.class) +@EnableConfigurationProperties(value = PropertyConversion.class) +@ContextConfiguration(classes = CustomCredentialsConverter.class) +@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-modules/spring-boot-testing/src/test/resources/application.yml b/spring-boot-modules/spring-boot-testing/src/test/resources/application.yml new file mode 100644 index 0000000000..7dfd4d98a8 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/test/resources/application.yml @@ -0,0 +1,15 @@ +spring: + profiles: test +server: + 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-modules/spring-boot-testing/src/test/resources/property-validation-test.properties b/spring-boot-modules/spring-boot-testing/src/test/resources/property-validation-test.properties new file mode 100644 index 0000000000..6b4c881dc0 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/test/resources/property-validation-test.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-modules/spring-boot-testing/src/test/resources/server-config-test.properties b/spring-boot-modules/spring-boot-testing/src/test/resources/server-config-test.properties new file mode 100644 index 0000000000..62b23ed1d6 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/test/resources/server-config-test.properties @@ -0,0 +1,6 @@ +server.address.ip=192.168.0.1 +server.resources_path.imgs=/root/imgs + +# 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-modules/spring-boot-testing/src/test/resources/spring-conversion-test.properties b/spring-boot-modules/spring-boot-testing/src/test/resources/spring-conversion-test.properties new file mode 100644 index 0000000000..87444cee10 --- /dev/null +++ b/spring-boot-modules/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 From a44ab7dc563658140230ba3d231b43e5d63d52ae Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Sun, 16 Feb 2020 15:20:48 +0200 Subject: [PATCH 07/21] deleting spring-boot-testing project --- .../configurationproperties/Credentials.java | 28 -------- .../CustomCredentialsConverter.java | 16 ----- .../configurationproperties/MailServer.java | 59 ---------------- .../PropertyConversion.java | 67 ------------------- .../configurationproperties/ServerConfig.java | 43 ------------ .../ServerConfigFactory.java | 15 ----- .../resources/property-validation.properties | 4 -- ...indingPropertiesToBeanMethodsUnitTest.java | 34 ---------- ...ngPropertiesToUserDefinedPOJOUnitTest.java | 32 --------- .../BindingYMLPropertiesUnitTest.java | 33 --------- .../PropertyValidationUnitTest.java | 37 ---------- .../SpringPropertiesConversionUnitTest.java | 45 ------------- .../src/test/resources/application.yml | 15 ----- .../property-validation-test.properties | 4 -- .../resources/server-config-test.properties | 6 -- .../spring-conversion-test.properties | 10 --- 16 files changed, 448 deletions(-) delete mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java delete mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java delete mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java delete mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java delete mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java delete mode 100644 spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java delete mode 100644 spring-boot-testing/src/main/resources/property-validation.properties delete 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/BindingPropertiesToUserDefinedPOJOUnitTest.java delete mode 100644 spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java delete mode 100644 spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java delete mode 100644 spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java delete mode 100644 spring-boot-testing/src/test/resources/application.yml delete mode 100644 spring-boot-testing/src/test/resources/property-validation-test.properties delete mode 100644 spring-boot-testing/src/test/resources/server-config-test.properties delete mode 100644 spring-boot-testing/src/test/resources/spring-conversion-test.properties 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 deleted file mode 100644 index 673adad41b..0000000000 --- a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.boot.configurationproperties; - -public class Credentials { - - private String username; - private String password; - - 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 deleted file mode 100644 index deb71dcdad..0000000000 --- a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java +++ /dev/null @@ -1,16 +0,0 @@ -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 deleted file mode 100644 index 8ba42d76f8..0000000000 --- a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java +++ /dev/null @@ -1,59 +0,0 @@ -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 deleted file mode 100644 index 9747858ed7..0000000000 --- a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java +++ /dev/null @@ -1,67 +0,0 @@ -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 deleted file mode 100644 index bfacc6e679..0000000000 --- a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.baeldung.boot.configurationproperties; - -import java.util.Map; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ConfigurationProperties(prefix = "server") -public class ServerConfig { - - private Address address; - private Map resourcesPath; - - public static class Address { - - private String ip; - - public String getIp() { - return ip; - } - - public void setIp(String ip) { - this.ip = ip; - } - } - - public Address getAddress() { - return address; - } - - public void setAddress(Address address) { - this.address = address; - } - - public Map getResourcesPath() { - return resourcesPath; - } - - 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 deleted file mode 100644 index 8d74de1732..0000000000 --- a/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java +++ /dev/null @@ -1,15 +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 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/main/resources/property-validation.properties b/spring-boot-testing/src/main/resources/property-validation.properties deleted file mode 100644 index 6b4c881dc0..0000000000 --- a/spring-boot-testing/src/main/resources/property-validation.properties +++ /dev/null @@ -1,4 +0,0 @@ -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/BindingPropertiesToBeanMethodsUnitTest.java b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java deleted file mode 100644 index a5bc09e290..0000000000 --- a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java +++ /dev/null @@ -1,34 +0,0 @@ -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/BindingPropertiesToUserDefinedPOJOUnitTest.java b/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java deleted file mode 100644 index 96bbc6639a..0000000000 --- a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java +++ /dev/null @@ -1,32 +0,0 @@ -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.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 { - - @Autowired - private ServerConfig serverConfig; - - @Test - void givenUserDefinedPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() { - - assertEquals("192.168.0.1", serverConfig.getAddress().getIp()); - - 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 deleted file mode 100644 index eda8eb7f6b..0000000000 --- a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java +++ /dev/null @@ -1,33 +0,0 @@ -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.boot.test.context.ConfigFileApplicationContextInitializer; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -@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("192.168.0.4", serverConfig.getAddress().getIp()); - - 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 deleted file mode 100644 index 8833011425..0000000000 --- a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java +++ /dev/null @@ -1,37 +0,0 @@ -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.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 = MailServer.class) -@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 deleted file mode 100644 index d24d563de4..0000000000 --- a/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java +++ /dev/null @@ -1,45 +0,0 @@ -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.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; - -@ExtendWith(SpringExtension.class) -@EnableConfigurationProperties(value = PropertyConversion.class) -@ContextConfiguration(classes = CustomCredentialsConverter.class) -@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 deleted file mode 100644 index 7dfd4d98a8..0000000000 --- a/spring-boot-testing/src/test/resources/application.yml +++ /dev/null @@ -1,15 +0,0 @@ -spring: - profiles: test -server: - 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/property-validation-test.properties b/spring-boot-testing/src/test/resources/property-validation-test.properties deleted file mode 100644 index 6b4c881dc0..0000000000 --- a/spring-boot-testing/src/test/resources/property-validation-test.properties +++ /dev/null @@ -1,4 +0,0 @@ -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/resources/server-config-test.properties b/spring-boot-testing/src/test/resources/server-config-test.properties deleted file mode 100644 index 62b23ed1d6..0000000000 --- a/spring-boot-testing/src/test/resources/server-config-test.properties +++ /dev/null @@ -1,6 +0,0 @@ -server.address.ip=192.168.0.1 -server.resources_path.imgs=/root/imgs - -# 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/spring-conversion-test.properties b/spring-boot-testing/src/test/resources/spring-conversion-test.properties deleted file mode 100644 index 87444cee10..0000000000 --- a/spring-boot-testing/src/test/resources/spring-conversion-test.properties +++ /dev/null @@ -1,10 +0,0 @@ -# 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 From 46765cf410196a629a54ee83befc741525b6cfdc Mon Sep 17 00:00:00 2001 From: pazis Date: Wed, 19 Feb 2020 21:28:48 +0000 Subject: [PATCH 08/21] init --- apache-beam/.gitignore | 1 + apache-beam/pom.xml | 44 ++++++++++++ .../com/baeldung/apache/beam/WordCount.java | 71 +++++++++++++++++++ .../apache/beam/WordCountUnitTest.java | 17 +++++ apache-beam/src/test/resources/wordcount.txt | 16 +++++ 5 files changed, 149 insertions(+) create mode 100644 apache-beam/.gitignore create mode 100644 apache-beam/pom.xml create mode 100644 apache-beam/src/main/java/com/baeldung/apache/beam/WordCount.java create mode 100644 apache-beam/src/test/java/com/baeldung/apache/beam/WordCountUnitTest.java create mode 100644 apache-beam/src/test/resources/wordcount.txt diff --git a/apache-beam/.gitignore b/apache-beam/.gitignore new file mode 100644 index 0000000000..84c048a73c --- /dev/null +++ b/apache-beam/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/apache-beam/pom.xml b/apache-beam/pom.xml new file mode 100644 index 0000000000..1076d06e2a --- /dev/null +++ b/apache-beam/pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + com.baeldung.apache + apache-beam + 0.0.1-SNAPSHOT + apache-beam + + + + org.apache.beam + beam-sdks-java-core + ${beam.version} + + + + org.apache.beam + beam-runners-direct-java + ${beam.version} + runtime + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + 2.19.0 + 3.6.1 + + + \ No newline at end of file diff --git a/apache-beam/src/main/java/com/baeldung/apache/beam/WordCount.java b/apache-beam/src/main/java/com/baeldung/apache/beam/WordCount.java new file mode 100644 index 0000000000..632cc97e6c --- /dev/null +++ b/apache-beam/src/main/java/com/baeldung/apache/beam/WordCount.java @@ -0,0 +1,71 @@ +package com.baeldung.apache.beam; + +import java.util.Arrays; + +import org.apache.beam.sdk.Pipeline; +import org.apache.beam.sdk.io.TextIO; +import org.apache.beam.sdk.options.PipelineOptions; +import org.apache.beam.sdk.options.PipelineOptionsFactory; +import org.apache.beam.sdk.transforms.Count; +import org.apache.beam.sdk.transforms.Filter; +import org.apache.beam.sdk.transforms.FlatMapElements; +import org.apache.beam.sdk.transforms.MapElements; +import org.apache.beam.sdk.values.KV; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.TypeDescriptors; + +public class WordCount { + + public static boolean wordCount(String inputFilePath, String outputFilePath) { + // We use default options + PipelineOptions options = PipelineOptionsFactory.create(); + // to create the pipeline + Pipeline p = Pipeline.create(options); + // Here is our workflow graph + PCollection> wordCount = p + .apply("(1) Read all lines", TextIO.read().from(inputFilePath)) + .apply("(2) Flatmap to a list of words", FlatMapElements.into(TypeDescriptors.strings()) + .via(line -> Arrays.asList(line.split("\\s")))) + .apply("(3) Lowercase all", MapElements.into(TypeDescriptors.strings()) + .via(word -> word.toLowerCase())) + .apply("(4) Trim punctuations", MapElements.into(TypeDescriptors.strings()) + .via(word -> trim(word))) + .apply("(5) Filter stopwords", Filter.by(word -> !isStopWord(word))) + .apply("(6) Count words", Count.perElement()); + // We convert the PCollection to String so that we can write it to file + wordCount.apply(MapElements.into(TypeDescriptors.strings()) + .via(count -> count.getKey() + " --> " + count.getValue())) + .apply(TextIO.write().to(outputFilePath)); + // Finally we must run the pipeline, otherwise it's only a definition + p.run().waitUntilFinish(); + return true; + } + + public static boolean isStopWord(String word) { + String[] stopwords = {"am", "are", "is", "i", "you", "me", + "he", "she", "they", "them", "was", + "were", "from", "in", "of", "to", "be", + "him", "her", "us", "and", "or"}; + for (String stopword : stopwords) { + if (stopword.compareTo(word) == 0) { + return true; + } + } + return false; + } + + public static String trim(String word) { + return word.replace("(","") + .replace(")", "") + .replace(",", "") + .replace(".", "") + .replace("\"", "") + .replace("'", "") + .replace(":", "") + .replace(";", "") + .replace("-", "") + .replace("?", "") + .replace("!", ""); + } + +} diff --git a/apache-beam/src/test/java/com/baeldung/apache/beam/WordCountUnitTest.java b/apache-beam/src/test/java/com/baeldung/apache/beam/WordCountUnitTest.java new file mode 100644 index 0000000000..ff338a292c --- /dev/null +++ b/apache-beam/src/test/java/com/baeldung/apache/beam/WordCountUnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.apache.beam; + +import static org.junit.Assert.assertTrue; + +import org.junit.Ignore; +import org.junit.Test; + +public class WordCountUnitTest { + + @Test + @Ignore + public void givenInputFile_whenWordCountRuns_thenJobFinishWithoutError() { + boolean jobDone = WordCount.wordCount("src/test/resources/wordcount.txt", "target/output"); + assertTrue(jobDone); + } + +} diff --git a/apache-beam/src/test/resources/wordcount.txt b/apache-beam/src/test/resources/wordcount.txt new file mode 100644 index 0000000000..542385379b --- /dev/null +++ b/apache-beam/src/test/resources/wordcount.txt @@ -0,0 +1,16 @@ +We've all heard the scare stories about North Korea: the homemade nuclear arsenal built while their people starve and then aimed imprecisely at the rest of the world, a +leader so deluded he makes L Ron Hubbard look like a man excessively overburdened with self-doubt and their deep-seated belief that foreign capitalists will invade at any +moment and steal all their bauxite. +The popular portrayal of this Marxist nation is something like one of the more harrowing episodes of M*A*S*H, only with the cast of wacky characters replaced by twitchy, +heavily armed Stalinist meth addicts +Cracked would like to take a moment to celebrate the good things about North Korea though, the things that the country's enemies prefer to suppress as part of their politically +motivated jealousy. Like how no different to you and me, there's nothing every North Korean likes more after an 18 hour shift at the phosphorus plant than a nice beer to go with +his dried fish ration. Ever attentive to its people's needs and in the twinkling of a decade, North Korea's leadership bought, disassembled, transported and rebuilt a British +brewery in order to discover and reproduce the secrets of beer and then brew the sweet nectar for its hardworking people, up to 18 bottles at a time. And with minimal fatalities. +When was the last time YOUR leader got a beer for YOU, American? (NB do not answer this question if you are Henry Louis Gates). +Or how about the fried chicken restaurant that downtown Pyongyang boasts? Yes real chicken, fried and then delivered to your sleeping cube, with optional beer if you like! You +don't even have to remove the feathers or pull out the gizzard yourself. Mostly. Americans must eat their fried chicken from a bucket, like swine, sold by a company so secretive +that even the very blend of seasoning used is intentionally kept from them. And they call North Korea paranoid? +And how many nations would entertain the syphilitic, bourgeois ramblings of Bill Clinton let alone permit him anywhere near their proud womenfolk? Only wise Kim Jong Il could see +past Bill's many, many imperfections and treat him with the pity and kindness he deserves, accepting his feeble pleas to pardon the American spies rightly convicted of photographing +the nation's sensitive beetroot fields. From 4414f4f390cd5a37fbfd21c1b1541746dcc96ae5 Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Thu, 20 Feb 2020 07:24:35 +0200 Subject: [PATCH 09/21] fixing formatting --- .../configurationproperties/Credentials.java | 36 ++++----- .../CustomCredentialsConverter.java | 10 +-- .../configurationproperties/MailServer.java | 56 +++++++------- .../PropertyConversion.java | 74 +++++++++---------- .../configurationproperties/ServerConfig.java | 46 ++++++------ .../ServerConfigFactory.java | 10 +-- ...indingPropertiesToBeanMethodsUnitTest.java | 22 +++--- ...ngPropertiesToUserDefinedPOJOUnitTest.java | 20 ++--- .../BindingYMLPropertiesUnitTest.java | 20 ++--- ...ridingConfigurationPropertiesUnitTest.java | 22 +++--- .../PropertyValidationUnitTest.java | 28 +++---- .../SpringPropertiesConversionUnitTest.java | 39 +++++----- .../src/test/resources/application.yml | 2 +- 13 files changed, 194 insertions(+), 191 deletions(-) diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java index 673adad41b..e51937cf57 100644 --- a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/Credentials.java @@ -2,27 +2,27 @@ package com.baeldung.boot.configurationproperties; public class Credentials { - private String username; - private String password; + private String username; + private String password; - public Credentials(String username, String password) { - this.username = username; - this.password = password; - } + public Credentials(String username, String password) { + this.username = username; + this.password = password; + } - public String getUsername() { - return username; - } + public String getUsername() { + return username; + } - public void setUsername(String username) { - this.username = username; - } + public void setUsername(String username) { + this.username = username; + } - public String getPassword() { - return password; - } + public String getPassword() { + return password; + } - public void setPassword(String password) { - this.password = password; - } + public void setPassword(String password) { + this.password = password; + } } diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java index deb71dcdad..ef160c8c9a 100644 --- a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/CustomCredentialsConverter.java @@ -8,9 +8,9 @@ import org.springframework.stereotype.Component; @ConfigurationPropertiesBinding public class CustomCredentialsConverter implements Converter { - @Override - public Credentials convert(String source) { - String data[] = source.split(","); - return new Credentials(data[0], data[1]); - } + @Override + public Credentials convert(String source) { + String[] data = source.split(","); + return new Credentials(data[0], data[1]); + } } diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java index 8ba42d76f8..e23b30759b 100644 --- a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/MailServer.java @@ -19,41 +19,41 @@ import org.springframework.validation.annotation.Validated; @Validated public class MailServer { - @NotNull - @NotEmpty - private Map propertiesMap; + @NotNull + @NotEmpty + private Map propertiesMap; - @Valid - private MailConfig mailConfig = new MailConfig(); + @Valid + private MailConfig mailConfig = new MailConfig(); - public static class MailConfig { + public static class MailConfig { - @NotBlank - @Email - private String address; + @NotBlank + @Email + private String address; - public String getAddress() { - return address; - } + public String getAddress() { + return address; + } - public void setAddress(String address) { - this.address = address; - } - } + public void setAddress(String address) { + this.address = address; + } + } - public Map getPropertiesMap() { - return propertiesMap; - } + public Map getPropertiesMap() { + return propertiesMap; + } - public void setPropertiesMap(Map propertiesMap) { - this.propertiesMap = propertiesMap; - } + public void setPropertiesMap(Map propertiesMap) { + this.propertiesMap = propertiesMap; + } - public MailConfig getMailConfig() { - return mailConfig; - } + public MailConfig getMailConfig() { + return mailConfig; + } - public void setMailConfig(MailConfig mailConfig) { - this.mailConfig = mailConfig; - } + public void setMailConfig(MailConfig mailConfig) { + this.mailConfig = mailConfig; + } } \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java index 9747858ed7..9b2ea39299 100644 --- a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/PropertyConversion.java @@ -13,55 +13,55 @@ import org.springframework.util.unit.DataUnit; @ConfigurationProperties(prefix = "server") public class PropertyConversion { - private DataSize uploadSpeed; + private DataSize uploadSpeed; - @DataSizeUnit(DataUnit.GIGABYTES) - private DataSize downloadSpeed; + @DataSizeUnit(DataUnit.GIGABYTES) + private DataSize downloadSpeed; - private Duration backupDay; + private Duration backupDay; - @DurationUnit(ChronoUnit.HOURS) - private Duration backupHour; + @DurationUnit(ChronoUnit.HOURS) + private Duration backupHour; - private Credentials credentials; + private Credentials credentials; - public Duration getBackupDay() { - return backupDay; - } + public Duration getBackupDay() { + return backupDay; + } - public void setBackupDay(Duration backupDay) { - this.backupDay = backupDay; - } + public void setBackupDay(Duration backupDay) { + this.backupDay = backupDay; + } - public Duration getBackupHour() { - return backupHour; - } + public Duration getBackupHour() { + return backupHour; + } - public void setBackupHour(Duration backupHour) { - this.backupHour = backupHour; - } + public void setBackupHour(Duration backupHour) { + this.backupHour = backupHour; + } - public DataSize getUploadSpeed() { - return uploadSpeed; - } + public DataSize getUploadSpeed() { + return uploadSpeed; + } - public void setUploadSpeed(DataSize uploadSpeed) { - this.uploadSpeed = uploadSpeed; - } + public void setUploadSpeed(DataSize uploadSpeed) { + this.uploadSpeed = uploadSpeed; + } - public DataSize getDownloadSpeed() { - return downloadSpeed; - } + public DataSize getDownloadSpeed() { + return downloadSpeed; + } - public void setDownloadSpeed(DataSize downloadSpeed) { - this.downloadSpeed = downloadSpeed; - } + public void setDownloadSpeed(DataSize downloadSpeed) { + this.downloadSpeed = downloadSpeed; + } - public Credentials getCredentials() { - return credentials; - } + public Credentials getCredentials() { + return credentials; + } - public void setCredentials(Credentials credentials) { - this.credentials = credentials; - } + public void setCredentials(Credentials credentials) { + this.credentials = credentials; + } } diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java index bfacc6e679..0c9e62445a 100644 --- a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfig.java @@ -9,35 +9,35 @@ import org.springframework.context.annotation.Configuration; @ConfigurationProperties(prefix = "server") public class ServerConfig { - private Address address; - private Map resourcesPath; + private Address address; + private Map resourcesPath; - public static class Address { + public static class Address { - private String ip; + private String ip; - public String getIp() { - return ip; - } + public String getIp() { + return ip; + } - public void setIp(String ip) { - this.ip = ip; - } - } + public void setIp(String ip) { + this.ip = ip; + } + } - public Address getAddress() { - return address; - } + public Address getAddress() { + return address; + } - public void setAddress(Address address) { - this.address = address; - } + public void setAddress(Address address) { + this.address = address; + } - public Map getResourcesPath() { - return resourcesPath; - } + public Map getResourcesPath() { + return resourcesPath; + } - public void setResourcesPath(Map resourcesPath) { - this.resourcesPath = resourcesPath; - } + public void setResourcesPath(Map resourcesPath) { + this.resourcesPath = resourcesPath; + } } diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java index 8d74de1732..9d337123c7 100644 --- a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java @@ -7,9 +7,9 @@ import org.springframework.context.annotation.Configuration; @Configuration public class ServerConfigFactory { - @Bean - @ConfigurationProperties(prefix = "server.default") - public ServerConfig getDefaultConfigs() { - return new ServerConfig(); - } + @Bean + @ConfigurationProperties(prefix = "server.default") + public ServerConfig getDefaultConfigs() { + return new ServerConfig(); + } } \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java index a5bc09e290..743cb26a18 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java @@ -19,16 +19,18 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @TestPropertySource("classpath:server-config-test.properties") public class BindingPropertiesToBeanMethodsUnitTest { - @Autowired - private ServerConfigFactory configFactory; + @Autowired + private ServerConfigFactory configFactory; - @Test - void givenBeanAnnotatedMethod_whenBindingProperties_thenAllFieldsAreSet() { + @Test + void givenBeanAnnotatedMethod_whenBindingProperties_thenAllFieldsAreSet() { + assertEquals("192.168.0.2", configFactory.getDefaultConfigs() + .getAddress() + .getIp()); - assertEquals("192.168.0.2", configFactory.getDefaultConfigs().getAddress().getIp()); - - Map expectedResourcesPath = new HashMap<>(); - expectedResourcesPath.put("imgs", "/root/def/imgs"); - assertEquals(expectedResourcesPath, configFactory.getDefaultConfigs().getResourcesPath()); - } + 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-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java index 96bbc6639a..9db906fa04 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToUserDefinedPOJOUnitTest.java @@ -17,16 +17,16 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @TestPropertySource("classpath:server-config-test.properties") public class BindingPropertiesToUserDefinedPOJOUnitTest { - @Autowired - private ServerConfig serverConfig; + @Autowired + private ServerConfig serverConfig; - @Test - void givenUserDefinedPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() { + @Test + void givenUserDefinedPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() { + assertEquals("192.168.0.1", serverConfig.getAddress() + .getIp()); - assertEquals("192.168.0.1", serverConfig.getAddress().getIp()); - - Map expectedResourcesPath = new HashMap<>(); - expectedResourcesPath.put("imgs", "/root/imgs"); - assertEquals(expectedResourcesPath, serverConfig.getResourcesPath()); - } + Map expectedResourcesPath = new HashMap<>(); + expectedResourcesPath.put("imgs", "/root/imgs"); + assertEquals(expectedResourcesPath, serverConfig.getResourcesPath()); + } } \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java index eda8eb7f6b..5543f5e9e8 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingYMLPropertiesUnitTest.java @@ -18,16 +18,16 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @ActiveProfiles("test") public class BindingYMLPropertiesUnitTest { - @Autowired - private ServerConfig serverConfig; + @Autowired + private ServerConfig serverConfig; - @Test - void whenBindingYMLConfigFile_thenAllFieldsAreSet() { + @Test + void whenBindingYMLConfigFile_thenAllFieldsAreSet() { + assertEquals("192.168.0.4", serverConfig.getAddress() + .getIp()); - assertEquals("192.168.0.4", serverConfig.getAddress().getIp()); - - Map expectedResourcesPath = new HashMap<>(); - expectedResourcesPath.put("imgs", "/etc/test/imgs"); - assertEquals(expectedResourcesPath, serverConfig.getResourcesPath()); - } + 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-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/OverridingConfigurationPropertiesUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/OverridingConfigurationPropertiesUnitTest.java index 8631308296..2779b0a313 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/OverridingConfigurationPropertiesUnitTest.java +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/OverridingConfigurationPropertiesUnitTest.java @@ -17,17 +17,17 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @TestPropertySource(properties = { "validate.mail_config.address=new_user@test" }) public class OverridingConfigurationPropertiesUnitTest { - @Autowired - private MailServer mailServer; + @Autowired + private MailServer mailServer; - @Test - void givenUsingPropertiesAttribute_whenAssiginingNewValueToProprty_thenSpringUsesNewValue() { + @Test + void givenUsingPropertiesAttribute_whenAssiginingNewValueToProprty_thenSpringUsesNewValue() { + assertEquals("new_user@test", mailServer.getMailConfig() + .getAddress()); - assertEquals("new_user@test", mailServer.getMailConfig().getAddress()); - - Map expectedMap = new HashMap<>(); - expectedMap.put("first", "prop1"); - expectedMap.put("second", "prop2"); - assertEquals(expectedMap, mailServer.getPropertiesMap()); - } + Map expectedMap = new HashMap<>(); + expectedMap.put("first", "prop1"); + expectedMap.put("second", "prop2"); + assertEquals(expectedMap, mailServer.getPropertiesMap()); + } } \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java index 8833011425..939471dd67 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/PropertyValidationUnitTest.java @@ -18,20 +18,22 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @TestPropertySource("classpath:property-validation-test.properties") public class PropertyValidationUnitTest { - @Autowired - private MailServer mailServer; + @Autowired + private MailServer mailServer; - private static Validator propertyValidator; + private static Validator propertyValidator; - @BeforeAll - public static void setup() { - propertyValidator = Validation.buildDefaultValidatorFactory().getValidator(); - } + @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()); - } + @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-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java index d24d563de4..3f2da2a669 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/SpringPropertiesConversionUnitTest.java @@ -19,27 +19,26 @@ import org.springframework.util.unit.DataSize; @TestPropertySource("classpath:spring-conversion-test.properties") public class SpringPropertiesConversionUnitTest { - @Autowired - private PropertyConversion propertyConversion; + @Autowired + private PropertyConversion propertyConversion; - @Test - void whenUsingSpringDefaultSizeConversion_thenDataSizeObjectIsSet() { + @Test + void whenUsingSpringDefaultSizeConversion_thenDataSizeObjectIsSet() { + assertEquals(DataSize.ofMegabytes(500), propertyConversion.getUploadSpeed()); + assertEquals(DataSize.ofGigabytes(10), propertyConversion.getDownloadSpeed()); + } - 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 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()); - } + @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-modules/spring-boot-testing/src/test/resources/application.yml b/spring-boot-modules/spring-boot-testing/src/test/resources/application.yml index 7dfd4d98a8..1b46b0f1ff 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/resources/application.yml +++ b/spring-boot-modules/spring-boot-testing/src/test/resources/application.yml @@ -4,7 +4,7 @@ server: address: ip: 192.168.0.4 resources_path: - imgs: /etc/test/imgs + imgs: /etc/test/imgs --- spring: profiles: dev From 43f74f9c47bdbf2e524596c42d0cdd159e2b3cc1 Mon Sep 17 00:00:00 2001 From: pazis Date: Wed, 26 Feb 2020 09:20:31 +0000 Subject: [PATCH 10/21] new package name --- apache-beam/pom.xml | 74 +++++++++---------- .../apache/beam/{ => intro}/WordCount.java | 2 +- .../beam/{ => intro}/WordCountUnitTest.java | 6 +- 3 files changed, 42 insertions(+), 40 deletions(-) rename apache-beam/src/main/java/com/baeldung/apache/beam/{ => intro}/WordCount.java (98%) rename apache-beam/src/test/java/com/baeldung/apache/beam/{ => intro}/WordCountUnitTest.java (77%) diff --git a/apache-beam/pom.xml b/apache-beam/pom.xml index 1076d06e2a..355e5b32e6 100644 --- a/apache-beam/pom.xml +++ b/apache-beam/pom.xml @@ -1,44 +1,44 @@ - 4.0.0 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + - com.baeldung.apache - apache-beam - 0.0.1-SNAPSHOT - apache-beam + com.baeldung.apache + apache-beam + 0.0.1-SNAPSHOT + apache-beam - - - org.apache.beam - beam-sdks-java-core - ${beam.version} - - - - org.apache.beam - beam-runners-direct-java - ${beam.version} - runtime - - - - org.assertj - assertj-core - ${assertj.version} - test - - + + + org.apache.beam + beam-sdks-java-core + ${beam.version} + + + + org.apache.beam + beam-runners-direct-java + ${beam.version} + runtime + + + + org.assertj + assertj-core + ${assertj.version} + test + + - - 2.19.0 - 3.6.1 - + + 2.19.0 + 3.6.1 + \ No newline at end of file diff --git a/apache-beam/src/main/java/com/baeldung/apache/beam/WordCount.java b/apache-beam/src/main/java/com/baeldung/apache/beam/intro/WordCount.java similarity index 98% rename from apache-beam/src/main/java/com/baeldung/apache/beam/WordCount.java rename to apache-beam/src/main/java/com/baeldung/apache/beam/intro/WordCount.java index 632cc97e6c..f2dfb47810 100644 --- a/apache-beam/src/main/java/com/baeldung/apache/beam/WordCount.java +++ b/apache-beam/src/main/java/com/baeldung/apache/beam/intro/WordCount.java @@ -1,4 +1,4 @@ -package com.baeldung.apache.beam; +package com.baeldung.apache.beam.intro; import java.util.Arrays; diff --git a/apache-beam/src/test/java/com/baeldung/apache/beam/WordCountUnitTest.java b/apache-beam/src/test/java/com/baeldung/apache/beam/intro/WordCountUnitTest.java similarity index 77% rename from apache-beam/src/test/java/com/baeldung/apache/beam/WordCountUnitTest.java rename to apache-beam/src/test/java/com/baeldung/apache/beam/intro/WordCountUnitTest.java index ff338a292c..f2558635dc 100644 --- a/apache-beam/src/test/java/com/baeldung/apache/beam/WordCountUnitTest.java +++ b/apache-beam/src/test/java/com/baeldung/apache/beam/intro/WordCountUnitTest.java @@ -1,14 +1,16 @@ -package com.baeldung.apache.beam; +package com.baeldung.apache.beam.intro; import static org.junit.Assert.assertTrue; import org.junit.Ignore; import org.junit.Test; +import com.baeldung.apache.beam.intro.WordCount; + public class WordCountUnitTest { @Test - @Ignore + // @Ignore public void givenInputFile_whenWordCountRuns_thenJobFinishWithoutError() { boolean jobDone = WordCount.wordCount("src/test/resources/wordcount.txt", "target/output"); assertTrue(jobDone); From e57dbf3c72c264f1622e6c1d3d8e672e47ccb160 Mon Sep 17 00:00:00 2001 From: Pazis Date: Wed, 26 Feb 2020 12:51:26 +0330 Subject: [PATCH 11/21] delete .gitignore --- apache-beam/.gitignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 apache-beam/.gitignore diff --git a/apache-beam/.gitignore b/apache-beam/.gitignore deleted file mode 100644 index 84c048a73c..0000000000 --- a/apache-beam/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/build/ From 862e7a3f20d3e8e599b805413fa944d5d48f50b7 Mon Sep 17 00:00:00 2001 From: Pazis Date: Wed, 26 Feb 2020 12:53:01 +0330 Subject: [PATCH 12/21] update main pom.xml added apache-beam module --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index ca5331b287..63f7b1dcc4 100644 --- a/pom.xml +++ b/pom.xml @@ -350,6 +350,7 @@ antlr apache-avro + apache-beam apache-bval apache-curator apache-cxf From e4f3ba564899b207b5a3acb15c4a66f3bbb099ae Mon Sep 17 00:00:00 2001 From: Pazis Date: Wed, 26 Feb 2020 20:03:23 +0330 Subject: [PATCH 13/21] update main pom.xml again --- pom.xml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 63f7b1dcc4..bf87ac0cbf 100644 --- a/pom.xml +++ b/pom.xml @@ -350,7 +350,6 @@ antlr apache-avro - apache-beam apache-bval apache-curator apache-cxf @@ -640,7 +639,6 @@ spring-batch spring-bom spring-boot-modules - spring-boot-parent spring-boot-rest spring-caching @@ -862,6 +860,7 @@ antlr apache-avro + apache-beam apache-bval apache-curator apache-cxf @@ -1142,7 +1141,6 @@ spring-batch spring-bom spring-boot-modules - spring-boot-parent spring-boot-rest spring-caching From 2b44a323ebcfd356a218accb5fbceab850dc1de5 Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Thu, 27 Feb 2020 16:59:16 +0200 Subject: [PATCH 14/21] Autowiring ServerConfig in @Bean method test class --- .../configurationproperties/ServerConfigFactory.java | 2 +- .../BindingPropertiesToBeanMethodsUnitTest.java | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java index 9d337123c7..ca85d59112 100644 --- a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java @@ -7,7 +7,7 @@ import org.springframework.context.annotation.Configuration; @Configuration public class ServerConfigFactory { - @Bean + @Bean(name = "default_bean") @ConfigurationProperties(prefix = "server.default") public ServerConfig getDefaultConfigs() { return new ServerConfig(); diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java index 743cb26a18..82c2a55c32 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java @@ -8,6 +8,7 @@ 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.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; @@ -20,17 +21,16 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; public class BindingPropertiesToBeanMethodsUnitTest { @Autowired - private ServerConfigFactory configFactory; + @Qualifier("default_bean") + private ServerConfig serverConfig; @Test void givenBeanAnnotatedMethod_whenBindingProperties_thenAllFieldsAreSet() { - assertEquals("192.168.0.2", configFactory.getDefaultConfigs() - .getAddress() + assertEquals("192.168.0.2", serverConfig.getAddress() .getIp()); Map expectedResourcesPath = new HashMap<>(); expectedResourcesPath.put("imgs", "/root/def/imgs"); - assertEquals(expectedResourcesPath, configFactory.getDefaultConfigs() - .getResourcesPath()); + assertEquals(expectedResourcesPath, serverConfig.getResourcesPath()); } } \ No newline at end of file From 77be69798059a66ea214f6d40bd715e015e67d2d Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Mon, 2 Mar 2020 15:02:08 -0500 Subject: [PATCH 15/21] BAEL-3900 Fixed ApplicationLiveTest. --- .../java/org/baeldung/custom/Application.java | 2 ++ .../baeldung/custom/config/SecurityConfig.java | 15 ++++++++++++++- .../main/java/org/baeldung/ip/IpApplication.java | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/Application.java b/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/Application.java index 2bd0da48d2..e051e5a853 100644 --- a/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/Application.java +++ b/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/Application.java @@ -3,9 +3,11 @@ package org.baeldung.custom; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; @SpringBootApplication +@ComponentScan("org.baeldung.custom") @PropertySource("classpath:application-defaults.properties") public class Application extends SpringBootServletInitializer { public static void main(String[] args) { diff --git a/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/config/SecurityConfig.java b/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/config/SecurityConfig.java index 739e4d3417..955b3bb214 100644 --- a/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/config/SecurityConfig.java +++ b/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/config/SecurityConfig.java @@ -2,12 +2,25 @@ package org.baeldung.custom.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration -public class SecurityConfig { +@EnableWebSecurity +public class SecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(final HttpSecurity http) throws Exception { + http.csrf() + .disable() + .authorizeRequests().anyRequest().authenticated() + .and() + .formLogin().permitAll(); + } + @Bean public PasswordEncoder encoder() { return new BCryptPasswordEncoder(11); diff --git a/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/ip/IpApplication.java b/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/ip/IpApplication.java index b68abbaed1..d77414c54e 100644 --- a/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/ip/IpApplication.java +++ b/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/ip/IpApplication.java @@ -3,9 +3,11 @@ package org.baeldung.ip; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; @SpringBootApplication +@ComponentScan("org.baeldung.ip") @PropertySource("classpath:application-defaults.properties") public class IpApplication extends SpringBootServletInitializer { public static void main(String[] args) { From 812f54038634d4f42c97c7dd29f89860aebffb8c Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 3 Mar 2020 17:28:47 -0500 Subject: [PATCH 16/21] fixed format --- .../java/org/baeldung/custom/config/SecurityConfig.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/config/SecurityConfig.java b/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/config/SecurityConfig.java index 955b3bb214..06357650dc 100644 --- a/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/config/SecurityConfig.java +++ b/spring-security-modules/spring-security-mvc-boot-1/src/main/java/org/baeldung/custom/config/SecurityConfig.java @@ -16,11 +16,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { protected void configure(final HttpSecurity http) throws Exception { http.csrf() .disable() - .authorizeRequests().anyRequest().authenticated() + .authorizeRequests() + .anyRequest() + .authenticated() .and() - .formLogin().permitAll(); + .formLogin() + .permitAll(); } - + @Bean public PasswordEncoder encoder() { return new BCryptPasswordEncoder(11); From 976839d8af88ea0d1c62fa0bc7b000b94123db24 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 3 Mar 2020 18:46:32 -0500 Subject: [PATCH 17/21] added notice on how to run the live tests --- .../src/test/java/org/baeldung/web/ApplicationLiveTest.java | 2 +- .../src/test/java/org/baeldung/web/IpLiveTest.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-security-modules/spring-security-mvc-boot-1/src/test/java/org/baeldung/web/ApplicationLiveTest.java b/spring-security-modules/spring-security-mvc-boot-1/src/test/java/org/baeldung/web/ApplicationLiveTest.java index dfcfcad609..e199411e58 100644 --- a/spring-security-modules/spring-security-mvc-boot-1/src/test/java/org/baeldung/web/ApplicationLiveTest.java +++ b/spring-security-modules/spring-security-mvc-boot-1/src/test/java/org/baeldung/web/ApplicationLiveTest.java @@ -13,7 +13,7 @@ import io.restassured.specification.RequestSpecification; import org.junit.Test; import org.springframework.http.MediaType; - +// In order to execute these tests, org.baeldung.custom.Application needs to be running. public class ApplicationLiveTest { @Test diff --git a/spring-security-modules/spring-security-mvc-boot-1/src/test/java/org/baeldung/web/IpLiveTest.java b/spring-security-modules/spring-security-mvc-boot-1/src/test/java/org/baeldung/web/IpLiveTest.java index e12e2f87b0..761f5450f7 100644 --- a/spring-security-modules/spring-security-mvc-boot-1/src/test/java/org/baeldung/web/IpLiveTest.java +++ b/spring-security-modules/spring-security-mvc-boot-1/src/test/java/org/baeldung/web/IpLiveTest.java @@ -8,6 +8,7 @@ import io.restassured.response.Response; import org.junit.Test; +// In order to execute these tests, org.baeldung.ip.IpApplication needs to be running. public class IpLiveTest { @Test From 17852cfe6d0523c33bd448c6d73a38b34bfba707 Mon Sep 17 00:00:00 2001 From: Pazis Date: Thu, 5 Mar 2020 06:42:17 +0330 Subject: [PATCH 18/21] Update pom.xml --- apache-beam/pom.xml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apache-beam/pom.xml b/apache-beam/pom.xml index 355e5b32e6..7a714ac480 100644 --- a/apache-beam/pom.xml +++ b/apache-beam/pom.xml @@ -12,7 +12,6 @@ com.baeldung.apache apache-beam 0.0.1-SNAPSHOT - apache-beam @@ -41,4 +40,4 @@ 3.6.1 - \ No newline at end of file + From 7a2c9410249efb879bc37f28eb0b373713be1d1b Mon Sep 17 00:00:00 2001 From: Sampada <46674082+sampada07@users.noreply.github.com> Date: Fri, 6 Mar 2020 21:19:00 +0530 Subject: [PATCH 19/21] BAEL-3883: Boruvka's Algorithm for Minimum-Spanning Trees (#8816) * BAEL-3883: Boruvka's Algorithm for Minimum-Spanning Trees * BAEL-3883: checking in pom.xml --- algorithms-miscellaneous-5/pom.xml | 12 ++++ .../algorithms/boruvka/BoruvkaMST.java | 61 +++++++++++++++++ .../algorithms/boruvka/DisjointSet.java | 49 ++++++++++++++ .../com/baeldung/algorithms/boruvka/Edge.java | 40 ++++++++++++ .../baeldung/algorithms/boruvka/Graph.java | 64 ++++++++++++++++++ .../baeldung/algorithms/boruvka/Input.java | 65 +++++++++++++++++++ .../com/baeldung/algorithms/boruvka/Tree.java | 63 ++++++++++++++++++ .../algorithms/boruvka/BoruvkaUnitTest.java | 48 ++++++++++++++ .../src/test/resources/input.json | 41 ++++++++++++ 9 files changed, 443 insertions(+) create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/BoruvkaMST.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/DisjointSet.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Edge.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Graph.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Input.java create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Tree.java create mode 100644 algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/boruvka/BoruvkaUnitTest.java create mode 100644 algorithms-miscellaneous-5/src/test/resources/input.json diff --git a/algorithms-miscellaneous-5/pom.xml b/algorithms-miscellaneous-5/pom.xml index 4f9cc8b711..2799c39971 100644 --- a/algorithms-miscellaneous-5/pom.xml +++ b/algorithms-miscellaneous-5/pom.xml @@ -39,6 +39,16 @@ guava ${guava.version} + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + org.junit.platform + junit-platform-commons + ${junit.platform.version} + org.assertj @@ -66,6 +76,8 @@ 1.11 3.6.1 28.1-jre + 2.10.2 + 1.6.0 \ No newline at end of file diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/BoruvkaMST.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/BoruvkaMST.java new file mode 100644 index 0000000000..0f8bc5b296 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/BoruvkaMST.java @@ -0,0 +1,61 @@ +package com.baeldung.algorithms.boruvka; + +public class BoruvkaMST { + + private static Tree mst = new Tree(); + private static int totalWeight; + + public BoruvkaMST(Graph graph) { + DisjointSet dSet = new DisjointSet(graph.getNodes()); + + // repeat at most log N times or until we have N-1 edges + for (int t = 1; t < graph.getNodes() && mst.getEdgeCount() < graph.getNodes() - 1; t = t + t) { + + // foreach tree in forest, find closest edge + Edge[] closestEdgeArray = new Edge[graph.getNodes()]; + for (Edge edge : graph.getAllEdges()) { + int first = edge.getFirst(); + int second = edge.getSecond(); + int firstParent = dSet.getParent(first); + int secondParent = dSet.getParent(second); + if (firstParent == secondParent) { + continue; // same tree + } + if (closestEdgeArray[firstParent] == null || edge.getWeight() < closestEdgeArray[firstParent].getWeight()) { + closestEdgeArray[firstParent] = edge; + } + if (closestEdgeArray[secondParent] == null || edge.getWeight() < closestEdgeArray[secondParent].getWeight()) { + closestEdgeArray[secondParent] = edge; + } + } + + // add newly discovered edges to MST + for (int i = 0; i < graph.getNodes(); i++) { + Edge edge = closestEdgeArray[i]; + if (edge != null) { + int first = edge.getFirst(); + int second = edge.getSecond(); + // don't add the same edge twice + if (dSet.getParent(first) != dSet.getParent(second)) { + mst.addEdge(edge); + totalWeight += edge.getWeight(); + dSet.union(first, second); + } + } + } + } + } + + public Iterable getMST() { + return mst; + } + + public int getTotalWeight() { + return totalWeight; + } + + public String toString() { + return "MST: " + mst.toString() + " | Total Weight: " + totalWeight; + } + +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/DisjointSet.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/DisjointSet.java new file mode 100644 index 0000000000..7769686e36 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/DisjointSet.java @@ -0,0 +1,49 @@ +package com.baeldung.algorithms.boruvka; + +import java.util.Arrays; + +public class DisjointSet { + + private int[] nodeParents; + private int[] nodeRanks; + + public DisjointSet(int n) { + nodeParents = new int[n]; + nodeRanks = new int[n]; + for (int i = 0; i < n; i++) { + nodeParents[i] = i; + nodeRanks[i] = 0; + } + } + + public int getParent(int node) { + while (node != nodeParents[node]) { + node = nodeParents[node]; + } + return node; + } + + public void union(int node1, int node2) { + int node1Parent = getParent(node1); + int node2Parent = getParent(node2); + if (node1Parent == node2Parent) { + return; + } + + if (nodeRanks[node1Parent] < nodeRanks[node2Parent]) { + nodeParents[node1Parent] = node2Parent; + } + else if (nodeRanks[node1Parent] > nodeRanks[node2Parent]) { + nodeParents[node2Parent] = node1Parent; + } + else { + nodeParents[node2Parent] = node1Parent; + nodeRanks[node1Parent]++; + } + } + + public String toString() { + return "Parent: " + Arrays.toString(nodeParents) + "Rank: " + Arrays.toString(nodeRanks); + } + +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Edge.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Edge.java new file mode 100644 index 0000000000..6ee136fc48 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Edge.java @@ -0,0 +1,40 @@ +package com.baeldung.algorithms.boruvka; + +public class Edge { + + private final int first; + private final int second; + private final int weight; + + public Edge(int first, int second, int weight) { + this.first = first; + this.second = second; + this.weight = weight; + } + + public double getWeight() { + return weight; + } + + public int getFirst() { + return first; + } + + public int getSecond() { + return second; + } + + public int getOtherNode(int firstNode) { + int secondNode = 0; + if (firstNode == first) + secondNode = second; + else if (firstNode == second) + secondNode = first; + return secondNode; + } + + public String toString() { + return String.format("%d-%d %d", first, second, weight); + } + +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Graph.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Graph.java new file mode 100644 index 0000000000..e899007dfa --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Graph.java @@ -0,0 +1,64 @@ +package com.baeldung.algorithms.boruvka; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; + +public class Graph { + + private int nodes; + private int edges; + private Tree[] trees; + + public Graph(Input jsonGraph) throws JsonParseException, JsonMappingException, IOException { + nodes = jsonGraph.getNodes(); + trees = (Tree[]) new Tree[nodes]; + for (int i = 0; i < nodes; i++) { + trees[i] = new Tree(); + } + + int edgesFromInput = jsonGraph.getEdges(); + for (int i = 0; i < edgesFromInput; i++) { + int first = jsonGraph.getEdgeList() + .get(i) + .getFirst(); + int second = jsonGraph.getEdgeList() + .get(i) + .getSecond(); + int weight = jsonGraph.getEdgeList() + .get(i) + .getWeight(); + Edge edge = new Edge(first, second, weight); + + trees[first].addEdge(edge); + trees[second].addEdge(edge); + edges++; + } + + } + + public int getNodes() { + return nodes; + } + + public int getEdges() { + return edges; + } + + public Iterable iterableTree(int i) { + return trees[i]; + } + + public Iterable getAllEdges() { + Iterable list = new Tree(); + for (int i = 0; i < nodes; i++) { + for (Edge edge : iterableTree(i)) { + if (edge.getOtherNode(i) > i) { + ((Tree) list).addEdge(edge); + } + } + } + return list; + } +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Input.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Input.java new file mode 100644 index 0000000000..b52720d5e9 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Input.java @@ -0,0 +1,65 @@ +package com.baeldung.algorithms.boruvka; + +import java.util.List; + +public class Input { + private int nodes; + private int edges; + private List edgeList; + + public int getNodes() { + return nodes; + } + + public void setNodes(int nodes) { + this.nodes = nodes; + } + + public int getEdges() { + return edges; + } + + public void setEdges(int edges) { + this.edges = edges; + } + + public List getEdgeList() { + return edgeList; + } + + public void setEdgeList(List edgeList) { + this.edgeList = edgeList; + } + + static class E { + private int first; + private int second; + private int weight; + + public int getFirst() { + return first; + } + + public void setFirst(int first) { + this.first = first; + } + + public int getSecond() { + return second; + } + + public void setSecond(int second) { + this.second = second; + } + + public int getWeight() { + return weight; + } + + public void setWeight(int weight) { + this.weight = weight; + } + + } + +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Tree.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Tree.java new file mode 100644 index 0000000000..cc28233e07 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/boruvka/Tree.java @@ -0,0 +1,63 @@ +package com.baeldung.algorithms.boruvka; + +import java.util.Iterator; + +public class Tree implements Iterable { + private Node root; + private int edgeCount; + + private static class Node { + private Edge edge; + private Node next; + + public String toString() { + String nextStr = next != null ? next.toString() : ""; + return edge.toString() + " | " + nextStr; + } + } + + public Tree() { + root = null; + edgeCount = 0; + } + + public int getEdgeCount() { + return edgeCount; + } + + public void addEdge(Edge edge) { + Node oldRoot = root; + root = new Node(); + root.edge = edge; + root.next = oldRoot; + edgeCount++; + } + + public String toString() { + String rootStr = root != null ? root.toString() : ""; + return "Tree: " + rootStr + "Size: " + edgeCount; + } + + public Iterator iterator() { + return new LinkedIterator(root); + } + + private class LinkedIterator implements Iterator { + private Node current; + + public LinkedIterator(Node root) { + current = root; + } + + public boolean hasNext() { + return current != null; + } + + public Edge next() { + Edge edge = current.edge; + current = current.next; + return edge; + } + } + +} diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/boruvka/BoruvkaUnitTest.java b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/boruvka/BoruvkaUnitTest.java new file mode 100644 index 0000000000..1d03d2d4d9 --- /dev/null +++ b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/boruvka/BoruvkaUnitTest.java @@ -0,0 +1,48 @@ +package com.baeldung.algorithms.boruvka; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +import org.junit.Before; +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class BoruvkaUnitTest { + + private Input input; + private static String INPUT_JSON = "/input.json"; + + @Before + public void convertInputJsonToObject() throws JsonParseException, JsonMappingException, IOException { + ObjectMapper mapper = new ObjectMapper(); + StringBuilder jsonStr = new StringBuilder(); + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(BoruvkaMST.class.getResourceAsStream(INPUT_JSON)))) { + String line; + while ((line = bufferedReader.readLine()) != null) { + jsonStr.append(line) + .append("\n"); + } + } + + input = mapper.readValue(jsonStr.toString(), Input.class); + + } + + @Test + public void givenInputGraph_whenBoruvkaPerformed_thenMinimumSpanningTree() throws JsonParseException, JsonMappingException, IOException { + Graph graph = new Graph(input); + BoruvkaMST boruvkaMST = new BoruvkaMST(graph); + + Tree mst = (Tree) boruvkaMST.getMST(); + + assertEquals(30, boruvkaMST.getTotalWeight()); + assertEquals(4, mst.getEdgeCount()); + } + +} diff --git a/algorithms-miscellaneous-5/src/test/resources/input.json b/algorithms-miscellaneous-5/src/test/resources/input.json new file mode 100644 index 0000000000..0151f83618 --- /dev/null +++ b/algorithms-miscellaneous-5/src/test/resources/input.json @@ -0,0 +1,41 @@ +{ + "nodes": 5, + "edges": 7, + "edgeList": [ + { + "first": 0, + "second": 1, + "weight": 8 + }, + { + "first": 0, + "second": 2, + "weight": 5 + }, + { + "first": 1, + "second": 2, + "weight": 9 + }, + { + "first": 1, + "second": 3, + "weight": 11 + }, + { + "first": 2, + "second": 3, + "weight": 15 + }, + { + "first": 2, + "second": 4, + "weight": 10 + }, + { + "first": 3, + "second": 4, + "weight": 7 + } + ] +} \ No newline at end of file From 40a17f0f63fb0fed6549dc920922fa4b1c825f58 Mon Sep 17 00:00:00 2001 From: sergio41 Date: Fri, 6 Mar 2020 18:05:48 +0100 Subject: [PATCH 20/21] BAEL-3765: Code Upload (#8804) --- .../com/baeldung/version/VersionObtainer.java | 20 +++++++++++++ .../version/VersionObtainerUnitTest.java | 30 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 spring-5/src/main/java/com/baeldung/version/VersionObtainer.java create mode 100644 spring-5/src/test/java/com/baeldung/version/VersionObtainerUnitTest.java diff --git a/spring-5/src/main/java/com/baeldung/version/VersionObtainer.java b/spring-5/src/main/java/com/baeldung/version/VersionObtainer.java new file mode 100644 index 0000000000..392cc44a72 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/version/VersionObtainer.java @@ -0,0 +1,20 @@ +package com.baeldung.version; + +import org.springframework.boot.system.JavaVersion; +import org.springframework.boot.system.SystemProperties; +import org.springframework.core.SpringVersion; + +public class VersionObtainer { + + public String getSpringVersion() { + return SpringVersion.getVersion(); + } + + public String getJavaVersion() { + return JavaVersion.getJavaVersion().toString(); + } + + public String getJdkVersion() { + return SystemProperties.get("java.version"); + } +} diff --git a/spring-5/src/test/java/com/baeldung/version/VersionObtainerUnitTest.java b/spring-5/src/test/java/com/baeldung/version/VersionObtainerUnitTest.java new file mode 100644 index 0000000000..6d77cd1403 --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/version/VersionObtainerUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.version; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest(classes = VersionObtainer.class) +public class VersionObtainerUnitTest { + + public VersionObtainer version = new VersionObtainer(); + + @Test + public void testGetSpringVersion() { + String res = version.getSpringVersion(); + assertThat(res).isNotEmpty(); + } + + @Test + public void testGetJdkVersion() { + String res = version.getJdkVersion(); + assertThat(res).isNotEmpty(); + } + + @Test + public void testGetJavaVersion() { + String res = version.getJavaVersion(); + assertThat(res).isNotEmpty(); + } +} From 6600942cef5b1a567945b409bf4a33ac60c16a4d Mon Sep 17 00:00:00 2001 From: Somnath Musib <7885767+musibs@users.noreply.github.com> Date: Sat, 7 Mar 2020 04:12:06 +1100 Subject: [PATCH 21/21] BAEL-3838 Capturing a Java Thread Dump (#8780) Co-authored-by: Somnath Musib --- .../com/baeldung/threaddump/ThreadDump.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 core-java-modules/core-java-perf/src/main/java/com/baeldung/threaddump/ThreadDump.java diff --git a/core-java-modules/core-java-perf/src/main/java/com/baeldung/threaddump/ThreadDump.java b/core-java-modules/core-java-perf/src/main/java/com/baeldung/threaddump/ThreadDump.java new file mode 100644 index 0000000000..17bc840b8d --- /dev/null +++ b/core-java-modules/core-java-perf/src/main/java/com/baeldung/threaddump/ThreadDump.java @@ -0,0 +1,28 @@ +package com.baeldung.threaddump; + +import java.io.IOException; +import java.lang.management.ManagementFactory; +import java.lang.management.ThreadInfo; +import java.lang.management.ThreadMXBean; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class ThreadDump { + + public static void main(String[] args) throws IOException { + threadDump(true, true); + } + + private static void threadDump(boolean lockedMonitors, boolean lockedSynchronizers) throws IOException { + Path threadDumpFile = Paths.get("ThreadDump.txt"); + + StringBuffer threadDump = new StringBuffer(System.lineSeparator()); + ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); + for(ThreadInfo threadInfo : threadMXBean.dumpAllThreads(lockedMonitors, lockedSynchronizers)) { + threadDump.append(threadInfo.toString()); + } + Files.write(threadDumpFile, threadDump.toString().getBytes()); + } + +}