Add CompositeHealthIndicator

Add CompositeHealthIndicator that allows multiple HealthIndicators
to be combined into a single result.

Fixes gh-782
This commit is contained in:
Tyler Frederick 2014-05-05 11:24:11 +01:00 committed by Phillip Webb
parent 5f02a67d8d
commit 697afe3842
2 changed files with 155 additions and 0 deletions

View File

@ -0,0 +1,61 @@
/*
* Copyright 2012-2014 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.LinkedHashMap;
import java.util.Map;
/**
* {@link HealthIndicator} that returns health indications from all registered delegates.
*
* @author Tyler J. Frederick
* @author Phillip Webb
*/
public class CompositeHealthIndicator implements HealthIndicator<Map<String, Object>> {
private final Map<String, HealthIndicator<?>> indicators;
/**
* Create a new {@link CompositeHealthIndicator}.
*/
public CompositeHealthIndicator() {
this.indicators = new LinkedHashMap<String, HealthIndicator<?>>();
}
/**
* Create a new {@link CompositeHealthIndicator} from the specified indicators.
* @param indicators a map of {@link HealthIndicator}s with the key being used as an
* indicator name.
*/
public CompositeHealthIndicator(Map<String, HealthIndicator<?>> indicators) {
this.indicators = new LinkedHashMap<String, HealthIndicator<?>>(indicators);
}
public void addHealthIndicator(String name, HealthIndicator<?> indicator) {
this.indicators.put(name, indicator);
}
@Override
public Map<String, Object> health() {
Map<String, Object> health = new LinkedHashMap<String, Object>();
for (Map.Entry<String, HealthIndicator<?>> entry : this.indicators.entrySet()) {
health.put(entry.getKey(), entry.getValue().health());
}
return health;
}
}

View File

@ -0,0 +1,94 @@
/*
* Copyright 2012-2014 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.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
/**
* Tests for {@link CompositeHealthIndicator}
*
* @author Tyler J. Frederick
* @author Phillip Webb
*/
public class CompositeHealthIndicatorTests {
@Mock
private HealthIndicator<String> one;
@Mock
private HealthIndicator<String> two;
@Mock
private HealthIndicator<String> three;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
given(this.one.health()).willReturn("1");
given(this.two.health()).willReturn("2");
given(this.three.health()).willReturn("3");
}
@Test
public void createWithIndicators() throws Exception {
Map<String, HealthIndicator<?>> indicators = new HashMap<String, HealthIndicator<?>>();
indicators.put("one", this.one);
indicators.put("two", this.two);
CompositeHealthIndicator composite = new CompositeHealthIndicator(indicators);
Map<String, Object> result = composite.health();
assertThat(result.size(), equalTo(2));
assertThat(result, hasEntry("one", (Object) "1"));
assertThat(result, hasEntry("two", (Object) "2"));
}
@Test
public void createWithIndicatorsAndAdd() throws Exception {
Map<String, HealthIndicator<?>> indicators = new HashMap<String, HealthIndicator<?>>();
indicators.put("one", this.one);
indicators.put("two", this.two);
CompositeHealthIndicator composite = new CompositeHealthIndicator(indicators);
composite.addHealthIndicator("three", this.three);
Map<String, Object> result = composite.health();
assertThat(result.size(), equalTo(3));
assertThat(result, hasEntry("one", (Object) "1"));
assertThat(result, hasEntry("two", (Object) "2"));
assertThat(result, hasEntry("three", (Object) "3"));
}
@Test
public void createWithoutAndAdd() throws Exception {
CompositeHealthIndicator composite = new CompositeHealthIndicator();
composite.addHealthIndicator("one", this.one);
composite.addHealthIndicator("two", this.two);
Map<String, Object> result = composite.health();
assertThat(result.size(), equalTo(2));
assertThat(result, hasEntry("one", (Object) "1"));
assertThat(result, hasEntry("two", (Object) "2"));
}
}