33
44package com .azure .spring .cloud .autoconfigure .implementation .eventhubs ;
55
6+ import com .azure .core .credential .TokenCredential ;
67import com .azure .messaging .eventhubs .CheckpointStore ;
8+ import com .azure .spring .cloud .autoconfigure .implementation .context .AzureGlobalPropertiesAutoConfiguration ;
9+ import com .azure .spring .cloud .autoconfigure .implementation .context .AzureTokenCredentialAutoConfiguration ;
710import com .azure .spring .cloud .autoconfigure .implementation .eventhubs .configuration .TestCheckpointStore ;
11+ import com .azure .spring .cloud .autoconfigure .implementation .eventhubs .properties .AzureEventHubsProperties ;
12+ import com .azure .spring .cloud .core .credential .AzureCredentialResolver ;
813import com .azure .spring .messaging .eventhubs .core .EventHubsProcessorFactory ;
14+ import com .azure .spring .messaging .eventhubs .core .EventHubsProducerFactory ;
915import com .azure .spring .messaging .eventhubs .core .EventHubsTemplate ;
1016import com .azure .spring .messaging .eventhubs .implementation .support .converter .EventHubsMessageConverter ;
1117import com .fasterxml .jackson .databind .ObjectMapper ;
1420import org .springframework .boot .autoconfigure .jackson .JacksonAutoConfiguration ;
1521import org .springframework .boot .test .context .FilteredClassLoader ;
1622import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
23+ import org .springframework .context .annotation .Bean ;
24+ import org .springframework .context .annotation .Configuration ;
25+
26+ import java .lang .reflect .Field ;
1727
1828import static com .azure .spring .cloud .autoconfigure .implementation .eventhubs .EventHubsTestUtils .CONNECTION_STRING_FORMAT ;
1929import static org .assertj .core .api .Assertions .assertThat ;
2030import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
31+ import static org .mockito .Mockito .mock ;
2132
2233class AzureEventHubsMessagingAutoConfigurationTests {
2334
@@ -121,7 +132,7 @@ void withUserProvidedObjectMapper() {
121132 .withPropertyValues ("spring.cloud.azure.eventhubs.connection-string=" + String .format (CONNECTION_STRING_FORMAT , "test-namespace" ),
122133 "spring.cloud.azure.message-converter.isolated-object-mapper=false" )
123134 .withUserConfiguration (AzureEventHubsPropertiesTestConfiguration .class )
124- .withBean ("userObjectMapper" , ObjectMapper .class , () -> new ObjectMapper () )
135+ .withBean ("userObjectMapper" , ObjectMapper .class , ObjectMapper :: new )
125136 .withConfiguration (AutoConfigurations .of (JacksonAutoConfiguration .class ))
126137 .run (context -> {
127138 assertThat (context ).hasBean ("userObjectMapper" );
@@ -130,4 +141,61 @@ void withUserProvidedObjectMapper() {
130141 });
131142 }
132143
144+ @ Test
145+ void testCustomTokenCredentialConfiguration () {
146+ this .contextRunner
147+ .withConfiguration (AutoConfigurations .of (CustomTokenCredentialConfiguration .class ,
148+ AzureTokenCredentialAutoConfiguration .class ,
149+ AzureGlobalPropertiesAutoConfiguration .class ))
150+ .withBean (EventHubsMessageConverter .class , EventHubsMessageConverter ::new )
151+ .withPropertyValues (
152+ "spring.cloud.azure.eventhubs.connection-string=" + String .format (CONNECTION_STRING_FORMAT , "test-namespace" ),
153+ "spring.cloud.azure.eventhubs.credential.token-credential-bean-name=customTokenCredential"
154+ )
155+ .withUserConfiguration (AzureEventHubsPropertiesTestConfiguration .class )
156+ .run (context -> {
157+
158+ // Verify that the properties contain the correct credential bean name
159+ AzureEventHubsProperties eventHubsProperties = context .getBean (AzureEventHubsProperties .class );
160+ assertThat (eventHubsProperties ).isNotNull ();
161+ assertThat (eventHubsProperties .getCredential ()).isNotNull ();
162+ assertThat (eventHubsProperties .getCredential ().getTokenCredentialBeanName ())
163+ .as ("The token-credential-bean-name property should be set to customTokenCredential" )
164+ .isEqualTo ("customTokenCredential" );
165+
166+ // Verify that the custom token credential bean exists
167+ assertThat (context ).hasBean ("customTokenCredential" );
168+ TokenCredential customCredential = context .getBean ("customTokenCredential" , TokenCredential .class );
169+ assertThat (customCredential ).isNotNull ();
170+
171+ // Verify the EventHubsProducerFactory has the tokenCredentialResolver configured
172+ assertThat (context ).hasSingleBean (EventHubsProducerFactory .class );
173+ EventHubsProducerFactory producerFactory = context .getBean (EventHubsProducerFactory .class );
174+ assertThat (producerFactory ).isNotNull ();
175+
176+ // Verify tokenCredentialResolver resolves to the custom credential
177+ Field tokenCredentialResolverField = producerFactory .getClass ().getDeclaredField ("tokenCredentialResolver" );
178+ tokenCredentialResolverField .setAccessible (true );
179+ Object tokenCredentialResolver = tokenCredentialResolverField .get (producerFactory );
180+ assertThat (tokenCredentialResolver ).as ("TokenCredentialResolver should be configured" ).isNotNull ();
181+
182+ // Cast to AzureCredentialResolver and invoke resolve() to verify it returns customTokenCredential
183+ @ SuppressWarnings ("unchecked" )
184+ AzureCredentialResolver <TokenCredential > resolver =
185+ (AzureCredentialResolver <TokenCredential >) tokenCredentialResolver ;
186+ TokenCredential resolvedCredential = resolver .resolve (eventHubsProperties );
187+ assertThat (resolvedCredential )
188+ .as ("The resolved credential should be the customTokenCredential bean" )
189+ .isSameAs (customCredential );
190+ });
191+ }
192+
193+ @ Configuration
194+ public static class CustomTokenCredentialConfiguration {
195+ @ Bean
196+ public TokenCredential customTokenCredential () {
197+ return mock (TokenCredential .class );
198+ }
199+ }
200+
133201}
0 commit comments