Browse Source
Deprecate `DiscoveryCompositeHealthIndicator` and add a replacement `DiscoveryCompositeHealthContributor` class following upstream Spring Boot deprecations.pull/592/head
Phillip Webb
5 years ago
7 changed files with 223 additions and 141 deletions
@ -0,0 +1,80 @@
@@ -0,0 +1,80 @@
|
||||
/* |
||||
* Copyright 2012-2019 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 |
||||
* |
||||
* https://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.cloud.client.discovery.health; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.Iterator; |
||||
import java.util.Map; |
||||
import java.util.function.Function; |
||||
import java.util.stream.Collectors; |
||||
|
||||
import org.springframework.boot.actuate.health.CompositeHealthContributor; |
||||
import org.springframework.boot.actuate.health.HealthContributor; |
||||
import org.springframework.boot.actuate.health.HealthIndicator; |
||||
import org.springframework.boot.actuate.health.NamedContributor; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Adapter that converts a collection of {@link DiscoveryHealthIndicator} beans into a |
||||
* {@link CompositeHealthContributor}. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 2.2.0 |
||||
*/ |
||||
public class DiscoveryCompositeHealthContributor implements CompositeHealthContributor { |
||||
|
||||
private Map<String, DiscoveryHealthIndicator> indicators; |
||||
|
||||
public DiscoveryCompositeHealthContributor( |
||||
Collection<DiscoveryHealthIndicator> indicators) { |
||||
Assert.notNull(indicators, "'indicators' must not be null"); |
||||
this.indicators = indicators.stream().collect( |
||||
Collectors.toMap(DiscoveryHealthIndicator::getName, Function.identity())); |
||||
} |
||||
|
||||
@Override |
||||
public HealthContributor getContributor(String name) { |
||||
return asHealthIndicator(this.indicators.get(name)); |
||||
} |
||||
|
||||
@Override |
||||
public Iterator<NamedContributor<HealthContributor>> iterator() { |
||||
return this.indicators.values().stream().map(this::asNamedContributor).iterator(); |
||||
} |
||||
|
||||
private NamedContributor<HealthContributor> asNamedContributor( |
||||
DiscoveryHealthIndicator indicator) { |
||||
return new NamedContributor<HealthContributor>() { |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return indicator.getName(); |
||||
} |
||||
|
||||
@Override |
||||
public HealthIndicator getContributor() { |
||||
return asHealthIndicator(indicator); |
||||
} |
||||
|
||||
}; |
||||
} |
||||
|
||||
private HealthIndicator asHealthIndicator(DiscoveryHealthIndicator indicator) { |
||||
return (indicator != null) ? () -> indicator.health() : null; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,111 @@
@@ -0,0 +1,111 @@
|
||||
/* |
||||
* Copyright 2012-2019 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 |
||||
* |
||||
* https://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.cloud.client.discovery.health; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.actuate.health.Health; |
||||
import org.springframework.boot.actuate.health.HealthContributor; |
||||
import org.springframework.boot.actuate.health.HealthIndicator; |
||||
import org.springframework.boot.actuate.health.NamedContributor; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
||||
|
||||
/** |
||||
* Tests for {@link DiscoveryCompositeHealthContributor}. |
||||
* |
||||
* @author Phillip Webb |
||||
*/ |
||||
public class DiscoveryCompositeHealthContributorTests { |
||||
|
||||
@Test |
||||
public void createWhenIndicatorsAreNullThrowsException() throws Exception { |
||||
assertThatIllegalArgumentException() |
||||
.isThrownBy(() -> new DiscoveryCompositeHealthContributor(null)) |
||||
.withMessage("'indicators' must not be null"); |
||||
} |
||||
|
||||
@Test |
||||
public void getContributorReturnsContributor() throws Exception { |
||||
TestDiscoveryHealthIndicator indicator = new TestDiscoveryHealthIndicator("test", |
||||
Health.up().build()); |
||||
DiscoveryCompositeHealthContributor composite = new DiscoveryCompositeHealthContributor( |
||||
Arrays.asList(indicator)); |
||||
HealthIndicator adapted = (HealthIndicator) composite.getContributor("test"); |
||||
assertThat(adapted).isNotNull(); |
||||
assertThat(adapted.health()).isSameAs(indicator.health()); |
||||
} |
||||
|
||||
@Test |
||||
public void getContributorWhenMissingReturnsNull() throws Exception { |
||||
TestDiscoveryHealthIndicator indicator = new TestDiscoveryHealthIndicator("test", |
||||
Health.up().build()); |
||||
DiscoveryCompositeHealthContributor composite = new DiscoveryCompositeHealthContributor( |
||||
Arrays.asList(indicator)); |
||||
assertThat((HealthIndicator) composite.getContributor("missing")).isNull(); |
||||
} |
||||
|
||||
@Test |
||||
public void iteratorIteratesNamedContributors() throws Exception { |
||||
TestDiscoveryHealthIndicator indicator1 = new TestDiscoveryHealthIndicator( |
||||
"test1", Health.up().build()); |
||||
TestDiscoveryHealthIndicator indicator2 = new TestDiscoveryHealthIndicator( |
||||
"test2", Health.down().build()); |
||||
DiscoveryCompositeHealthContributor composite = new DiscoveryCompositeHealthContributor( |
||||
Arrays.asList(indicator1, indicator2)); |
||||
List<NamedContributor<HealthContributor>> contributors = new ArrayList<>(); |
||||
for (NamedContributor<HealthContributor> contributor : composite) { |
||||
contributors.add(contributor); |
||||
} |
||||
assertThat(contributors).hasSize(2); |
||||
assertThat(contributors).extracting("name").containsExactlyInAnyOrder("test1", |
||||
"test2"); |
||||
assertThat(contributors).extracting("contributor").extracting("health") |
||||
.containsExactlyInAnyOrder(indicator1.health(), indicator2.health()); |
||||
} |
||||
|
||||
private static class TestDiscoveryHealthIndicator |
||||
implements DiscoveryHealthIndicator { |
||||
|
||||
private final String name; |
||||
|
||||
private final Health health; |
||||
|
||||
TestDiscoveryHealthIndicator(String name, Health health) { |
||||
super(); |
||||
this.name = name; |
||||
this.health = health; |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return this.name; |
||||
} |
||||
|
||||
@Override |
||||
public Health health() { |
||||
return this.health; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -1,110 +0,0 @@
@@ -1,110 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2019 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 |
||||
* |
||||
* https://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.cloud.client.discovery.health; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.actuate.health.Health; |
||||
import org.springframework.boot.actuate.health.HealthAggregator; |
||||
import org.springframework.boot.actuate.health.OrderedHealthAggregator; |
||||
import org.springframework.boot.actuate.health.Status; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.cloud.client.CommonsClientAutoConfiguration; |
||||
import org.springframework.cloud.client.discovery.DiscoveryClient; |
||||
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
||||
import static org.assertj.core.api.BDDAssertions.then; |
||||
import static org.mockito.BDDMockito.given; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
@RunWith(SpringRunner.class) |
||||
@SpringBootTest(classes = { DiscoveryCompositeHealthIndicatorTests.Config.class, |
||||
CommonsClientAutoConfiguration.class }) |
||||
public class DiscoveryCompositeHealthIndicatorTests { |
||||
|
||||
@Autowired |
||||
private DiscoveryCompositeHealthIndicator healthIndicator; |
||||
|
||||
@Autowired |
||||
private DiscoveryClientHealthIndicator clientHealthIndicator; |
||||
|
||||
@Test |
||||
public void testHealthIndicator() { |
||||
then(this.healthIndicator).as("healthIndicator was null").isNotNull(); |
||||
Health health = this.healthIndicator.health(); |
||||
assertHealth(health, Status.UNKNOWN); |
||||
|
||||
this.clientHealthIndicator |
||||
.onApplicationEvent(new InstanceRegisteredEvent<>(this, null)); |
||||
|
||||
health = this.healthIndicator.health(); |
||||
Status status = assertHealth(health, Status.UP); |
||||
then("").isEqualTo(status.getDescription()).as("status description was wrong"); |
||||
} |
||||
|
||||
protected Status assertHealth(Health health, Status expected) { |
||||
then(health).as("health was null").isNotNull(); |
||||
Status status = health.getStatus(); |
||||
then(status).as("status was null").isNotNull(); |
||||
then(expected.getCode()).isEqualTo(status.getCode()).as("status code was wrong"); |
||||
return status; |
||||
} |
||||
|
||||
@Configuration |
||||
public static class Config { |
||||
|
||||
@Bean |
||||
public HealthAggregator healthAggregator() { |
||||
return new OrderedHealthAggregator(); |
||||
} |
||||
|
||||
@Bean |
||||
public DiscoveryClient discoveryClient() { |
||||
DiscoveryClient mock = mock(DiscoveryClient.class); |
||||
given(mock.description()).willReturn("TestDiscoveryClient"); |
||||
given(mock.getServices()).willReturn(Arrays.asList("TestService1")); |
||||
return mock; |
||||
} |
||||
|
||||
@Bean |
||||
public DiscoveryHealthIndicator discoveryHealthIndicator() { |
||||
return new DiscoveryHealthIndicator() { |
||||
@Override |
||||
public String getName() { |
||||
return "testDiscoveryHealthIndicator"; |
||||
} |
||||
|
||||
@Override |
||||
public Health health() { |
||||
return new Health.Builder().unknown().build(); |
||||
} |
||||
}; |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue