first review round changes

This commit is contained in:
m.raheem 2020-02-13 22:03:25 +02:00
parent 5b245cde28
commit 275a1f4545
16 changed files with 130 additions and 188 deletions

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -5,8 +5,6 @@ public class Credentials {
private String username; private String username;
private String password; private String password;
public Credentials() {System.out.println("### INIT2 ###");}
public Credentials(String username, String password) { public Credentials(String username, String password) {
this.username = username; this.username = username;
this.password = password; this.password = password;

View File

@ -1,7 +1,7 @@
package com.baeldung.boot.configurationproperties; package com.baeldung.boot.configurationproperties;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -9,15 +9,12 @@ import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "server") @ConfigurationProperties(prefix = "server")
public class ServerConfig { public class ServerConfig {
private String name;
private Address address; private Address address;
private Map<String, String> dirs; private Map<String, String> resourcesPath;
private Set<String> imgIds;
public static class Address { public static class Address {
private String ip; private String ip;
private int port;
public String getIp() { public String getIp() {
return ip; return ip;
@ -26,22 +23,6 @@ public class ServerConfig {
public void setIp(String ip) { public void setIp(String ip) {
this.ip = 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() { public Address getAddress() {
@ -52,19 +33,11 @@ public class ServerConfig {
this.address = address; this.address = address;
} }
public Set<String> getImgIds() { public Map<String, String> getResourcesPath() {
return imgIds; return resourcesPath;
} }
public void setImgIds(Set<String> imgIds) { public void setResourcesPath(Map<String, String> resourcesPath) {
this.imgIds = imgIds; this.resourcesPath = resourcesPath;
}
public Map<String, String> getDirs() {
return dirs;
}
public void setDirs(Map<String, String> dirs) {
this.dirs = dirs;
} }
} }

View File

@ -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();
}
}

View File

@ -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<String, String> expectedResourcesPath = new HashMap<>();
expectedResourcesPath.put("imgs", "/root/def/imgs");
assertEquals(expectedResourcesPath, configFactory.getDefaultConfigs().getResourcesPath());
}
}

View File

@ -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());
}
}

View File

@ -3,15 +3,17 @@ package com.baeldung.boot.configurationproperties;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; 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") @TestPropertySource("classpath:server-config-test.properties")
public class BindingPropertiesToUserDefinedPOJOUnitTest { public class BindingPropertiesToUserDefinedPOJOUnitTest {
@ -21,19 +23,10 @@ public class BindingPropertiesToUserDefinedPOJOUnitTest {
@Test @Test
void givenUserDefinedPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() { void givenUserDefinedPOJO_whenBindingPropertiesFile_thenAllFieldsAreSet() {
assertEquals("node1", serverConfig.getName());
Set<String> expectedImgs = new HashSet<>();
expectedImgs.add("img1.jpg");
expectedImgs.add("img2.jpg");
assertEquals(expectedImgs, serverConfig.getImgIds());
assertEquals("192.168.0.1", serverConfig.getAddress().getIp()); assertEquals("192.168.0.1", serverConfig.getAddress().getIp());
assertEquals(8099, serverConfig.getAddress().getPort());
Map<String, String> expectedDirs = new HashMap<>(); Map<String, String> expectedResourcesPath = new HashMap<>();
expectedDirs.put("imgs", "/root/imgs"); expectedResourcesPath.put("imgs", "/root/imgs");
expectedDirs.put("html", "/root/html"); assertEquals(expectedResourcesPath, serverConfig.getResourcesPath());
assertEquals(expectedDirs, serverConfig.getDirs());
} }
} }

View File

@ -1,16 +1,21 @@
package com.baeldung.boot.configurationproperties; package com.baeldung.boot.configurationproperties;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test; 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.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 { public class BindingYMLPropertiesUnitTest {
@Autowired @Autowired
@ -19,19 +24,10 @@ public class BindingYMLPropertiesUnitTest {
@Test @Test
void whenBindingYMLConfigFile_thenAllFieldsAreSet() { void whenBindingYMLConfigFile_thenAllFieldsAreSet() {
assertEquals("node2", serverConfig.getName()); assertEquals("192.168.0.4", serverConfig.getAddress().getIp());
Set<String> expectedImgs = new HashSet<>(); Map<String, String> expectedResourcesPath = new HashMap<>();
expectedImgs.add("img1.png"); expectedResourcesPath.put("imgs", "/etc/test/imgs");
expectedImgs.add("img2.png"); assertEquals(expectedResourcesPath, serverConfig.getResourcesPath());
assertEquals(expectedImgs, serverConfig.getImgIds());
assertEquals("192.168.0.2", serverConfig.getAddress().getIp());
assertEquals(5000, serverConfig.getAddress().getPort());
Map<String, String> expectedDirs = new HashMap<>();
expectedDirs.put("imgs", "/etc/imgs");
expectedDirs.put("html", "/etc/html");
assertEquals(expectedDirs, serverConfig.getDirs());
} }
} }

View File

@ -7,11 +7,14 @@ import javax.validation.Validator;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; 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.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.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@SpringBootTest @ExtendWith(SpringExtension.class)
@EnableConfigurationProperties(value = MailServer.class)
@TestPropertySource("classpath:property-validation-test.properties") @TestPropertySource("classpath:property-validation-test.properties")
public class PropertyValidationUnitTest { public class PropertyValidationUnitTest {

View File

@ -1,14 +1,21 @@
package com.baeldung.boot.configurationproperties; package com.baeldung.boot.configurationproperties;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Duration; import java.time.Duration;
import org.junit.jupiter.api.Test; 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.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.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.util.unit.DataSize; import org.springframework.util.unit.DataSize;
@SpringBootTest @ExtendWith(SpringExtension.class)
@EnableConfigurationProperties(value = PropertyConversion.class)
@ContextConfiguration(classes = CustomCredentialsConverter.class)
@TestPropertySource("classpath:spring-conversion-test.properties") @TestPropertySource("classpath:spring-conversion-test.properties")
public class SpringPropertiesConversionUnitTest { public class SpringPropertiesConversionUnitTest {

View File

@ -1,11 +1,15 @@
spring:
profiles: test
server: server:
name: node2
address: address:
ip: 192.168.0.2 ip: 192.168.0.4
port: 5000 resources_path:
dirs: imgs: /etc/test/imgs
imgs: /etc/imgs ---
html: /etc/html spring:
imgIds: profiles: dev
- img1.png server:
- img2.png address:
ip: 192.168.0.5
resources_path:
imgs: /etc/dev/imgs

View File

@ -1,11 +1,6 @@
server.NAME=node1
server.address.ip=192.168.0.1 server.address.ip=192.168.0.1
server.address.port=8099 server.resources_path.imgs=/root/imgs
# directories # default config
server.dirs.imgs=/root/imgs server.default.address.ip=192.168.0.2
server.dirs.html=/root/html server.default.resources_path.imgs=/root/def/imgs
server.img_ids[0]=img1.jpg
server.img_ids[1]=img2.jpg

View File

@ -1,7 +0,0 @@
# testing
testing.authorization=foo
testing.timeout=50
# live
live.authorization=bar
live.timeout=100