diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/appconfiguration/AzureAppConfigurationAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/appconfiguration/AzureAppConfigurationAutoConfiguration.java index 2871d7708faa3..ba4d8a7704ace 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/appconfiguration/AzureAppConfigurationAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/appconfiguration/AzureAppConfigurationAutoConfiguration.java @@ -11,7 +11,12 @@ import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; import com.azure.spring.core.AzureSpringIdentifier; +import com.azure.spring.core.connectionstring.ConnectionStringProvider; +import com.azure.spring.core.connectionstring.StaticConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; +import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.appconfiguration.ConfigurationClientBuilderFactory; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -51,13 +56,28 @@ public ConfigurationAsyncClient azureConfigurationAsyncClient(ConfigurationClien @Bean @ConditionalOnMissingBean public ConfigurationClientBuilder configurationClientBuilder(ConfigurationClientBuilderFactory factory) { - factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_APP_CONFIG); return factory.build(); } @Bean @ConditionalOnMissingBean - public ConfigurationClientBuilderFactory configurationClientBuilderFactory(AzureAppConfigurationProperties properties) { - return new ConfigurationClientBuilderFactory(properties); + public ConfigurationClientBuilderFactory configurationClientBuilderFactory( + AzureAppConfigurationProperties properties, + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { + ConfigurationClientBuilderFactory factory = new ConfigurationClientBuilderFactory(properties); + + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_APP_CONFIG); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; + } + + @Bean + @ConditionalOnProperty("spring.cloud.azure.appconfiguration.connection-string") + public StaticConnectionStringProvider staticAppConfigurationConnectionStringProvider( + AzureAppConfigurationProperties properties) { + + return new StaticConnectionStringProvider<>(AzureServiceType.APP_CONFIGURATION, properties.getConnectionString()); } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/context/AzureDefaultTokenCredentialAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/context/AzureDefaultTokenCredentialAutoConfiguration.java index 00ef6beb97ba8..603a94b585ef0 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/context/AzureDefaultTokenCredentialAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/context/AzureDefaultTokenCredentialAutoConfiguration.java @@ -6,6 +6,7 @@ import com.azure.core.credential.TokenCredential; import com.azure.identity.DefaultAzureCredentialBuilder; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; import com.azure.spring.core.factory.AbstractAzureServiceClientBuilderFactory; import com.azure.spring.core.factory.credential.AbstractAzureCredentialBuilderFactory; import com.azure.spring.core.factory.credential.DefaultAzureCredentialBuilderFactory; @@ -20,8 +21,6 @@ import org.springframework.core.annotation.Order; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -import java.util.concurrent.ThreadPoolExecutor; - import static com.azure.spring.cloud.autoconfigure.context.AzureContextUtils.DEFAULT_TOKEN_CREDENTIAL_BEAN_NAME; /** @@ -35,6 +34,7 @@ public class AzureDefaultTokenCredentialAutoConfiguration { public AzureDefaultTokenCredentialAutoConfiguration(AzureGlobalProperties azureGlobalProperties) { this.azureGlobalProperties = azureGlobalProperties; } + @ConditionalOnMissingBean(name = DEFAULT_TOKEN_CREDENTIAL_BEAN_NAME) @Bean(name = DEFAULT_TOKEN_CREDENTIAL_BEAN_NAME) @Order @@ -45,12 +45,14 @@ public TokenCredential tokenCredential(AbstractAzureCredentialBuilderFactory azureCredentialBuilderFactory( + ObjectProvider> customizers, ObjectProvider threadPoolTaskExecutors) { - ThreadPoolExecutor threadPoolExecutor = null; - if (threadPoolTaskExecutors.getIfAvailable() != null) { - threadPoolExecutor = threadPoolTaskExecutors.getObject().getThreadPoolExecutor(); - } - return new DefaultAzureCredentialBuilderFactory(azureGlobalProperties, threadPoolExecutor); + DefaultAzureCredentialBuilderFactory factory = new DefaultAzureCredentialBuilderFactory(azureGlobalProperties); + + threadPoolTaskExecutors.ifAvailable(tpe -> factory.setExecutorService(tpe.getThreadPoolExecutor())); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + + return factory; } @Bean diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/cosmos/AzureCosmosAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/cosmos/AzureCosmosAutoConfiguration.java index 60fe87fc48d0c..7ea92362c6b0d 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/cosmos/AzureCosmosAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/cosmos/AzureCosmosAutoConfiguration.java @@ -10,7 +10,9 @@ import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty; import com.azure.spring.cloud.autoconfigure.cosmos.properties.AzureCosmosProperties; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; import com.azure.spring.service.cosmos.CosmosClientBuilderFactory; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -56,8 +58,12 @@ public CosmosClientBuilder cosmosClientBuilder(CosmosClientBuilderFactory factor @Bean @ConditionalOnMissingBean - public CosmosClientBuilderFactory cosmosClientBuilderFactory(AzureCosmosProperties properties) { - return new CosmosClientBuilderFactory(properties); + public CosmosClientBuilderFactory cosmosClientBuilderFactory(AzureCosmosProperties properties, + ObjectProvider> customizers) { + CosmosClientBuilderFactory factory = new CosmosClientBuilderFactory(properties); + + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureBlobCheckpointStoreConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureBlobCheckpointStoreConfiguration.java index b45a17335f861..31a9e9a6eb7e6 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureBlobCheckpointStoreConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureBlobCheckpointStoreConfiguration.java @@ -6,8 +6,11 @@ import com.azure.messaging.eventhubs.EventHubClientBuilder; import com.azure.messaging.eventhubs.checkpointstore.blob.BlobCheckpointStore; import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubsProperties; +import com.azure.spring.core.AzureSpringIdentifier; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; import com.azure.spring.service.storage.blob.BlobServiceClientBuilderFactory; import com.azure.storage.blob.BlobContainerAsyncClient; +import com.azure.storage.blob.BlobServiceClientBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; @@ -46,7 +49,7 @@ public BlobCheckpointStore blobCheckpointStore( .buildAsyncClient() .getBlobContainerAsyncClient(eventHubsProperties.getProcessor().getCheckpointStore().getContainerName()); - initializers.ifAvailable(initializer -> initializer.init(blobContainerAsyncClient)); + initializers.orderedStream().forEach(i -> i.init(blobContainerAsyncClient)); return new BlobCheckpointStore(blobContainerAsyncClient); } @@ -66,8 +69,14 @@ public BlobCheckpointStoreContainerInitializer blobCheckpointStoreContainerIniti @Bean(EVENT_HUB_PROCESSOR_CHECKPOINT_STORE_STORAGE_CLIENT_BUILDER_FACTORY_BEAN_NAME) @ConditionalOnMissingBean(name = EVENT_HUB_PROCESSOR_CHECKPOINT_STORE_STORAGE_CLIENT_BUILDER_FACTORY_BEAN_NAME) public BlobServiceClientBuilderFactory eventHubProcessorBlobServiceClientBuilderFactory( - AzureEventHubsProperties eventHubsProperties) { - return new BlobServiceClientBuilderFactory(getCheckpointStoreProperties(eventHubsProperties)); + AzureEventHubsProperties eventHubsProperties, + ObjectProvider> customizers) { + BlobServiceClientBuilderFactory factory = + new BlobServiceClientBuilderFactory(getCheckpointStoreProperties(eventHubsProperties)); + + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } private AzureEventHubsProperties.Processor.BlobCheckpointStore getCheckpointStoreProperties( diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsClientBuilderConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsClientBuilderConfiguration.java index 3616c316c7053..8adf3b67ae201 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsClientBuilderConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsClientBuilderConfiguration.java @@ -8,6 +8,7 @@ import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubsProperties; import com.azure.spring.core.AzureSpringIdentifier; import com.azure.spring.core.connectionstring.ConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.eventhubs.factory.EventHubClientBuilderFactory; import org.springframework.beans.factory.ObjectProvider; @@ -34,12 +35,14 @@ public EventHubClientBuilder eventHubClientBuilder(EventHubClientBuilderFactory @Bean @ConditionalOnMissingBean public EventHubClientBuilderFactory eventHubClientBuilderFactory(AzureEventHubsProperties properties, - ObjectProvider> connectionStringProviders) { - final EventHubClientBuilderFactory builderFactory = new EventHubClientBuilderFactory(properties); + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { + final EventHubClientBuilderFactory factory = new EventHubClientBuilderFactory(properties); - builderFactory.setConnectionStringProvider(connectionStringProviders.getIfAvailable()); - builderFactory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS); - return builderFactory; + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsConsumerClientConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsConsumerClientConfiguration.java index 684bc6b3b35cd..fc636268c0b79 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsConsumerClientConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsConsumerClientConfiguration.java @@ -10,10 +10,12 @@ import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnMissingProperty; import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubsProperties; import com.azure.spring.core.AzureSpringIdentifier; -import com.azure.spring.core.connectionstring.StaticConnectionStringProvider; +import com.azure.spring.core.connectionstring.ConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; import com.azure.spring.core.properties.util.PropertyMapper; import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.eventhubs.factory.EventHubClientBuilderFactory; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -79,14 +81,16 @@ static class DedicatedConsumerConnectionConfiguration { @Bean(EVENT_HUB_CONSUMER_CLIENT_BUILDER_FACTORY_BEAN_NAME) @ConditionalOnMissingBean(name = EVENT_HUB_CONSUMER_CLIENT_BUILDER_FACTORY_BEAN_NAME) - public EventHubClientBuilderFactory eventHubClientBuilderFactoryForConsumer() { + public EventHubClientBuilderFactory eventHubClientBuilderFactoryForConsumer( + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { - final EventHubClientBuilderFactory builderFactory = new EventHubClientBuilderFactory(this.consumerProperties); + final EventHubClientBuilderFactory factory = new EventHubClientBuilderFactory(this.consumerProperties); - builderFactory.setConnectionStringProvider(new StaticConnectionStringProvider<>(AzureServiceType.EVENT_HUBS, - this.consumerProperties.getConnectionString())); - builderFactory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS); - return builderFactory; + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } @Bean(EVENT_HUB_CONSUMER_CLIENT_BUILDER_BEAN_NAME) diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProcessorClientConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProcessorClientConfiguration.java index 25be6369861d2..7fd2571ed2654 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProcessorClientConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProcessorClientConfiguration.java @@ -8,8 +8,9 @@ import com.azure.messaging.eventhubs.EventProcessorClientBuilder; import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty; import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubsProperties; -import com.azure.spring.cloud.resourcemanager.connectionstring.AbstractArmConnectionStringProvider; -import com.azure.spring.core.connectionstring.StaticConnectionStringProvider; +import com.azure.spring.core.AzureSpringIdentifier; +import com.azure.spring.core.connectionstring.ConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.eventhubs.factory.EventProcessorClientBuilderFactory; import com.azure.spring.service.eventhubs.processor.EventProcessingListener; @@ -22,9 +23,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.util.StringUtils; - -import static com.azure.spring.core.service.AzureServiceType.EVENT_HUBS; /** * Configures a {@link EventProcessorClient}. @@ -52,16 +50,14 @@ public EventProcessorClient eventProcessorClient(EventProcessorClientBuilder bui @ConditionalOnMissingBean public EventProcessorClientBuilderFactory eventProcessorClientBuilderFactory( CheckpointStore checkpointStore, EventProcessingListener listener, - ObjectProvider> connectionStringProviders) { + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { final EventProcessorClientBuilderFactory factory = new EventProcessorClientBuilderFactory(this.processorProperties, checkpointStore, listener); - if (StringUtils.hasText(this.processorProperties.getConnectionString())) { - factory.setConnectionStringProvider( - new StaticConnectionStringProvider<>(EVENT_HUBS, this.processorProperties.getConnectionString())); - } else { - factory.setConnectionStringProvider(connectionStringProviders.getIfAvailable()); - } + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); return factory; } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProducerClientConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProducerClientConfiguration.java index 515bb130edaa4..e7d621b7c085f 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProducerClientConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProducerClientConfiguration.java @@ -10,9 +10,11 @@ import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnMissingProperty; import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubsProperties; import com.azure.spring.core.AzureSpringIdentifier; -import com.azure.spring.core.connectionstring.StaticConnectionStringProvider; +import com.azure.spring.core.connectionstring.ConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.eventhubs.factory.EventHubClientBuilderFactory; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -61,14 +63,16 @@ static class DedicatedProducerConnectionConfiguration { @Bean(EVENT_HUB_PRODUCER_CLIENT_BUILDER_FACTORY_BEAN_NAME) @ConditionalOnMissingBean(name = EVENT_HUB_PRODUCER_CLIENT_BUILDER_FACTORY_BEAN_NAME) - public EventHubClientBuilderFactory eventHubClientBuilderFactoryForProducer() { + public EventHubClientBuilderFactory eventHubClientBuilderFactoryForProducer( + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { - final EventHubClientBuilderFactory builderFactory = new EventHubClientBuilderFactory(this.producerProperties); + final EventHubClientBuilderFactory factory = new EventHubClientBuilderFactory(this.producerProperties); - builderFactory.setConnectionStringProvider(new StaticConnectionStringProvider<>(AzureServiceType.EVENT_HUBS, - this.producerProperties.getConnectionString())); - builderFactory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS); - return builderFactory; + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } @Bean(EVENT_HUB_PRODUCER_CLIENT_BUILDER_BEAN_NAME) diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/keyvault/certificates/AzureKeyVaultCertificateAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/keyvault/certificates/AzureKeyVaultCertificateAutoConfiguration.java index c11ec58d33866..b2a27ae96c5ad 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/keyvault/certificates/AzureKeyVaultCertificateAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/keyvault/certificates/AzureKeyVaultCertificateAutoConfiguration.java @@ -10,7 +10,9 @@ import com.azure.spring.cloud.autoconfigure.keyvault.certificates.properties.AzureKeyVaultCertificateProperties; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; import com.azure.spring.core.AzureSpringIdentifier; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; import com.azure.spring.service.keyvault.certificates.CertificateClientBuilderFactory; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -52,14 +54,19 @@ public CertificateAsyncClient azureKeyVaultCertificateAsyncClient(CertificateCli @Bean @ConditionalOnMissingBean public CertificateClientBuilder certificateClientBuilder(CertificateClientBuilderFactory factory) { - factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_KEY_VAULT); return factory.build(); } @Bean @ConditionalOnMissingBean - public CertificateClientBuilderFactory certificateClientBuilderFactory(AzureKeyVaultCertificateProperties properties) { - return new CertificateClientBuilderFactory(properties); + public CertificateClientBuilderFactory certificateClientBuilderFactory( + AzureKeyVaultCertificateProperties properties, + ObjectProvider> customizers) { + CertificateClientBuilderFactory factory = new CertificateClientBuilderFactory(properties); + + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_KEY_VAULT); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/keyvault/secrets/AzureKeyVaultSecretAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/keyvault/secrets/AzureKeyVaultSecretAutoConfiguration.java index 8069c4324b582..dfd9130445596 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/keyvault/secrets/AzureKeyVaultSecretAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/keyvault/secrets/AzureKeyVaultSecretAutoConfiguration.java @@ -11,7 +11,9 @@ import com.azure.spring.cloud.autoconfigure.keyvault.secrets.properties.AzureKeyVaultSecretProperties; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; import com.azure.spring.core.AzureSpringIdentifier; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; import com.azure.spring.service.keyvault.secrets.SecretClientBuilderFactory; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -52,14 +54,19 @@ public SecretAsyncClient azureKeyVaultSecretAsyncClient(SecretClientBuilder buil @Bean @ConditionalOnMissingBean public SecretClientBuilder secretClientBuilder(SecretClientBuilderFactory factory) { - factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_KEY_VAULT); return factory.build(); } @Bean @ConditionalOnMissingBean - public SecretClientBuilderFactory secretClientBuilderFactory(AzureKeyVaultSecretProperties properties) { - return new SecretClientBuilderFactory(properties); + public SecretClientBuilderFactory secretClientBuilderFactory( + AzureKeyVaultSecretProperties properties, + ObjectProvider> customizers) { + SecretClientBuilderFactory factory = new SecretClientBuilderFactory(properties); + + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_KEY_VAULT); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusClientBuilderConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusClientBuilderConfiguration.java index 1a99952997654..53599715be16c 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusClientBuilderConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusClientBuilderConfiguration.java @@ -9,6 +9,7 @@ import com.azure.spring.core.AzureSpringIdentifier; import com.azure.spring.core.connectionstring.ConnectionStringProvider; import com.azure.spring.core.connectionstring.StaticConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.servicebus.factory.ServiceBusClientBuilderFactory; import org.springframework.beans.factory.ObjectProvider; @@ -32,11 +33,14 @@ class AzureServiceBusClientBuilderConfiguration { @Bean @ConditionalOnMissingBean public ServiceBusClientBuilderFactory serviceBusClientBuilderFactory( - ObjectProvider> connectionStringProviders) { + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { final ServiceBusClientBuilderFactory factory = new ServiceBusClientBuilderFactory(this.serviceBusProperties); - factory.setConnectionStringProvider(connectionStringProviders.getIfAvailable()); + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_SERVICE_BUS); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); return factory; } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusConsumerClientConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusConsumerClientConfiguration.java index 430be10a30cdf..e92a6b7979385 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusConsumerClientConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusConsumerClientConfiguration.java @@ -11,6 +11,9 @@ import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty; import com.azure.spring.cloud.autoconfigure.servicebus.properties.AzureServiceBusProperties; import com.azure.spring.core.AzureSpringIdentifier; +import com.azure.spring.core.connectionstring.ConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; +import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.servicebus.factory.ServiceBusReceiverClientBuilderFactory; import com.azure.spring.service.servicebus.factory.ServiceBusSessionReceiverClientBuilderFactory; import org.springframework.beans.factory.ObjectProvider; @@ -43,17 +46,21 @@ static class NoneSessionConsumerClientConfiguration { @ConditionalOnMissingBean public ServiceBusReceiverClientBuilderFactory serviceBusReceiverClientBuilderFactory( AzureServiceBusProperties serviceBusProperties, - ObjectProvider serviceBusClientBuilders) { + ObjectProvider serviceBusClientBuilders, + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { - ServiceBusReceiverClientBuilderFactory builderFactory; + ServiceBusReceiverClientBuilderFactory factory; if (isDedicatedConnection(serviceBusProperties.getConsumer())) { - builderFactory = new ServiceBusReceiverClientBuilderFactory(serviceBusProperties.buildConsumerProperties()); + factory = new ServiceBusReceiverClientBuilderFactory(serviceBusProperties.buildConsumerProperties()); } else { - builderFactory = new ServiceBusReceiverClientBuilderFactory( + factory = new ServiceBusReceiverClientBuilderFactory( serviceBusClientBuilders.getIfAvailable(), serviceBusProperties.buildConsumerProperties()); } - builderFactory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_SERVICE_BUS); - return builderFactory; + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_SERVICE_BUS); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } @Bean @@ -88,18 +95,21 @@ static class SessionConsumerClientConfiguration { @ConditionalOnMissingBean public ServiceBusSessionReceiverClientBuilderFactory serviceBusSessionReceiverClientBuilderFactory( AzureServiceBusProperties serviceBusProperties, - ObjectProvider serviceBusClientBuilders) { - - ServiceBusSessionReceiverClientBuilderFactory builderFactory; + ObjectProvider serviceBusClientBuilders, + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { + ServiceBusSessionReceiverClientBuilderFactory factory; if (isDedicatedConnection(serviceBusProperties.getConsumer())) { - builderFactory = new ServiceBusSessionReceiverClientBuilderFactory(serviceBusProperties.buildConsumerProperties()); + factory = new ServiceBusSessionReceiverClientBuilderFactory(serviceBusProperties.buildConsumerProperties()); } else { - builderFactory = new ServiceBusSessionReceiverClientBuilderFactory( + factory = new ServiceBusSessionReceiverClientBuilderFactory( serviceBusClientBuilders.getIfAvailable(), serviceBusProperties.buildConsumerProperties()); } - builderFactory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_SERVICE_BUS); - return builderFactory; + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_SERVICE_BUS); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } @Bean diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusMessagingAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusMessagingAutoConfiguration.java index 8f9e8b63af7a7..c39061e4e652d 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusMessagingAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusMessagingAutoConfiguration.java @@ -63,8 +63,9 @@ public static class ProcessorContainerConfiguration { @Bean @ConditionalOnMissingBean - public ServiceBusProcessorFactory defaultServiceBusNamespaceProcessorFactory(NamespaceProperties properties, - ObjectProvider, ProcessorProperties>> suppliers) { + public ServiceBusProcessorFactory defaultServiceBusNamespaceProcessorFactory( + NamespaceProperties properties, + ObjectProvider, ProcessorProperties>> suppliers) { return new DefaultServiceBusNamespaceProcessorFactory(properties, suppliers.getIfAvailable()); } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProcessorClientConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProcessorClientConfiguration.java index d053445f48158..e459c6555ca1a 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProcessorClientConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProcessorClientConfiguration.java @@ -8,6 +8,9 @@ import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty; import com.azure.spring.cloud.autoconfigure.servicebus.properties.AzureServiceBusProperties; import com.azure.spring.core.AzureSpringIdentifier; +import com.azure.spring.core.connectionstring.ConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; +import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.servicebus.factory.ServiceBusProcessorClientBuilderFactory; import com.azure.spring.service.servicebus.factory.ServiceBusSessionProcessorClientBuilderFactory; import com.azure.spring.service.servicebus.processor.MessageProcessingListener; @@ -42,18 +45,22 @@ static class NoneSessionProcessorClientConfiguration { @ConditionalOnMissingBean public ServiceBusProcessorClientBuilderFactory serviceBusProcessorClientBuilderFactory( AzureServiceBusProperties serviceBusProperties, + MessageProcessingListener listener, ObjectProvider serviceBusClientBuilders, - MessageProcessingListener listener) { + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { - ServiceBusProcessorClientBuilderFactory builderFactory; + ServiceBusProcessorClientBuilderFactory factory; if (isDedicatedConnection(serviceBusProperties.getProcessor())) { - builderFactory = new ServiceBusProcessorClientBuilderFactory(serviceBusProperties.buildProcessorProperties(), listener); + factory = new ServiceBusProcessorClientBuilderFactory(serviceBusProperties.buildProcessorProperties(), listener); } else { - builderFactory = new ServiceBusProcessorClientBuilderFactory( + factory = new ServiceBusProcessorClientBuilderFactory( serviceBusClientBuilders.getIfAvailable(), serviceBusProperties.buildProcessorProperties(), listener); } - builderFactory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_SERVICE_BUS); - return builderFactory; + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_SERVICE_BUS); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } @Bean @@ -81,19 +88,23 @@ static class SessionProcessorClientConfiguration { @ConditionalOnMissingBean public ServiceBusSessionProcessorClientBuilderFactory serviceBusSessionProcessorClientBuilderFactory( AzureServiceBusProperties serviceBusProperties, + MessageProcessingListener listener, ObjectProvider serviceBusClientBuilders, - MessageProcessingListener listener) { + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { - ServiceBusSessionProcessorClientBuilderFactory builderFactory; + ServiceBusSessionProcessorClientBuilderFactory factory; if (isDedicatedConnection(serviceBusProperties.getProcessor())) { - builderFactory = new ServiceBusSessionProcessorClientBuilderFactory( + factory = new ServiceBusSessionProcessorClientBuilderFactory( serviceBusProperties.buildProcessorProperties(), listener); } else { - builderFactory = new ServiceBusSessionProcessorClientBuilderFactory( + factory = new ServiceBusSessionProcessorClientBuilderFactory( serviceBusClientBuilders.getIfAvailable(), serviceBusProperties.buildProcessorProperties(), listener); } - builderFactory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_SERVICE_BUS); - return builderFactory; + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_SERVICE_BUS); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } @Bean diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProducerClientConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProducerClientConfiguration.java index f11c755cfabc2..228cf0493363d 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProducerClientConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProducerClientConfiguration.java @@ -9,6 +9,9 @@ import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty; import com.azure.spring.cloud.autoconfigure.servicebus.properties.AzureServiceBusProperties; import com.azure.spring.core.AzureSpringIdentifier; +import com.azure.spring.core.connectionstring.ConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; +import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.servicebus.factory.ServiceBusSenderClientBuilderFactory; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; @@ -29,17 +32,21 @@ class AzureServiceBusProducerClientConfiguration { @ConditionalOnAnyProperty(prefix = "spring.cloud.azure.servicebus", name = { "entity-type", "producer.entity-type" }) public ServiceBusSenderClientBuilderFactory serviceBusSenderClientBuilderFactory( AzureServiceBusProperties serviceBusProperties, - ObjectProvider serviceBusClientBuilders) { + ObjectProvider serviceBusClientBuilders, + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { - ServiceBusSenderClientBuilderFactory builderFactory; + ServiceBusSenderClientBuilderFactory factory; if (isDedicatedConnection(serviceBusProperties.getProducer())) { - builderFactory = new ServiceBusSenderClientBuilderFactory(serviceBusProperties.buildProducerProperties()); + factory = new ServiceBusSenderClientBuilderFactory(serviceBusProperties.buildProducerProperties()); } else { - builderFactory = new ServiceBusSenderClientBuilderFactory( + factory = new ServiceBusSenderClientBuilderFactory( serviceBusClientBuilders.getIfAvailable(), serviceBusProperties.buildProducerProperties()); } - builderFactory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_SERVICE_BUS); - return builderFactory; + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_SERVICE_BUS); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } @Bean diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/blob/AzureStorageBlobAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/blob/AzureStorageBlobAutoConfiguration.java index 122d8c7427d33..1c5a8173665f6 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/blob/AzureStorageBlobAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/blob/AzureStorageBlobAutoConfiguration.java @@ -8,6 +8,10 @@ import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; import com.azure.spring.cloud.autoconfigure.storage.blob.properties.AzureStorageBlobProperties; import com.azure.spring.core.AzureSpringIdentifier; +import com.azure.spring.core.connectionstring.ConnectionStringProvider; +import com.azure.spring.core.connectionstring.StaticConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; +import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.storage.blob.BlobServiceClientBuilderFactory; import com.azure.storage.blob.BlobAsyncClient; import com.azure.storage.blob.BlobClient; @@ -16,6 +20,7 @@ import com.azure.storage.blob.BlobServiceAsyncClient; import com.azure.storage.blob.BlobServiceClient; import com.azure.storage.blob.BlobServiceClientBuilder; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -89,16 +94,30 @@ public BlobServiceClient blobServiceClient(BlobServiceClientBuilder builder) { @Bean(STORAGE_BLOB_CLIENT_BUILDER_FACTORY_BEAN_NAME) @ConditionalOnMissingBean(name = STORAGE_BLOB_CLIENT_BUILDER_FACTORY_BEAN_NAME) - public BlobServiceClientBuilderFactory blobServiceClientBuilderFactory(AzureStorageBlobProperties properties) { - return new BlobServiceClientBuilderFactory(properties); + public BlobServiceClientBuilderFactory blobServiceClientBuilderFactory( + AzureStorageBlobProperties properties, + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { + BlobServiceClientBuilderFactory factory = new BlobServiceClientBuilderFactory(properties); + + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_STORAGE_BLOB); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } @Bean @ConditionalOnMissingBean public BlobServiceClientBuilder blobServiceClientBuilder(@Qualifier(STORAGE_BLOB_CLIENT_BUILDER_FACTORY_BEAN_NAME) BlobServiceClientBuilderFactory factory) { - factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_STORAGE_BLOB); return factory.build(); } + @Bean + @ConditionalOnProperty("spring.cloud.azure.storage.blob.connection-string") + public StaticConnectionStringProvider staticStorageBlobConnectionStringProvider( + AzureStorageBlobProperties properties) { + return new StaticConnectionStringProvider<>(AzureServiceType.STORAGE_BLOB, properties.getConnectionString()); + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/AzureStorageFileShareAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/AzureStorageFileShareAutoConfiguration.java index 5303d0b1f3f3e..e6c55c809ccdc 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/AzureStorageFileShareAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/AzureStorageFileShareAutoConfiguration.java @@ -8,6 +8,10 @@ import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; import com.azure.spring.cloud.autoconfigure.storage.fileshare.properties.AzureStorageFileShareProperties; import com.azure.spring.core.AzureSpringIdentifier; +import com.azure.spring.core.connectionstring.ConnectionStringProvider; +import com.azure.spring.core.connectionstring.StaticConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; +import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.storage.fileshare.ShareServiceClientBuilderFactory; import com.azure.storage.file.share.ShareAsyncClient; import com.azure.storage.file.share.ShareClient; @@ -16,6 +20,7 @@ import com.azure.storage.file.share.ShareServiceAsyncClient; import com.azure.storage.file.share.ShareServiceClient; import com.azure.storage.file.share.ShareServiceClientBuilder; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -54,14 +59,21 @@ public ShareServiceAsyncClient shareServiceAsyncClient(ShareServiceClientBuilder @Bean @ConditionalOnMissingBean - public ShareServiceClientBuilderFactory shareServiceClientBuilderFactory(AzureStorageFileShareProperties properties) { - return new ShareServiceClientBuilderFactory(properties); + public ShareServiceClientBuilderFactory shareServiceClientBuilderFactory( + AzureStorageFileShareProperties properties, + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { + ShareServiceClientBuilderFactory factory = new ShareServiceClientBuilderFactory(properties); + + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_STORAGE_FILES); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); + return factory; } @Bean @ConditionalOnMissingBean public ShareServiceClientBuilder shareServiceClientBuilder(ShareServiceClientBuilderFactory factory) { - factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_STORAGE_FILES); return factory.build(); } @@ -97,5 +109,12 @@ public ShareClient shareClient(AzureStorageFileShareProperties properties, return shareServiceClient.getShareClient(properties.getShareName()); } + @Bean + @ConditionalOnProperty("spring.cloud.azure.storage.fileshare.connection-string") + public StaticConnectionStringProvider staticStorageBlobConnectionStringProvider( + AzureStorageFileShareProperties properties) { + return new StaticConnectionStringProvider<>(AzureServiceType.STORAGE_FILE_SHARE, properties.getConnectionString()); + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/queue/AzureStorageQueueAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/queue/AzureStorageQueueAutoConfiguration.java index 48a053cf890fb..75e66fdbe8261 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/queue/AzureStorageQueueAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/queue/AzureStorageQueueAutoConfiguration.java @@ -10,6 +10,7 @@ import com.azure.spring.core.AzureSpringIdentifier; import com.azure.spring.core.connectionstring.ConnectionStringProvider; import com.azure.spring.core.connectionstring.StaticConnectionStringProvider; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.storage.queue.QueueServiceClientBuilderFactory; import com.azure.storage.queue.QueueServiceAsyncClient; @@ -54,17 +55,22 @@ public QueueServiceAsyncClient queueServiceAsyncClient(QueueServiceClientBuilder @Bean @ConditionalOnMissingBean - public QueueServiceClientBuilderFactory queueServiceClientBuilderFactory(AzureStorageQueueProperties properties, - ObjectProvider> connectionStringProviders) { + public QueueServiceClientBuilderFactory queueServiceClientBuilderFactory( + AzureStorageQueueProperties properties, + ObjectProvider> connectionStringProviders, + ObjectProvider> customizers) { + final QueueServiceClientBuilderFactory factory = new QueueServiceClientBuilderFactory(properties); - factory.setConnectionStringProvider(connectionStringProviders.getIfAvailable()); + + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_STORAGE_QUEUE); + connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider); + customizers.orderedStream().forEach(factory::addBuilderCustomizer); return factory; } @Bean @ConditionalOnMissingBean public QueueServiceClientBuilder queueServiceClientBuilder(QueueServiceClientBuilderFactory factory) { - factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_STORAGE_QUEUE); return factory.build(); } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/TestBuilderCustomizer.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/TestBuilderCustomizer.java new file mode 100644 index 0000000000000..1362923c40703 --- /dev/null +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/TestBuilderCustomizer.java @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.cloud.autoconfigure; + +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; + +public class TestBuilderCustomizer implements AzureServiceClientBuilderCustomizer { + + private int customizedTimes = 0; + + @Override + public void customize(T builder) { + customizedTimes++; + } + + public int getCustomizedTimes() { + return customizedTimes; + } + +} diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/appconfiguration/AzureAppConfigurationAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/appconfiguration/AzureAppConfigurationAutoConfigurationTest.java index 0870d29e8feaa..3c3ecb448e33c 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/appconfiguration/AzureAppConfigurationAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/appconfiguration/AzureAppConfigurationAutoConfigurationTest.java @@ -6,6 +6,8 @@ import com.azure.data.appconfiguration.ConfigurationAsyncClient; import com.azure.data.appconfiguration.ConfigurationClient; import com.azure.data.appconfiguration.ConfigurationClientBuilder; +import com.azure.messaging.eventhubs.EventHubClientBuilder; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.cloud.autoconfigure.appconfiguration.properties.AzureAppConfigurationProperties; import com.azure.spring.cloud.autoconfigure.keyvault.secrets.AzureKeyVaultSecretAutoConfiguration; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; @@ -66,4 +68,38 @@ void withEndpointShouldConfigure() { }); } + @Test + void customizerShouldBeCalled() { + AppConfigurationBuilderCustomizer customizer = new AppConfigurationBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.appconfiguration.endpoint=" + String.format(ENDPOINT, "my-appconfig")) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", AppConfigurationBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", AppConfigurationBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + AppConfigurationBuilderCustomizer customizer = new AppConfigurationBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.appconfiguration.endpoint=" + String.format(ENDPOINT, "my-appconfig")) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", AppConfigurationBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", AppConfigurationBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class AppConfigurationBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/context/AzureDefaultTokenCredentialAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/context/AzureDefaultTokenCredentialAutoConfigurationTest.java index 1d0d3c46fa82c..4d2bba02c380d 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/context/AzureDefaultTokenCredentialAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/context/AzureDefaultTokenCredentialAutoConfigurationTest.java @@ -5,6 +5,9 @@ import com.azure.core.credential.TokenCredential; import com.azure.identity.DefaultAzureCredential; +import com.azure.identity.DefaultAzureCredentialBuilder; +import com.azure.messaging.eventhubs.EventHubClientBuilder; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; import com.azure.spring.core.factory.credential.AbstractAzureCredentialBuilderFactory; import org.junit.jupiter.api.Assertions; @@ -36,4 +39,37 @@ void byDefaultShouldConfigure() { }); } + @Test + void customizerShouldBeCalled() { + DefaultTokenCredentialBuilderCustomizer customizer = new DefaultTokenCredentialBuilderCustomizer(); + this.contextRunner + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", DefaultTokenCredentialBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", DefaultTokenCredentialBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + DefaultTokenCredentialBuilderCustomizer customizer = new DefaultTokenCredentialBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", DefaultTokenCredentialBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", DefaultTokenCredentialBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class DefaultTokenCredentialBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/cosmos/AzureCosmosAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/cosmos/AzureCosmosAutoConfigurationTest.java index 15f6a9f7740d6..a7ed965068e80 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/cosmos/AzureCosmosAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/cosmos/AzureCosmosAutoConfigurationTest.java @@ -6,6 +6,8 @@ import com.azure.cosmos.CosmosAsyncClient; import com.azure.cosmos.CosmosClient; import com.azure.cosmos.CosmosClientBuilder; +import com.azure.messaging.eventhubs.EventHubClientBuilder; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.cloud.autoconfigure.cosmos.properties.AzureCosmosProperties; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; import com.azure.spring.service.cosmos.CosmosClientBuilderFactory; @@ -78,11 +80,12 @@ void configureAzureCosmosProperties() { this.contextRunner .withBean(AzureGlobalProperties.class, () -> azureProperties) .withBean(CosmosClientBuilder.class, () -> mock(CosmosClientBuilder.class)) - .withPropertyValues("spring.cloud.azure.cosmos.credential.client-id=cosmos-client-id", - "spring.cloud.azure.cosmos.proxy.nonProxyHosts=127.0.0.1", - "spring.cloud.azure.cosmos.endpoint=" + TEST_ENDPOINT_HTTPS, - "spring.cloud.azure.cosmos.key=cosmos-key" - ) + .withPropertyValues( + "spring.cloud.azure.cosmos.credential.client-id=cosmos-client-id", + "spring.cloud.azure.cosmos.proxy.nonProxyHosts=127.0.0.1", + "spring.cloud.azure.cosmos.endpoint=" + TEST_ENDPOINT_HTTPS, + "spring.cloud.azure.cosmos.key=cosmos-key" + ) .run(context -> { assertThat(context).hasSingleBean(AzureCosmosProperties.class); final AzureCosmosProperties properties = context.getBean(AzureCosmosProperties.class); @@ -97,4 +100,45 @@ void configureAzureCosmosProperties() { }); } + @Test + void customizerShouldBeCalled() { + CosmosBuilderCustomizer customizer = new CosmosBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.cosmos.endpoint=" + TEST_ENDPOINT_HTTPS, + "spring.cloud.azure.cosmos.key=cosmos-key" + ) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", CosmosBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", CosmosBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + CosmosBuilderCustomizer customizer = new CosmosBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.cosmos.endpoint=" + TEST_ENDPOINT_HTTPS, + "spring.cloud.azure.cosmos.key=cosmos-key" + ) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", CosmosBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", CosmosBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class CosmosBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureBlobCheckpointStoreConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureBlobCheckpointStoreConfigurationTest.java index 67c69522e50dd..c33fff869d27c 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureBlobCheckpointStoreConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureBlobCheckpointStoreConfigurationTest.java @@ -3,7 +3,9 @@ package com.azure.spring.cloud.autoconfigure.eventhubs; +import com.azure.messaging.eventhubs.EventHubClientBuilder; import com.azure.messaging.eventhubs.checkpointstore.blob.BlobCheckpointStore; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.cloud.autoconfigure.context.AzureContextUtils; import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubsProperties; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; @@ -94,4 +96,45 @@ void shouldWorkWithStorageClientConfiguration() { }, "There should be two beans of type BlobServiceClientBuilderFactory")); }); } + + @Test + void customizerShouldBeCalled() { + StorageBlobBuilderCustomizer customizer = new StorageBlobBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.eventhubs.processor.checkpoint-store.container-name=abc", + "spring.cloud.azure.eventhubs.processor.checkpoint-store.account-name=sa" + ) + .withBean(AzureEventHubsProperties.class, AzureEventHubsProperties::new) + .withBean("customizer1", StorageBlobBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", StorageBlobBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + StorageBlobBuilderCustomizer customizer = new StorageBlobBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.eventhubs.processor.checkpoint-store.container-name=abc", + "spring.cloud.azure.eventhubs.processor.checkpoint-store.account-name=sa" + ) + .withBean(AzureEventHubsProperties.class, AzureEventHubsProperties::new) + .withBean("customizer1", StorageBlobBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", StorageBlobBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class StorageBlobBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsClientBuilderConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsClientBuilderConfigurationTest.java index bbbdf6fafc058..66e1518ee3753 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsClientBuilderConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsClientBuilderConfigurationTest.java @@ -3,7 +3,9 @@ package com.azure.spring.cloud.autoconfigure.eventhubs; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.messaging.eventhubs.EventHubClientBuilder; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.service.eventhubs.factory.EventHubClientBuilderFactory; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -38,4 +40,45 @@ void connectionStringProvidedShouldConfigure() { }); } + @Test + void customizerShouldBeCalled() { + EventHubBuilderCustomizer customizer = new EventHubBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.eventhubs.connection-string=" + String.format(CONNECTION_STRING_FORMAT, "test-namespace"), + "spring.cloud.azure.eventhubs.event-hub-name=test-event-hub" + ) + .withUserConfiguration(AzureEventHubsPropertiesTestConfiguration.class) + .withBean("customizer1", EventHubBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", EventHubBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + EventHubBuilderCustomizer customizer = new EventHubBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.eventhubs.connection-string=" + String.format(CONNECTION_STRING_FORMAT, "test-namespace"), + "spring.cloud.azure.eventhubs.event-hub-name=test-event-hub" + ) + .withUserConfiguration(AzureEventHubsPropertiesTestConfiguration.class) + .withBean("customizer1", EventHubBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", EventHubBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class EventHubBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsConsumerClientConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsConsumerClientConfigurationTest.java index 3358b051997ba..cb9b7fd8e4567 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsConsumerClientConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsConsumerClientConfigurationTest.java @@ -3,9 +3,11 @@ package com.azure.spring.cloud.autoconfigure.eventhubs; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.messaging.eventhubs.EventHubClientBuilder; import com.azure.messaging.eventhubs.EventHubConsumerAsyncClient; import com.azure.messaging.eventhubs.EventHubConsumerClient; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.cloud.autoconfigure.context.AzureContextUtils; import com.azure.spring.service.eventhubs.factory.EventHubClientBuilderFactory; import org.junit.jupiter.api.Test; @@ -88,12 +90,12 @@ void withDedicatedEvenHubConnectionSetShouldConfigureDedicated() { "spring.cloud.azure.eventhubs.namespace=test-namespace" ) .withUserConfiguration(AzureEventHubsPropertiesTestConfiguration.class) - .withBean(EventHubClientBuilder.class, EventHubClientBuilder::new) .run( context -> { assertThat(context).doesNotHaveBean(AzureEventHubsConsumerClientConfiguration.SharedConsumerConnectionConfiguration.class); assertThat(context).hasSingleBean(AzureEventHubsConsumerClientConfiguration.DedicatedConsumerConnectionConfiguration.class); assertThat(context).hasSingleBean(EventHubClientBuilderFactory.class); + assertThat(context).hasSingleBean(EventHubClientBuilder.class); assertThat(context).hasSingleBean(EventHubConsumerAsyncClient.class); assertThat(context).hasSingleBean(EventHubConsumerClient.class); assertThat(context).hasBean(AzureContextUtils.EVENT_HUB_CONSUMER_CLIENT_BUILDER_FACTORY_BEAN_NAME); @@ -102,4 +104,49 @@ void withDedicatedEvenHubConnectionSetShouldConfigureDedicated() { ); } + @Test + void customizerShouldBeCalled() { + EventHubBuilderCustomizer customizer = new EventHubBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.eventhubs.consumer.namespace=test-namespace", + "spring.cloud.azure.eventhubs.consumer.event-hub-name=test-event-hub", + "spring.cloud.azure.eventhubs.consumer.consumer-group=test-consumer-group", + "spring.cloud.azure.eventhubs.namespace=test-namespace" + ) + .withUserConfiguration(AzureEventHubsPropertiesTestConfiguration.class) + .withBean("customizer1", EventHubBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", EventHubBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + EventHubBuilderCustomizer customizer = new EventHubBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.eventhubs.consumer.namespace=test-namespace", + "spring.cloud.azure.eventhubs.consumer.event-hub-name=test-event-hub", + "spring.cloud.azure.eventhubs.consumer.consumer-group=test-consumer-group", + "spring.cloud.azure.eventhubs.namespace=test-namespace" + ) + .withUserConfiguration(AzureEventHubsPropertiesTestConfiguration.class) + .withBean("customizer1", EventHubBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", EventHubBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class EventHubBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProcessorClientConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProcessorClientConfigurationTest.java index 1f60e1e3871e2..cd81e2ddc8d94 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProcessorClientConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProcessorClientConfigurationTest.java @@ -3,10 +3,12 @@ package com.azure.spring.cloud.autoconfigure.eventhubs; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.messaging.eventhubs.CheckpointStore; import com.azure.messaging.eventhubs.EventProcessorClient; import com.azure.messaging.eventhubs.EventProcessorClientBuilder; import com.azure.messaging.eventhubs.models.EventContext; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.service.eventhubs.factory.EventProcessorClientBuilderFactory; import com.azure.spring.service.eventhubs.processor.EventProcessingListener; import com.azure.spring.service.eventhubs.processor.RecordEventProcessingListener; @@ -54,6 +56,53 @@ void eventHubNameAndConsumerGroupProvidedShouldConfigure() { }); } + @Test + void customizerShouldBeCalled() { + EventProcessorBuilderCustomizer customizer = new EventProcessorBuilderCustomizer(); + this.contextRunner + .withBean(EventProcessingListener.class, TestEventProcessorListener::new) + .withBean(CheckpointStore.class, TestCheckpointStore::new) + .withUserConfiguration(AzureEventHubsPropertiesTestConfiguration.class) + .withPropertyValues( + "spring.cloud.azure.eventhubs.namespace=test-namespace", + "spring.cloud.azure.eventhubs.event-hub-name=test-eventhub", + "spring.cloud.azure.eventhubs.processor.consumer-group=test-consumer-group" + ) + .withBean("customizer1", EventProcessorBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", EventProcessorBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + EventProcessorBuilderCustomizer customizer = new EventProcessorBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withBean(EventProcessingListener.class, TestEventProcessorListener::new) + .withBean(CheckpointStore.class, TestCheckpointStore::new) + .withUserConfiguration(AzureEventHubsPropertiesTestConfiguration.class) + .withPropertyValues( + "spring.cloud.azure.eventhubs.namespace=test-namespace", + "spring.cloud.azure.eventhubs.event-hub-name=test-eventhub", + "spring.cloud.azure.eventhubs.processor.consumer-group=test-consumer-group" + ) + .withBean("customizer1", EventProcessorBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", EventProcessorBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class EventProcessorBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + private static class TestEventProcessorListener implements RecordEventProcessingListener { @Override diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProducerClientConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProducerClientConfigurationTest.java index 2fbc5f8625c11..266dea9af2d85 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProducerClientConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsProducerClientConfigurationTest.java @@ -3,9 +3,11 @@ package com.azure.spring.cloud.autoconfigure.eventhubs; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.messaging.eventhubs.EventHubClientBuilder; import com.azure.messaging.eventhubs.EventHubProducerAsyncClient; import com.azure.messaging.eventhubs.EventHubProducerClient; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.cloud.autoconfigure.context.AzureContextUtils; import com.azure.spring.service.eventhubs.factory.EventHubClientBuilderFactory; import org.junit.jupiter.api.Test; @@ -83,12 +85,12 @@ void withDedicatedEvenHubConnectionSetShouldConfigureDedicated() { "spring.cloud.azure.eventhubs.namespace=test-namespace" ) .withUserConfiguration(AzureEventHubsPropertiesTestConfiguration.class) - .withBean(EventHubClientBuilder.class, EventHubClientBuilder::new) .run( context -> { assertThat(context).doesNotHaveBean(AzureEventHubsProducerClientConfiguration.SharedProducerConnectionConfiguration.class); assertThat(context).hasSingleBean(AzureEventHubsProducerClientConfiguration.DedicatedProducerConnectionConfiguration.class); assertThat(context).hasSingleBean(EventHubClientBuilderFactory.class); + assertThat(context).hasSingleBean(EventHubClientBuilder.class); assertThat(context).hasSingleBean(EventHubProducerClient.class); assertThat(context).hasSingleBean(EventHubProducerAsyncClient.class); assertThat(context).hasBean(AzureContextUtils.EVENT_HUB_PRODUCER_CLIENT_BUILDER_FACTORY_BEAN_NAME); @@ -97,4 +99,47 @@ void withDedicatedEvenHubConnectionSetShouldConfigureDedicated() { ); } + @Test + void customizerShouldBeCalled() { + EventHubBuilderCustomizer customizer = new EventHubBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.eventhubs.producer.namespace=test-namespace", + "spring.cloud.azure.eventhubs.producer.event-hub-name=test-event-hub", + "spring.cloud.azure.eventhubs.namespace=test-namespace" + ) + .withUserConfiguration(AzureEventHubsPropertiesTestConfiguration.class) + .withBean("customizer1", EventHubBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", EventHubBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + EventHubBuilderCustomizer customizer = new EventHubBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.eventhubs.producer.namespace=test-namespace", + "spring.cloud.azure.eventhubs.producer.event-hub-name=test-event-hub", + "spring.cloud.azure.eventhubs.namespace=test-namespace" + ) + .withUserConfiguration(AzureEventHubsPropertiesTestConfiguration.class) + .withBean("customizer1", EventHubBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", EventHubBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class EventHubBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/keyvault/certificates/AzureKeyVaultCertificateAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/keyvault/certificates/AzureKeyVaultCertificateAutoConfigurationTest.java new file mode 100644 index 0000000000000..71bd435bfc2b1 --- /dev/null +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/keyvault/certificates/AzureKeyVaultCertificateAutoConfigurationTest.java @@ -0,0 +1,103 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.cloud.autoconfigure.keyvault.certificates; + +import com.azure.data.appconfiguration.ConfigurationClientBuilder; +import com.azure.security.keyvault.certificates.CertificateAsyncClient; +import com.azure.security.keyvault.certificates.CertificateClient; +import com.azure.security.keyvault.certificates.CertificateClientBuilder; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; +import com.azure.spring.cloud.autoconfigure.keyvault.certificates.properties.AzureKeyVaultCertificateProperties; +import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; +import com.azure.spring.service.keyvault.certificates.CertificateClientBuilderFactory; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.FilteredClassLoader; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +class AzureKeyVaultCertificateAutoConfigurationTest { + + + private static final String ENDPOINT = "https:/%s.vault.azure.net/"; + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(AzureKeyVaultCertificateAutoConfiguration.class)); + + @Test + void withoutCertificateClientBuilderShouldNotConfigure() { + this.contextRunner + .withClassLoader(new FilteredClassLoader(CertificateClientBuilder.class)) + .withPropertyValues("spring.cloud.azure.keyvault.certificate.endpoint=" + String.format(ENDPOINT, "mykv")) + .run(context -> assertThat(context).doesNotHaveBean(AzureKeyVaultCertificateAutoConfiguration.class)); + } + + @Test + void disableKeyVaultCertificateShouldNotConfigure() { + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.keyvault.certificate.enabled=false", + "spring.cloud.azure.keyvault.certificate.endpoint=" + String.format(ENDPOINT, "mykv") + ) + .run(context -> assertThat(context).doesNotHaveBean(AzureKeyVaultCertificateAutoConfiguration.class)); + } + + @Test + void withoutVaultEndpointShouldNotConfigure() { + this.contextRunner + .run(context -> assertThat(context).doesNotHaveBean(AzureKeyVaultCertificateAutoConfiguration.class)); + } + + @Test + void withVaultEndpointShouldConfigure() { + this.contextRunner + .withPropertyValues("spring.cloud.azure.keyvault.certificate.endpoint=" + String.format(ENDPOINT, "mykv")) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .run(context -> { + assertThat(context).hasSingleBean(AzureKeyVaultCertificateAutoConfiguration.class); + assertThat(context).hasSingleBean(AzureKeyVaultCertificateProperties.class); + assertThat(context).hasSingleBean(CertificateClient.class); + assertThat(context).hasSingleBean(CertificateAsyncClient.class); + assertThat(context).hasSingleBean(CertificateClientBuilder.class); + assertThat(context).hasSingleBean(CertificateClientBuilderFactory.class); + }); + } + + @Test + void customizerShouldBeCalled() { + CertificateBuilderCustomizer customizer = new CertificateBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.keyvault.certificate.endpoint=" + String.format(ENDPOINT, "mykv")) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", CertificateBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", CertificateBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + CertificateBuilderCustomizer customizer = new CertificateBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.keyvault.certificate.endpoint=" + String.format(ENDPOINT, "mykv")) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", CertificateBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", CertificateBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class CertificateBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + +} diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/keyvault/secrets/AzureKeyVaultSecretAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/keyvault/secrets/AzureKeyVaultSecretAutoConfigurationTest.java index d15f5984dea4a..3a1f4a57c2e4b 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/keyvault/secrets/AzureKeyVaultSecretAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/keyvault/secrets/AzureKeyVaultSecretAutoConfigurationTest.java @@ -3,9 +3,11 @@ package com.azure.spring.cloud.autoconfigure.keyvault.secrets; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.security.keyvault.secrets.SecretAsyncClient; import com.azure.security.keyvault.secrets.SecretClient; import com.azure.security.keyvault.secrets.SecretClientBuilder; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.cloud.autoconfigure.keyvault.secrets.properties.AzureKeyVaultSecretProperties; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; import com.azure.spring.service.keyvault.secrets.SecretClientBuilderFactory; @@ -62,4 +64,39 @@ void withVaultEndpointShouldConfigure() { }); } + @Test + void customizerShouldBeCalled() { + SecretBuilderCustomizer customizer = new SecretBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.keyvault.secret.endpoint=" + String.format(ENDPOINT, "mykv")) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", SecretBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", SecretBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + SecretBuilderCustomizer customizer = new SecretBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.keyvault.secret.endpoint=" + String.format(ENDPOINT, "mykv")) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", SecretBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", SecretBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class SecretBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusClientBuilderConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusClientBuilderConfigurationTest.java index b1851746abea7..07572c02cb1e2 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusClientBuilderConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusClientBuilderConfigurationTest.java @@ -3,7 +3,9 @@ package com.azure.spring.cloud.autoconfigure.servicebus; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.messaging.servicebus.ServiceBusClientBuilder; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.core.connectionstring.StaticConnectionStringProvider; import com.azure.spring.core.service.AzureServiceType; import com.azure.spring.service.servicebus.factory.ServiceBusClientBuilderFactory; @@ -44,4 +46,43 @@ void connectionStringProvidedShouldConfigure() { }); } + @Test + void customizerShouldBeCalled() { + ServiceBusBuilderCustomizer customizer = new ServiceBusBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.servicebus.connection-string=" + String.format(CONNECTION_STRING, "test-namespace") + ) + .withUserConfiguration(AzureServiceBusPropertiesTestConfiguration.class) + .withBean("customizer1", ServiceBusBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", ServiceBusBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + ServiceBusBuilderCustomizer customizer = new ServiceBusBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.servicebus.connection-string=" + String.format(CONNECTION_STRING, "test-namespace") + ) + .withUserConfiguration(AzureServiceBusPropertiesTestConfiguration.class) + .withBean("customizer1", ServiceBusBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", ServiceBusBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class ServiceBusBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusConsumerClientConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusConsumerClientConfigurationTest.java index d70126e8c7139..286dbeb0b1735 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusConsumerClientConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusConsumerClientConfigurationTest.java @@ -3,9 +3,11 @@ package com.azure.spring.cloud.autoconfigure.servicebus; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.messaging.servicebus.ServiceBusClientBuilder; import com.azure.messaging.servicebus.ServiceBusReceiverClient; import com.azure.messaging.servicebus.ServiceBusSessionReceiverClient; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; @@ -172,4 +174,66 @@ void sessionAwareEnabledWithDedicatedConnectionShouldConfigureSession() { }); } + @Test + void customizerShouldBeCalledForNonSession() { + ServiceBusReceiverClientBuilderCustomizer customizer = new ServiceBusReceiverClientBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.servicebus.consumer.entity-name=test-queue", + "spring.cloud.azure.servicebus.consumer.entity-type=queue", + "spring.cloud.azure.servicebus.consumer.connection-string=" + String.format(CONNECTION_STRING, "test-namespace") + ) + .withUserConfiguration(AzureServiceBusPropertiesTestConfiguration.class) + .withBean("customizer1", ServiceBusReceiverClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", ServiceBusReceiverClientBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void customizerShouldBeCalledForSession() { + ServiceBusSessionReceiverClientBuilderCustomizer customizer = new ServiceBusSessionReceiverClientBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.servicebus.consumer.entity-name=test-queue", + "spring.cloud.azure.servicebus.consumer.entity-type=queue", + "spring.cloud.azure.servicebus.consumer.connection-string=" + String.format(CONNECTION_STRING, "test-namespace"), + "spring.cloud.azure.servicebus.consumer.session-enabled=true" + ) + .withUserConfiguration(AzureServiceBusPropertiesTestConfiguration.class) + .withBean("customizer1", ServiceBusSessionReceiverClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", ServiceBusSessionReceiverClientBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + ServiceBusReceiverClientBuilderCustomizer customizer = new ServiceBusReceiverClientBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.servicebus.consumer.entity-name=test-queue", + "spring.cloud.azure.servicebus.consumer.entity-type=queue", + "spring.cloud.azure.servicebus.consumer.connection-string=" + String.format(CONNECTION_STRING, "test-namespace") + ) + .withUserConfiguration(AzureServiceBusPropertiesTestConfiguration.class) + .withBean("customizer1", ServiceBusReceiverClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", ServiceBusReceiverClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class ServiceBusReceiverClientBuilderCustomizer extends TestBuilderCustomizer { + + } + private static class ServiceBusSessionReceiverClientBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProcessorClientConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProcessorClientConfigurationTest.java index c8a86a6bed4bf..f3d394dd57ea8 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProcessorClientConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProcessorClientConfigurationTest.java @@ -3,9 +3,11 @@ package com.azure.spring.cloud.autoconfigure.servicebus; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.messaging.servicebus.ServiceBusClientBuilder; import com.azure.messaging.servicebus.ServiceBusProcessorClient; import com.azure.messaging.servicebus.ServiceBusReceivedMessageContext; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.service.servicebus.factory.ServiceBusSessionProcessorClientBuilderFactory; import com.azure.spring.service.servicebus.processor.MessageProcessingListener; import com.azure.spring.service.servicebus.processor.RecordMessageProcessingListener; @@ -181,6 +183,56 @@ void sessionAwareEnabledWithDedicatedConnectionShouldConfigureSession() { }); } + @Test + void customizerShouldBeCalledForSession() { + ServiceBusSessionProcessorClientBuilderCustomizer customizer = new ServiceBusSessionProcessorClientBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.servicebus.processor.entity-name=test-queue", + "spring.cloud.azure.servicebus.processor.entity-type=queue", + "spring.cloud.azure.servicebus.processor.connection-string=" + String.format(CONNECTION_STRING, "test-namespace"), + "spring.cloud.azure.servicebus.processor.session-enabled=true" + ) + .withUserConfiguration(AzureServiceBusPropertiesTestConfiguration.class) + .withBean(MessageProcessingListener.class, TestMessageProcessingListener::new) + .withBean("customizer1", ServiceBusSessionProcessorClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", ServiceBusSessionProcessorClientBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + ServiceBusProcessorClientBuilderCustomizer customizer = new ServiceBusProcessorClientBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.servicebus.processor.entity-name=test-queue", + "spring.cloud.azure.servicebus.processor.entity-type=queue", + "spring.cloud.azure.servicebus.processor.connection-string=" + String.format(CONNECTION_STRING, "test-namespace") + ) + .withUserConfiguration(AzureServiceBusPropertiesTestConfiguration.class) + .withBean(MessageProcessingListener.class, TestMessageProcessingListener::new) + .withBean("customizer1", ServiceBusProcessorClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", ServiceBusProcessorClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class ServiceBusProcessorClientBuilderCustomizer extends TestBuilderCustomizer { + + } + private static class ServiceBusSessionProcessorClientBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + + static class TestMessageProcessingListener implements RecordMessageProcessingListener { @Override diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProducerClientConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProducerClientConfigurationTest.java index 9dae3148876b9..71b17d2ae911b 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProducerClientConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/servicebus/AzureServiceBusProducerClientConfigurationTest.java @@ -3,8 +3,10 @@ package com.azure.spring.cloud.autoconfigure.servicebus; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.messaging.servicebus.ServiceBusClientBuilder; import com.azure.messaging.servicebus.ServiceBusSenderClient; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.service.servicebus.factory.ServiceBusSenderClientBuilderFactory; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -76,4 +78,47 @@ void dedicatedConnectionInfoProvidedShouldConfigureDedicated() { }); } + @Test + void customizerShouldBeCalled() { + ServiceBusSenderClientBuilderCustomizer customizer = new ServiceBusSenderClientBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.servicebus.producer.entity-name=test-queue", + "spring.cloud.azure.servicebus.producer.entity-type=topic", + "spring.cloud.azure.servicebus.producer.connection-string=" + String.format(CONNECTION_STRING, "test-namespace") + ) + .withUserConfiguration(AzureServiceBusPropertiesTestConfiguration.class) + .withBean("customizer1", ServiceBusSenderClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", ServiceBusSenderClientBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + ServiceBusSenderClientBuilderCustomizer customizer = new ServiceBusSenderClientBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.servicebus.producer.entity-name=test-queue", + "spring.cloud.azure.servicebus.producer.entity-type=topic", + "spring.cloud.azure.servicebus.producer.connection-string=" + String.format(CONNECTION_STRING, "test-namespace") + ) + .withUserConfiguration(AzureServiceBusPropertiesTestConfiguration.class) + .withBean("customizer1", ServiceBusSenderClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", ServiceBusSenderClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class ServiceBusSenderClientBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/blob/AzureStorageBlobAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/blob/AzureStorageBlobAutoConfigurationTest.java index 34be441603cf8..8795856a77079 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/blob/AzureStorageBlobAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/blob/AzureStorageBlobAutoConfigurationTest.java @@ -3,6 +3,8 @@ package com.azure.spring.cloud.autoconfigure.storage.blob; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; import com.azure.spring.cloud.autoconfigure.storage.blob.properties.AzureStorageBlobProperties; import com.azure.spring.service.storage.blob.BlobServiceClientBuilderFactory; @@ -54,4 +56,39 @@ void accountNameSetShouldConfigure() { }); } + @Test + void customizerShouldBeCalled() { + BlobServiceClientBuilderCustomizer customizer = new BlobServiceClientBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.storage.blob.account-name=sa") + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", BlobServiceClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", BlobServiceClientBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + BlobServiceClientBuilderCustomizer customizer = new BlobServiceClientBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.storage.blob.account-name=sa") + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", BlobServiceClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", BlobServiceClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class BlobServiceClientBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } + } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/AzureStorageFileShareAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/AzureStorageFileShareAutoConfigurationTest.java index 54b76e57e1d63..acbed0a18dd24 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/AzureStorageFileShareAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/AzureStorageFileShareAutoConfigurationTest.java @@ -3,6 +3,8 @@ package com.azure.spring.cloud.autoconfigure.storage.fileshare; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; import com.azure.spring.cloud.autoconfigure.storage.fileshare.properties.AzureStorageFileShareProperties; import com.azure.spring.service.storage.fileshare.ShareServiceClientBuilderFactory; @@ -41,7 +43,7 @@ void configureWithStorageFileShareDisabled() { } @Test - @Disabled // TODO (xiada): fix this after default token credential could be set + @Disabled // TODO (xiada): fix this after token credential is supported in a share service client void accountNameSetShouldConfigure() { this.contextRunner .withPropertyValues("spring.cloud.azure.storage.fileshare.account-name=sa") @@ -55,5 +57,40 @@ void accountNameSetShouldConfigure() { assertThat(context).hasSingleBean(ShareServiceClientBuilderFactory.class); }); } + + @Test + void customizerShouldBeCalled() { + ShareServiceClientBuilderCustomizer customizer = new ShareServiceClientBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.storage.fileshare.account-name=sa") + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", ShareServiceClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", ShareServiceClientBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + ShareServiceClientBuilderCustomizer customizer = new ShareServiceClientBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.storage.fileshare.account-name=sa") + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", ShareServiceClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", ShareServiceClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class ShareServiceClientBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/queue/AzureStorageQueueAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/queue/AzureStorageQueueAutoConfigurationTest.java index ae9b056695ec6..b4ac1a62afe2b 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/queue/AzureStorageQueueAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/storage/queue/AzureStorageQueueAutoConfigurationTest.java @@ -3,6 +3,8 @@ package com.azure.spring.cloud.autoconfigure.storage.queue; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; +import com.azure.spring.cloud.autoconfigure.TestBuilderCustomizer; import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties; import com.azure.spring.cloud.autoconfigure.storage.queue.properties.AzureStorageQueueProperties; import com.azure.spring.service.storage.queue.QueueServiceClientBuilderFactory; @@ -53,4 +55,39 @@ void accountNameSetShouldConfigure() { assertThat(context).hasSingleBean(QueueServiceClientBuilderFactory.class); }); } + + @Test + void customizerShouldBeCalled() { + QueueServiceClientBuilderCustomizer customizer = new QueueServiceClientBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.storage.queue.account-name=sa") + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", QueueServiceClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", QueueServiceClientBuilderCustomizer.class, () -> customizer) + .run(context -> assertThat(customizer.getCustomizedTimes()).isEqualTo(2)); + } + + @Test + void otherCustomizerShouldNotBeCalled() { + QueueServiceClientBuilderCustomizer customizer = new QueueServiceClientBuilderCustomizer(); + OtherBuilderCustomizer otherBuilderCustomizer = new OtherBuilderCustomizer(); + this.contextRunner + .withPropertyValues("spring.cloud.azure.storage.queue.account-name=sa") + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean("customizer1", QueueServiceClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer2", QueueServiceClientBuilderCustomizer.class, () -> customizer) + .withBean("customizer3", OtherBuilderCustomizer.class, () -> otherBuilderCustomizer) + .run(context -> { + assertThat(customizer.getCustomizedTimes()).isEqualTo(2); + assertThat(otherBuilderCustomizer.getCustomizedTimes()).isEqualTo(0); + }); + } + + private static class QueueServiceClientBuilderCustomizer extends TestBuilderCustomizer { + + } + + private static class OtherBuilderCustomizer extends TestBuilderCustomizer { + + } } diff --git a/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/factory/AbstractAzureServiceClientBuilderFactory.java b/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/factory/AbstractAzureServiceClientBuilderFactory.java index aec9e5a4c4d96..4ec2b180fb19a 100644 --- a/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/factory/AbstractAzureServiceClientBuilderFactory.java +++ b/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/factory/AbstractAzureServiceClientBuilderFactory.java @@ -14,13 +14,12 @@ import com.azure.spring.core.credential.resolver.AzureCredentialResolver; import com.azure.spring.core.credential.resolver.AzureCredentialResolvers; import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; -import com.azure.spring.core.customizer.NoOpAzureServiceClientBuilderCustomizer; import com.azure.spring.core.properties.AzureProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; -import java.util.Collections; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.function.BiConsumer; @@ -57,11 +56,11 @@ public abstract class AbstractAzureServiceClientBuilderFactory implements Azu protected abstract BiConsumer consumeConnectionString(); protected TokenCredential defaultTokenCredential = DEFAULT_TOKEN_CREDENTIAL; - private String applicationId; // end-user private String springIdentifier; private ConnectionStringProvider connectionStringProvider; private boolean credentialConfigured = false; protected final Configuration configuration = new Configuration(); + private final List> customizers = new ArrayList<>(); /** *
    @@ -99,7 +98,7 @@ protected void configureCore(T builder) { * @param builder the service client builder */ protected void configureApplicationId(T builder) { - String applicationId = getApplicationId() + this.springIdentifier; + String applicationId = getApplicationId() + (this.springIdentifier == null ? "" : this.springIdentifier); consumeApplicationId().accept(builder, applicationId); } @@ -134,23 +133,26 @@ protected void configureCredential(T builder) { } protected void configureConnectionString(T builder) { + AzureProperties azureProperties = getAzureProperties(); + + // connection string set to properties will advantage the one from connection string provider + if (azureProperties instanceof ConnectionStringAware) { + String connectionString = ((ConnectionStringAware) azureProperties).getConnectionString(); + + if (StringUtils.hasText(connectionString)) { + consumeConnectionString().accept(builder, connectionString); + credentialConfigured = true; + LOGGER.debug("Connection string configured for class {}.", builder.getClass().getSimpleName()); + return; + } + } + if (this.connectionStringProvider != null && StringUtils.hasText(this.connectionStringProvider.getConnectionString())) { consumeConnectionString().accept(builder, this.connectionStringProvider.getConnectionString()); credentialConfigured = true; LOGGER.debug("Connection string configured for class {}.", builder.getClass().getSimpleName()); - } else { - AzureProperties azureProperties = getAzureProperties(); - if (azureProperties instanceof ConnectionStringAware) { - String connectionString = ((ConnectionStringAware) azureProperties).getConnectionString(); - if (StringUtils.hasText(connectionString)) { - consumeConnectionString().accept(builder, connectionString); - credentialConfigured = true; - LOGGER.debug("Connection string configured for class {}.", builder.getClass().getSimpleName()); - } - } } - } protected void configureDefaultCredential(T builder) { @@ -160,8 +162,12 @@ protected void configureDefaultCredential(T builder) { } } + public void addBuilderCustomizer(AzureServiceClientBuilderCustomizer customizer) { + this.customizers.add(customizer); + } + protected List> getBuilderCustomizers() { - return Collections.singletonList(new NoOpAzureServiceClientBuilderCustomizer<>()); + return this.customizers; } protected void customizeBuilder(T builder) { diff --git a/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/factory/credential/DefaultAzureCredentialBuilderFactory.java b/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/factory/credential/DefaultAzureCredentialBuilderFactory.java index 0d8981aef2a41..7a6e917939d10 100644 --- a/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/factory/credential/DefaultAzureCredentialBuilderFactory.java +++ b/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/factory/credential/DefaultAzureCredentialBuilderFactory.java @@ -15,16 +15,10 @@ */ public class DefaultAzureCredentialBuilderFactory extends AbstractAzureCredentialBuilderFactory { - private final ExecutorService executorService; + private ExecutorService executorService = null; public DefaultAzureCredentialBuilderFactory(AzureProperties azureProperties) { - this(azureProperties, null); - } - - public DefaultAzureCredentialBuilderFactory(AzureProperties azureProperties, - ExecutorService executorService) { super(azureProperties); - this.executorService = executorService; } @Override @@ -42,4 +36,8 @@ protected void configureService(DefaultAzureCredentialBuilder builder) { map.from(azureProperties.getCredential().getManagedIdentityClientId()).to(builder::managedIdentityClientId); map.from(executorService).to(builder::executorService); } + + public void setExecutorService(ExecutorService executorService) { + this.executorService = executorService; + } } diff --git a/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/service/AzureServiceType.java b/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/service/AzureServiceType.java index de465a364cc7c..c70fd63545c9d 100644 --- a/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/service/AzureServiceType.java +++ b/sdk/spring/spring-cloud-azure-core/src/main/java/com/azure/spring/core/service/AzureServiceType.java @@ -11,6 +11,7 @@ public final class AzureServiceType { public static final ServiceBus SERVICE_BUS = new ServiceBus(); public static final EventHubs EVENT_HUBS = new EventHubs(); public static final StorageBlob STORAGE_BLOB = new StorageBlob(); + public static final StorageFileShare STORAGE_FILE_SHARE = new StorageFileShare(); public static final StorageQueue STORAGE_QUEUE = new StorageQueue(); public static final AppConfiguration APP_CONFIGURATION = new AppConfiguration(); @@ -35,6 +36,13 @@ public static class StorageBlob { } + /** + * The Storage File Share service. + */ + public static class StorageFileShare { + + } + /** * The Storage Queue service. */ diff --git a/sdk/spring/spring-cloud-azure-core/src/test/java/com/azure/spring/core/factory/AbstractAzureServiceClientBuilderFactoryTest.java b/sdk/spring/spring-cloud-azure-core/src/test/java/com/azure/spring/core/factory/AbstractAzureServiceClientBuilderFactoryTest.java new file mode 100644 index 0000000000000..8fb75f9457379 --- /dev/null +++ b/sdk/spring/spring-cloud-azure-core/src/test/java/com/azure/spring/core/factory/AbstractAzureServiceClientBuilderFactoryTest.java @@ -0,0 +1,238 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.core.factory; + +import com.azure.core.credential.TokenCredential; +import com.azure.core.util.Configuration; +import com.azure.spring.core.aware.authentication.ConnectionStringAware; +import com.azure.spring.core.connectionstring.StaticConnectionStringProvider; +import com.azure.spring.core.credential.descriptor.AuthenticationDescriptor; +import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer; +import com.azure.spring.core.properties.AbstractAzureSdkProperties; +import com.azure.spring.core.properties.AzureProperties; +import com.azure.spring.core.properties.client.ClientProperties; +import com.azure.spring.core.properties.proxy.ProxyProperties; +import com.azure.spring.core.properties.retry.RetryProperties; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.List; +import java.util.function.BiConsumer; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class AbstractAzureServiceClientBuilderFactoryTest { + + @Test + void emptyPropertiesShouldWork() { + TestClientBuilderFactory factory = new TestClientBuilderFactory(new TestAzureProperties()); + factory.build(); + } + + @Test + void connectionStringFromPropertiesShouldHavePriority() { + TestAzureProperties testAzureProperties = new TestAzureProperties(); + testAzureProperties.setConnectionString("connection-string"); + + TestClientBuilderFactory factory = new TestClientBuilderFactory(testAzureProperties); + factory.setConnectionStringProvider(new StaticConnectionStringProvider<>("Test Service", "provider")); + TestClientBuilder builder = factory.build(); + assertEquals("connection-string", builder.getConnectionString()); + } + + @Test + void connectionStringFromPropertyShouldBeConfigured() { + TestAzureProperties testAzureProperties = new TestAzureProperties(); + testAzureProperties.setConnectionString("connection-string"); + + TestClientBuilderFactory factory = new TestClientBuilderFactory(testAzureProperties); + TestClientBuilder builder = factory.build(); + assertEquals("connection-string", builder.getConnectionString()); + } + + @Test + void connectionStringFromProviderShouldBeConfigured() { + TestClientBuilderFactory factory = new TestClientBuilderFactory(new TestAzureProperties()); + factory.setConnectionStringProvider(new StaticConnectionStringProvider<>("Test Service", "provider")); + TestClientBuilder builder = factory.build(); + assertEquals("provider", builder.getConnectionString()); + } + + @Test + void springIdentifierShouldBeConfigured() { + TestClientBuilderFactory factory = new TestClientBuilderFactory(new TestAzureProperties()); + factory.setSpringIdentifier("identifier"); + TestClientBuilder builder = factory.build(); + assertEquals("identifier", builder.getApplicationId()); + } + + @Test + void applicationIdShouldBeConfigured() { + TestAzureProperties testAzureProperties = new TestAzureProperties(); + testAzureProperties.getClient().setApplicationId("application-id"); + + TestClientBuilderFactory factory = new TestClientBuilderFactory(testAzureProperties); + TestClientBuilder builder = factory.build(); + assertEquals("application-id", builder.getApplicationId()); + } + + @Test + void applicationIdAndSpringIdentifierShouldBeConfigured() { + TestAzureProperties testAzureProperties = new TestAzureProperties(); + testAzureProperties.getClient().setApplicationId("application-id"); + TestClientBuilderFactory factory = new TestClientBuilderFactory(testAzureProperties); + factory.setSpringIdentifier("identifier"); + TestClientBuilder builder = factory.build(); + assertEquals("application-ididentifier", builder.getApplicationId()); + } + + @Test + void customizerShouldEffect() { + TestBuilderCustomizer customizer = new TestBuilderCustomizer(); + + TestClientBuilderFactory factory = new TestClientBuilderFactory(new TestAzureProperties()); + factory.addBuilderCustomizer(customizer); + TestClientBuilder builder = factory.build(); + assertEquals(1, builder.getCustomizedTimes()); + + TestClientBuilderFactory anotherFactory = new TestClientBuilderFactory(new TestAzureProperties()); + anotherFactory.addBuilderCustomizer(customizer); + anotherFactory.addBuilderCustomizer(customizer); + TestClientBuilder anotherBuilder = factory.build(); + assertEquals(2, anotherBuilder.getCustomizedTimes()); + } + + static class TestBuilderCustomizer implements AzureServiceClientBuilderCustomizer { + + @Override + public void customize(TestClientBuilder builder) { + builder.setCustomizedTimes(builder.getCustomizedTimes() + 1); + } + } + + static class TestClientBuilderFactory extends AbstractAzureServiceClientBuilderFactory { + + final TestAzureProperties properties; + final TestClientBuilder builder = new TestClientBuilder(); + + TestClientBuilderFactory(TestAzureProperties properties) { + this.properties = properties; + } + + @Override + protected TestClientBuilder createBuilderInstance() { + return this.builder; + } + + @Override + protected AzureProperties getAzureProperties() { + return this.properties; + } + + @Override + protected List> getAuthenticationDescriptors(TestClientBuilder builder) { + return Collections.emptyList(); + } + + @Override + protected void configureProxy(TestClientBuilder builder) { + + } + + @Override + protected void configureRetry(TestClientBuilder builder) { + + } + + @Override + protected void configureService(TestClientBuilder builder) { + + } + + @Override + protected BiConsumer consumeApplicationId() { + return TestClientBuilder::setApplicationId; + } + + @Override + protected BiConsumer consumeConfiguration() { + return (a, b) -> { }; + } + + @Override + protected BiConsumer consumeDefaultTokenCredential() { + return (a, b) -> { }; + } + + @Override + protected BiConsumer consumeConnectionString() { + return TestClientBuilder::setConnectionString; + } + } + + static class TestClientBuilder { + + private String connectionString; + private String applicationId; + private int customizedTimes = 0; + + public String getConnectionString() { + return connectionString; + } + + public void setConnectionString(String connectionString) { + this.connectionString = connectionString; + } + + public String getApplicationId() { + return applicationId; + } + + public void setApplicationId(String applicationId) { + this.applicationId = applicationId; + } + + public int getCustomizedTimes() { + return customizedTimes; + } + + public void setCustomizedTimes(int customizedTimes) { + this.customizedTimes = customizedTimes; + } + } + + static class TestAzureProperties extends AbstractAzureSdkProperties implements ConnectionStringAware { + private String connectionString; + private final ClientProperties client = new ClientProperties(); + private final ProxyProperties proxy = new ProxyProperties(); + private final RetryProperties retry = new RetryProperties(); + + @Override + public ClientProperties getClient() { + return this.client; + } + + @Override + public String getConnectionString() { + return connectionString; + } + + public void setConnectionString(String connectionString) { + this.connectionString = connectionString; + } + + @Override + public ProxyProperties getProxy() { + return proxy; + } + + @Override + public RetryProperties getRetry() { + return retry; + } + } + + + +} diff --git a/sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/service/credential/AzureDefaultAzureCredentialBuilderFactoryTest.java b/sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/service/credential/AzureDefaultAzureCredentialBuilderFactoryTest.java index 81afb4af4c734..c0b604ea8fbec 100644 --- a/sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/service/credential/AzureDefaultAzureCredentialBuilderFactoryTest.java +++ b/sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/service/credential/AzureDefaultAzureCredentialBuilderFactoryTest.java @@ -20,7 +20,6 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.time.Duration; -import java.util.concurrent.ExecutorService; import java.util.concurrent.ThreadPoolExecutor; import static org.mockito.ArgumentMatchers.any; @@ -40,9 +39,11 @@ protected TestAzureGlobalProperties createMinimalServiceProperties() { @Test void testAuthorityHostAndExecutorServiceConfigured() { AzureProperties properties = createMinimalServiceProperties(); + + DefaultAzureCredentialBuilderFactoryExt factory = new DefaultAzureCredentialBuilderFactoryExt(properties); ThreadPoolExecutor executor = getThreadPoolExecutor(); - DefaultAzureCredentialBuilderFactoryExt factory = new DefaultAzureCredentialBuilderFactoryExt( - properties, executor); + factory.setExecutorService(executor); + DefaultAzureCredentialBuilder credentialBuilder = factory.build(); DefaultAzureCredentialBuilder builder = factory.getBuilder(); verify(builder, times(1)).executorService(executor); @@ -54,9 +55,8 @@ void testAuthorityHostAndExecutorServiceConfigured() { @Test void testHttpClientConfigured() { AzureProperties properties = createMinimalServiceProperties(); - ThreadPoolExecutor executor = getThreadPoolExecutor(); - DefaultAzureCredentialBuilderFactoryExt factory = new DefaultAzureCredentialBuilderFactoryExt( - properties, executor); + DefaultAzureCredentialBuilderFactoryExt factory = new DefaultAzureCredentialBuilderFactoryExt(properties); + factory.setExecutorService(getThreadPoolExecutor()); factory.setHttpClientProvider(new TestHttpClientProvider()); DefaultAzureCredentialBuilder credentialBuilder = factory.build(); verify(factory.getBuilder(), times(1)).httpClient(any(TestHttpClient.class)); @@ -69,9 +69,9 @@ void testRetryOptionsConfigured() { retryProperties.setMaxAttempts(3); Duration duration = Duration.ofMillis(3); retryProperties.setTimeout(duration); - ThreadPoolExecutor executor = getThreadPoolExecutor(); - DefaultAzureCredentialBuilderFactoryExt factory = new DefaultAzureCredentialBuilderFactoryExt( - properties, executor); + + DefaultAzureCredentialBuilderFactoryExt factory = new DefaultAzureCredentialBuilderFactoryExt(properties); + factory.setExecutorService(getThreadPoolExecutor()); DefaultAzureCredentialBuilder credentialBuilder = factory.build(); verify(factory.getBuilder(), times(1)).maxRetry(3); } @@ -82,9 +82,8 @@ void testProxyOptionsConfigured() { ProxyProperties proxyProperties = properties.getProxy(); proxyProperties.setHostname("localhost"); proxyProperties.setPort(8080); - ThreadPoolExecutor executor = getThreadPoolExecutor(); - DefaultAzureCredentialBuilderFactoryProxyExt factory = new DefaultAzureCredentialBuilderFactoryProxyExt( - properties, executor); + DefaultAzureCredentialBuilderFactoryProxyExt factory = new DefaultAzureCredentialBuilderFactoryProxyExt(properties); + factory.setExecutorService(getThreadPoolExecutor()); DefaultAzureCredentialBuilder builder = factory.getBuilder(); HttpClientProvider defaultHttpClientProvider = factory.getDefaultHttpClientProvider(); @@ -103,9 +102,8 @@ static class DefaultAzureCredentialBuilderFactoryExt extends DefaultAzureCredent private final DefaultAzureCredentialBuilder builder = mock(DefaultAzureCredentialBuilder.class); - DefaultAzureCredentialBuilderFactoryExt(AzureProperties properties, - ExecutorService executorService) { - super(properties, executorService); + DefaultAzureCredentialBuilderFactoryExt(AzureProperties properties) { + super(properties); } @Override @@ -122,8 +120,8 @@ static class DefaultAzureCredentialBuilderFactoryProxyExt extends DefaultAzureCr private HttpClientProvider httpClientProvider = mock(DefaultHttpProvider.class); - DefaultAzureCredentialBuilderFactoryProxyExt(AzureProperties properties, ExecutorService executorService) { - super(properties, executorService); + DefaultAzureCredentialBuilderFactoryProxyExt(AzureProperties properties) { + super(properties); HttpClient httpClient = mock(HttpClient.class); when(this.httpClientProvider.createInstance(any(HttpClientOptions.class))).thenReturn(httpClient);