Improve health contributor null support

Update `NamedContributorsMapAdapter` to check for `null` keys or values
during construction. Also update `HealthEndpointSupport` to allow
null component entries.

See gh-18687
pull/18727/head
Phillip Webb 5 years ago
parent 7c9ac03014
commit ba30ee03df

@ -134,8 +134,8 @@ abstract class HealthEndpointSupport<C, T> {
protected final CompositeHealth getCompositeHealth(ApiVersion apiVersion, Map<String, HealthComponent> components,
StatusAggregator statusAggregator, boolean showComponents, Set<String> groupNames) {
Status status = statusAggregator.getAggregateStatus(
components.values().stream().map(HealthComponent::getStatus).collect(Collectors.toSet()));
Status status = statusAggregator
.getAggregateStatus(components.values().stream().map(this::getStatus).collect(Collectors.toSet()));
Map<String, HealthComponent> instances = showComponents ? components : null;
if (groupNames != null) {
return new SystemHealth(apiVersion, status, instances, groupNames);
@ -143,6 +143,10 @@ abstract class HealthEndpointSupport<C, T> {
return new CompositeHealth(apiVersion, status, instances);
}
private Status getStatus(HealthComponent component) {
return (component != null) ? component.getStatus() : Status.UNKNOWN;
}
/**
* A health result containing health and the group that created it.
*

@ -43,6 +43,9 @@ abstract class NamedContributorsMapAdapter<V, C> implements NamedContributors<C>
NamedContributorsMapAdapter(Map<String, V> map, Function<V, ? extends C> valueAdapter) {
Assert.notNull(map, "Map must not be null");
Assert.notNull(valueAdapter, "ValueAdapter must not be null");
map.keySet().stream().forEach((key) -> Assert.notNull(key, "Map must not contain null keys"));
map.values().stream().map(valueAdapter)
.forEach((value) -> Assert.notNull(value, "Map must not contain null values"));
this.map = Collections.unmodifiableMap(new LinkedHashMap<>(map));
this.valueAdapter = valueAdapter;
}

@ -48,6 +48,22 @@ class NamedContributorsMapAdapterTests {
.withMessage("ValueAdapter must not be null");
}
@Test
void createWhenMapContainsNullValueThrowsException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new TestNamedContributorsMapAdapter<>(Collections.singletonMap("test", null),
Function.identity()))
.withMessage("Map must not contain null values");
}
@Test
void createWhenMapContainsNullKeyThrowsException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new TestNamedContributorsMapAdapter<>(Collections.singletonMap(null, "test"),
Function.identity()))
.withMessage("Map must not contain null keys");
}
@Test
void iterateReturnsAdaptedEntries() {
TestNamedContributorsMapAdapter<String> adapter = createAdapter();

Loading…
Cancel
Save