Skip to content

Commit

Permalink
Fix 404 when composite contributor is added to a group
Browse files Browse the repository at this point in the history
Fixes gh-19974
  • Loading branch information
mbhave committed Feb 11, 2020
1 parent 76c2157 commit d485708
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -81,7 +81,7 @@ private HealthResult<T> getHealth(ApiVersion apiVersion, HealthEndpointGroup gro
}
Object contributor = getContributor(path, pathOffset);
T health = getContribution(apiVersion, group, contributor, showComponents, showDetails,
isSystemHealth ? this.groups.getNames() : null);
isSystemHealth ? this.groups.getNames() : null, false);
return (health != null) ? new HealthResult<>(health, group) : null;
}

Expand All @@ -100,23 +100,24 @@ private Object getContributor(String[] path, int pathOffset) {

@SuppressWarnings("unchecked")
private T getContribution(ApiVersion apiVersion, HealthEndpointGroup group, Object contributor,
boolean showComponents, boolean showDetails, Set<String> groupNames) {
boolean showComponents, boolean showDetails, Set<String> groupNames, boolean isNested) {
if (contributor instanceof NamedContributors) {
return getAggregateHealth(apiVersion, group, (NamedContributors<C>) contributor, showComponents,
showDetails, groupNames);
showDetails, groupNames, isNested);
}
return (contributor != null) ? getHealth((C) contributor, showDetails) : null;
}

private T getAggregateHealth(ApiVersion apiVersion, HealthEndpointGroup group,
NamedContributors<C> namedContributors, boolean showComponents, boolean showDetails,
Set<String> groupNames) {
NamedContributors<C> namedContributors, boolean showComponents, boolean showDetails, Set<String> groupNames,
boolean isNested) {
Map<String, T> contributions = new LinkedHashMap<>();
for (NamedContributor<C> namedContributor : namedContributors) {
String name = namedContributor.getName();
C contributor = namedContributor.getContributor();
if (group.isMember(name)) {
T contribution = getContribution(apiVersion, group, contributor, showComponents, showDetails, null);
if (group.isMember(name) || isNested) {
T contribution = getContribution(apiVersion, group, contributor, showComponents, showDetails, null,
true);
if (contribution != null) {
contributions.put(name, contribution);
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -38,6 +38,7 @@
* @param <C> the contributor type
* @param <T> the contributed health component type
* @author Phillip Webb
* @author Madhura Bhave
*/
abstract class HealthEndpointSupportTests<R extends ContributorRegistry<C>, C, T> {

Expand Down Expand Up @@ -215,6 +216,20 @@ void getHealthWithEmptyCompositeReturnsNullResult() { // gh-18687
assertThat(result).isNull();
}

@Test
void getHealthWhenGroupContainsCompositeContributorReturnsHealth() {
C contributor = createContributor(this.up);
C compositeContributor = createCompositeContributor(Collections.singletonMap("spring", contributor));
this.registry.registerContributor("test", compositeContributor);
TestHealthEndpointGroup testGroup = new TestHealthEndpointGroup((name) -> name.startsWith("test"));
HealthEndpointGroups groups = HealthEndpointGroups.of(this.primaryGroup,
Collections.singletonMap("testGroup", testGroup));
HealthResult<T> result = create(this.registry, groups).getHealth(ApiVersion.V3, SecurityContext.NONE, false,
"testGroup");
CompositeHealth health = (CompositeHealth) getHealth(result);
assertThat(health.getComponents()).containsKey("test");
}

protected abstract HealthEndpointSupport<C, T> create(R registry, HealthEndpointGroups groups);

protected abstract R createRegistry();
Expand Down

0 comments on commit d485708

Please sign in to comment.