Polish "Add health indicator for Cassandra that uses the CqlSession"

See gh-20887
pull/21936/head
Stephane Nicoll 4 years ago
parent dad9ec86d5
commit 35e069e2cd

@ -1,59 +0,0 @@
/*
* 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.
* 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.cassandra;
import java.util.Map;
import com.datastax.oss.driver.api.core.CqlSession;
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraDriverHealthIndicator;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* {@link EnableAutoConfiguration Auto-configuration} for
* {@link CassandraDriverHealthIndicator}.
*
* @author Alexandre Dutra
* @since 2.4.0
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(CqlSession.class)
@ConditionalOnBean(CqlSession.class)
@ConditionalOnEnabledHealthIndicator("cassandra")
@AutoConfigureAfter({ CassandraAutoConfiguration.class, CassandraReactiveHealthContributorAutoConfiguration.class,
CassandraHealthContributorAutoConfiguration.class,
CassandraDriverReactiveHealthContributorAutoConfiguration.class })
public class CassandraDriverHealthContributorAutoConfiguration
extends CompositeHealthContributorConfiguration<CassandraDriverHealthIndicator, CqlSession> {
@Bean
@ConditionalOnMissingBean(name = { "cassandraHealthIndicator", "cassandraHealthContributor" })
public HealthContributor cassandraHealthContributor(Map<String, CqlSession> sessions) {
return createContributor(sessions);
}
}

@ -1,58 +0,0 @@
/*
* 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.
* 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.cassandra;
import java.util.Map;
import com.datastax.oss.driver.api.core.CqlSession;
import reactor.core.publisher.Flux;
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraDriverReactiveHealthIndicator;
import org.springframework.boot.actuate.health.ReactiveHealthContributor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* {@link EnableAutoConfiguration Auto-configuration} for
* {@link CassandraDriverReactiveHealthIndicator}.
*
* @author Alexandre Dutra
* @since 2.4.0
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ CqlSession.class, Flux.class })
@ConditionalOnBean(CqlSession.class)
@ConditionalOnEnabledHealthIndicator("cassandra")
@AutoConfigureAfter({ CassandraAutoConfiguration.class, CassandraReactiveHealthContributorAutoConfiguration.class,
CassandraHealthContributorAutoConfiguration.class })
public class CassandraDriverReactiveHealthContributorAutoConfiguration
extends CompositeReactiveHealthContributorConfiguration<CassandraDriverReactiveHealthIndicator, CqlSession> {
@Bean
@ConditionalOnMissingBean(name = { "cassandraHealthIndicator", "cassandraHealthContributor" })
public ReactiveHealthContributor cassandraHealthContributor(Map<String, CqlSession> sessions) {
return createContributor(sessions);
}
}

@ -16,24 +16,19 @@
package org.springframework.boot.actuate.autoconfigure.cassandra;
import java.util.Map;
import com.datastax.oss.driver.api.core.CqlSession;
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.cassandra.CassandraHealthContributorConfigurations.CassandraDriverConfiguration;
import org.springframework.boot.actuate.autoconfigure.cassandra.CassandraHealthContributorConfigurations.CassandraOperationsConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraHealthIndicator;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
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.autoconfigure.data.cassandra.CassandraDataAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.context.annotation.Import;
/**
* {@link EnableAutoConfiguration Auto-configuration} for
@ -44,18 +39,11 @@ import org.springframework.data.cassandra.core.CassandraOperations;
* @since 2.1.0
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ CqlSession.class, CassandraOperations.class })
@ConditionalOnBean(CassandraOperations.class)
@ConditionalOnClass(CqlSession.class)
@ConditionalOnEnabledHealthIndicator("cassandra")
@AutoConfigureAfter({ CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
CassandraReactiveHealthContributorAutoConfiguration.class })
public class CassandraHealthContributorAutoConfiguration
extends CompositeHealthContributorConfiguration<CassandraHealthIndicator, CassandraOperations> {
@Bean
@ConditionalOnMissingBean(name = { "cassandraHealthIndicator", "cassandraHealthContributor" })
public HealthContributor cassandraHealthContributor(Map<String, CassandraOperations> cassandraOperations) {
return createContributor(cassandraOperations);
}
@Import({ CassandraOperationsConfiguration.class, CassandraDriverConfiguration.class })
public class CassandraHealthContributorAutoConfiguration {
}

@ -0,0 +1,101 @@
/*
* 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.
* 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.cassandra;
import java.util.Map;
import com.datastax.oss.driver.api.core.CqlSession;
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthContributorConfiguration;
import org.springframework.boot.actuate.cassandra.CassandraDriverHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraDriverReactiveHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraReactiveHealthIndicator;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.actuate.health.ReactiveHealthContributor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
/**
* Health contributor options for Cassandra.
*
* @author Stephane Nicoll
*/
class CassandraHealthContributorConfigurations {
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(CqlSession.class)
static class CassandraDriverConfiguration
extends CompositeHealthContributorConfiguration<CassandraDriverHealthIndicator, CqlSession> {
@Bean
@ConditionalOnMissingBean(name = { "cassandraHealthIndicator", "cassandraHealthContributor" })
HealthContributor cassandraHealthContributor(Map<String, CqlSession> sessions) {
return createContributor(sessions);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(CassandraOperations.class)
@ConditionalOnBean(CassandraOperations.class)
static class CassandraOperationsConfiguration
extends CompositeHealthContributorConfiguration<CassandraHealthIndicator, CassandraOperations> {
@Bean
@ConditionalOnMissingBean(name = { "cassandraHealthIndicator", "cassandraHealthContributor" })
HealthContributor cassandraHealthContributor(Map<String, CassandraOperations> cassandraOperations) {
return createContributor(cassandraOperations);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(CqlSession.class)
static class CassandraReactiveDriverConfiguration extends
CompositeReactiveHealthContributorConfiguration<CassandraDriverReactiveHealthIndicator, CqlSession> {
@Bean
@ConditionalOnMissingBean(name = { "cassandraHealthIndicator", "cassandraHealthContributor" })
ReactiveHealthContributor cassandraHealthContributor(Map<String, CqlSession> sessions) {
return createContributor(sessions);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(ReactiveCassandraOperations.class)
@ConditionalOnBean(ReactiveCassandraOperations.class)
static class CassandraReactiveOperationsConfiguration extends
CompositeReactiveHealthContributorConfiguration<CassandraReactiveHealthIndicator, ReactiveCassandraOperations> {
@Bean
@ConditionalOnMissingBean(name = { "cassandraHealthIndicator", "cassandraHealthContributor" })
ReactiveHealthContributor cassandraHealthContributor(
Map<String, ReactiveCassandraOperations> reactiveCassandraOperations) {
return createContributor(reactiveCassandraOperations);
}
}
}

@ -15,24 +15,19 @@
*/
package org.springframework.boot.actuate.autoconfigure.cassandra;
import java.util.Map;
import com.datastax.oss.driver.api.core.CqlSession;
import reactor.core.publisher.Flux;
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.cassandra.CassandraHealthContributorConfigurations.CassandraReactiveDriverConfiguration;
import org.springframework.boot.actuate.autoconfigure.cassandra.CassandraHealthContributorConfigurations.CassandraReactiveOperationsConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraReactiveHealthIndicator;
import org.springframework.boot.actuate.health.ReactiveHealthContributor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
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.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
import org.springframework.context.annotation.Import;
/**
* {@link EnableAutoConfiguration Auto-configuration} for
@ -43,18 +38,10 @@ import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
* @since 2.1.0
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ CqlSession.class, ReactiveCassandraOperations.class, Flux.class })
@ConditionalOnBean(ReactiveCassandraOperations.class)
@ConditionalOnClass({ CqlSession.class, Flux.class })
@ConditionalOnEnabledHealthIndicator("cassandra")
@AutoConfigureAfter(CassandraReactiveDataAutoConfiguration.class)
public class CassandraReactiveHealthContributorAutoConfiguration extends
CompositeReactiveHealthContributorConfiguration<CassandraReactiveHealthIndicator, ReactiveCassandraOperations> {
@Bean
@ConditionalOnMissingBean(name = { "cassandraHealthIndicator", "cassandraHealthContributor" })
public ReactiveHealthContributor cassandraHealthContributor(
Map<String, ReactiveCassandraOperations> reactiveCassandraOperations) {
return createContributor(reactiveCassandraOperations);
}
@Import({ CassandraReactiveOperationsConfiguration.class, CassandraReactiveDriverConfiguration.class })
public class CassandraReactiveHealthContributorAutoConfiguration {
}

@ -7,8 +7,6 @@ org.springframework.boot.actuate.autoconfigure.beans.BeansEndpointAutoConfigurat
org.springframework.boot.actuate.autoconfigure.cache.CachesEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.cassandra.CassandraHealthContributorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.cassandra.CassandraReactiveHealthContributorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.cassandra.CassandraDriverHealthContributorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.cassandra.CassandraDriverReactiveHealthContributorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet.CloudFoundryActuatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.cloudfoundry.reactive.ReactiveCloudFoundryActuatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.condition.ConditionsReportEndpointAutoConfiguration,\

@ -1,89 +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.boot.actuate.autoconfigure.cassandra;
import com.datastax.oss.driver.api.core.CqlSession;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
import org.springframework.boot.actuate.cassandra.CassandraDriverHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraDriverReactiveHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraReactiveHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link CassandraDriverHealthContributorAutoConfiguration}.
*
* @author Alexandre Dutra
* @since 2.4.0
*/
class CassandraDriverHealthContributorAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withBean(CqlSession.class, () -> mock(CqlSession.class)).withConfiguration(AutoConfigurations.of(
CassandraDriverHealthContributorAutoConfiguration.class, HealthContributorAutoConfiguration.class));
@Test
void runShouldCreateDriverIndicator() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(CassandraDriverHealthIndicator.class)
.hasBean("cassandraHealthContributor").doesNotHaveBean(CassandraHealthIndicator.class)
.doesNotHaveBean(CassandraReactiveHealthIndicator.class)
.doesNotHaveBean(CassandraDriverReactiveHealthIndicator.class));
}
@Test
void runWhenDisabledShouldNotCreateDriverIndicator() {
this.contextRunner.withPropertyValues("management.health.cassandra.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean(CassandraDriverHealthIndicator.class)
.doesNotHaveBean("cassandraHealthContributor"));
}
@Test
void runWhenSpringDataPresentShouldNotCreateDriverIndicator() {
this.contextRunner.withConfiguration(AutoConfigurations.of(CassandraHealthContributorAutoConfiguration.class))
.withBean(CassandraOperations.class, () -> mock(CassandraOperations.class))
.run((context) -> assertThat(context).doesNotHaveBean(CassandraDriverHealthIndicator.class)
.hasSingleBean(CassandraHealthIndicator.class).hasBean("cassandraHealthContributor"));
}
@Test
void runWhenReactorPresentShouldNotCreateDriverIndicator() {
this.contextRunner
.withConfiguration(
AutoConfigurations.of(CassandraDriverReactiveHealthContributorAutoConfiguration.class))
.run((context) -> assertThat(context).doesNotHaveBean(CassandraDriverHealthIndicator.class)
.hasSingleBean(CassandraDriverReactiveHealthIndicator.class)
.hasBean("cassandraHealthContributor"));
}
@Test
void runWhenSpringDataAndReactorPresentShouldNotCreateDriverIndicator() {
this.contextRunner
.withConfiguration(AutoConfigurations.of(CassandraReactiveHealthContributorAutoConfiguration.class))
.withBean(ReactiveCassandraOperations.class, () -> mock(ReactiveCassandraOperations.class))
.run((context) -> assertThat(context).doesNotHaveBean(CassandraDriverHealthIndicator.class)
.hasSingleBean(CassandraReactiveHealthIndicator.class).hasBean("cassandraHealthContributor"));
}
}

@ -1,79 +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.boot.actuate.autoconfigure.cassandra;
import com.datastax.oss.driver.api.core.CqlSession;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
import org.springframework.boot.actuate.cassandra.CassandraDriverReactiveHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraReactiveHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link CassandraDriverReactiveHealthContributorAutoConfiguration}.
*
* @author Alexandre Dutra
* @since 2.4.0
*/
class CassandraDriverReactiveHealthContributorAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withBean(CqlSession.class, () -> mock(CqlSession.class))
.withConfiguration(AutoConfigurations.of(CassandraDriverReactiveHealthContributorAutoConfiguration.class,
HealthContributorAutoConfiguration.class));
@Test
void runShouldCreateDriverReactiveIndicator() {
this.contextRunner
.run((context) -> assertThat(context).hasSingleBean(CassandraDriverReactiveHealthIndicator.class)
.hasBean("cassandraHealthContributor").doesNotHaveBean(CassandraHealthIndicator.class)
.doesNotHaveBean(CassandraReactiveHealthIndicator.class));
}
@Test
void runWhenDisabledShouldNotCreateDriverReactiveIndicator() {
this.contextRunner.withPropertyValues("management.health.cassandra.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean(CassandraDriverReactiveHealthIndicator.class)
.doesNotHaveBean("cassandraHealthContributor"));
}
@Test
void runWhenSpringDataPresentShouldNotCreateDriverReactiveIndicator() {
this.contextRunner.withConfiguration(AutoConfigurations.of(CassandraHealthContributorAutoConfiguration.class))
.withBean(CassandraOperations.class, () -> mock(CassandraOperations.class))
.run((context) -> assertThat(context).doesNotHaveBean(CassandraDriverReactiveHealthIndicator.class)
.hasSingleBean(CassandraHealthIndicator.class).hasBean("cassandraHealthContributor"));
}
@Test
void runWhenSpringDataAndReactorPresentShouldNotCreateDriverReactiveIndicator() {
this.contextRunner
.withConfiguration(AutoConfigurations.of(CassandraReactiveHealthContributorAutoConfiguration.class))
.withBean(ReactiveCassandraOperations.class, () -> mock(ReactiveCassandraOperations.class))
.run((context) -> assertThat(context).doesNotHaveBean(CassandraDriverReactiveHealthIndicator.class)
.hasSingleBean(CassandraReactiveHealthIndicator.class).hasBean("cassandraHealthContributor"));
}
}

@ -16,15 +16,15 @@
package org.springframework.boot.actuate.autoconfigure.cassandra;
import com.datastax.oss.driver.api.core.CqlSession;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
import org.springframework.boot.actuate.cassandra.CassandraDriverHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.CassandraOperations;
import static org.assertj.core.api.Assertions.assertThat;
@ -34,33 +34,50 @@ import static org.mockito.Mockito.mock;
* Tests for {@link CassandraHealthContributorAutoConfiguration}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
class CassandraHealthContributorAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(CassandraConfiguration.class,
CassandraHealthContributorAutoConfiguration.class, HealthContributorAutoConfiguration.class));
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(CassandraHealthContributorAutoConfiguration.class,
HealthContributorAutoConfiguration.class));
@Test
void runShouldCreateIndicator() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(CassandraHealthIndicator.class));
void runWithoutCqlSessionOrCassandraOperationsShouldNotCreateIndicator() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean("cassandraHealthContributor")
.doesNotHaveBean(CassandraHealthIndicator.class).doesNotHaveBean(CassandraDriverHealthIndicator.class));
}
@Test
void runWhenDisabledShouldNotCreateIndicator() {
this.contextRunner.withPropertyValues("management.health.cassandra.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean(CassandraHealthIndicator.class));
void runWithCassandraOperationsShouldCreateRegularIndicator() {
this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class))
.withBean(CassandraOperations.class, () -> mock(CassandraOperations.class))
.run((context) -> assertThat(context).hasSingleBean(CassandraHealthIndicator.class)
.doesNotHaveBean(CassandraDriverHealthIndicator.class));
}
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(CassandraHealthContributorAutoConfiguration.class)
static class CassandraConfiguration {
@Test
void runWithCqlSessionOnlyShouldCreateDriverIndicator() {
this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class)).run((context) -> assertThat(context)
.hasSingleBean(CassandraDriverHealthIndicator.class).doesNotHaveBean(CassandraHealthIndicator.class));
}
@Bean
CassandraOperations cassandraOperations() {
return mock(CassandraOperations.class);
}
@Test
void runWithCqlSessionAndSpringDataAbsentShouldCreateDriverIndicator() {
this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class))
.withClassLoader(new FilteredClassLoader("org.springframework.data"))
.run((context) -> assertThat(context).hasSingleBean(CassandraDriverHealthIndicator.class)
.doesNotHaveBean(CassandraHealthIndicator.class));
}
@Test
void runWhenDisabledShouldNotCreateIndicator() {
this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class))
.withBean(CassandraOperations.class, () -> mock(CassandraOperations.class))
.withPropertyValues("management.health.cassandra.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean("cassandraHealthContributor")
.doesNotHaveBean(CassandraHealthIndicator.class)
.doesNotHaveBean(CassandraDriverHealthIndicator.class));
}
}

@ -16,14 +16,18 @@
package org.springframework.boot.actuate.autoconfigure.cassandra;
import com.datastax.oss.driver.api.core.CqlSession;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.cassandra.CassandraHealthContributorAutoConfigurationTests.CassandraConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
import org.springframework.boot.actuate.cassandra.CassandraDriverHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraDriverReactiveHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraHealthIndicator;
import org.springframework.boot.actuate.cassandra.CassandraReactiveHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
import static org.assertj.core.api.Assertions.assertThat;
@ -37,31 +41,53 @@ import static org.mockito.Mockito.mock;
*/
class CassandraReactiveHealthContributorAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withBean(ReactiveCassandraOperations.class, () -> mock(ReactiveCassandraOperations.class))
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(CassandraReactiveHealthContributorAutoConfiguration.class,
HealthContributorAutoConfiguration.class));
CassandraHealthContributorAutoConfiguration.class, HealthContributorAutoConfiguration.class));
@Test
void runShouldCreateIndicator() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(CassandraReactiveHealthIndicator.class)
.hasBean("cassandraHealthContributor"));
void runWithoutCqlSessionOrReactiveCassandraOperationsShouldNotCreateIndicator() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean("cassandraHealthContributor")
.doesNotHaveBean(CassandraReactiveHealthIndicator.class)
.doesNotHaveBean(CassandraDriverReactiveHealthIndicator.class));
}
@Test
void runWithRegularIndicatorShouldOnlyCreateReactiveIndicator() {
this.contextRunner
.withConfiguration(AutoConfigurations.of(CassandraConfiguration.class,
CassandraHealthContributorAutoConfiguration.class))
.run((context) -> assertThat(context).hasSingleBean(CassandraReactiveHealthIndicator.class)
.hasBean("cassandraHealthContributor").doesNotHaveBean(CassandraHealthIndicator.class));
void runWithReactiveCassandraOperationsShouldOnlyCreateReactiveIndicator() {
this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class))
.withBean(ReactiveCassandraOperations.class, () -> mock(ReactiveCassandraOperations.class))
.withBean(CassandraOperations.class, () -> mock(CassandraOperations.class))
.run((context) -> assertThat(context).hasBean("cassandraHealthContributor")
.hasSingleBean(CassandraReactiveHealthIndicator.class)
.doesNotHaveBean(CassandraDriverReactiveHealthIndicator.class)
.doesNotHaveBean(CassandraHealthIndicator.class)
.doesNotHaveBean(CassandraDriverHealthIndicator.class));
}
@Test
void runWithCqlSessionShouldCreateDriverIndicator() {
this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class))
.run((context) -> assertThat(context).hasBean("cassandraHealthContributor")
.hasSingleBean(CassandraDriverReactiveHealthIndicator.class)
.doesNotHaveBean(CassandraReactiveHealthIndicator.class));
}
@Test
void runWithCqlSessionAndSpringDataAbsentShouldACreateDriverIndicator() {
this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class))
.withClassLoader(new FilteredClassLoader("org.springframework.data"))
.run((context) -> assertThat(context).hasBean("cassandraHealthContributor")
.hasSingleBean(CassandraDriverReactiveHealthIndicator.class)
.doesNotHaveBean(CassandraReactiveHealthIndicator.class));
}
@Test
void runWhenDisabledShouldNotCreateIndicator() {
this.contextRunner.withPropertyValues("management.health.cassandra.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean(CassandraReactiveHealthIndicator.class)
.doesNotHaveBean("cassandraHealthContributor"));
this.contextRunner.withBean(CqlSession.class, () -> mock(CqlSession.class))
.withBean(ReactiveCassandraOperations.class, () -> mock(ReactiveCassandraOperations.class))
.withPropertyValues("management.health.cassandra.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean("cassandraHealthContributor")
.doesNotHaveBean(CassandraReactiveHealthIndicator.class));
}
}

@ -30,9 +30,6 @@ import org.springframework.util.Assert;
* Simple implementation of a {@link HealthIndicator} returning status information for
* Cassandra data stores.
*
* This health indicator is automatically used when Spring Data Cassandra is not present,
* but the Cassandra driver is.
*
* @author Alexandre Dutra
* @since 2.4.0
*/

@ -29,9 +29,6 @@ import org.springframework.util.Assert;
* Simple implementation of a {@link ReactiveHealthIndicator} returning status information
* for Cassandra data stores.
*
* This health indicator is automatically used when Spring Data Cassandra is not present,
* but the Cassandra driver is.
*
* @author Alexandre Dutra
* @since 2.4.0
*/

@ -58,7 +58,6 @@ class CassandraDriverReactiveHealthIndicatorTests {
given(session.executeReactive(any(SimpleStatement.class))).willReturn(results);
doAnswer(mockReactiveResultSetBehavior(row)).when(results).subscribe(any());
given(row.getString(0)).willReturn("6.0.0");
CassandraDriverReactiveHealthIndicator cassandraReactiveHealthIndicator = new CassandraDriverReactiveHealthIndicator(
session);
Mono<Health> health = cassandraReactiveHealthIndicator.health();
@ -74,7 +73,6 @@ class CassandraDriverReactiveHealthIndicatorTests {
CqlSession session = mock(CqlSession.class);
given(session.executeReactive(any(SimpleStatement.class)))
.willThrow(new DriverTimeoutException("Test Exception"));
CassandraDriverReactiveHealthIndicator cassandraReactiveHealthIndicator = new CassandraDriverReactiveHealthIndicator(
session);
Mono<Health> health = cassandraReactiveHealthIndicator.health();

@ -655,7 +655,7 @@ The following `HealthIndicators` are auto-configured by Spring Boot when appropr
|===
| Name | Description
| {spring-boot-actuator-module-code}/cassandra/CassandraHealthIndicator.java[`CassandraHealthIndicator`]
| {spring-boot-actuator-module-code}/cassandra/CassandraHealthIndicator.java[`CassandraHealthIndicator`] or {spring-boot-actuator-module-code}/cassandra/CassandraDriverHealthIndicator.java[`CassandraDriverHealthIndicator`]
| Checks that a Cassandra database is up.
| {spring-boot-actuator-module-code}/couchbase/CouchbaseHealthIndicator.java[`CouchbaseHealthIndicator`]
@ -830,7 +830,7 @@ The following `ReactiveHealthIndicators` are auto-configured by Spring Boot when
|===
| Name | Description
| {spring-boot-actuator-module-code}/cassandra/CassandraReactiveHealthIndicator.java[`CassandraReactiveHealthIndicator`]
| {spring-boot-actuator-module-code}/cassandra/CassandraReactiveHealthIndicator.java[`CassandraReactiveHealthIndicator`] or {spring-boot-actuator-module-code}/cassandra/CassandraDriverReactiveHealthIndicator.java[`CassandraDriverReactiveHealthIndicator`]
| Checks that a Cassandra database is up.
| {spring-boot-actuator-module-code}/couchbase/CouchbaseReactiveHealthIndicator.java[`CouchbaseReactiveHealthIndicator`]

Loading…
Cancel
Save