Move Couchbase health auto-configuration into spring-boot-couchbase

This commit is contained in:
Andy Wilkinson
2025-05-16 14:57:27 +01:00
committed by Phillip Webb
parent 8c7de6ea79
commit 79a842ec64
10 changed files with 33 additions and 30 deletions

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2012-2025 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.couchbase.actuate.health.autoconfigure;
import com.couchbase.client.java.Cluster;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.couchbase.actuate.health.CouchbaseHealthIndicator;
import org.springframework.boot.couchbase.autoconfigure.CouchbaseAutoConfiguration;
import org.springframework.context.annotation.Bean;
/**
* {@link EnableAutoConfiguration Auto-configuration} for
* {@link CouchbaseHealthIndicator}.
*
* @author Eddú Meléndez
* @author Stephane Nicoll
* @author Andy Wilkinson Nicoll
* @since 4.0.0
*/
@AutoConfiguration(
after = { CouchbaseAutoConfiguration.class, CouchbaseReactiveHealthContributorAutoConfiguration.class })
@ConditionalOnClass({ Cluster.class, CouchbaseHealthIndicator.class, ConditionalOnEnabledHealthIndicator.class })
@ConditionalOnBean(Cluster.class)
@ConditionalOnEnabledHealthIndicator("couchbase")
public class CouchbaseHealthContributorAutoConfiguration
extends CompositeHealthContributorConfiguration<CouchbaseHealthIndicator, Cluster> {
public CouchbaseHealthContributorAutoConfiguration() {
super(CouchbaseHealthIndicator::new);
}
@Bean
@ConditionalOnMissingBean(name = { "couchbaseHealthIndicator", "couchbaseHealthContributor" })
public HealthContributor couchbaseHealthContributor(ConfigurableListableBeanFactory beanFactory) {
return createContributor(beanFactory, Cluster.class);
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2012-2025 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.couchbase.actuate.health.autoconfigure;
import com.couchbase.client.java.Cluster;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.health.ReactiveHealthContributor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.couchbase.actuate.health.CouchbaseReactiveHealthIndicator;
import org.springframework.boot.couchbase.autoconfigure.CouchbaseAutoConfiguration;
import org.springframework.context.annotation.Bean;
/**
* {@link EnableAutoConfiguration Auto-configuration} for
* {@link CouchbaseReactiveHealthIndicator}.
*
* @author Mikalai Lushchytski
* @author Stephane Nicoll
* @since 4.0.0
*/
@AutoConfiguration(after = CouchbaseAutoConfiguration.class)
@ConditionalOnClass({ Cluster.class, Flux.class, CouchbaseReactiveHealthIndicator.class,
ConditionalOnEnabledHealthIndicator.class })
@ConditionalOnBean(Cluster.class)
@ConditionalOnEnabledHealthIndicator("couchbase")
public class CouchbaseReactiveHealthContributorAutoConfiguration
extends CompositeReactiveHealthContributorConfiguration<CouchbaseReactiveHealthIndicator, Cluster> {
public CouchbaseReactiveHealthContributorAutoConfiguration() {
super(CouchbaseReactiveHealthIndicator::new);
}
@Bean
@ConditionalOnMissingBean(name = { "couchbaseHealthIndicator", "couchbaseHealthContributor" })
public ReactiveHealthContributor couchbaseHealthContributor(ConfigurableListableBeanFactory beanFactory) {
return createContributor(beanFactory, Cluster.class);
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2025 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.
*/
/**
* Auto-configuration for actuator Couchbase concerns.
*/
package org.springframework.boot.couchbase.actuate.health.autoconfigure;

View File

@@ -1,6 +1,21 @@
{
"groups": [],
"properties": [
{
"name": "management.health.couchbase.enabled",
"type": "java.lang.Boolean",
"description": "Whether to enable Couchbase health check.",
"defaultValue": true
},
{
"name": "management.health.couchbase.timeout",
"type": "java.time.Duration",
"description": "Timeout for getting the Bucket information from the server.",
"defaultValue": "1000ms",
"deprecation": {
"level": "error"
}
},
{
"name": "spring.couchbase.bootstrap-hosts",
"type": "java.util.List<java.lang.String>",

View File

@@ -1 +1,3 @@
org.springframework.boot.couchbase.actuate.health.autoconfigure.CouchbaseHealthContributorAutoConfiguration
org.springframework.boot.couchbase.actuate.health.autoconfigure.CouchbaseReactiveHealthContributorAutoConfiguration
org.springframework.boot.couchbase.autoconfigure.CouchbaseAutoConfiguration

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2012-2025 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.couchbase.actuate.health.autoconfigure;
import com.couchbase.client.java.Cluster;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.couchbase.actuate.health.CouchbaseHealthIndicator;
import org.springframework.boot.couchbase.actuate.health.CouchbaseReactiveHealthIndicator;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link CouchbaseHealthContributorAutoConfiguration}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
class CouchbaseHealthContributorAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withBean(Cluster.class, () -> mock(Cluster.class))
.withConfiguration(AutoConfigurations.of(CouchbaseHealthContributorAutoConfiguration.class,
HealthContributorAutoConfiguration.class));
@Test
void runShouldCreateIndicator() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(CouchbaseHealthIndicator.class)
.doesNotHaveBean(CouchbaseReactiveHealthIndicator.class));
}
@Test
void runWhenDisabledShouldNotCreateIndicator() {
this.contextRunner.withPropertyValues("management.health.couchbase.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean(CouchbaseHealthIndicator.class));
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2012-2025 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.couchbase.actuate.health.autoconfigure;
import com.couchbase.client.java.Cluster;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.couchbase.actuate.health.CouchbaseHealthIndicator;
import org.springframework.boot.couchbase.actuate.health.CouchbaseReactiveHealthIndicator;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link CouchbaseReactiveHealthContributorAutoConfiguration}.
*
* @author Mikalai Lushchytski
*/
class CouchbaseReactiveHealthContributorAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withBean(Cluster.class, () -> mock(Cluster.class))
.withConfiguration(AutoConfigurations.of(CouchbaseReactiveHealthContributorAutoConfiguration.class,
HealthContributorAutoConfiguration.class));
@Test
void runShouldCreateIndicator() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(CouchbaseReactiveHealthIndicator.class)
.hasBean("couchbaseHealthContributor"));
}
@Test
void runWithRegularIndicatorShouldOnlyCreateReactiveIndicator() {
this.contextRunner.withConfiguration(AutoConfigurations.of(CouchbaseHealthContributorAutoConfiguration.class))
.run((context) -> assertThat(context).hasSingleBean(CouchbaseReactiveHealthIndicator.class)
.hasBean("couchbaseHealthContributor")
.doesNotHaveBean(CouchbaseHealthIndicator.class));
}
@Test
void runWhenDisabledShouldNotCreateIndicator() {
this.contextRunner.withPropertyValues("management.health.couchbase.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean(CouchbaseReactiveHealthIndicator.class)
.doesNotHaveBean("couchbaseHealthContributor"));
}
}