mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Improve structure of response from configprops endpoint
Closes gh-10162
This commit is contained in:
parent
27c1b5eb4c
commit
ab54801143
@ -83,42 +83,35 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext
|
||||
}
|
||||
|
||||
@ReadOperation
|
||||
public Map<String, Object> configurationProperties() {
|
||||
public ConfigurationPropertiesDescriptor configurationProperties() {
|
||||
return extract(this.context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract beans annotated {@link ConfigurationProperties} and serialize into
|
||||
* {@link Map}.
|
||||
* @param context the application context
|
||||
* @return the beans
|
||||
*/
|
||||
protected Map<String, Object> extract(ApplicationContext context) {
|
||||
// Serialize beans into map structure and sanitize values
|
||||
private ConfigurationPropertiesDescriptor extract(ApplicationContext context) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
configureObjectMapper(mapper);
|
||||
return extract(context, mapper);
|
||||
return describeConfigurationProperties(context, mapper);
|
||||
}
|
||||
|
||||
private Map<String, Object> extract(ApplicationContext context, ObjectMapper mapper) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
private ConfigurationPropertiesDescriptor describeConfigurationProperties(
|
||||
ApplicationContext context, ObjectMapper mapper) {
|
||||
if (context == null) {
|
||||
return null;
|
||||
}
|
||||
ConfigurationBeanFactoryMetaData beanFactoryMetaData = getBeanFactoryMetaData(
|
||||
context);
|
||||
Map<String, Object> beans = getConfigurationPropertiesBeans(context,
|
||||
beanFactoryMetaData);
|
||||
Map<String, ConfigurationPropertiesBeanDescriptor> beanDescriptors = new HashMap<>();
|
||||
for (Map.Entry<String, Object> entry : beans.entrySet()) {
|
||||
String beanName = entry.getKey();
|
||||
Object bean = entry.getValue();
|
||||
Map<String, Object> root = new HashMap<>();
|
||||
String prefix = extractPrefix(context, beanFactoryMetaData, beanName, bean);
|
||||
root.put("prefix", prefix);
|
||||
root.put("properties", sanitize(prefix, safeSerialize(mapper, bean, prefix)));
|
||||
result.put(beanName, root);
|
||||
beanDescriptors.put(beanName, new ConfigurationPropertiesBeanDescriptor(
|
||||
prefix, sanitize(prefix, safeSerialize(mapper, bean, prefix))));
|
||||
}
|
||||
if (context.getParent() != null) {
|
||||
result.put("parent", extract(context.getParent(), mapper));
|
||||
}
|
||||
return result;
|
||||
return new ConfigurationPropertiesDescriptor(beanDescriptors,
|
||||
describeConfigurationProperties(context.getParent(), mapper));
|
||||
}
|
||||
|
||||
private ConfigurationBeanFactoryMetaData getBeanFactoryMetaData(
|
||||
@ -359,4 +352,58 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A description of an application context's {@link ConfigurationProperties} beans.
|
||||
* Primarily intended for serialization to JSON.
|
||||
*/
|
||||
public static final class ConfigurationPropertiesDescriptor {
|
||||
|
||||
private final Map<String, ConfigurationPropertiesBeanDescriptor> beans;
|
||||
|
||||
private final ConfigurationPropertiesDescriptor parent;
|
||||
|
||||
private ConfigurationPropertiesDescriptor(
|
||||
Map<String, ConfigurationPropertiesBeanDescriptor> beans,
|
||||
ConfigurationPropertiesDescriptor parent) {
|
||||
this.beans = beans;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public Map<String, ConfigurationPropertiesBeanDescriptor> getBeans() {
|
||||
return this.beans;
|
||||
}
|
||||
|
||||
public ConfigurationPropertiesDescriptor getParent() {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A description of a {@link ConfigurationProperties} bean. Primarily intended for
|
||||
* serialization to JSON.
|
||||
*/
|
||||
public static final class ConfigurationPropertiesBeanDescriptor {
|
||||
|
||||
private final String prefix;
|
||||
|
||||
private final Map<String, Object> properties;
|
||||
|
||||
private ConfigurationPropertiesBeanDescriptor(String prefix,
|
||||
Map<String, Object> properties) {
|
||||
this.prefix = prefix;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return this.prefix;
|
||||
}
|
||||
|
||||
public Map<String, Object> getProperties() {
|
||||
return this.properties;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,10 +16,10 @@
|
||||
|
||||
package org.springframework.boot.actuate.endpoint;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint.ConfigurationPropertiesBeanDescriptor;
|
||||
import org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint.ConfigurationPropertiesDescriptor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
@ -37,7 +37,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class ConfigurationPropertiesReportEndpointMethodAnnotationsTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testNaming() {
|
||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(Config.class)
|
||||
@ -45,17 +44,18 @@ public class ConfigurationPropertiesReportEndpointMethodAnnotationsTests {
|
||||
contextRunner.run((context) -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = endpoint.configurationProperties();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
ConfigurationPropertiesDescriptor properties = endpoint
|
||||
.configurationProperties();
|
||||
ConfigurationPropertiesBeanDescriptor other = properties.getBeans()
|
||||
.get("other");
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
assertThat(nestedProperties.get("prefix")).isEqualTo("other");
|
||||
assertThat(nestedProperties.get("properties")).isNotNull();
|
||||
assertThat(other).isNotNull();
|
||||
assertThat(other.getPrefix()).isEqualTo("other");
|
||||
assertThat(other.getProperties()).isNotNull();
|
||||
assertThat(other.getProperties()).isNotEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void prefixFromBeanMethodConfigurationPropertiesCanOverridePrefixOnClass() {
|
||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(OverriddenPrefix.class)
|
||||
@ -63,12 +63,13 @@ public class ConfigurationPropertiesReportEndpointMethodAnnotationsTests {
|
||||
contextRunner.run((context) -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = endpoint.configurationProperties();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
.get("bar");
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
assertThat(nestedProperties.get("prefix")).isEqualTo("other");
|
||||
assertThat(nestedProperties.get("properties")).isNotNull();
|
||||
ConfigurationPropertiesDescriptor properties = endpoint
|
||||
.configurationProperties();
|
||||
ConfigurationPropertiesBeanDescriptor bar = properties.getBeans().get("bar");
|
||||
assertThat(bar).isNotNull();
|
||||
assertThat(bar.getPrefix()).isEqualTo("other");
|
||||
assertThat(bar.getProperties()).isNotNull();
|
||||
assertThat(bar.getProperties()).isNotEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -16,10 +16,9 @@
|
||||
|
||||
package org.springframework.boot.actuate.endpoint;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint.ConfigurationPropertiesDescriptor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
@ -38,7 +37,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class ConfigurationPropertiesReportEndpointParentTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void configurationPropertiesClass() throws Exception {
|
||||
new ApplicationContextRunner().withUserConfiguration(Parent.class)
|
||||
.run((parent) -> {
|
||||
@ -47,17 +45,17 @@ public class ConfigurationPropertiesReportEndpointParentTests {
|
||||
.withParent(parent).run(child -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = child
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> result = endpoint.configurationProperties();
|
||||
assertThat(result.keySet()).containsExactlyInAnyOrder("parent",
|
||||
"endpoint", "someProperties");
|
||||
assertThat(((Map<String, Object>) result.get("parent")).keySet())
|
||||
ConfigurationPropertiesDescriptor result = endpoint
|
||||
.configurationProperties();
|
||||
assertThat(result.getBeans().keySet())
|
||||
.containsExactlyInAnyOrder("endpoint", "someProperties");
|
||||
assertThat((result.getParent().getBeans().keySet()))
|
||||
.containsExactly("testProperties");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void configurationPropertiesBeanMethod() throws Exception {
|
||||
new ApplicationContextRunner().withUserConfiguration(Parent.class)
|
||||
.run((parent) -> {
|
||||
@ -67,10 +65,11 @@ public class ConfigurationPropertiesReportEndpointParentTests {
|
||||
.withParent(parent).run(child -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = child
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> result = endpoint.configurationProperties();
|
||||
assertThat(result.keySet()).containsExactlyInAnyOrder("parent",
|
||||
"endpoint", "otherProperties");
|
||||
assertThat(((Map<String, Object>) result.get("parent")).keySet())
|
||||
ConfigurationPropertiesDescriptor result = endpoint
|
||||
.configurationProperties();
|
||||
assertThat(result.getBeans().keySet())
|
||||
.containsExactlyInAnyOrder("endpoint", "otherProperties");
|
||||
assertThat((result.getParent().getBeans().keySet()))
|
||||
.containsExactly("testProperties");
|
||||
});
|
||||
});
|
||||
|
@ -16,12 +16,12 @@
|
||||
|
||||
package org.springframework.boot.actuate.endpoint;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint.ConfigurationPropertiesBeanDescriptor;
|
||||
import org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint.ConfigurationPropertiesDescriptor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
@ -52,10 +52,12 @@ public class ConfigurationPropertiesReportEndpointProxyTests {
|
||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(Config.class, SqlExecutor.class);
|
||||
contextRunner.run((context) -> {
|
||||
Map<String, Object> report = context
|
||||
ConfigurationPropertiesDescriptor report = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class)
|
||||
.configurationProperties();
|
||||
assertThat(report.toString()).contains("prefix=executor.sql");
|
||||
assertThat(report.getBeans().values().stream()
|
||||
.map(ConfigurationPropertiesBeanDescriptor::getPrefix)
|
||||
.filter("executor.sql"::equals).findFirst()).isNotEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,8 @@ import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint.ConfigurationPropertiesBeanDescriptor;
|
||||
import org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint.ConfigurationPropertiesDescriptor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
@ -44,7 +46,6 @@ import static org.assertj.core.api.Assertions.entry;
|
||||
public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testNaming() throws Exception {
|
||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(FooConfig.class)
|
||||
@ -52,13 +53,12 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
contextRunner.run((context) -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = endpoint.configurationProperties();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
.get("foo");
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
assertThat(nestedProperties.get("prefix")).isEqualTo("foo");
|
||||
Map<String, Object> map = (Map<String, Object>) nestedProperties
|
||||
.get("properties");
|
||||
ConfigurationPropertiesDescriptor properties = endpoint
|
||||
.configurationProperties();
|
||||
ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo");
|
||||
assertThat(foo).isNotNull();
|
||||
assertThat(foo.getPrefix()).isEqualTo("foo");
|
||||
Map<String, Object> map = foo.getProperties();
|
||||
assertThat(map).isNotNull();
|
||||
assertThat(map).hasSize(2);
|
||||
assertThat(map.get("name")).isEqualTo("foo");
|
||||
@ -74,12 +74,11 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
contextRunner.run((context) -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = endpoint.configurationProperties();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
.get("foo");
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
Map<String, Object> map = (Map<String, Object>) nestedProperties
|
||||
.get("properties");
|
||||
ConfigurationPropertiesDescriptor properties = endpoint
|
||||
.configurationProperties();
|
||||
ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo");
|
||||
assertThat(foo).isNotNull();
|
||||
Map<String, Object> map = foo.getProperties();
|
||||
assertThat(map).isNotNull();
|
||||
assertThat(map).hasSize(2);
|
||||
assertThat(((Map<String, Object>) map.get("bar")).get("name"))
|
||||
@ -88,7 +87,6 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testCycle() throws Exception {
|
||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(CycleConfig.class)
|
||||
@ -96,13 +94,12 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
contextRunner.run((context) -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = endpoint.configurationProperties();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
.get("foo");
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
assertThat(nestedProperties.get("prefix")).isEqualTo("foo");
|
||||
Map<String, Object> map = (Map<String, Object>) nestedProperties
|
||||
.get("properties");
|
||||
ConfigurationPropertiesDescriptor properties = endpoint
|
||||
.configurationProperties();
|
||||
ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo");
|
||||
assertThat(foo).isNotNull();
|
||||
assertThat(foo.getPrefix()).isEqualTo("foo");
|
||||
Map<String, Object> map = foo.getProperties();
|
||||
assertThat(map).isNotNull();
|
||||
assertThat(map).hasSize(1);
|
||||
assertThat(map.get("error")).isEqualTo("Cannot serialize 'foo'");
|
||||
@ -118,13 +115,13 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
contextRunner.run((context) -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = endpoint.configurationProperties();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
ConfigurationPropertiesDescriptor properties = endpoint
|
||||
.configurationProperties();
|
||||
ConfigurationPropertiesBeanDescriptor fooProperties = properties.getBeans()
|
||||
.get("foo");
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
assertThat(nestedProperties.get("prefix")).isEqualTo("foo");
|
||||
Map<String, Object> map = (Map<String, Object>) nestedProperties
|
||||
.get("properties");
|
||||
assertThat(fooProperties).isNotNull();
|
||||
assertThat(fooProperties.getPrefix()).isEqualTo("foo");
|
||||
Map<String, Object> map = fooProperties.getProperties();
|
||||
assertThat(map).isNotNull();
|
||||
assertThat(map).hasSize(3);
|
||||
assertThat(((Map<String, Object>) map.get("map")).get("name"))
|
||||
@ -133,21 +130,18 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testEmptyMapIsNotAdded() throws Exception {
|
||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(MapConfig.class);
|
||||
contextRunner.run((context) -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = endpoint.configurationProperties();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
.get("foo");
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
System.err.println(nestedProperties);
|
||||
assertThat(nestedProperties.get("prefix")).isEqualTo("foo");
|
||||
Map<String, Object> map = (Map<String, Object>) nestedProperties
|
||||
.get("properties");
|
||||
ConfigurationPropertiesDescriptor properties = endpoint
|
||||
.configurationProperties();
|
||||
ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo");
|
||||
assertThat(foo).isNotNull();
|
||||
assertThat(foo.getPrefix()).isEqualTo("foo");
|
||||
Map<String, Object> map = foo.getProperties();
|
||||
assertThat(map).isNotNull();
|
||||
assertThat(map).hasSize(2);
|
||||
assertThat(map).doesNotContainKey("map");
|
||||
@ -163,13 +157,12 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
contextRunner.run((context) -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = endpoint.configurationProperties();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
.get("foo");
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
assertThat(nestedProperties.get("prefix")).isEqualTo("foo");
|
||||
Map<String, Object> map = (Map<String, Object>) nestedProperties
|
||||
.get("properties");
|
||||
ConfigurationPropertiesDescriptor properties = endpoint
|
||||
.configurationProperties();
|
||||
ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo");
|
||||
assertThat(foo).isNotNull();
|
||||
assertThat(foo.getPrefix()).isEqualTo("foo");
|
||||
Map<String, Object> map = foo.getProperties();
|
||||
assertThat(map).isNotNull();
|
||||
assertThat(map).hasSize(3);
|
||||
assertThat(((List<String>) map.get("list")).get(0)).isEqualTo("foo");
|
||||
@ -177,7 +170,6 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testInetAddress() throws Exception {
|
||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(AddressedConfig.class)
|
||||
@ -185,14 +177,12 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
contextRunner.run((context) -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = endpoint.configurationProperties();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
.get("foo");
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
System.err.println(nestedProperties);
|
||||
assertThat(nestedProperties.get("prefix")).isEqualTo("foo");
|
||||
Map<String, Object> map = (Map<String, Object>) nestedProperties
|
||||
.get("properties");
|
||||
ConfigurationPropertiesDescriptor properties = endpoint
|
||||
.configurationProperties();
|
||||
ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo");
|
||||
assertThat(foo).isNotNull();
|
||||
assertThat(foo.getPrefix()).isEqualTo("foo");
|
||||
Map<String, Object> map = foo.getProperties();
|
||||
assertThat(map).isNotNull();
|
||||
assertThat(map).hasSize(3);
|
||||
assertThat(map.get("address")).isEqualTo("192.168.1.10");
|
||||
@ -209,14 +199,12 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
contextRunner.run((context) -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = endpoint.configurationProperties();
|
||||
assertThat(properties).containsKeys("foo");
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
.get("foo");
|
||||
assertThat(nestedProperties).containsOnlyKeys("prefix", "properties");
|
||||
assertThat(nestedProperties.get("prefix")).isEqualTo("foo");
|
||||
Map<String, Object> propertiesMap = (Map<String, Object>) nestedProperties
|
||||
.get("properties");
|
||||
ConfigurationPropertiesDescriptor properties = endpoint
|
||||
.configurationProperties();
|
||||
assertThat(properties.getBeans()).containsKeys("foo");
|
||||
ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo");
|
||||
assertThat(foo.getPrefix()).isEqualTo("foo");
|
||||
Map<String, Object> propertiesMap = foo.getProperties();
|
||||
assertThat(propertiesMap).containsOnlyKeys("bar", "name", "map", "list");
|
||||
Map<String, Object> map = (Map<String, Object>) propertiesMap.get("map");
|
||||
assertThat(map).containsOnly(entry("entryOne", true));
|
||||
|
@ -26,6 +26,8 @@ import java.util.function.Consumer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint.ConfigurationPropertiesBeanDescriptor;
|
||||
import org.springframework.boot.actuate.endpoint.ConfigurationPropertiesReportEndpoint.ConfigurationPropertiesDescriptor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
@ -44,35 +46,32 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class ConfigurationPropertiesReportEndpointTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void configurationPropertiesAreReturned() throws Exception {
|
||||
load((properties) -> {
|
||||
assertThat(properties.size()).isGreaterThan(0);
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
assertThat(properties.getBeans().size()).isGreaterThan(0);
|
||||
ConfigurationPropertiesBeanDescriptor nestedProperties = properties.getBeans()
|
||||
.get("testProperties");
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
assertThat(nestedProperties.get("prefix")).isEqualTo("test");
|
||||
assertThat(nestedProperties.get("properties")).isNotNull();
|
||||
assertThat(nestedProperties.getPrefix()).isEqualTo("test");
|
||||
assertThat(nestedProperties.getProperties()).isNotNull();
|
||||
assertThat(nestedProperties.getProperties()).isNotEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void entriesWithNullValuesAreNotIncluded() {
|
||||
load((properties) -> {
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
.get("testProperties");
|
||||
assertThat((Map<String, Object>) nestedProperties.get("properties"))
|
||||
.doesNotContainKey("nullValue");
|
||||
Map<String, Object> nestedProperties = properties.getBeans()
|
||||
.get("testProperties").getProperties();
|
||||
assertThat(nestedProperties).doesNotContainKey("nullValue");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void defaultKeySanitization() throws Exception {
|
||||
load((properties) -> {
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
|
||||
.get("testProperties")).get("properties");
|
||||
Map<String, Object> nestedProperties = properties.getBeans()
|
||||
.get("testProperties").getProperties();
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
assertThat(nestedProperties.get("dbPassword")).isEqualTo("******");
|
||||
assertThat(nestedProperties.get("myTestProperty")).isEqualTo("654321");
|
||||
@ -80,30 +79,27 @@ public class ConfigurationPropertiesReportEndpointTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void customKeySanitization() throws Exception {
|
||||
load("property", (properties) -> {
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
|
||||
.get("testProperties")).get("properties");
|
||||
Map<String, Object> nestedProperties = properties.getBeans()
|
||||
.get("testProperties").getProperties();
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
assertThat(nestedProperties.get("dbPassword")).isEqualTo("123456");
|
||||
assertThat(nestedProperties.get("myTestProperty")).isEqualTo("******");
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void customPatternKeySanitization() throws Exception {
|
||||
load(".*pass.*", (properties) -> {
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
|
||||
.get("testProperties")).get("properties");
|
||||
Map<String, Object> nestedProperties = properties.getBeans()
|
||||
.get("testProperties").getProperties();
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
assertThat(nestedProperties.get("dbPassword")).isEqualTo("******");
|
||||
assertThat(nestedProperties.get("myTestProperty")).isEqualTo("654321");
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void keysToSanitizeCanBeConfiguredViaTheEnvironment() throws Exception {
|
||||
ApplicationContextRunner tester = new ApplicationContextRunner()
|
||||
@ -113,9 +109,10 @@ public class ConfigurationPropertiesReportEndpointTests {
|
||||
tester.run((context) -> {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = endpoint.configurationProperties();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
|
||||
.get("testProperties")).get("properties");
|
||||
ConfigurationPropertiesDescriptor properties = endpoint
|
||||
.configurationProperties();
|
||||
Map<String, Object> nestedProperties = properties.getBeans()
|
||||
.get("testProperties").getProperties();
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
assertThat(nestedProperties.get("dbPassword")).isEqualTo("******");
|
||||
assertThat(nestedProperties.get("myTestProperty")).isEqualTo("******");
|
||||
@ -127,8 +124,8 @@ public class ConfigurationPropertiesReportEndpointTests {
|
||||
public void keySanitizationWithCustomPatternUsingCompositeKeys() throws Exception {
|
||||
// gh-4415
|
||||
load(Arrays.asList(".*\\.secrets\\..*", ".*\\.hidden\\..*"), (properties) -> {
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
|
||||
.get("testProperties")).get("properties");
|
||||
Map<String, Object> nestedProperties = properties.getBeans()
|
||||
.get("testProperties").getProperties();
|
||||
assertThat(nestedProperties).isNotNull();
|
||||
Map<String, Object> secrets = (Map<String, Object>) nestedProperties
|
||||
.get("secrets");
|
||||
@ -141,11 +138,10 @@ public class ConfigurationPropertiesReportEndpointTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void mixedBoolean() throws Exception {
|
||||
load((properties) -> {
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
|
||||
.get("testProperties")).get("properties");
|
||||
Map<String, Object> nestedProperties = properties.getBeans()
|
||||
.get("testProperties").getProperties();
|
||||
assertThat(nestedProperties.get("mixedBoolean")).isEqualTo(true);
|
||||
});
|
||||
}
|
||||
@ -154,8 +150,8 @@ public class ConfigurationPropertiesReportEndpointTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void listsAreSanitized() throws Exception {
|
||||
load((properties) -> {
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
|
||||
.get("testProperties")).get("properties");
|
||||
Map<String, Object> nestedProperties = properties.getBeans()
|
||||
.get("testProperties").getProperties();
|
||||
assertThat(nestedProperties.get("listItems")).isInstanceOf(List.class);
|
||||
List<Object> list = (List<Object>) nestedProperties.get("listItems");
|
||||
assertThat(list).hasSize(1);
|
||||
@ -168,8 +164,8 @@ public class ConfigurationPropertiesReportEndpointTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void listsOfListsAreSanitized() throws Exception {
|
||||
load((properties) -> {
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
|
||||
.get("testProperties")).get("properties");
|
||||
Map<String, Object> nestedProperties = properties.getBeans()
|
||||
.get("testProperties").getProperties();
|
||||
assertThat(nestedProperties.get("listOfListItems")).isInstanceOf(List.class);
|
||||
List<List<Object>> listOfLists = (List<List<Object>>) nestedProperties
|
||||
.get("listOfListItems");
|
||||
@ -181,16 +177,17 @@ public class ConfigurationPropertiesReportEndpointTests {
|
||||
});
|
||||
}
|
||||
|
||||
private void load(Consumer<Map<String, Object>> properties) {
|
||||
private void load(Consumer<ConfigurationPropertiesDescriptor> properties) {
|
||||
load(Collections.emptyList(), properties);
|
||||
}
|
||||
|
||||
private void load(String keyToSanitize, Consumer<Map<String, Object>> properties) {
|
||||
private void load(String keyToSanitize,
|
||||
Consumer<ConfigurationPropertiesDescriptor> properties) {
|
||||
load(Collections.singletonList(keyToSanitize), properties);
|
||||
}
|
||||
|
||||
private void load(List<String> keysToSanitize,
|
||||
Consumer<Map<String, Object>> properties) {
|
||||
Consumer<ConfigurationPropertiesDescriptor> properties) {
|
||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(Config.class);
|
||||
contextRunner.run((context) -> {
|
||||
|
@ -223,6 +223,7 @@ public class SampleActuatorApplicationTests {
|
||||
assertThat(((String) body.get("id"))).startsWith("application");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testConfigProps() throws Exception {
|
||||
@SuppressWarnings("rawtypes")
|
||||
@ -230,9 +231,8 @@ public class SampleActuatorApplicationTests {
|
||||
.withBasicAuth("user", getPassword())
|
||||
.getForEntity("/application/configprops", Map.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> body = entity.getBody();
|
||||
assertThat(body)
|
||||
assertThat((Map<String, Object>) body.get("beans"))
|
||||
.containsKey("spring.datasource-" + DataSourceProperties.class.getName());
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user