From ab5480114304be329ed1d4c75d2f70f8b1e2ae41 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 5 Sep 2017 10:34:13 +0100 Subject: [PATCH] Improve structure of response from configprops endpoint Closes gh-10162 --- ...ConfigurationPropertiesReportEndpoint.java | 87 ++++++++++---- ...sReportEndpointMethodAnnotationsTests.java | 31 ++--- ...onPropertiesReportEndpointParentTests.java | 23 ++-- ...ionPropertiesReportEndpointProxyTests.java | 10 +- ...rtiesReportEndpointSerializationTests.java | 110 ++++++++---------- ...gurationPropertiesReportEndpointTests.java | 67 +++++------ .../SampleActuatorApplicationTests.java | 4 +- 7 files changed, 183 insertions(+), 149 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpoint.java index fa8d5264b2..63015098b0 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpoint.java @@ -83,42 +83,35 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext } @ReadOperation - public Map 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 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 extract(ApplicationContext context, ObjectMapper mapper) { - Map result = new HashMap<>(); + private ConfigurationPropertiesDescriptor describeConfigurationProperties( + ApplicationContext context, ObjectMapper mapper) { + if (context == null) { + return null; + } ConfigurationBeanFactoryMetaData beanFactoryMetaData = getBeanFactoryMetaData( context); Map beans = getConfigurationPropertiesBeans(context, beanFactoryMetaData); + Map beanDescriptors = new HashMap<>(); for (Map.Entry entry : beans.entrySet()) { String beanName = entry.getKey(); Object bean = entry.getValue(); - Map 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 beans; + + private final ConfigurationPropertiesDescriptor parent; + + private ConfigurationPropertiesDescriptor( + Map beans, + ConfigurationPropertiesDescriptor parent) { + this.beans = beans; + this.parent = parent; + } + + public Map 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 properties; + + private ConfigurationPropertiesBeanDescriptor(String prefix, + Map properties) { + this.prefix = prefix; + this.properties = properties; + } + + public String getPrefix() { + return this.prefix; + } + + public Map getProperties() { + return this.properties; + } + + } + } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointMethodAnnotationsTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointMethodAnnotationsTests.java index 05d28f7664..240e3b0575 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointMethodAnnotationsTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointMethodAnnotationsTests.java @@ -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 properties = endpoint.configurationProperties(); - Map nestedProperties = (Map) 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 properties = endpoint.configurationProperties(); - Map nestedProperties = (Map) 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(); }); } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointParentTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointParentTests.java index 0986894a96..1bf99e80ed 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointParentTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointParentTests.java @@ -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 result = endpoint.configurationProperties(); - assertThat(result.keySet()).containsExactlyInAnyOrder("parent", - "endpoint", "someProperties"); - assertThat(((Map) 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 result = endpoint.configurationProperties(); - assertThat(result.keySet()).containsExactlyInAnyOrder("parent", - "endpoint", "otherProperties"); - assertThat(((Map) result.get("parent")).keySet()) + ConfigurationPropertiesDescriptor result = endpoint + .configurationProperties(); + assertThat(result.getBeans().keySet()) + .containsExactlyInAnyOrder("endpoint", "otherProperties"); + assertThat((result.getParent().getBeans().keySet())) .containsExactly("testProperties"); }); }); diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointProxyTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointProxyTests.java index 267f87e314..67e88b43d2 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointProxyTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointProxyTests.java @@ -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 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(); }); } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointSerializationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointSerializationTests.java index df3ab8082b..272b2de30c 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointSerializationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointSerializationTests.java @@ -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 properties = endpoint.configurationProperties(); - Map nestedProperties = (Map) properties - .get("foo"); - assertThat(nestedProperties).isNotNull(); - assertThat(nestedProperties.get("prefix")).isEqualTo("foo"); - Map map = (Map) nestedProperties - .get("properties"); + ConfigurationPropertiesDescriptor properties = endpoint + .configurationProperties(); + ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo"); + assertThat(foo).isNotNull(); + assertThat(foo.getPrefix()).isEqualTo("foo"); + Map 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 properties = endpoint.configurationProperties(); - Map nestedProperties = (Map) properties - .get("foo"); - assertThat(nestedProperties).isNotNull(); - Map map = (Map) nestedProperties - .get("properties"); + ConfigurationPropertiesDescriptor properties = endpoint + .configurationProperties(); + ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo"); + assertThat(foo).isNotNull(); + Map map = foo.getProperties(); assertThat(map).isNotNull(); assertThat(map).hasSize(2); assertThat(((Map) 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 properties = endpoint.configurationProperties(); - Map nestedProperties = (Map) properties - .get("foo"); - assertThat(nestedProperties).isNotNull(); - assertThat(nestedProperties.get("prefix")).isEqualTo("foo"); - Map map = (Map) nestedProperties - .get("properties"); + ConfigurationPropertiesDescriptor properties = endpoint + .configurationProperties(); + ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo"); + assertThat(foo).isNotNull(); + assertThat(foo.getPrefix()).isEqualTo("foo"); + Map 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 properties = endpoint.configurationProperties(); - Map nestedProperties = (Map) properties + ConfigurationPropertiesDescriptor properties = endpoint + .configurationProperties(); + ConfigurationPropertiesBeanDescriptor fooProperties = properties.getBeans() .get("foo"); - assertThat(nestedProperties).isNotNull(); - assertThat(nestedProperties.get("prefix")).isEqualTo("foo"); - Map map = (Map) nestedProperties - .get("properties"); + assertThat(fooProperties).isNotNull(); + assertThat(fooProperties.getPrefix()).isEqualTo("foo"); + Map map = fooProperties.getProperties(); assertThat(map).isNotNull(); assertThat(map).hasSize(3); assertThat(((Map) 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 properties = endpoint.configurationProperties(); - Map nestedProperties = (Map) properties - .get("foo"); - assertThat(nestedProperties).isNotNull(); - System.err.println(nestedProperties); - assertThat(nestedProperties.get("prefix")).isEqualTo("foo"); - Map map = (Map) nestedProperties - .get("properties"); + ConfigurationPropertiesDescriptor properties = endpoint + .configurationProperties(); + ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo"); + assertThat(foo).isNotNull(); + assertThat(foo.getPrefix()).isEqualTo("foo"); + Map 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 properties = endpoint.configurationProperties(); - Map nestedProperties = (Map) properties - .get("foo"); - assertThat(nestedProperties).isNotNull(); - assertThat(nestedProperties.get("prefix")).isEqualTo("foo"); - Map map = (Map) nestedProperties - .get("properties"); + ConfigurationPropertiesDescriptor properties = endpoint + .configurationProperties(); + ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo"); + assertThat(foo).isNotNull(); + assertThat(foo.getPrefix()).isEqualTo("foo"); + Map map = foo.getProperties(); assertThat(map).isNotNull(); assertThat(map).hasSize(3); assertThat(((List) 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 properties = endpoint.configurationProperties(); - Map nestedProperties = (Map) properties - .get("foo"); - assertThat(nestedProperties).isNotNull(); - System.err.println(nestedProperties); - assertThat(nestedProperties.get("prefix")).isEqualTo("foo"); - Map map = (Map) nestedProperties - .get("properties"); + ConfigurationPropertiesDescriptor properties = endpoint + .configurationProperties(); + ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo"); + assertThat(foo).isNotNull(); + assertThat(foo.getPrefix()).isEqualTo("foo"); + Map 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 properties = endpoint.configurationProperties(); - assertThat(properties).containsKeys("foo"); - Map nestedProperties = (Map) properties - .get("foo"); - assertThat(nestedProperties).containsOnlyKeys("prefix", "properties"); - assertThat(nestedProperties.get("prefix")).isEqualTo("foo"); - Map propertiesMap = (Map) nestedProperties - .get("properties"); + ConfigurationPropertiesDescriptor properties = endpoint + .configurationProperties(); + assertThat(properties.getBeans()).containsKeys("foo"); + ConfigurationPropertiesBeanDescriptor foo = properties.getBeans().get("foo"); + assertThat(foo.getPrefix()).isEqualTo("foo"); + Map propertiesMap = foo.getProperties(); assertThat(propertiesMap).containsOnlyKeys("bar", "name", "map", "list"); Map map = (Map) propertiesMap.get("map"); assertThat(map).containsOnly(entry("entryOne", true)); diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointTests.java index 02e870c7f6..4e7f8193ec 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointTests.java @@ -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 nestedProperties = (Map) 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 nestedProperties = (Map) properties - .get("testProperties"); - assertThat((Map) nestedProperties.get("properties")) - .doesNotContainKey("nullValue"); + Map nestedProperties = properties.getBeans() + .get("testProperties").getProperties(); + assertThat(nestedProperties).doesNotContainKey("nullValue"); }); } @Test - @SuppressWarnings("unchecked") public void defaultKeySanitization() throws Exception { load((properties) -> { - Map nestedProperties = (Map) ((Map) properties - .get("testProperties")).get("properties"); + Map 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 nestedProperties = (Map) ((Map) properties - .get("testProperties")).get("properties"); + Map 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 nestedProperties = (Map) ((Map) properties - .get("testProperties")).get("properties"); + Map 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 properties = endpoint.configurationProperties(); - Map nestedProperties = (Map) ((Map) properties - .get("testProperties")).get("properties"); + ConfigurationPropertiesDescriptor properties = endpoint + .configurationProperties(); + Map 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 nestedProperties = (Map) ((Map) properties - .get("testProperties")).get("properties"); + Map nestedProperties = properties.getBeans() + .get("testProperties").getProperties(); assertThat(nestedProperties).isNotNull(); Map secrets = (Map) nestedProperties .get("secrets"); @@ -141,11 +138,10 @@ public class ConfigurationPropertiesReportEndpointTests { } @Test - @SuppressWarnings("unchecked") public void mixedBoolean() throws Exception { load((properties) -> { - Map nestedProperties = (Map) ((Map) properties - .get("testProperties")).get("properties"); + Map 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 nestedProperties = (Map) ((Map) properties - .get("testProperties")).get("properties"); + Map nestedProperties = properties.getBeans() + .get("testProperties").getProperties(); assertThat(nestedProperties.get("listItems")).isInstanceOf(List.class); List list = (List) nestedProperties.get("listItems"); assertThat(list).hasSize(1); @@ -168,8 +164,8 @@ public class ConfigurationPropertiesReportEndpointTests { @SuppressWarnings("unchecked") public void listsOfListsAreSanitized() throws Exception { load((properties) -> { - Map nestedProperties = (Map) ((Map) properties - .get("testProperties")).get("properties"); + Map nestedProperties = properties.getBeans() + .get("testProperties").getProperties(); assertThat(nestedProperties.get("listOfListItems")).isInstanceOf(List.class); List> listOfLists = (List>) nestedProperties .get("listOfListItems"); @@ -181,16 +177,17 @@ public class ConfigurationPropertiesReportEndpointTests { }); } - private void load(Consumer> properties) { + private void load(Consumer properties) { load(Collections.emptyList(), properties); } - private void load(String keyToSanitize, Consumer> properties) { + private void load(String keyToSanitize, + Consumer properties) { load(Collections.singletonList(keyToSanitize), properties); } private void load(List keysToSanitize, - Consumer> properties) { + Consumer properties) { ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withUserConfiguration(Config.class); contextRunner.run((context) -> { diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java index ec6cb816fe..322d0c266e 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java @@ -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 body = entity.getBody(); - assertThat(body) + assertThat((Map) body.get("beans")) .containsKey("spring.datasource-" + DataSourceProperties.class.getName()); }