Merge pull request #5085 from Vizsoro/spring-boot-json-properties
loading json props
This commit is contained in:
commit
89fbcce41d
|
@ -0,0 +1,117 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.hibernate.validator.constraints.NotBlank;
|
||||
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
|
||||
@PropertySource("classpath:configprops.properties")
|
||||
@ConfigurationProperties(prefix = "mail")
|
||||
@Validated
|
||||
public class ConfigProperties {
|
||||
|
||||
@Validated
|
||||
public static class Credentials {
|
||||
|
||||
@Length(max = 4, min = 1)
|
||||
private String authMethod;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getAuthMethod() {
|
||||
return authMethod;
|
||||
}
|
||||
|
||||
public void setAuthMethod(String authMethod) {
|
||||
this.authMethod = authMethod;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
||||
@NotBlank
|
||||
private String host;
|
||||
|
||||
@Min(1025)
|
||||
@Max(65536)
|
||||
private int port;
|
||||
|
||||
@Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$")
|
||||
private String from;
|
||||
|
||||
private Credentials credentials;
|
||||
private List<String> defaultRecipients;
|
||||
private Map<String, String> additionalHeaders;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public Credentials getCredentials() {
|
||||
return credentials;
|
||||
}
|
||||
|
||||
public void setCredentials(Credentials credentials) {
|
||||
this.credentials = credentials;
|
||||
}
|
||||
|
||||
public List<String> getDefaultRecipients() {
|
||||
return defaultRecipients;
|
||||
}
|
||||
|
||||
public void setDefaultRecipients(List<String> defaultRecipients) {
|
||||
this.defaultRecipients = defaultRecipients;
|
||||
}
|
||||
|
||||
public Map<String, String> getAdditionalHeaders() {
|
||||
return additionalHeaders;
|
||||
}
|
||||
|
||||
public void setAdditionalHeaders(Map<String, String> additionalHeaders) {
|
||||
this.additionalHeaders = additionalHeaders;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
|
||||
public class ConfigPropertiesDemoApplication {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())
|
||||
.run();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "custom")
|
||||
public class CustomJsonProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean resend;
|
||||
|
||||
private Person sender;
|
||||
|
||||
public static class Person {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean isResend() {
|
||||
return resend;
|
||||
}
|
||||
|
||||
public void setResend(boolean resend) {
|
||||
this.resend = resend;
|
||||
}
|
||||
|
||||
public Person getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(Person sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
|
||||
@ConfigurationProperties
|
||||
public class JsonProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean resend;
|
||||
|
||||
private List<String> topics;
|
||||
|
||||
private LinkedHashMap<String, ?> sender;
|
||||
|
||||
public LinkedHashMap<String, ?> getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(LinkedHashMap<String, ?> sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public List<String> getTopics() {
|
||||
return topics;
|
||||
}
|
||||
|
||||
public void setTopics(List<String> topics) {
|
||||
this.topics = topics;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean isResend() {
|
||||
return resend;
|
||||
}
|
||||
|
||||
public void setResend(boolean resend) {
|
||||
this.resend = resend;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JsonPropertyContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
private final static String CUSTOM_PREFIX = "custom.";
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||
try {
|
||||
Resource resource = configurableApplicationContext.getResource("classpath:configprops.json");
|
||||
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
|
||||
Set<Map.Entry> set = readValue.entrySet();
|
||||
List<MapPropertySource> propertySources = convertEntrySet(set, Optional.empty());
|
||||
for (PropertySource propertySource : propertySources) {
|
||||
configurableApplicationContext.getEnvironment()
|
||||
.getPropertySources()
|
||||
.addFirst(propertySource);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<MapPropertySource> convertEntrySet(Set<Map.Entry> entrySet, Optional<String> parentKey) {
|
||||
return entrySet.stream()
|
||||
.map((Map.Entry e) -> convertToPropertySourceList(e, parentKey))
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static List<MapPropertySource> convertToPropertySourceList(Map.Entry e, Optional<String> parentKey) {
|
||||
String key = parentKey.map(s -> s + ".")
|
||||
.orElse("") + (String) e.getKey();
|
||||
Object value = e.getValue();
|
||||
return covertToPropertySourceList(key, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static List<MapPropertySource> covertToPropertySourceList(String key, Object value) {
|
||||
if (value instanceof LinkedHashMap) {
|
||||
LinkedHashMap map = (LinkedHashMap) value;
|
||||
Set<Map.Entry> entrySet = map.entrySet();
|
||||
return convertEntrySet(entrySet, Optional.ofNullable(key));
|
||||
}
|
||||
String finalKey = CUSTOM_PREFIX + key;
|
||||
return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value)));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
import org.springframework.core.io.support.PropertySourceFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JsonPropertySourceFactory implements PropertySourceFactory {
|
||||
|
||||
@Override
|
||||
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
|
||||
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
|
||||
return new MapPropertySource("json-property", readValue);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,13 +1,15 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan(basePackageClasses = ConfigProperties.class)
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
|
||||
public class ConfigPropertiesDemoApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ConfigPropertiesDemoApplication.class);
|
||||
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())
|
||||
.run();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "custom")
|
||||
public class CustomJsonProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean resend;
|
||||
|
||||
private Person sender;
|
||||
|
||||
public static class Person {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean isResend() {
|
||||
return resend;
|
||||
}
|
||||
|
||||
public void setResend(boolean resend) {
|
||||
this.resend = resend;
|
||||
}
|
||||
|
||||
public Person getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(Person sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
|
||||
@ConfigurationProperties
|
||||
public class JsonProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean resend;
|
||||
|
||||
private List<String> topics;
|
||||
|
||||
private LinkedHashMap<String, ?> sender;
|
||||
|
||||
public LinkedHashMap<String, ?> getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(LinkedHashMap<String, ?> sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public List<String> getTopics() {
|
||||
return topics;
|
||||
}
|
||||
|
||||
public void setTopics(List<String> topics) {
|
||||
this.topics = topics;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean isResend() {
|
||||
return resend;
|
||||
}
|
||||
|
||||
public void setResend(boolean resend) {
|
||||
this.resend = resend;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JsonPropertyContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
private final static String CUSTOM_PREFIX = "custom.";
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||
try {
|
||||
Resource resource = configurableApplicationContext.getResource("classpath:configprops.json");
|
||||
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
|
||||
Set<Map.Entry> set = readValue.entrySet();
|
||||
List<MapPropertySource> propertySources = convertEntrySet(set, Optional.empty());
|
||||
for (PropertySource propertySource : propertySources) {
|
||||
configurableApplicationContext.getEnvironment()
|
||||
.getPropertySources()
|
||||
.addFirst(propertySource);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<MapPropertySource> convertEntrySet(Set<Map.Entry> entrySet, Optional<String> parentKey) {
|
||||
return entrySet.stream()
|
||||
.map((Map.Entry e) -> convertToPropertySourceList(e, parentKey))
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static List<MapPropertySource> convertToPropertySourceList(Map.Entry e, Optional<String> parentKey) {
|
||||
String key = parentKey.map(s -> s + ".")
|
||||
.orElse("") + (String) e.getKey();
|
||||
Object value = e.getValue();
|
||||
return covertToPropertySourceList(key, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static List<MapPropertySource> covertToPropertySourceList(String key, Object value) {
|
||||
if (value instanceof LinkedHashMap) {
|
||||
LinkedHashMap map = (LinkedHashMap) value;
|
||||
Set<Map.Entry> entrySet = map.entrySet();
|
||||
return convertEntrySet(entrySet, Optional.ofNullable(key));
|
||||
}
|
||||
String finalKey = CUSTOM_PREFIX + key;
|
||||
return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value)));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
import org.springframework.core.io.support.PropertySourceFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JsonPropertySourceFactory implements PropertySourceFactory {
|
||||
|
||||
@Override
|
||||
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
|
||||
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
|
||||
return new MapPropertySource("json-property", readValue);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"host" : "mailer@mail.com",
|
||||
"port" : 9090,
|
||||
"resend" : true,
|
||||
"topics" : ["spring", "boot"],
|
||||
"sender" : {
|
||||
"name": "sender",
|
||||
"address": "street"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import org.baeldung.properties.ConfigProperties;
|
||||
import org.baeldung.properties.ConfigPropertiesDemoApplication;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ConfigPropertiesDemoApplication.class)
|
||||
@TestPropertySource("classpath:configprops-test.properties")
|
||||
public class ConfigPropertiesIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ConfigProperties properties;
|
||||
|
||||
@Test
|
||||
public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("From address is read as null!", properties.getFrom() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListPropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients().size() == 2);
|
||||
Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients().size() == 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMapPropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("Couldn't bind map property!", properties.getAdditionalHeaders() != null);
|
||||
Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders().size() == 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception {
|
||||
Assert.assertTrue("Couldn't bind map property!", properties.getCredentials() != null);
|
||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getAuthMethod().equals("SHA1"));
|
||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getUsername().equals("john"));
|
||||
Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getPassword().equals("password"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.baeldung.properties.ConfigPropertiesDemoApplication;
|
||||
import org.baeldung.properties.CustomJsonProperties;
|
||||
import org.baeldung.properties.JsonProperties;
|
||||
import org.baeldung.properties.JsonPropertyContextInitializer;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class)
|
||||
public class JsonPropertiesIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private JsonProperties jsonProperties;
|
||||
|
||||
@Autowired
|
||||
private CustomJsonProperties customJsonProperties;
|
||||
|
||||
@Test
|
||||
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() {
|
||||
Assert.assertEquals("mailer@mail.com", jsonProperties.getHost());
|
||||
Assert.assertEquals(9090, jsonProperties.getPort());
|
||||
Assert.assertTrue(jsonProperties.isResend());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() {
|
||||
Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() {
|
||||
Assert.assertEquals("sender", jsonProperties.getSender()
|
||||
.get("name"));
|
||||
Assert.assertEquals("street", jsonProperties.getSender()
|
||||
.get("address"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() {
|
||||
Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost());
|
||||
Assert.assertEquals(9090, customJsonProperties.getPort());
|
||||
Assert.assertTrue(customJsonProperties.isResend());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() {
|
||||
Assert.assertNotNull(customJsonProperties.getSender());
|
||||
Assert.assertEquals("sender", customJsonProperties.getSender()
|
||||
.getName());
|
||||
Assert.assertEquals("street", customJsonProperties.getSender()
|
||||
.getAddress());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class)
|
||||
public class JsonPropertiesIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private JsonProperties jsonProperties;
|
||||
|
||||
@Autowired
|
||||
private CustomJsonProperties customJsonProperties;
|
||||
|
||||
@Test
|
||||
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() {
|
||||
Assert.assertEquals("mailer@mail.com", jsonProperties.getHost());
|
||||
Assert.assertEquals(9090, jsonProperties.getPort());
|
||||
Assert.assertTrue(jsonProperties.isResend());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() {
|
||||
Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() {
|
||||
Assert.assertEquals("sender", jsonProperties.getSender()
|
||||
.get("name"));
|
||||
Assert.assertEquals("street", jsonProperties.getSender()
|
||||
.get("address"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() {
|
||||
Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost());
|
||||
Assert.assertEquals(9090, customJsonProperties.getPort());
|
||||
Assert.assertTrue(customJsonProperties.isResend());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() {
|
||||
Assert.assertNotNull(customJsonProperties.getSender());
|
||||
Assert.assertEquals("sender", customJsonProperties.getSender()
|
||||
.getName());
|
||||
Assert.assertEquals("street", customJsonProperties.getSender()
|
||||
.getAddress());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue