diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthAggregator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthAggregator.java index d5c8f0b8e15..f3ede3cd185 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthAggregator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthAggregator.java @@ -38,7 +38,7 @@ public abstract class AbstractHealthAggregator implements HealthAggregator { details.put(entry.getKey(), entry.getValue()); statusCandidates.add(entry.getValue().getStatus()); } - return new Health(aggregateStatus(statusCandidates), details); + return new Health.Builder(aggregateStatus(statusCandidates), details).build(); } /** diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthIndicator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthIndicator.java index 04a874d93e0..21a00996a1a 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthIndicator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/AbstractHealthIndicator.java @@ -16,12 +16,15 @@ package org.springframework.boot.actuate.health; +import org.springframework.boot.actuate.health.Health.Builder; + /** * Base {@link HealthIndicator} implementations that encapsulates creation of * {@link Health} instance and error handling. *

* This implementation is only suitable if an {@link Exception} raised from - * {@link #doHealthCheck()} should create a {@link Status#DOWN} health status. + * {@link #doHealthCheck(org.springframework.boot.actuate.health.Health.Builder)} should + * create a {@link Status#DOWN} health status. * * @author Christian Dupuis * @since 1.1.0 @@ -30,19 +33,21 @@ public abstract class AbstractHealthIndicator implements HealthIndicator { @Override public final Health health() { + Health.Builder builder = new Health.Builder(); try { - return doHealthCheck(); + doHealthCheck(builder); } catch (Exception ex) { - return Health.down(ex); + builder.down(ex); } + return builder.build(); } /** * Actual health check logic. - * @return the {@link Health} + * @param builder the {@link Builder} to report health status and details * @throws Exception any {@link Exception} that should create a {@link Status#DOWN} * system status. */ - protected abstract Health doHealthCheck() throws Exception; + protected abstract void doHealthCheck(Health.Builder builder) throws Exception; } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DataSourceHealthIndicator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DataSourceHealthIndicator.java index e5c97039428..210b2a38c17 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DataSourceHealthIndicator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DataSourceHealthIndicator.java @@ -70,27 +70,28 @@ public class DataSourceHealthIndicator extends AbstractHealthIndicator { } @Override - protected Health doHealthCheck() throws Exception { + protected void doHealthCheck(Health.Builder builder) throws Exception { if (this.dataSource == null) { - return Health.up().withDetail("database", "unknown"); + builder.up().withDetail("database", "unknown"); + } + else { + doDataSourceHealthCheck(builder); } - return doDataSourceHealthCheck(); } - private Health doDataSourceHealthCheck() throws Exception { + private void doDataSourceHealthCheck(Health.Builder builder) throws Exception { String product = getProduct(); - Health health = Health.up().withDetail("database", product); + builder.up().withDetail("database", product); String query = detectQuery(product); if (StringUtils.hasText(query)) { try { - health = health.withDetail("hello", + builder.withDetail("hello", this.jdbcTemplate.queryForObject(query, Object.class)); } catch (Exception ex) { - return Health.down().withDetail("database", product).withException(ex); + builder.down(ex); } } - return health; } private String getProduct() { diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java index 558dd96e840..61e63b8d82f 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java @@ -23,7 +23,6 @@ import java.util.Map; import org.springframework.util.Assert; import com.fasterxml.jackson.annotation.JsonAnyGetter; -import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonUnwrapped; @@ -34,16 +33,16 @@ import com.fasterxml.jackson.annotation.JsonUnwrapped; * {@link Health} contains a {@link Status} to express the state of a component or * subsystem and some additional details to carry some contextual information. *

- * {@link Health} has a fluent API to make it easy to construct instances. Typical usage - * in a {@link HealthIndicator} would be: + * {@link Health} instances can be created by using {@link Builder}'s fluent API. Typical + * usage in a {@link HealthIndicator} would be: * *

  * try {
  * 	// do some test to determine state of component
- * 	return Health.up("version", "1.1.2");
+ * 	return new Health.Builder().up().withDetail("version", "1.1.2").build();
  * }
  * catch (Exception ex) {
- * 	return Health.down(ex);
+ * 	return new Health.Builder().down(ex).build();
  * }
  * 
* @@ -54,23 +53,18 @@ import com.fasterxml.jackson.annotation.JsonUnwrapped; @JsonInclude(Include.NON_EMPTY) public final class Health { - private static final Map NO_DETAILS = Collections - . emptyMap(); - private final Status status; private final Map details; /** * Create a new {@link Health} instance with the specified status and details. - * @param status the status - * @param details the details or {@code null} + * @param builder the Builder to use */ - public Health(Status status, Map details) { - Assert.notNull(status, "Status must not be null"); - this.status = status; - this.details = Collections.unmodifiableMap(details == null ? NO_DETAILS - : new LinkedHashMap(details)); + private Health(Builder builder) { + Assert.notNull(builder, "Builder must not be null"); + this.status = builder.status; + this.details = Collections.unmodifiableMap(builder.details); } /** @@ -113,87 +107,193 @@ public final class Health { } /** - * Create a new {@link Health} object from this one, containing an additional - * exception detail. - * @param ex the exception - * @return a new {@link Health} instance - */ - public Health withException(Exception ex) { - Assert.notNull(ex, "Exception must not be null"); - return withDetail("error", ex.getClass().getName() + ": " + ex.getMessage()); - } - - /** - * Create a new {@link Health} object from this one, containing an additional detail. - * @param key the detail key - * @param data the detail data - * @return a new {@link Health} instance + * Create a new {@link Builder} instance with an {@link Status#UNKNOWN} status. + * @return a new {@link Builder} instance */ - @JsonAnySetter - public Health withDetail(String key, Object data) { - Assert.notNull(key, "Key must not be null"); - Assert.notNull(data, "Data must not be null"); - Map details = new LinkedHashMap(this.details); - details.put(key, data); - return new Health(this.status, details); - } - - /** - * Create a new {@link Health} instance with an {@link Status#UNKNOWN} status. - * @return a new {@link Health} instance - */ - public static Health unknown() { + public static Builder unknown() { return status(Status.UNKNOWN); } /** - * Create a new {@link Health} instance with an {@link Status#UP} status. - * @return a new {@link Health} instance + * Create a new {@link Builder} instance with an {@link Status#UP} status. + * @return a new {@link Builder} instance */ - public static Health up() { + public static Builder up() { return status(Status.UP); } /** - * Create a new {@link Health} instance with an {@link Status#DOWN} status an the + * Create a new {@link Builder} instance with an {@link Status#DOWN} status an the * specified exception details. * @param ex the exception - * @return a new {@link Health} instance + * @return a new {@link Builder} instance */ - public static Health down(Exception ex) { + public static Builder down(Exception ex) { return down().withException(ex); } /** - * Create a new {@link Health} instance with a {@link Status#DOWN} status. - * @return a new {@link Health} instance + * Create a new {@link Builder} instance with a {@link Status#DOWN} status. + * @return a new {@link Builder} instance */ - public static Health down() { + public static Builder down() { return status(Status.DOWN); } /** - * Create a new {@link Health} instance with an {@link Status#OUT_OF_SERVICE} status. - * @return a new {@link Health} instance + * Create a new {@link Builder} instance with an {@link Status#OUT_OF_SERVICE} status. + * @return a new {@link Builder} instance */ - public static Health outOfService() { + public static Builder outOfService() { return status(Status.OUT_OF_SERVICE); } /** - * Create a new {@link Health} instance with a specific status code. - * @return a new {@link Health} instance + * Create a new {@link Builder} instance with a specific status code. + * @return a new {@link Builder} instance */ - public static Health status(String statusCode) { + public static Builder status(String statusCode) { return status(new Status(statusCode)); } /** - * Create a new {@link Health} instance with a specific {@link Status}. - * @return a new {@link Health} instance + * Create a new {@link Builder} instance with a specific {@link Status}. + * @return a new {@link Builder} instance */ - public static Health status(Status status) { - return new Health(status, null); + public static Builder status(Status status) { + return new Builder(status); + } + + /** + * Builder for creating immutable {@link Health} instances. + */ + public static class Builder { + + private Status status; + + private Map details; + + /** + * Create new Builder instance. + */ + public Builder() { + this.status = Status.UNKNOWN; + this.details = new LinkedHashMap(); + } + + /** + * Create new Builder instance, setting status to given status. + * @param status the {@link Status} to use + */ + public Builder(Status status) { + Assert.notNull(status, "Status must not be null"); + this.status = status; + this.details = new LinkedHashMap(); + } + + /** + * Create new Builder instance, setting status to given status and + * details to given details. + * @param status the {@link Status} to use + * @param details the details {@link Map} to use + */ + public Builder(Status status, Map details) { + Assert.notNull(status, "Status must not be null"); + Assert.notNull(details, "Details must not be null"); + this.status = status; + this.details = new LinkedHashMap(details); + } + + /** + * Record detail for given {@link Exception}. + * @param ex the exception + * @return this {@link Builder} instance + */ + public Builder withException(Exception ex) { + Assert.notNull(ex, "Exception must not be null"); + return withDetail("error", ex.getClass().getName() + ": " + ex.getMessage()); + } + + /** + * Record detail using key and value. + * @param key the detail key + * @param data the detail data + * @return this {@link Builder} instance + */ + public Builder withDetail(String key, Object data) { + Assert.notNull(key, "Key must not be null"); + Assert.notNull(data, "Data must not be null"); + this.details.put(key, data); + return this; + } + + /** + * Set status to {@link Status#UNKNOWN} status. + * @return this {@link Builder} instance + */ + public Builder unknown() { + return status(Status.UNKNOWN); + } + + /** + * Set status to {@link Status#UP} status. + * @return this {@link Builder} instance + */ + public Builder up() { + return status(Status.UP); + } + + /** + * Set status to {@link Status#DOWN} and add details for given {@link Exception}. + * @param ex the exception + * @return this {@link Builder} instance + */ + public Builder down(Exception ex) { + return down().withException(ex); + } + + /** + * Set status to {@link Status#DOWN}. + * @return this {@link Builder} instance + */ + public Builder down() { + return status(Status.DOWN); + } + + /** + * Set status to {@link Status#OUT_OF_SERVICE}. + * @return this {@link Builder} instance + */ + public Builder outOfService() { + return status(Status.OUT_OF_SERVICE); + } + + /** + * Set status to given statusCode. + * @return this {@link Builder} instance + */ + public Builder status(String statusCode) { + return status(new Status(statusCode)); + } + + /** + * Set status to given {@link Status} instance + * @param status + * @return this {@link Builder} instance + */ + public Builder status(Status status) { + this.status = status; + return this; + } + + /** + * Create a new {@link Health} instance with the previously specified code and + * details. + * @return a new {@link Health} instance + */ + public Health build() { + return new Health(this); + } } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/MongoHealthIndicator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/MongoHealthIndicator.java index a7840f84e2c..d716fd41b39 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/MongoHealthIndicator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/MongoHealthIndicator.java @@ -38,9 +38,9 @@ public class MongoHealthIndicator extends AbstractHealthIndicator { } @Override - protected Health doHealthCheck() throws Exception { + protected void doHealthCheck(Health.Builder builder) throws Exception { CommandResult result = this.mongoTemplate.executeCommand("{ serverStatus: 1 }"); - return Health.up().withDetail("version", result.getString("version")); + builder.up().withDetail("version", result.getString("version")); } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/RabbitHealthIndicator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/RabbitHealthIndicator.java index 567d967d536..52c7373e88b 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/RabbitHealthIndicator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/RabbitHealthIndicator.java @@ -41,8 +41,8 @@ public class RabbitHealthIndicator extends AbstractHealthIndicator { } @Override - protected Health doHealthCheck() throws Exception { - return Health.up().withDetail("version", getVersion()); + protected void doHealthCheck(Health.Builder builder) throws Exception { + builder.up().withDetail("version", getVersion()); } private String getVersion() { diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/RedisHealthIndicator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/RedisHealthIndicator.java index 201d0d6f34a..0a98d845d88 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/RedisHealthIndicator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/RedisHealthIndicator.java @@ -40,12 +40,12 @@ public class RedisHealthIndicator extends AbstractHealthIndicator { } @Override - protected Health doHealthCheck() throws Exception { + protected void doHealthCheck(Health.Builder builder) throws Exception { RedisConnection connection = RedisConnectionUtils .getConnection(this.redisConnectionFactory); try { Properties info = connection.info(); - return Health.up().withDetail("version", info.getProperty("redis_version")); + builder.up().withDetail("version", info.getProperty("redis_version")); } finally { RedisConnectionUtils.releaseConnection(connection, diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SolrHealthIndicator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SolrHealthIndicator.java index 0098a50a083..35280b5325a 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SolrHealthIndicator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/SolrHealthIndicator.java @@ -19,7 +19,7 @@ package org.springframework.boot.actuate.health; import org.apache.solr.client.solrj.SolrServer; /** - * {@link HealthIndicator} for Apache Solr + * {@link HealthIndicator} for Apache Solr. * * @author Andy Wilkinson * @since 1.1.0 @@ -33,9 +33,9 @@ public class SolrHealthIndicator extends AbstractHealthIndicator { } @Override - protected Health doHealthCheck() throws Exception { + protected void doHealthCheck(Health.Builder builder) throws Exception { Object status = this.solrServer.ping().getResponse().get("status"); - return Health.up().withDetail("solrStatus", status); + builder.up().withDetail("solrStatus", status); } } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/VanillaHealthIndicator.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/VanillaHealthIndicator.java index 35862964b39..5c8c63118d3 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/VanillaHealthIndicator.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/VanillaHealthIndicator.java @@ -25,8 +25,8 @@ package org.springframework.boot.actuate.health; public class VanillaHealthIndicator extends AbstractHealthIndicator { @Override - protected Health doHealthCheck() throws Exception { - return Health.up(); + protected void doHealthCheck(Health.Builder builder) throws Exception { + builder.up(); } } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/HealthEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/HealthEndpointTests.java index 74ff29684a9..d10d6899052 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/HealthEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/HealthEndpointTests.java @@ -65,7 +65,7 @@ public class HealthEndpointTests extends AbstractEndpointTests { @Override public Health health() { - return Health.status("FINE"); + return new Health.Builder().status("FINE").build(); } }; } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpointTests.java index 655f9fccc6b..bbbd1ad5c6d 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpointTests.java @@ -51,7 +51,7 @@ public class HealthMvcEndpointTests { @Test public void up() { - when(this.endpoint.invoke()).thenReturn(Health.up()); + when(this.endpoint.invoke()).thenReturn(new Health.Builder().up().build()); Object result = this.mvc.invoke(); assertTrue(result instanceof Health); assertTrue(((Health) result).getStatus() == Status.UP); @@ -60,7 +60,7 @@ public class HealthMvcEndpointTests { @SuppressWarnings("unchecked") @Test public void down() { - when(this.endpoint.invoke()).thenReturn(Health.down()); + when(this.endpoint.invoke()).thenReturn(new Health.Builder().down().build()); Object result = this.mvc.invoke(); assertTrue(result instanceof ResponseEntity); ResponseEntity response = (ResponseEntity) result; @@ -71,7 +71,8 @@ public class HealthMvcEndpointTests { @SuppressWarnings("unchecked") @Test public void customMapping() { - when(this.endpoint.invoke()).thenReturn(Health.status("OK")); + when(this.endpoint.invoke()) + .thenReturn(new Health.Builder().status("OK").build()); this.mvc.setStatusMapping(Collections.singletonMap("OK", HttpStatus.INTERNAL_SERVER_ERROR)); Object result = this.mvc.invoke(); diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthIndicatorTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthIndicatorTests.java index 8df29d4079d..23b921b6eeb 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthIndicatorTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/CompositeHealthIndicatorTests.java @@ -55,9 +55,12 @@ public class CompositeHealthIndicatorTests { @Before public void setup() { MockitoAnnotations.initMocks(this); - given(this.one.health()).willReturn(Health.unknown().withDetail("1", "1")); - given(this.two.health()).willReturn(Health.unknown().withDetail("2", "2")); - given(this.three.health()).willReturn(Health.unknown().withDetail("3", "3")); + given(this.one.health()).willReturn( + new Health.Builder().unknown().withDetail("1", "1").build()); + given(this.two.health()).willReturn( + new Health.Builder().unknown().withDetail("2", "2").build()); + given(this.three.health()).willReturn( + new Health.Builder().unknown().withDetail("3", "3").build()); this.healthAggregator = new OrderedHealthAggregator(); } @@ -71,10 +74,16 @@ public class CompositeHealthIndicatorTests { this.healthAggregator, indicators); Health result = composite.health(); assertThat(result.getDetails().size(), equalTo(2)); - assertThat(result.getDetails(), - hasEntry("one", (Object) Health.unknown().withDetail("1", "1"))); - assertThat(result.getDetails(), - hasEntry("two", (Object) Health.unknown().withDetail("2", "2"))); + assertThat( + result.getDetails(), + hasEntry("one", + (Object) new Health.Builder().unknown().withDetail("1", "1") + .build())); + assertThat( + result.getDetails(), + hasEntry("two", + (Object) new Health.Builder().unknown().withDetail("2", "2") + .build())); } @Test @@ -87,12 +96,21 @@ public class CompositeHealthIndicatorTests { composite.addHealthIndicator("three", this.three); Health result = composite.health(); assertThat(result.getDetails().size(), equalTo(3)); - assertThat(result.getDetails(), - hasEntry("one", (Object) Health.unknown().withDetail("1", "1"))); - assertThat(result.getDetails(), - hasEntry("two", (Object) Health.unknown().withDetail("2", "2"))); - assertThat(result.getDetails(), - hasEntry("three", (Object) Health.unknown().withDetail("3", "3"))); + assertThat( + result.getDetails(), + hasEntry("one", + (Object) new Health.Builder().unknown().withDetail("1", "1") + .build())); + assertThat( + result.getDetails(), + hasEntry("two", + (Object) new Health.Builder().unknown().withDetail("2", "2") + .build())); + assertThat( + result.getDetails(), + hasEntry("three", + (Object) new Health.Builder().unknown().withDetail("3", "3") + .build())); } @Test @@ -103,10 +121,16 @@ public class CompositeHealthIndicatorTests { composite.addHealthIndicator("two", this.two); Health result = composite.health(); assertThat(result.getDetails().size(), equalTo(2)); - assertThat(result.getDetails(), - hasEntry("one", (Object) Health.unknown().withDetail("1", "1"))); - assertThat(result.getDetails(), - hasEntry("two", (Object) Health.unknown().withDetail("2", "2"))); + assertThat( + result.getDetails(), + hasEntry("one", + (Object) new Health.Builder().unknown().withDetail("1", "1") + .build())); + assertThat( + result.getDetails(), + hasEntry("two", + (Object) new Health.Builder().unknown().withDetail("2", "2") + .build())); } @Test diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/HealthTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/HealthTests.java index bfc44628ab4..72e34a8c4cc 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/HealthTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/HealthTests.java @@ -40,28 +40,31 @@ public class HealthTests { public void statusMustNotBeNull() throws Exception { this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("Status must not be null"); - new Health(null, null); + new Health.Builder(null, null); } @Test public void createWithStatus() throws Exception { - Health health = new Health(Status.UP, null); + Health health = Health.status(Status.UP).build(); assertThat(health.getStatus(), equalTo(Status.UP)); assertThat(health.getDetails().size(), equalTo(0)); } @Test public void createWithDetails() throws Exception { - Health health = new Health(Status.UP, Collections.singletonMap("a", "b")); + Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b")) + .build(); assertThat(health.getStatus(), equalTo(Status.UP)); assertThat(health.getDetails().get("a"), equalTo((Object) "b")); } @Test public void equalsAndHashCode() throws Exception { - Health h1 = new Health(Status.UP, Collections.singletonMap("a", "b")); - Health h2 = new Health(Status.UP, Collections.singletonMap("a", "b")); - Health h3 = new Health(Status.UP, null); + Health h1 = new Health.Builder(Status.UP, Collections.singletonMap("a", "b")) + .build(); + Health h2 = new Health.Builder(Status.UP, Collections.singletonMap("a", "b")) + .build(); + Health h3 = new Health.Builder(Status.UP).build(); assertThat(h1, equalTo(h1)); assertThat(h1, equalTo(h2)); assertThat(h1, not(equalTo(h3))); @@ -73,8 +76,8 @@ public class HealthTests { @Test public void withException() throws Exception { RuntimeException ex = new RuntimeException("bang"); - Health health = new Health(Status.UP, Collections.singletonMap("a", "b")) - .withException(ex); + Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b")) + .withException(ex).build(); assertThat(health.getDetails().get("a"), equalTo((Object) "b")); assertThat(health.getDetails().get("error"), equalTo((Object) "java.lang.RuntimeException: bang")); @@ -82,36 +85,36 @@ public class HealthTests { @Test public void withDetails() throws Exception { - Health health = new Health(Status.UP, Collections.singletonMap("a", "b")) - .withDetail("c", "d"); + Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b")) + .withDetail("c", "d").build(); assertThat(health.getDetails().get("a"), equalTo((Object) "b")); assertThat(health.getDetails().get("c"), equalTo((Object) "d")); } @Test public void unknownWithDetails() throws Exception { - Health health = Health.unknown().withDetail("a", "b"); + Health health = new Health.Builder().unknown().withDetail("a", "b").build(); assertThat(health.getStatus(), equalTo(Status.UNKNOWN)); assertThat(health.getDetails().get("a"), equalTo((Object) "b")); } @Test public void unknown() throws Exception { - Health health = Health.unknown(); + Health health = new Health.Builder().unknown().build(); assertThat(health.getStatus(), equalTo(Status.UNKNOWN)); assertThat(health.getDetails().size(), equalTo(0)); } @Test public void upWithDetails() throws Exception { - Health health = Health.up().withDetail("a", "b"); + Health health = new Health.Builder().up().withDetail("a", "b").build(); assertThat(health.getStatus(), equalTo(Status.UP)); assertThat(health.getDetails().get("a"), equalTo((Object) "b")); } @Test public void up() throws Exception { - Health health = Health.up(); + Health health = new Health.Builder().up().build(); assertThat(health.getStatus(), equalTo(Status.UP)); assertThat(health.getDetails().size(), equalTo(0)); } @@ -119,7 +122,7 @@ public class HealthTests { @Test public void downWithException() throws Exception { RuntimeException ex = new RuntimeException("bang"); - Health health = Health.down(ex); + Health health = Health.down(ex).build(); assertThat(health.getStatus(), equalTo(Status.DOWN)); assertThat(health.getDetails().get("error"), equalTo((Object) "java.lang.RuntimeException: bang")); @@ -127,28 +130,28 @@ public class HealthTests { @Test public void down() throws Exception { - Health health = Health.down(); + Health health = Health.down().build(); assertThat(health.getStatus(), equalTo(Status.DOWN)); assertThat(health.getDetails().size(), equalTo(0)); } @Test public void outOfService() throws Exception { - Health health = Health.outOfService(); + Health health = Health.outOfService().build(); assertThat(health.getStatus(), equalTo(Status.OUT_OF_SERVICE)); assertThat(health.getDetails().size(), equalTo(0)); } @Test public void statusCode() throws Exception { - Health health = Health.status("UP"); + Health health = Health.status("UP").build(); assertThat(health.getStatus(), equalTo(Status.UP)); assertThat(health.getDetails().size(), equalTo(0)); } @Test public void status() throws Exception { - Health health = Health.status(Status.UP); + Health health = Health.status(Status.UP).build(); assertThat(health.getStatus(), equalTo(Status.UP)); assertThat(health.getDetails().size(), equalTo(0)); } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/OrderedHealthAggregatorTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/OrderedHealthAggregatorTests.java index 8309aaba08a..3a66b17031e 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/OrderedHealthAggregatorTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/OrderedHealthAggregatorTests.java @@ -42,10 +42,10 @@ public class OrderedHealthAggregatorTests { @Test public void defaultOrder() { Map healths = new HashMap(); - healths.put("h1", Health.status(Status.DOWN)); - healths.put("h2", Health.status(Status.UP)); - healths.put("h3", Health.status(Status.UNKNOWN)); - healths.put("h4", Health.status(Status.OUT_OF_SERVICE)); + healths.put("h1", new Health.Builder().status(Status.DOWN).build()); + healths.put("h2", new Health.Builder().status(Status.UP).build()); + healths.put("h3", new Health.Builder().status(Status.UNKNOWN).build()); + healths.put("h4", new Health.Builder().status(Status.OUT_OF_SERVICE).build()); assertEquals(Status.DOWN, this.healthAggregator.aggregate(healths).getStatus()); } @@ -54,21 +54,21 @@ public class OrderedHealthAggregatorTests { this.healthAggregator.setStatusOrder(Status.UNKNOWN, Status.UP, Status.OUT_OF_SERVICE, Status.DOWN); Map healths = new HashMap(); - healths.put("h1", Health.status(Status.DOWN)); - healths.put("h2", Health.status(Status.UP)); - healths.put("h3", Health.status(Status.UNKNOWN)); - healths.put("h4", Health.status(Status.OUT_OF_SERVICE)); + healths.put("h1", new Health.Builder().status(Status.DOWN).build()); + healths.put("h2", new Health.Builder().status(Status.UP).build()); + healths.put("h3", new Health.Builder().status(Status.UNKNOWN).build()); + healths.put("h4", new Health.Builder().status(Status.OUT_OF_SERVICE).build()); assertEquals(Status.UNKNOWN, this.healthAggregator.aggregate(healths).getStatus()); } @Test public void defaultOrderWithCustomStatus() { Map healths = new HashMap(); - healths.put("h1", Health.status(Status.DOWN)); - healths.put("h2", Health.status(Status.UP)); - healths.put("h3", Health.status(Status.UNKNOWN)); - healths.put("h4", Health.status(Status.OUT_OF_SERVICE)); - healths.put("h5", Health.status(new Status("CUSTOM"))); + healths.put("h1", new Health.Builder().status(Status.DOWN).build()); + healths.put("h2", new Health.Builder().status(Status.UP).build()); + healths.put("h3", new Health.Builder().status(Status.UNKNOWN).build()); + healths.put("h4", new Health.Builder().status(Status.OUT_OF_SERVICE).build()); + healths.put("h5", new Health.Builder().status(new Status("CUSTOM")).build()); assertEquals(new Status("CUSTOM"), this.healthAggregator.aggregate(healths) .getStatus()); } @@ -78,11 +78,11 @@ public class OrderedHealthAggregatorTests { this.healthAggregator.setStatusOrder(Arrays.asList("DOWN", "OUT_OF_SERVICE", "UP", "UNKNOWN", "CUSTOM")); Map healths = new HashMap(); - healths.put("h1", Health.status(Status.DOWN)); - healths.put("h2", Health.status(Status.UP)); - healths.put("h3", Health.status(Status.UNKNOWN)); - healths.put("h4", Health.status(Status.OUT_OF_SERVICE)); - healths.put("h5", Health.status(new Status("CUSTOM"))); + healths.put("h1", new Health.Builder().status(Status.DOWN).build()); + healths.put("h2", new Health.Builder().status(Status.UP).build()); + healths.put("h3", new Health.Builder().status(Status.UNKNOWN).build()); + healths.put("h4", new Health.Builder().status(Status.OUT_OF_SERVICE).build()); + healths.put("h5", new Health.Builder().status(new Status("CUSTOM")).build()); assertEquals(Status.DOWN, this.healthAggregator.aggregate(healths).getStatus()); }