@ -49,6 +49,7 @@ import static org.mockito.Mockito.mock;
*
* @author Phillip Webb
* @author Julio Gomez
* @author Safeer Ansari
* /
class DataSourceHealthContributorAutoConfigurationTests {
@ -136,6 +137,20 @@ class DataSourceHealthContributorAutoConfigurationTests {
. doesNotHaveBean ( CompositeHealthContributor . class ) ) ;
}
@Test
void runWhenDataSourceHasNullRoutingKeyShouldProduceUnnamedComposedIndicator ( ) {
this . contextRunner . withUserConfiguration ( NullKeyRoutingDataSourceConfig . class ) . run ( ( context ) - > {
assertThat ( context ) . hasSingleBean ( RoutingDataSourceHealthContributor . class ) ;
RoutingDataSourceHealthContributor routingHealthContributor = context
. getBean ( RoutingDataSourceHealthContributor . class ) ;
assertThat ( routingHealthContributor . getContributor ( "unnamed" ) )
. isInstanceOf ( DataSourceHealthIndicator . class ) ;
assertThat ( routingHealthContributor . getContributor ( "one" ) ) . isInstanceOf ( DataSourceHealthIndicator . class ) ;
assertThat ( routingHealthContributor . iterator ( ) ) . toIterable ( ) . extracting ( "name" )
. containsExactlyInAnyOrder ( "unnamed" , "one" ) ;
} ) ;
}
@Configuration ( proxyBeanMethods = false )
@EnableConfigurationProperties
static class DataSourceConfig {
@ -164,4 +179,19 @@ class DataSourceHealthContributorAutoConfigurationTests {
}
@Configuration ( proxyBeanMethods = false )
static class NullKeyRoutingDataSourceConfig {
@Bean
AbstractRoutingDataSource routingDataSource ( ) {
Map < Object , DataSource > dataSources = new HashMap < > ( ) ;
dataSources . put ( null , mock ( DataSource . class ) ) ;
dataSources . put ( "one" , mock ( DataSource . class ) ) ;
AbstractRoutingDataSource routingDataSource = mock ( AbstractRoutingDataSource . class ) ;
given ( routingDataSource . getResolvedDataSources ( ) ) . willReturn ( dataSources ) ;
return routingDataSource ;
}
}
}