Migrate HealthIndicatorAutoConfigurationTests to ContextLoader

pull/9645/merge
Stephane Nicoll 7 years ago
parent 0fcf6a0e51
commit 6e32f49630

@ -21,7 +21,6 @@ import java.util.Map;
import javax.sql.DataSource; import javax.sql.DataSource;
import io.searchbox.client.JestClient; import io.searchbox.client.JestClient;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.neo4j.ogm.session.SessionFactory; import org.neo4j.ogm.session.SessionFactory;
@ -44,13 +43,11 @@ import org.springframework.boot.actuate.health.RabbitHealthIndicator;
import org.springframework.boot.actuate.health.RedisHealthIndicator; import org.springframework.boot.actuate.health.RedisHealthIndicator;
import org.springframework.boot.actuate.health.SolrHealthIndicator; import org.springframework.boot.actuate.health.SolrHealthIndicator;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration; import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration; import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration; import org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvidersConfiguration; import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvidersConfiguration;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration; import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration;
@ -59,8 +56,8 @@ import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration; import org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.context.ContextConsumer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.boot.test.context.ContextLoader;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.CassandraOperations; import org.springframework.data.cassandra.core.CassandraOperations;
@ -82,531 +79,303 @@ import static org.mockito.Mockito.mock;
*/ */
public class HealthIndicatorAutoConfigurationTests { public class HealthIndicatorAutoConfigurationTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); public final ContextLoader contextLoader = new ContextLoader()
.autoConfig(HealthIndicatorAutoConfiguration.class,
@After ManagementServerProperties.class);
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void defaultHealthIndicator() { public void defaultHealthIndicator() {
this.context.register(HealthIndicatorAutoConfiguration.class, this.contextLoader.env("management.health.diskspace.enabled:false")
ManagementServerProperties.class); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
TestPropertyValues.of("management.health.diskspace.enabled:false")
.applyTo(this.context);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void defaultHealthIndicatorsDisabled() { public void defaultHealthIndicatorsDisabled() {
this.context.register(HealthIndicatorAutoConfiguration.class, this.contextLoader.env("management.health.defaults.enabled:false")
ManagementServerProperties.class); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
TestPropertyValues.of("management.health.defaults.enabled:false")
.applyTo(this.context);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void defaultHealthIndicatorsDisabledWithCustomOne() { public void defaultHealthIndicatorsDisabledWithCustomOne() {
this.context.register(CustomHealthIndicator.class, this.contextLoader.config(CustomHealthIndicator.class)
HealthIndicatorAutoConfiguration.class, ManagementServerProperties.class); .env("management.health.defaults.enabled:false").load(context -> {
TestPropertyValues.of("management.health.defaults.enabled:false") Map<String, HealthIndicator> beans = context
.applyTo(this.context);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class); .getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1); assertThat(beans).hasSize(1);
assertThat(this.context.getBean("customHealthIndicator")) assertThat(context.getBean("customHealthIndicator"))
.isSameAs(beans.values().iterator().next()); .isSameAs(beans.values().iterator().next());
});
} }
@Test @Test
public void defaultHealthIndicatorsDisabledButOne() { public void defaultHealthIndicatorsDisabledButOne() {
this.context.register(HealthIndicatorAutoConfiguration.class, this.contextLoader
ManagementServerProperties.class); .env("management.health.defaults.enabled:false",
TestPropertyValues.of("management.health.defaults.enabled:false", "management.health.diskspace.enabled:true")
"management.health.diskspace.enabled:true").applyTo(this.context); .load(hasSingleHealthIndicator(DiskSpaceHealthIndicator.class));
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(DiskSpaceHealthIndicator.class);
} }
@Test @Test
public void redisHealthIndicator() { public void redisHealthIndicator() {
this.context.register(RedisAutoConfiguration.class, this.contextLoader.autoConfigFirst(RedisAutoConfiguration.class)
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); .env("management.health.diskspace.enabled:false")
TestPropertyValues.of("management.health.diskspace.enabled:false") .load(hasSingleHealthIndicator(RedisHealthIndicator.class));
.applyTo(this.context);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(RedisHealthIndicator.class);
} }
@Test @Test
public void notRedisHealthIndicator() { public void notRedisHealthIndicator() {
this.context.register(RedisAutoConfiguration.class, this.contextLoader.autoConfigFirst(RedisAutoConfiguration.class)
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); .env("management.health.redis.enabled:false",
TestPropertyValues
.of("management.health.redis.enabled:false",
"management.health.diskspace.enabled:false") "management.health.diskspace.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void mongoHealthIndicator() { public void mongoHealthIndicator() {
this.context.register(MongoAutoConfiguration.class, this.contextLoader.autoConfigFirst(MongoAutoConfiguration.class,
ManagementServerProperties.class, MongoDataAutoConfiguration.class, MongoDataAutoConfiguration.class)
HealthIndicatorAutoConfiguration.class); .env("management.health.diskspace.enabled:false")
TestPropertyValues.of("management.health.diskspace.enabled:false") .load(hasSingleHealthIndicator(MongoHealthIndicator.class));
.applyTo(this.context);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(MongoHealthIndicator.class);
} }
@Test @Test
public void notMongoHealthIndicator() { public void notMongoHealthIndicator() {
this.context.register(MongoAutoConfiguration.class, this.contextLoader.autoConfigFirst(MongoAutoConfiguration.class,
ManagementServerProperties.class, MongoDataAutoConfiguration.class, MongoDataAutoConfiguration.class)
HealthIndicatorAutoConfiguration.class); .env("management.health.mongo.enabled:false",
TestPropertyValues
.of("management.health.mongo.enabled:false",
"management.health.diskspace.enabled:false") "management.health.diskspace.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void combinedHealthIndicator() { public void combinedHealthIndicator() {
this.context.register(MongoAutoConfiguration.class, RedisAutoConfiguration.class, this.contextLoader.autoConfigFirst(MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class, SolrAutoConfiguration.class, RedisAutoConfiguration.class, MongoDataAutoConfiguration.class,
HealthIndicatorAutoConfiguration.class); SolrAutoConfiguration.class).load(context -> {
this.context.refresh(); Map<String, HealthIndicator> beans = context
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class); .getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(4); assertThat(beans).hasSize(4);
});
} }
@Test @Test
public void dataSourceHealthIndicator() { public void dataSourceHealthIndicator() {
this.context.register(EmbeddedDataSourceConfiguration.class, this.contextLoader.autoConfigFirst(EmbeddedDataSourceConfiguration.class)
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); .env("management.health.diskspace.enabled:false")
TestPropertyValues.of("management.health.diskspace.enabled:false") .load(hasSingleHealthIndicator(DataSourceHealthIndicator.class));
.applyTo(this.context);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(DataSourceHealthIndicator.class);
} }
@Test @Test
public void dataSourceHealthIndicatorWithSeveralDataSources() { public void dataSourceHealthIndicatorWithSeveralDataSources() {
this.context.register(EmbeddedDataSourceConfiguration.class, this.contextLoader.config(EmbeddedDataSourceConfiguration.class,
DataSourceConfig.class, ManagementServerProperties.class, DataSourceConfig.class).env("management.health.diskspace.enabled:false")
HealthIndicatorAutoConfiguration.class); .load(context -> {
TestPropertyValues.of("management.health.diskspace.enabled:false") Map<String, HealthIndicator> beans = context
.applyTo(this.context);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class); .getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1); assertThat(beans).hasSize(1);
HealthIndicator bean = beans.values().iterator().next(); HealthIndicator bean = beans.values().iterator().next();
assertThat(bean).isExactlyInstanceOf(CompositeHealthIndicator.class); assertThat(bean).isExactlyInstanceOf(CompositeHealthIndicator.class);
assertThat(bean.health().getDetails()).containsOnlyKeys("dataSource", assertThat(bean.health().getDetails()).containsOnlyKeys("dataSource",
"testDataSource"); "testDataSource");
});
} }
@Test @Test
public void dataSourceHealthIndicatorWithAbstractRoutingDataSource() { public void dataSourceHealthIndicatorWithAbstractRoutingDataSource() {
this.context.register(EmbeddedDataSourceConfiguration.class, this.contextLoader.config(EmbeddedDataSourceConfiguration.class,
RoutingDatasourceConfig.class, ManagementServerProperties.class, RoutingDatasourceConfig.class)
HealthIndicatorAutoConfiguration.class); .env("management.health.diskspace.enabled:false")
TestPropertyValues.of("management.health.diskspace.enabled:false") .load(hasSingleHealthIndicator(DataSourceHealthIndicator.class));
.applyTo(this.context);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next())
.isExactlyInstanceOf(DataSourceHealthIndicator.class);
} }
@Test @Test
public void dataSourceHealthIndicatorWithCustomValidationQuery() { public void dataSourceHealthIndicatorWithCustomValidationQuery() {
this.context.register(PropertyPlaceholderAutoConfiguration.class, this.contextLoader.config(DataSourceConfig.class,
ManagementServerProperties.class, DataSourceProperties.class,
DataSourceConfig.class,
DataSourcePoolMetadataProvidersConfiguration.class, DataSourcePoolMetadataProvidersConfiguration.class,
HealthIndicatorAutoConfiguration.class); HealthIndicatorAutoConfiguration.class).env(
TestPropertyValues "spring.datasource.test.validation-query:SELECT from FOOBAR",
.of("spring.datasource.test.validation-query:SELECT from FOOBAR", "management.health.diskspace.enabled:false").load(context -> {
"management.health.diskspace.enabled:false") Map<String, HealthIndicator> beans = context
.applyTo(this.context);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class); .getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1); assertThat(beans).hasSize(1);
HealthIndicator healthIndicator = beans.values().iterator().next(); HealthIndicator healthIndicator = beans.values().iterator().next();
assertThat(healthIndicator.getClass()).isEqualTo(DataSourceHealthIndicator.class); assertThat(healthIndicator.getClass()).isEqualTo(DataSourceHealthIndicator.class);
DataSourceHealthIndicator dataSourceHealthIndicator = (DataSourceHealthIndicator) healthIndicator; DataSourceHealthIndicator dataSourceHealthIndicator = (DataSourceHealthIndicator) healthIndicator;
assertThat(dataSourceHealthIndicator.getQuery()).isEqualTo("SELECT from FOOBAR"); assertThat(dataSourceHealthIndicator.getQuery()).isEqualTo("SELECT from FOOBAR");
});
} }
@Test @Test
public void notDataSourceHealthIndicator() { public void notDataSourceHealthIndicator() {
this.context.register(EmbeddedDataSourceConfiguration.class, this.contextLoader.config(EmbeddedDataSourceConfiguration.class)
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); .env(
TestPropertyValues "management.health.db.enabled:false",
.of("management.health.db.enabled:false",
"management.health.diskspace.enabled:false") "management.health.diskspace.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void rabbitHealthIndicator() { public void rabbitHealthIndicator() {
this.context.register(RabbitAutoConfiguration.class, this.contextLoader.autoConfigFirst(RabbitAutoConfiguration.class)
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); .env("management.health.diskspace.enabled:false")
TestPropertyValues.of("management.health.diskspace.enabled:false") .load(hasSingleHealthIndicator(RabbitHealthIndicator.class));
.applyTo(this.context);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(RabbitHealthIndicator.class);
} }
@Test @Test
public void notRabbitHealthIndicator() { public void notRabbitHealthIndicator() {
this.context.register(RabbitAutoConfiguration.class, this.contextLoader.autoConfigFirst(RabbitAutoConfiguration.class)
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); .env("management.health.rabbit.enabled:false",
TestPropertyValues
.of("management.health.rabbit.enabled:false",
"management.health.diskspace.enabled:false") "management.health.diskspace.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void solrHealthIndicator() { public void solrHealthIndicator() {
this.context.register(SolrAutoConfiguration.class, this.contextLoader.autoConfigFirst(SolrAutoConfiguration.class)
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); .env("management.health.diskspace.enabled:false")
TestPropertyValues.of("management.health.diskspace.enabled:false") .load(hasSingleHealthIndicator(SolrHealthIndicator.class));
.applyTo(this.context);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(SolrHealthIndicator.class);
} }
@Test @Test
public void notSolrHealthIndicator() { public void notSolrHealthIndicator() {
this.context.register(SolrAutoConfiguration.class, this.contextLoader.autoConfigFirst(SolrAutoConfiguration.class)
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); .env("management.health.solr.enabled:false",
TestPropertyValues
.of("management.health.solr.enabled:false",
"management.health.diskspace.enabled:false") "management.health.diskspace.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void diskSpaceHealthIndicator() { public void diskSpaceHealthIndicator() {
this.context.register(HealthIndicatorAutoConfiguration.class); this.contextLoader.load(hasSingleHealthIndicator(DiskSpaceHealthIndicator.class));
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(DiskSpaceHealthIndicator.class);
} }
@Test @Test
public void mailHealthIndicator() { public void mailHealthIndicator() {
TestPropertyValues this.contextLoader.autoConfigFirst(MailSenderAutoConfiguration.class)
.of("spring.mail.host:smtp.acme.org", .env("spring.mail.host:smtp.acme.org",
"management.health.diskspace.enabled:false") "management.health.diskspace.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(MailHealthIndicator.class));
this.context.register(MailSenderAutoConfiguration.class,
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(MailHealthIndicator.class);
} }
@Test @Test
public void notMailHealthIndicator() { public void notMailHealthIndicator() {
TestPropertyValues this.contextLoader.autoConfigFirst(MailSenderAutoConfiguration.class)
.of("spring.mail.host:smtp.acme.org", .env("spring.mail.host:smtp.acme.org",
"management.health.mail.enabled:false", "management.health.mail.enabled:false",
"management.health.diskspace.enabled:false") "management.health.diskspace.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.register(MailSenderAutoConfiguration.class,
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void jmsHealthIndicator() { public void jmsHealthIndicator() {
TestPropertyValues.of("management.health.diskspace.enabled:false") this.contextLoader.autoConfigFirst(ActiveMQAutoConfiguration.class)
.applyTo(this.context); .env("management.health.diskspace.enabled:false")
this.context.register(ActiveMQAutoConfiguration.class, .load(hasSingleHealthIndicator(JmsHealthIndicator.class));
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(JmsHealthIndicator.class);
} }
@Test @Test
public void notJmsHealthIndicator() { public void notJmsHealthIndicator() {
TestPropertyValues this.contextLoader.autoConfigFirst(ActiveMQAutoConfiguration.class)
.of("management.health.jms.enabled:false", .env("management.health.jms.enabled:false",
"management.health.diskspace.enabled:false") "management.health.diskspace.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.register(ActiveMQAutoConfiguration.class,
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void elasticsearchHealthIndicator() { public void elasticsearchHealthIndicator() {
TestPropertyValues this.contextLoader.autoConfigFirst(JestClientConfiguration.class,
.of("spring.data.elasticsearch.cluster-nodes:localhost:0", JestAutoConfiguration.class, ElasticsearchAutoConfiguration.class)
.env("spring.data.elasticsearch.cluster-nodes:localhost:0",
"management.health.diskspace.enabled:false") "management.health.diskspace.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(ElasticsearchHealthIndicator.class));
this.context.register(JestClientConfiguration.class, JestAutoConfiguration.class,
ElasticsearchAutoConfiguration.class, ManagementServerProperties.class,
HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ElasticsearchHealthIndicator.class);
} }
@Test @Test
public void elasticsearchJestHealthIndicator() { public void elasticsearchJestHealthIndicator() {
TestPropertyValues.of("management.health.diskspace.enabled:false") this.contextLoader.autoConfigFirst(JestClientConfiguration.class,
.applyTo(this.context); JestAutoConfiguration.class)
this.context.register(JestClientConfiguration.class, JestAutoConfiguration.class, .env("management.health.diskspace.enabled:false")
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); .load(hasSingleHealthIndicator(ElasticsearchJestHealthIndicator.class));
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ElasticsearchJestHealthIndicator.class);
} }
@Test @Test
public void notElasticsearchHealthIndicator() { public void notElasticsearchHealthIndicator() {
TestPropertyValues this.contextLoader.autoConfigFirst(JestClientConfiguration.class,
.of("management.health.elasticsearch.enabled:false", JestAutoConfiguration.class, ElasticsearchAutoConfiguration.class)
.env("management.health.elasticsearch.enabled:false",
"spring.data.elasticsearch.properties.path.home:target", "spring.data.elasticsearch.properties.path.home:target",
"management.health.diskspace.enabled:false") "management.health.diskspace.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.register(JestClientConfiguration.class, JestAutoConfiguration.class,
ElasticsearchAutoConfiguration.class, ManagementServerProperties.class,
HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void cassandraHealthIndicator() throws Exception { public void cassandraHealthIndicator() throws Exception {
TestPropertyValues.of("management.health.diskspace.enabled:false") this.contextLoader.autoConfigFirst(CassandraConfiguration.class)
.applyTo(this.context); .env("management.health.diskspace.enabled:false")
this.context.register(CassandraConfiguration.class, .load(hasSingleHealthIndicator(CassandraHealthIndicator.class));
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(CassandraHealthIndicator.class);
} }
@Test @Test
public void notCassandraHealthIndicator() throws Exception { public void notCassandraHealthIndicator() throws Exception {
TestPropertyValues this.contextLoader.autoConfigFirst(CassandraConfiguration.class)
.of("management.health.diskspace.enabled:false", .env("management.health.diskspace.enabled:false",
"management.health.cassandra.enabled:false") "management.health.cassandra.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.register(CassandraConfiguration.class,
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void couchbaseHealthIndicator() throws Exception { public void couchbaseHealthIndicator() throws Exception {
TestPropertyValues.of("management.health.diskspace.enabled:false") this.contextLoader.autoConfigFirst(CouchbaseConfiguration.class)
.applyTo(this.context); .env("management.health.diskspace.enabled:false")
this.context.register(CouchbaseConfiguration.class, .load(hasSingleHealthIndicator(CouchbaseHealthIndicator.class));
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans.size()).isEqualTo(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(CouchbaseHealthIndicator.class);
} }
@Test @Test
public void notCouchbaseHealthIndicator() throws Exception { public void notCouchbaseHealthIndicator() throws Exception {
TestPropertyValues this.contextLoader.autoConfigFirst(CouchbaseConfiguration.class)
.of("management.health.diskspace.enabled:false", .env("management.health.diskspace.enabled:false",
"management.health.couchbase.enabled:false") "management.health.couchbase.enabled:false")
.applyTo(this.context); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.register(CouchbaseConfiguration.class,
ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans.size()).isEqualTo(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void ldapHealthIndicator() throws Exception { public void ldapHealthIndicator() throws Exception {
TestPropertyValues.of("management.health.diskspace.enabled:false") this.contextLoader.autoConfigFirst(LdapConfiguration.class)
.applyTo(this.context); .env("management.health.diskspace.enabled:false")
this.context.register(LdapConfiguration.class, ManagementServerProperties.class, .load(hasSingleHealthIndicator(LdapHealthIndicator.class));
HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans.size()).isEqualTo(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(LdapHealthIndicator.class);
} }
@Test @Test
public void notLdapHealthIndicator() throws Exception { public void notLdapHealthIndicator() throws Exception {
TestPropertyValues.of("management.health.diskspace.enabled:false", this.contextLoader.autoConfigFirst(LdapConfiguration.class)
"management.health.ldap.enabled:false").applyTo(this.context); .env("management.health.diskspace.enabled:false",
this.context.register(LdapConfiguration.class, ManagementServerProperties.class, "management.health.ldap.enabled:false")
HealthIndicatorAutoConfiguration.class); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans.size()).isEqualTo(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(ApplicationHealthIndicator.class);
} }
@Test @Test
public void neo4jHealthIndicator() throws Exception { public void neo4jHealthIndicator() throws Exception {
TestPropertyValues.of("management.health.diskspace.enabled:false") this.contextLoader.autoConfigFirst(Neo4jConfiguration.class)
.applyTo(this.context); .env("management.health.diskspace.enabled:false")
this.context.register(Neo4jConfiguration.class, ManagementServerProperties.class, .load(hasSingleHealthIndicator(Neo4jHealthIndicator.class));
HealthIndicatorAutoConfiguration.class);
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans.size()).isEqualTo(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(Neo4jHealthIndicator.class);
} }
@Test @Test
public void notNeo4jHealthIndicator() throws Exception { public void notNeo4jHealthIndicator() throws Exception {
TestPropertyValues.of("management.health.diskspace.enabled:false", this.contextLoader.autoConfigFirst(Neo4jConfiguration.class)
"management.health.neo4j.enabled:false").applyTo(this.context); .env("management.health.diskspace.enabled:false",
this.context.register(Neo4jConfiguration.class, ManagementServerProperties.class, "management.health.neo4j.enabled:false")
HealthIndicatorAutoConfiguration.class); .load(hasSingleHealthIndicator(ApplicationHealthIndicator.class));
this.context.refresh(); }
Map<String, HealthIndicator> beans = this.context
private ContextConsumer hasSingleHealthIndicator(
Class<? extends HealthIndicator> type) {
return context -> {
Map<String, HealthIndicator> beans = context
.getBeansOfType(HealthIndicator.class); .getBeansOfType(HealthIndicator.class);
assertThat(beans.size()).isEqualTo(1); assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass()) assertThat(beans.values().iterator().next().getClass()).isEqualTo(type);
.isEqualTo(ApplicationHealthIndicator.class); };
} }
@Configuration @Configuration
@ -654,8 +423,7 @@ public class HealthIndicatorAutoConfigurationTests {
@Bean @Bean
public CassandraOperations cassandraOperations() { public CassandraOperations cassandraOperations() {
CassandraOperations operations = mock(CassandraOperations.class); return mock(CassandraOperations.class);
return operations;
} }
} }
@ -665,8 +433,7 @@ public class HealthIndicatorAutoConfigurationTests {
@Bean @Bean
public CouchbaseOperations couchbaseOperations() { public CouchbaseOperations couchbaseOperations() {
CouchbaseOperations operations = mock(CouchbaseOperations.class); return mock(CouchbaseOperations.class);
return operations;
} }
} }

Loading…
Cancel
Save