Change query in MongoHealthIndicator

This commit changes the query in MongoHealthIndicator from serverStatus to buildInfo to avoid unprivileged access and corresponding errors.

fixes #1289
pull/1332/head
Christian Dupuis 10 years ago
parent 7945b29669
commit 68163a66a5

@ -39,7 +39,7 @@ public class MongoHealthIndicator extends AbstractHealthIndicator {
@Override @Override
protected void doHealthCheck(Health.Builder builder) throws Exception { protected void doHealthCheck(Health.Builder builder) throws Exception {
CommandResult result = this.mongoTemplate.executeCommand("{ serverStatus: 1 }"); CommandResult result = this.mongoTemplate.executeCommand("{ buildInfo: 1 }");
builder.up().withDetail("version", result.getString("version")); builder.up().withDetail("version", result.getString("version"));
} }

@ -67,7 +67,7 @@ public class MongoHealthIndicatorTests {
CommandResult commandResult = Mockito.mock(CommandResult.class); CommandResult commandResult = Mockito.mock(CommandResult.class);
Mockito.when(commandResult.getString("version")).thenReturn("2.6.4"); Mockito.when(commandResult.getString("version")).thenReturn("2.6.4");
MongoTemplate mongoTemplate = Mockito.mock(MongoTemplate.class); MongoTemplate mongoTemplate = Mockito.mock(MongoTemplate.class);
Mockito.when(mongoTemplate.executeCommand("{ serverStatus: 1 }")).thenReturn( Mockito.when(mongoTemplate.executeCommand("{ buildInfo: 1 }")).thenReturn(
commandResult); commandResult);
MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate); MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate);
@ -76,13 +76,13 @@ public class MongoHealthIndicatorTests {
assertEquals("2.6.4", health.getDetails().get("version")); assertEquals("2.6.4", health.getDetails().get("version"));
Mockito.verify(commandResult).getString("version"); Mockito.verify(commandResult).getString("version");
Mockito.verify(mongoTemplate).executeCommand("{ serverStatus: 1 }"); Mockito.verify(mongoTemplate).executeCommand("{ buildInfo: 1 }");
} }
@Test @Test
public void mongoIsDown() throws Exception { public void mongoIsDown() throws Exception {
MongoTemplate mongoTemplate = Mockito.mock(MongoTemplate.class); MongoTemplate mongoTemplate = Mockito.mock(MongoTemplate.class);
Mockito.when(mongoTemplate.executeCommand("{ serverStatus: 1 }")).thenThrow( Mockito.when(mongoTemplate.executeCommand("{ buildInfo: 1 }")).thenThrow(
new MongoException("Connection failed")); new MongoException("Connection failed"));
MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate); MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate);
@ -91,6 +91,6 @@ public class MongoHealthIndicatorTests {
assertTrue(((String) health.getDetails().get("error")) assertTrue(((String) health.getDetails().get("error"))
.contains("Connection failed")); .contains("Connection failed"));
Mockito.verify(mongoTemplate).executeCommand("{ serverStatus: 1 }"); Mockito.verify(mongoTemplate).executeCommand("{ buildInfo: 1 }");
} }
} }

Loading…
Cancel
Save