5 changed files with 238 additions and 1 deletions
@ -0,0 +1,64 @@
@@ -0,0 +1,64 @@
|
||||
/* |
||||
* Copyright 2012-2017 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.health; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.Map; |
||||
|
||||
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 |
||||
*/ |
||||
@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"; |
||||
|
||||
/** |
||||
* Create a new {@link Neo4jHealthIndicator} using the specified |
||||
* {@link SessionFactory}. |
||||
* @param sessionFactory the SessionFactory |
||||
*/ |
||||
public Neo4jHealthIndicator(SessionFactory sessionFactory) { |
||||
this.sessionFactory = sessionFactory; |
||||
} |
||||
|
||||
@Override |
||||
protected void doHealthCheck(Health.Builder builder) throws Exception { |
||||
Session session = this.sessionFactory.openSession(); |
||||
|
||||
Result result = session.query(CYPHER, Collections.emptyMap()); |
||||
Iterable<Map<String, Object>> results = result.queryResults(); |
||||
int nodes = (int) results.iterator().next().get("nodes"); |
||||
|
||||
builder.up().withDetail("nodes", nodes); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,100 @@
@@ -0,0 +1,100 @@
|
||||
/* |
||||
* Copyright 2012-2017 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.health; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.neo4j.ogm.exception.CypherException; |
||||
import org.neo4j.ogm.model.Result; |
||||
import org.neo4j.ogm.session.Session; |
||||
import org.neo4j.ogm.session.SessionFactory; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.BDDMockito.given; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link Neo4jHealthIndicator}. |
||||
* |
||||
* @author Eric Spiegelberg |
||||
*/ |
||||
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); |
||||
} |
||||
|
||||
@Test |
||||
public void neo4jUp() { |
||||
given(this.session.query(Neo4jHealthIndicator.CYPHER, this.emptyParameters)) |
||||
.willReturn(this.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); |
||||
|
||||
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)) |
||||
.willThrow(cypherException); |
||||
|
||||
Health health = this.neo4jHealthIndicator.health(); |
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue