Commit d7a472b8 authored by Phillip Webb's avatar Phillip Webb

Restore HealthIndicatorRegistry beans

Restore `HealthIndicatorRegistry` and `ReactiveHealthIndicatorRegistry`
auto-configured beans with a version that adapts to the new contributor
interfaces.

Closes gh-16903
parent 5076d856
/*
* 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.boot.actuate.autoconfigure.health;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.actuate.health.HealthContributorRegistry;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.actuate.health.NamedContributor;
import org.springframework.util.Assert;
/**
* Adapter class to convert a {@link HealthContributorRegistry} to a legacy
* {@link HealthIndicatorRegistry}.
*
* @author Phillip Webb
*/
@SuppressWarnings("deprecation")
class HealthContributorRegistryHealthIndicatorRegistryAdapter implements HealthIndicatorRegistry {
private final HealthContributorRegistry contributorRegistry;
HealthContributorRegistryHealthIndicatorRegistryAdapter(HealthContributorRegistry contributorRegistry) {
Assert.notNull(contributorRegistry, "ContributorRegistry must not be null");
this.contributorRegistry = contributorRegistry;
}
@Override
public void register(String name, HealthIndicator healthIndicator) {
this.contributorRegistry.registerContributor(name, healthIndicator);
}
@Override
public HealthIndicator unregister(String name) {
HealthContributor contributor = this.contributorRegistry.unregisterContributor(name);
if (contributor instanceof HealthIndicator) {
return (HealthIndicator) contributor;
}
return null;
}
@Override
public HealthIndicator get(String name) {
HealthContributor contributor = this.contributorRegistry.getContributor(name);
if (contributor instanceof HealthIndicator) {
return (HealthIndicator) contributor;
}
return null;
}
@Override
public Map<String, HealthIndicator> getAll() {
Map<String, HealthIndicator> all = new LinkedHashMap<String, HealthIndicator>();
for (NamedContributor<?> namedContributor : this.contributorRegistry) {
if (namedContributor.getContributor() instanceof HealthIndicator) {
all.put(namedContributor.getName(), (HealthIndicator) namedContributor.getContributor());
}
}
return all;
}
}
...@@ -20,6 +20,7 @@ import org.springframework.boot.actuate.autoconfigure.endpoint.condition.Conditi ...@@ -20,6 +20,7 @@ import org.springframework.boot.actuate.autoconfigure.endpoint.condition.Conditi
import org.springframework.boot.actuate.health.HealthEndpoint; import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
...@@ -32,11 +33,20 @@ import org.springframework.context.annotation.Import; ...@@ -32,11 +33,20 @@ import org.springframework.context.annotation.Import;
* @since 2.0.0 * @since 2.0.0
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(HealthEndpointProperties.class)
@ConditionalOnAvailableEndpoint(endpoint = HealthEndpoint.class) @ConditionalOnAvailableEndpoint(endpoint = HealthEndpoint.class)
@EnableConfigurationProperties
@Import({ LegacyHealthEndpointAdaptersConfiguration.class, LegacyHealthEndpointCompatibilityConfiguration.class, @Import({ LegacyHealthEndpointAdaptersConfiguration.class, LegacyHealthEndpointCompatibilityConfiguration.class,
HealthEndpointConfiguration.class, ReactiveHealthEndpointConfiguration.class, HealthEndpointConfiguration.class, ReactiveHealthEndpointConfiguration.class,
HealthEndpointWebExtensionConfiguration.class, HealthEndpointReactiveWebExtensionConfiguration.class }) HealthEndpointWebExtensionConfiguration.class, HealthEndpointReactiveWebExtensionConfiguration.class })
public class HealthEndpointAutoConfiguration { public class HealthEndpointAutoConfiguration {
@Bean
@SuppressWarnings("deprecation")
HealthEndpointProperties healthEndpointProperties(HealthIndicatorProperties healthIndicatorProperties) {
HealthEndpointProperties healthEndpointProperties = new HealthEndpointProperties();
healthEndpointProperties.getStatus().getOrder().addAll(healthIndicatorProperties.getOrder());
healthEndpointProperties.getStatus().getHttpMapping().putAll(healthIndicatorProperties.getHttpMapping());
return healthEndpointProperties;
}
} }
...@@ -16,9 +16,12 @@ ...@@ -16,9 +16,12 @@
package org.springframework.boot.actuate.autoconfigure.health; package org.springframework.boot.actuate.autoconfigure.health;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty; import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
/** /**
...@@ -29,26 +32,25 @@ import org.springframework.boot.context.properties.DeprecatedConfigurationProper ...@@ -29,26 +32,25 @@ import org.springframework.boot.context.properties.DeprecatedConfigurationProper
* @deprecated since 2.2.0 in favor of {@link HealthEndpointProperties} * @deprecated since 2.2.0 in favor of {@link HealthEndpointProperties}
*/ */
@Deprecated @Deprecated
@ConfigurationProperties(prefix = "management.health.status")
public class HealthIndicatorProperties { public class HealthIndicatorProperties {
private final HealthEndpointProperties healthEndpointProperties; private List<String> order = new ArrayList<>();
HealthIndicatorProperties(HealthEndpointProperties healthEndpointProperties) { private final Map<String, Integer> httpMapping = new LinkedHashMap<String, Integer>();
this.healthEndpointProperties = healthEndpointProperties;
}
@DeprecatedConfigurationProperty(replacement = "management.endpoint.health.status.order") @DeprecatedConfigurationProperty(replacement = "management.endpoint.health.status.order")
public List<String> getOrder() { public List<String> getOrder() {
return this.healthEndpointProperties.getStatus().getOrder(); return this.order;
} }
public void setOrder(List<String> statusOrder) { public void setOrder(List<String> order) {
this.healthEndpointProperties.getStatus().setOrder(statusOrder); this.order = order;
} }
@DeprecatedConfigurationProperty(replacement = "management.endpoint.health.status.http-mapping") @DeprecatedConfigurationProperty(replacement = "management.endpoint.health.status.http-mapping")
public Map<String, Integer> getHttpMapping() { public Map<String, Integer> getHttpMapping() {
return this.healthEndpointProperties.getStatus().getHttpMapping(); return this.httpMapping;
} }
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.actuate.autoconfigure.health; package org.springframework.boot.actuate.autoconfigure.health;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
...@@ -76,7 +77,7 @@ public abstract class HealthProperties { ...@@ -76,7 +77,7 @@ public abstract class HealthProperties {
/** /**
* Comma-separated list of health statuses in order of severity. * Comma-separated list of health statuses in order of severity.
*/ */
private List<String> order = null; private List<String> order = new ArrayList<>();
/** /**
* Mapping of health statuses to HTTP status codes. By default, registered health * Mapping of health statuses to HTTP status codes. By default, registered health
......
...@@ -16,11 +16,18 @@ ...@@ -16,11 +16,18 @@
package org.springframework.boot.actuate.autoconfigure.health; package org.springframework.boot.actuate.autoconfigure.health;
import io.micrometer.shaded.reactor.core.publisher.Mono;
import org.springframework.boot.actuate.health.HealthAggregator; import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthContributorRegistry;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.actuate.health.HealthStatusHttpMapper; import org.springframework.boot.actuate.health.HealthStatusHttpMapper;
import org.springframework.boot.actuate.health.OrderedHealthAggregator; import org.springframework.boot.actuate.health.OrderedHealthAggregator;
import org.springframework.boot.actuate.health.ReactiveHealthContributorRegistry;
import org.springframework.boot.actuate.health.ReactiveHealthIndicatorRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -32,12 +39,12 @@ import org.springframework.context.annotation.Configuration; ...@@ -32,12 +39,12 @@ import org.springframework.context.annotation.Configuration;
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@EnableConfigurationProperties
class LegacyHealthEndpointCompatibilityConfiguration { class LegacyHealthEndpointCompatibilityConfiguration {
@Bean @Bean
@ConfigurationProperties(prefix = "management.health.status") HealthIndicatorProperties healthIndicatorProperties() {
HealthIndicatorProperties healthIndicatorProperties(HealthEndpointProperties healthEndpointProperties) { return new HealthIndicatorProperties();
return new HealthIndicatorProperties(healthEndpointProperties);
} }
@Bean @Bean
...@@ -60,4 +67,25 @@ class LegacyHealthEndpointCompatibilityConfiguration { ...@@ -60,4 +67,25 @@ class LegacyHealthEndpointCompatibilityConfiguration {
return mapper; return mapper;
} }
@Bean
@ConditionalOnMissingBean(HealthIndicatorRegistry.class)
HealthContributorRegistryHealthIndicatorRegistryAdapter healthIndicatorRegistry(
HealthContributorRegistry healthContributorRegistry) {
return new HealthContributorRegistryHealthIndicatorRegistryAdapter(healthContributorRegistry);
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Mono.class)
static class LegacyReactiveHealthEndpointCompatibilityConfiguration {
@Bean
@ConditionalOnMissingBean(ReactiveHealthIndicatorRegistry.class)
ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter reactiveHealthIndicatorRegistry(
ReactiveHealthContributorRegistry reactiveHealthContributorRegistry) {
return new ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter(
reactiveHealthContributorRegistry);
}
}
} }
/*
* 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.boot.actuate.autoconfigure.health;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.boot.actuate.health.HealthContributorRegistry;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.actuate.health.NamedContributor;
import org.springframework.boot.actuate.health.ReactiveHealthContributor;
import org.springframework.boot.actuate.health.ReactiveHealthContributorRegistry;
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
import org.springframework.boot.actuate.health.ReactiveHealthIndicatorRegistry;
import org.springframework.util.Assert;
/**
* Adapter class to convert a {@link HealthContributorRegistry} to a legacy
* {@link HealthIndicatorRegistry}.
*
* @author Phillip Webb
*/
@SuppressWarnings("deprecation")
class ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter
implements ReactiveHealthIndicatorRegistry {
private final ReactiveHealthContributorRegistry contributorRegistry;
ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter(
ReactiveHealthContributorRegistry contributorRegistry) {
Assert.notNull(contributorRegistry, "ContributorRegistry must not be null");
this.contributorRegistry = contributorRegistry;
}
@Override
public void register(String name, ReactiveHealthIndicator healthIndicator) {
this.contributorRegistry.registerContributor(name, healthIndicator);
}
@Override
public ReactiveHealthIndicator unregister(String name) {
ReactiveHealthContributor contributor = this.contributorRegistry.unregisterContributor(name);
if (contributor instanceof ReactiveHealthIndicator) {
return (ReactiveHealthIndicator) contributor;
}
return null;
}
@Override
public ReactiveHealthIndicator get(String name) {
ReactiveHealthContributor contributor = this.contributorRegistry.getContributor(name);
if (contributor instanceof ReactiveHealthIndicator) {
return (ReactiveHealthIndicator) contributor;
}
return null;
}
@Override
public Map<String, ReactiveHealthIndicator> getAll() {
Map<String, ReactiveHealthIndicator> all = new LinkedHashMap<>();
for (NamedContributor<?> namedContributor : this.contributorRegistry) {
if (namedContributor.getContributor() instanceof ReactiveHealthIndicator) {
all.put(namedContributor.getName(), (ReactiveHealthIndicator) namedContributor.getContributor());
}
}
return all;
}
}
/*
* 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.boot.actuate.autoconfigure.health;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.CompositeHealthContributor;
import org.springframework.boot.actuate.health.DefaultHealthContributorRegistry;
import org.springframework.boot.actuate.health.HealthContributorRegistry;
import org.springframework.boot.actuate.health.HealthIndicator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link HealthContributorRegistryHealthIndicatorRegistryAdapter}.
*
* @author Phillip Webb
*/
class HealthContributorRegistryHealthIndicatorRegistryAdapterTests {
private HealthContributorRegistry contributorRegistry = new DefaultHealthContributorRegistry();
private HealthContributorRegistryHealthIndicatorRegistryAdapter adapter = new HealthContributorRegistryHealthIndicatorRegistryAdapter(
this.contributorRegistry);
@Test
void createWhenContributorRegistryIsNullThrowsException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new HealthContributorRegistryHealthIndicatorRegistryAdapter(null))
.withMessage("ContributorRegistry must not be null");
}
@Test
void registerDelegatesToContributorRegistry() {
HealthIndicator healthIndicator = mock(HealthIndicator.class);
this.adapter.register("test", healthIndicator);
assertThat(this.contributorRegistry.getContributor("test")).isSameAs(healthIndicator);
}
@Test
void unregisterWhenDelegatesToContributorRegistry() {
HealthIndicator healthIndicator = mock(HealthIndicator.class);
this.contributorRegistry.registerContributor("test", healthIndicator);
HealthIndicator unregistered = this.adapter.unregister("test");
assertThat(unregistered).isSameAs(healthIndicator);
assertThat(this.contributorRegistry.getContributor("test")).isNull();
}
@Test
void unregisterWhenContributorRegistryResultIsNotHealthIndicatorReturnsNull() {
CompositeHealthContributor healthContributor = mock(CompositeHealthContributor.class);
this.contributorRegistry.registerContributor("test", healthContributor);
HealthIndicator unregistered = this.adapter.unregister("test");
assertThat(unregistered).isNull();
assertThat(this.contributorRegistry.getContributor("test")).isNull();
}
@Test
void getDelegatesToContributorRegistry() {
HealthIndicator healthIndicator = mock(HealthIndicator.class);
this.contributorRegistry.registerContributor("test", healthIndicator);
assertThat(this.adapter.get("test")).isSameAs(healthIndicator);
}
@Test
void getWhenContributorRegistryResultIsNotHealthIndicatorReturnsNull() {
CompositeHealthContributor healthContributor = mock(CompositeHealthContributor.class);
this.contributorRegistry.registerContributor("test", healthContributor);
assertThat(this.adapter.get("test")).isNull();
}
@Test
void getAllDelegatesContributorRegistry() {
HealthIndicator healthIndicator = mock(HealthIndicator.class);
this.contributorRegistry.registerContributor("test", healthIndicator);
Map<String, HealthIndicator> all = this.adapter.getAll();
assertThat(all).containsOnly(entry("test", healthIndicator));
}
@Test
void getAllWhenContributorRegistryContainsNonHealthIndicatorInstancesReturnsFilteredMap() {
CompositeHealthContributor healthContributor = mock(CompositeHealthContributor.class);
this.contributorRegistry.registerContributor("test1", healthContributor);
HealthIndicator healthIndicator = mock(HealthIndicator.class);
this.contributorRegistry.registerContributor("test2", healthIndicator);
Map<String, HealthIndicator> all = this.adapter.getAll();
assertThat(all).containsOnly(entry("test2", healthIndicator));
}
}
/*
* 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.boot.actuate.autoconfigure.health;
import java.util.List;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test to ensure that the legacy {@link HealthIndicatorRegistry} can still be
* injected.
*
* @author Phillip Webb
*/
@SuppressWarnings("deprecation")
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
public class HealthIndicatorRegistryInjectionIntegrationTests {
// gh-18194
@Test
void meterRegistryBeanHasBeenConfigured(@Autowired MeterRegistry meterRegistry) {
assertThat(meterRegistry).isNotNull();
assertThat(meterRegistry.get("health").gauge()).isNotNull();
}
@Configuration
@ImportAutoConfiguration({ HealthEndpointAutoConfiguration.class, HealthContributorAutoConfiguration.class,
CompositeMeterRegistryAutoConfiguration.class, MetricsAutoConfiguration.class })
static class Config {
Config(HealthAggregator healthAggregator, HealthIndicatorRegistry healthIndicatorRegistry,
List<HealthIndicator> healthIndicators, MeterRegistry registry) {
CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator(healthAggregator,
healthIndicatorRegistry);
for (int i = 0; i < healthIndicators.size(); i++) {
healthIndicatorRegistry.register(Integer.toString(i), healthIndicators.get(i));
}
Gauge.builder("health", healthIndicator, this::getGuageValue)
.description("Spring boot health indicator. 3=UP, 2=OUT_OF_SERVICE, 1=DOWN, 0=UNKNOWN")
.strongReference(true).register(registry);
}
private double getGuageValue(CompositeHealthIndicator health) {
Status status = health.health().getStatus();
switch (status.getCode()) {
case "UP":
return 3;
case "OUT_OF_SERVICE":
return 2;
case "DOWN":
return 1;
case "UNKNOWN":
default:
return 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.boot.actuate.autoconfigure.health;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.CompositeReactiveHealthContributor;
import org.springframework.boot.actuate.health.DefaultReactiveHealthContributorRegistry;
import org.springframework.boot.actuate.health.ReactiveHealthContributorRegistry;
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.Mockito.mock;
/**
* Test for
* {@link ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter}.
*
* @author Phillip Webb
*/
class ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapterTests {
private ReactiveHealthContributorRegistry contributorRegistry = new DefaultReactiveHealthContributorRegistry();
private ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter adapter = new ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter(
this.contributorRegistry);
@Test
void createWhenContributorRegistryIsNullThrowsException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter(null))
.withMessage("ContributorRegistry must not be null");
}
@Test
void registerDelegatesToContributorRegistry() {
ReactiveHealthIndicator healthIndicator = mock(ReactiveHealthIndicator.class);
this.adapter.register("test", healthIndicator);
assertThat(this.contributorRegistry.getContributor("test")).isSameAs(healthIndicator);
}
@Test
void unregisterWhenDelegatesToContributorRegistry() {
ReactiveHealthIndicator healthIndicator = mock(ReactiveHealthIndicator.class);
this.contributorRegistry.registerContributor("test", healthIndicator);
ReactiveHealthIndicator unregistered = this.adapter.unregister("test");
assertThat(unregistered).isSameAs(healthIndicator);
assertThat(this.contributorRegistry.getContributor("test")).isNull();
}
@Test
void unregisterWhenContributorRegistryResultIsNotHealthIndicatorReturnsNull() {
CompositeReactiveHealthContributor healthContributor = mock(CompositeReactiveHealthContributor.class);
this.contributorRegistry.registerContributor("test", healthContributor);
ReactiveHealthIndicator unregistered = this.adapter.unregister("test");
assertThat(unregistered).isNull();
assertThat(this.contributorRegistry.getContributor("test")).isNull();
}
@Test
void getDelegatesToContributorRegistry() {
ReactiveHealthIndicator healthIndicator = mock(ReactiveHealthIndicator.class);
this.contributorRegistry.registerContributor("test", healthIndicator);
assertThat(this.adapter.get("test")).isSameAs(healthIndicator);
}
@Test
void getWhenContributorRegistryResultIsNotHealthIndicatorReturnsNull() {
CompositeReactiveHealthContributor healthContributor = mock(CompositeReactiveHealthContributor.class);
this.contributorRegistry.registerContributor("test", healthContributor);
assertThat(this.adapter.get("test")).isNull();
}
@Test
void getAllDelegatesContributorRegistry() {
ReactiveHealthIndicator healthIndicator = mock(ReactiveHealthIndicator.class);
this.contributorRegistry.registerContributor("test", healthIndicator);
Map<String, ReactiveHealthIndicator> all = this.adapter.getAll();
assertThat(all).containsOnly(entry("test", healthIndicator));
}
@Test
void getAllWhenContributorRegistryContainsNonHealthIndicatorInstancesReturnsFilteredMap() {
CompositeReactiveHealthContributor healthContributor = mock(CompositeReactiveHealthContributor.class);
this.contributorRegistry.registerContributor("test1", healthContributor);
ReactiveHealthIndicator healthIndicator = mock(ReactiveHealthIndicator.class);
this.contributorRegistry.registerContributor("test2", healthIndicator);
Map<String, ReactiveHealthIndicator> all = this.adapter.getAll();
assertThat(all).containsOnly(entry("test2", healthIndicator));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment