|
28 | 28 | import com.rabbitmq.client.Connection;
|
29 | 29 | import com.rabbitmq.client.SslContextFactory;
|
30 | 30 | import com.rabbitmq.client.TrustEverythingTrustManager;
|
| 31 | +import com.rabbitmq.client.impl.CredentialsProvider; |
| 32 | +import com.rabbitmq.client.impl.CredentialsRefreshService; |
| 33 | +import com.rabbitmq.client.impl.DefaultCredentialsProvider; |
31 | 34 | import org.aopalliance.aop.Advice;
|
32 | 35 | import org.junit.jupiter.api.Test;
|
33 | 36 |
|
@@ -716,6 +719,49 @@ void enableSslWithValidateServerCertificateDefault() throws Exception {
|
716 | 719 | });
|
717 | 720 | }
|
718 | 721 |
|
| 722 | + @Test |
| 723 | + void whenACredentialsProviderIsAvailableThenConnectionFactoryIsConfiguredToUseIt() throws Exception { |
| 724 | + this.contextRunner.withUserConfiguration(CredentialsProviderConfiguration.class) |
| 725 | + .run((context) -> assertThat(getTargetConnectionFactory(context).params(null).getCredentialsProvider()) |
| 726 | + .isEqualTo(CredentialsProviderConfiguration.credentialsProvider)); |
| 727 | + } |
| 728 | + |
| 729 | + @Test |
| 730 | + void whenAPrimaryCredentialsProviderIsAvailableThenConnectionFactoryIsConfiguredToUseIt() throws Exception { |
| 731 | + this.contextRunner.withUserConfiguration(PrimaryCredentialsProviderConfiguration.class) |
| 732 | + .run((context) -> assertThat(getTargetConnectionFactory(context).params(null).getCredentialsProvider()) |
| 733 | + .isEqualTo(PrimaryCredentialsProviderConfiguration.credentialsProvider)); |
| 734 | + } |
| 735 | + |
| 736 | + @Test |
| 737 | + void whenMultipleCredentialsProvidersAreAvailableThenConnectionFactoryUsesDefaultProvider() throws Exception { |
| 738 | + this.contextRunner.withUserConfiguration(MultipleCredentialsProvidersConfiguration.class) |
| 739 | + .run((context) -> assertThat(getTargetConnectionFactory(context).params(null).getCredentialsProvider()) |
| 740 | + .isInstanceOf(DefaultCredentialsProvider.class)); |
| 741 | + } |
| 742 | + |
| 743 | + @Test |
| 744 | + void whenACredentialsRefreshServiceIsAvailableThenConnectionFactoryIsConfiguredToUseIt() throws Exception { |
| 745 | + this.contextRunner.withUserConfiguration(CredentialsRefreshServiceConfiguration.class).run( |
| 746 | + (context) -> assertThat(getTargetConnectionFactory(context).params(null).getCredentialsRefreshService()) |
| 747 | + .isEqualTo(CredentialsRefreshServiceConfiguration.credentialsRefreshService)); |
| 748 | + } |
| 749 | + |
| 750 | + @Test |
| 751 | + void whenAPrimaryCredentialsRefreshServiceIsAvailableThenConnectionFactoryIsConfiguredToUseIt() throws Exception { |
| 752 | + this.contextRunner.withUserConfiguration(PrimaryCredentialsRefreshServiceConfiguration.class).run( |
| 753 | + (context) -> assertThat(getTargetConnectionFactory(context).params(null).getCredentialsRefreshService()) |
| 754 | + .isEqualTo(PrimaryCredentialsRefreshServiceConfiguration.credentialsRefreshService)); |
| 755 | + } |
| 756 | + |
| 757 | + @Test |
| 758 | + void whenMultipleCredentialsRefreshServiceAreAvailableThenConnectionFactoryHasNoCredentialsRefreshService() |
| 759 | + throws Exception { |
| 760 | + this.contextRunner.withUserConfiguration(MultipleCredentialsRefreshServicesConfiguration.class).run( |
| 761 | + (context) -> assertThat(getTargetConnectionFactory(context).params(null).getCredentialsRefreshService()) |
| 762 | + .isNull()); |
| 763 | + } |
| 764 | + |
719 | 765 | private TrustManager getTrustManager(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory) {
|
720 | 766 | SslContextFactory sslContextFactory = (SslContextFactory) ReflectionTestUtils.getField(rabbitConnectionFactory,
|
721 | 767 | "sslContextFactory");
|
@@ -873,4 +919,96 @@ static class NoEnableRabbitConfiguration {
|
873 | 919 |
|
874 | 920 | }
|
875 | 921 |
|
| 922 | + @Configuration(proxyBeanMethods = false) |
| 923 | + static class CredentialsProviderConfiguration { |
| 924 | + |
| 925 | + private static final CredentialsProvider credentialsProvider = mock(CredentialsProvider.class); |
| 926 | + |
| 927 | + @Bean |
| 928 | + CredentialsProvider credentialsProvider() { |
| 929 | + return credentialsProvider; |
| 930 | + } |
| 931 | + |
| 932 | + } |
| 933 | + |
| 934 | + @Configuration(proxyBeanMethods = false) |
| 935 | + static class PrimaryCredentialsProviderConfiguration { |
| 936 | + |
| 937 | + private static final CredentialsProvider credentialsProvider = mock(CredentialsProvider.class); |
| 938 | + |
| 939 | + @Bean |
| 940 | + @Primary |
| 941 | + CredentialsProvider credentialsProvider() { |
| 942 | + return credentialsProvider; |
| 943 | + } |
| 944 | + |
| 945 | + @Bean |
| 946 | + CredentialsProvider credentialsProvider1() { |
| 947 | + return mock(CredentialsProvider.class); |
| 948 | + } |
| 949 | + |
| 950 | + } |
| 951 | + |
| 952 | + @Configuration(proxyBeanMethods = false) |
| 953 | + static class MultipleCredentialsProvidersConfiguration { |
| 954 | + |
| 955 | + @Bean |
| 956 | + CredentialsProvider credentialsProvider1() { |
| 957 | + return mock(CredentialsProvider.class); |
| 958 | + } |
| 959 | + |
| 960 | + @Bean |
| 961 | + CredentialsProvider credentialsProvider2() { |
| 962 | + return mock(CredentialsProvider.class); |
| 963 | + } |
| 964 | + |
| 965 | + } |
| 966 | + |
| 967 | + @Configuration(proxyBeanMethods = false) |
| 968 | + static class CredentialsRefreshServiceConfiguration { |
| 969 | + |
| 970 | + private static final CredentialsRefreshService credentialsRefreshService = mock( |
| 971 | + CredentialsRefreshService.class); |
| 972 | + |
| 973 | + @Bean |
| 974 | + CredentialsRefreshService credentialsRefreshService() { |
| 975 | + return credentialsRefreshService; |
| 976 | + } |
| 977 | + |
| 978 | + } |
| 979 | + |
| 980 | + @Configuration(proxyBeanMethods = false) |
| 981 | + static class PrimaryCredentialsRefreshServiceConfiguration { |
| 982 | + |
| 983 | + private static final CredentialsRefreshService credentialsRefreshService = mock( |
| 984 | + CredentialsRefreshService.class); |
| 985 | + |
| 986 | + @Bean |
| 987 | + @Primary |
| 988 | + CredentialsRefreshService credentialsRefreshService1() { |
| 989 | + return credentialsRefreshService; |
| 990 | + } |
| 991 | + |
| 992 | + @Bean |
| 993 | + CredentialsRefreshService credentialsRefreshService2() { |
| 994 | + return mock(CredentialsRefreshService.class); |
| 995 | + } |
| 996 | + |
| 997 | + } |
| 998 | + |
| 999 | + @Configuration(proxyBeanMethods = false) |
| 1000 | + static class MultipleCredentialsRefreshServicesConfiguration { |
| 1001 | + |
| 1002 | + @Bean |
| 1003 | + CredentialsRefreshService credentialsRefreshService1() { |
| 1004 | + return mock(CredentialsRefreshService.class); |
| 1005 | + } |
| 1006 | + |
| 1007 | + @Bean |
| 1008 | + CredentialsRefreshService credentialsRefreshService2() { |
| 1009 | + return mock(CredentialsRefreshService.class); |
| 1010 | + } |
| 1011 | + |
| 1012 | + } |
| 1013 | + |
876 | 1014 | }
|
0 commit comments