From c5ec735afb1b7a8cc3b7153fe7572ca36d2b798d Mon Sep 17 00:00:00 2001 From: Christian Dupuis Date: Thu, 8 May 2014 15:56:29 +0200 Subject: [PATCH] Add unit test for (Mongo|Redis)HealthIndicator --- ...HealthIndicatorAutoConfigurationTests.java | 1 - .../health/MongoHealthIndicatorTests.java | 40 +++++++++++++-- .../health/RedisHealthIndicatorTests.java | 49 +++++++++++++++++-- 3 files changed, 83 insertions(+), 7 deletions(-) diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java index 96c1aec404..9ec1ecef49 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationTests.java @@ -38,7 +38,6 @@ import static org.junit.Assert.assertEquals; * Tests for {@link HealthIndicatorAutoConfiguration}. * * @author Christian Dupuis - * @since 1.1.0 */ public class HealthIndicatorAutoConfigurationTests { diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/MongoHealthIndicatorTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/MongoHealthIndicatorTests.java index 8d2b89f0d2..eebf168bff 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/MongoHealthIndicatorTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/MongoHealthIndicatorTests.java @@ -16,8 +16,11 @@ package org.springframework.boot.actuate.health; +import java.util.Map; + import org.junit.After; import org.junit.Test; +import org.mockito.Mockito; import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; @@ -26,14 +29,17 @@ import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.data.mongodb.core.MongoTemplate; +import com.mongodb.CommandResult; +import com.mongodb.MongoException; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; /** * Tests for {@link MongoHealthIndicator}. * * @author Christian Dupuis - * @since 1.1.0 */ public class MongoHealthIndicatorTests { @@ -47,7 +53,7 @@ public class MongoHealthIndicatorTests { } @Test - public void healthIndicatorExists() { + public void indicatorExists() { this.context = new AnnotationConfigApplicationContext( PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, EndpointAutoConfiguration.class, @@ -58,6 +64,34 @@ public class MongoHealthIndicatorTests { assertNotNull(healthIndicator); } - // TODO add tests for actual health check + @Test + public void mongoIsUp() throws Exception { + CommandResult commandResult = Mockito.mock(CommandResult.class); + Mockito.when(commandResult.getString("version")).thenReturn("2.6.4"); + MongoTemplate mongoTemplate = Mockito.mock(MongoTemplate.class); + Mockito.when(mongoTemplate.executeCommand("{ serverStatus: 1 }")).thenReturn( + commandResult); + MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate); + + Map health = healthIndicator.health(); + assertEquals("ok", health.get("status")); + assertEquals("2.6.4", health.get("version")); + + Mockito.verify(commandResult).getString("version"); + Mockito.verify(mongoTemplate).executeCommand("{ serverStatus: 1 }"); + } + + @Test + public void mongoIsDown() throws Exception { + MongoTemplate mongoTemplate = Mockito.mock(MongoTemplate.class); + Mockito.when(mongoTemplate.executeCommand("{ serverStatus: 1 }")).thenThrow( + new MongoException("Connection failed")); + MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate); + Map health = healthIndicator.health(); + assertEquals("error", health.get("status")); + assertTrue(((String) health.get("error")).contains("Connection failed")); + + Mockito.verify(mongoTemplate).executeCommand("{ serverStatus: 1 }"); + } } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/RedisHealthIndicatorTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/RedisHealthIndicatorTests.java index 576bd10808..c599920f13 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/RedisHealthIndicatorTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/RedisHealthIndicatorTests.java @@ -16,23 +16,29 @@ package org.springframework.boot.actuate.health; +import java.util.Map; +import java.util.Properties; + import org.junit.After; import org.junit.Test; +import org.mockito.Mockito; import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.data.redis.RedisConnectionFailureException; +import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; /** * Tests for {@link RedisHealthIndicator}. * * @author Christian Dupuis - * @since 1.1.0 */ public class RedisHealthIndicatorTests { @@ -46,7 +52,7 @@ public class RedisHealthIndicatorTests { } @Test - public void healthIndicatorExists() { + public void indicatorExists() { this.context = new AnnotationConfigApplicationContext( PropertyPlaceholderAutoConfiguration.class, RedisAutoConfiguration.class, EndpointAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); @@ -57,6 +63,43 @@ public class RedisHealthIndicatorTests { assertNotNull(healthIndicator); } - // TODO add tests for actual health check + @Test + public void redisIsUp() throws Exception { + Properties info = new Properties(); + info.put("redis_version", "2.8.9"); + + RedisConnection redisConnection = Mockito.mock(RedisConnection.class); + RedisConnectionFactory redisConnectionFactory = Mockito + .mock(RedisConnectionFactory.class); + Mockito.when(redisConnectionFactory.getConnection()).thenReturn(redisConnection); + Mockito.when(redisConnection.info()).thenReturn(info); + RedisHealthIndicator healthIndicator = new RedisHealthIndicator( + redisConnectionFactory); + + Map health = healthIndicator.health(); + assertEquals("ok", health.get("status")); + assertEquals("2.8.9", health.get("version")); + + Mockito.verify(redisConnectionFactory).getConnection(); + Mockito.verify(redisConnection).info(); + } + + @Test + public void redisIsDown() throws Exception { + RedisConnection redisConnection = Mockito.mock(RedisConnection.class); + RedisConnectionFactory redisConnectionFactory = Mockito + .mock(RedisConnectionFactory.class); + Mockito.when(redisConnectionFactory.getConnection()).thenReturn(redisConnection); + Mockito.when(redisConnection.info()).thenThrow( + new RedisConnectionFailureException("Connection failed")); + RedisHealthIndicator healthIndicator = new RedisHealthIndicator( + redisConnectionFactory); + Map health = healthIndicator.health(); + assertEquals("error", health.get("status")); + assertTrue(((String) health.get("error")).contains("Connection failed")); + + Mockito.verify(redisConnectionFactory).getConnection(); + Mockito.verify(redisConnection).info(); + } }