diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfiguration.java index 6b1c5c528a..834b7e4804 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfiguration.java @@ -105,8 +105,8 @@ import org.springframework.mail.javamail.JavaMailSenderImpl; JestAutoConfiguration.class, JmsAutoConfiguration.class, LdapDataAutoConfiguration.class, MailSenderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, - RabbitAutoConfiguration.class, RedisAutoConfiguration.class, - SolrAutoConfiguration.class, Neo4jDataAutoConfiguration.class }) + Neo4jDataAutoConfiguration.class, RabbitAutoConfiguration.class, + RedisAutoConfiguration.class, SolrAutoConfiguration.class }) @EnableConfigurationProperties({ HealthIndicatorProperties.class }) @Import({ ElasticsearchHealthIndicatorConfiguration.ElasticsearchClientHealthIndicatorConfiguration.class, @@ -261,6 +261,27 @@ public class HealthIndicatorAutoConfiguration { } + @Configuration + @ConditionalOnBean(MongoTemplate.class) + @ConditionalOnEnabledHealthIndicator("mongo") + public static class MongoHealthIndicatorConfiguration extends + CompositeHealthIndicatorConfiguration { + + private final Map mongoTemplates; + + public MongoHealthIndicatorConfiguration( + Map mongoTemplates) { + this.mongoTemplates = mongoTemplates; + } + + @Bean + @ConditionalOnMissingBean(name = "mongoHealthIndicator") + public HealthIndicator mongoHealthIndicator() { + return createHealthIndicator(this.mongoTemplates); + } + + } + @Configuration @ConditionalOnClass(SessionFactory.class) @ConditionalOnBean(SessionFactory.class) @@ -283,27 +304,6 @@ public class HealthIndicatorAutoConfiguration { } - @Configuration - @ConditionalOnBean(MongoTemplate.class) - @ConditionalOnEnabledHealthIndicator("mongo") - public static class MongoHealthIndicatorConfiguration extends - CompositeHealthIndicatorConfiguration { - - private final Map mongoTemplates; - - public MongoHealthIndicatorConfiguration( - Map mongoTemplates) { - this.mongoTemplates = mongoTemplates; - } - - @Bean - @ConditionalOnMissingBean(name = "mongoHealthIndicator") - public HealthIndicator mongoHealthIndicator() { - return createHealthIndicator(this.mongoTemplates); - } - - } - @Configuration @ConditionalOnBean(RedisConnectionFactory.class) @ConditionalOnEnabledHealthIndicator("redis") diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Neo4jHealthIndicator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Neo4jHealthIndicator.java index 3292bc9577..3bc5e87497 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Neo4jHealthIndicator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Neo4jHealthIndicator.java @@ -23,23 +23,21 @@ import org.neo4j.ogm.model.Result; import org.neo4j.ogm.session.Session; import org.neo4j.ogm.session.SessionFactory; -import org.springframework.boot.context.properties.ConfigurationProperties; - /** * {@link HealthIndicator} that tests the status of a Neo4j by executing a Cypher * statement. * * @author Eric Spiegelberg + * @since 2.0.0 */ -@ConfigurationProperties(prefix = "management.health.neo4j", ignoreUnknownFields = false) public class Neo4jHealthIndicator extends AbstractHealthIndicator { - private final SessionFactory sessionFactory; - /** * The Cypher statement used to verify Neo4j is up. */ - public static final String CYPHER = "match (n) return count(n) as nodes"; + static final String CYPHER = "match (n) return count(n) as nodes"; + + private final SessionFactory sessionFactory; /** * Create a new {@link Neo4jHealthIndicator} using the specified @@ -54,7 +52,7 @@ public class Neo4jHealthIndicator extends AbstractHealthIndicator { protected void doHealthCheck(Health.Builder builder) throws Exception { Session session = this.sessionFactory.openSession(); - Result result = session.query(CYPHER, Collections.emptyMap()); + Result result = session.query(CYPHER, Collections.EMPTY_MAP); Iterable> results = result.queryResults(); int nodes = (int) results.iterator().next().get("nodes"); diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/Neo4jHealthIndicatorTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/Neo4jHealthIndicatorTests.java index fa4983e417..b9c253e11d 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/Neo4jHealthIndicatorTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/Neo4jHealthIndicatorTests.java @@ -17,6 +17,7 @@ package org.springframework.boot.actuate.health; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -37,60 +38,49 @@ import static org.mockito.Mockito.mock; * Tests for {@link Neo4jHealthIndicator}. * * @author Eric Spiegelberg + * @author Stephane Nicoll */ public class Neo4jHealthIndicatorTests { - private Result result; private Session session; - private SessionFactory sessionFactory; private Neo4jHealthIndicator neo4jHealthIndicator; - private Map emptyParameters = new HashMap<>(); - @Before public void before() { - this.result = mock(Result.class); this.session = mock(Session.class); - this.sessionFactory = mock(SessionFactory.class); - - given(this.sessionFactory.openSession()).willReturn(this.session); - - this.neo4jHealthIndicator = new Neo4jHealthIndicator(this.sessionFactory); + SessionFactory sessionFactory = mock(SessionFactory.class); + given(sessionFactory.openSession()).willReturn(this.session); + this.neo4jHealthIndicator = new Neo4jHealthIndicator(sessionFactory); } @Test public void neo4jUp() { - given(this.session.query(Neo4jHealthIndicator.CYPHER, this.emptyParameters)) - .willReturn(this.result); + Result result = mock(Result.class); + given(this.session.query(Neo4jHealthIndicator.CYPHER, Collections.EMPTY_MAP)) + .willReturn(result); int nodeCount = 500; Map expectedCypherDetails = new HashMap<>(); expectedCypherDetails.put("nodes", nodeCount); - List> queryResults = new ArrayList<>(); queryResults.add(expectedCypherDetails); - - given(this.result.queryResults()).willReturn(queryResults); + given(result.queryResults()).willReturn(queryResults); Health health = this.neo4jHealthIndicator.health(); assertThat(health.getStatus()).isEqualTo(Status.UP); - Map details = health.getDetails(); int nodeCountFromDetails = (int) details.get("nodes"); Assert.assertEquals(nodeCount, nodeCountFromDetails); - } @Test public void neo4jDown() { - CypherException cypherException = new CypherException("Error executing Cypher", "Neo.ClientError.Statement.SyntaxError", "Unable to execute invalid Cypher"); - - given(this.session.query(Neo4jHealthIndicator.CYPHER, this.emptyParameters)) + given(this.session.query(Neo4jHealthIndicator.CYPHER, Collections.EMPTY_MAP)) .willThrow(cypherException); Health health = this.neo4jHealthIndicator.health(); diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 7b4f2c8db1..fb06267013 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -1214,6 +1214,7 @@ content into your application; rather pick only the properties that you need. management.health.ldap.enabled=true # Enable LDAP health check. management.health.mail.enabled=true # Enable Mail health check. management.health.mongo.enabled=true # Enable MongoDB health check. + management.health.neo4j.enabled=true # Enable Neo4j health check. management.health.rabbit.enabled=true # Enable RabbitMQ health check. management.health.redis.enabled=true # Enable Redis health check. management.health.solr.enabled=true # Enable Solr health check.