Introduce Health.Builder to clean up HealthIndicator implementations
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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.
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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.
|
||||
* <p>
|
||||
* {@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:
|
||||
*
|
||||
* <pre class="code">
|
||||
* 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();
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
@@ -54,23 +53,18 @@ import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
||||
@JsonInclude(Include.NON_EMPTY)
|
||||
public final class Health {
|
||||
|
||||
private static final Map<String, Object> NO_DETAILS = Collections
|
||||
.<String, Object> emptyMap();
|
||||
|
||||
private final Status status;
|
||||
|
||||
private final Map<String, Object> 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<String, ?> details) {
|
||||
Assert.notNull(status, "Status must not be null");
|
||||
this.status = status;
|
||||
this.details = Collections.unmodifiableMap(details == null ? NO_DETAILS
|
||||
: new LinkedHashMap<String, Object>(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
|
||||
* Create a new {@link Builder} instance with an {@link Status#UNKNOWN} status.
|
||||
* @return a new {@link Builder} 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
|
||||
*/
|
||||
@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<String, Object> details = new LinkedHashMap<String, Object>(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<String, Object> details;
|
||||
|
||||
/**
|
||||
* Create new Builder instance.
|
||||
*/
|
||||
public Builder() {
|
||||
this.status = Status.UNKNOWN;
|
||||
this.details = new LinkedHashMap<String, Object>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new Builder instance, setting status to given <code>status</code>.
|
||||
* @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<String, Object>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new Builder instance, setting status to given <code>status</code> and
|
||||
* details to given <code>details</code>.
|
||||
* @param status the {@link Status} to use
|
||||
* @param details the details {@link Map} to use
|
||||
*/
|
||||
public Builder(Status status, Map<String, ?> details) {
|
||||
Assert.notNull(status, "Status must not be null");
|
||||
Assert.notNull(details, "Details must not be null");
|
||||
this.status = status;
|
||||
this.details = new LinkedHashMap<String, Object>(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 <code>key</code> and <code>value</code>.
|
||||
* @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 <code>statusCode</code>.
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class HealthEndpointTests extends AbstractEndpointTests<HealthEndpoint> {
|
||||
|
||||
@Override
|
||||
public Health health() {
|
||||
return Health.status("FINE");
|
||||
return new Health.Builder().status("FINE").build();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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<Health> response = (ResponseEntity<Health>) 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();
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -42,10 +42,10 @@ public class OrderedHealthAggregatorTests {
|
||||
@Test
|
||||
public void defaultOrder() {
|
||||
Map<String, Health> healths = new HashMap<String, Health>();
|
||||
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<String, Health> healths = new HashMap<String, Health>();
|
||||
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<String, Health> healths = new HashMap<String, Health>();
|
||||
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<String, Health> healths = new HashMap<String, Health>();
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user