Skip to content

Commit

Permalink
Improve health contributor null support
Browse files Browse the repository at this point in the history
Update `NamedContributorsMapAdapter` to check for `null` keys or values
during construction. Also update `HealthEndpointSupport` to allow
null component entries.

See gh-18687
  • Loading branch information
philwebb committed Oct 23, 2019
1 parent 7c9ac03 commit ba30ee0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Expand Up @@ -134,15 +134,19 @@ protected abstract T aggregateContributions(ApiVersion apiVersion, Map<String, 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);
}
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.
*
Expand Down
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -48,6 +48,22 @@ void createWhenValueAdapterIsNullThrowsException() {
.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();
Expand Down

0 comments on commit ba30ee0

Please sign in to comment.