Polish "Add health indicator for Neo4j"

Closes gh-9557
pull/9593/merge
Stephane Nicoll 8 years ago
parent 4c97dcb53a
commit 01272fa0cc

@ -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<MongoHealthIndicator, MongoTemplate> {
private final Map<String, MongoTemplate> mongoTemplates;
public MongoHealthIndicatorConfiguration(
Map<String, MongoTemplate> 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<MongoHealthIndicator, MongoTemplate> {
private final Map<String, MongoTemplate> mongoTemplates;
public MongoHealthIndicatorConfiguration(
Map<String, MongoTemplate> mongoTemplates) {
this.mongoTemplates = mongoTemplates;
}
@Bean
@ConditionalOnMissingBean(name = "mongoHealthIndicator")
public HealthIndicator mongoHealthIndicator() {
return createHealthIndicator(this.mongoTemplates);
}
}
@Configuration
@ConditionalOnBean(RedisConnectionFactory.class)
@ConditionalOnEnabledHealthIndicator("redis")

@ -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<Map<String, Object>> results = result.queryResults();
int nodes = (int) results.iterator().next().get("nodes");

@ -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<String, Object> 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<String, Object> expectedCypherDetails = new HashMap<>();
expectedCypherDetails.put("nodes", nodeCount);
List<Map<String, Object>> 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<String, Object> 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();

@ -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.

Loading…
Cancel
Save