From 8bd294179ff8d53c9a6f044f575ac6ad7df8e12f Mon Sep 17 00:00:00 2001 From: Moary Chen Date: Fri, 5 Nov 2021 19:28:23 +0800 Subject: [PATCH 1/3] Update resource manager auto config and add testcases --- ...AzureBlobCheckpointStoreConfiguration.java | 16 +++--- ...ureEventHubMessagingAutoConfiguration.java | 3 ++ ...ntHubResourceManagerAutoConfiguration.java | 9 ++-- ...ceBusResourceManagerAutoConfiguration.java | 7 ++- ...QueueResourceManagerAutoConfiguration.java | 3 ++ ...eBlobCheckpointStoreConfigurationTest.java | 13 +---- ...bResourceManagerAutoConfigurationTest.java | 45 +++++++++++++++- ...eResourceManagerAutoConfigurationTest.java | 45 ++++++++++++++++ ...sResourceManagerAutoConfigurationTest.java | 51 +++++++++++++++++++ ...eResourceManagerAutoConfigurationTest.java | 25 +++++++++ 10 files changed, 192 insertions(+), 25 deletions(-) create mode 100644 sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfigurationTest.java create mode 100644 sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfigurationTest.java 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 4f440caec5bb8..313c5c937e510 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 @@ -3,6 +3,7 @@ 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.eventhubs.properties.AzureEventHubProperties; import com.azure.spring.service.storage.blob.BlobServiceClientBuilderFactory; @@ -27,7 +28,7 @@ * Configures a {@link BlobCheckpointStore} */ @Configuration(proxyBeanMethods = false) -@ConditionalOnClass(BlobCheckpointStore.class) +@ConditionalOnClass({ BlobCheckpointStore.class, EventHubClientBuilder.class}) @ConditionalOnProperty(prefix = "spring.cloud.azure.eventhubs.processor.checkpoint-store", name = { "container-name", "account-name" }) public class AzureBlobCheckpointStoreConfiguration { @@ -40,11 +41,10 @@ public BlobCheckpointStore blobCheckpointStore( BlobServiceClientBuilderFactory factory, AzureEventHubProperties eventHubProperties, ObjectProvider initializers) { - final AzureEventHubProperties.Processor.BlobCheckpointStore csProperties = - getCheckpointStoreProperties(eventHubProperties); - final BlobContainerAsyncClient blobContainerAsyncClient = factory - .build().buildAsyncClient().getBlobContainerAsyncClient(csProperties.getContainerName()); + .build() + .buildAsyncClient() + .getBlobContainerAsyncClient(eventHubProperties.getProcessor().getCheckpointStore().getContainerName()); initializers.ifAvailable(initializer -> initializer.init(blobContainerAsyncClient)); @@ -55,10 +55,10 @@ public BlobCheckpointStore blobCheckpointStore( @ConditionalOnMissingBean public BlobCheckpointStoreContainerInitializer blobCheckpointStoreContainerInitializer() { return containerAsyncClient -> { - if (Boolean.FALSE.equals(containerAsyncClient.exists().block(Duration.ofSeconds(3)))) { + if (Boolean.FALSE.equals(containerAsyncClient.exists().block(Duration.ofSeconds(300)))) { LOGGER.debug("The checkpoint store container [{}] doesn't exist, will create the blob container now.", containerAsyncClient.getBlobContainerName()); - containerAsyncClient.create().block(Duration.ofSeconds(3)); + containerAsyncClient.create().block(Duration.ofSeconds(300)); } }; } @@ -66,7 +66,7 @@ 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(AzureEventHubProperties eventHubProperties) { - return new BlobServiceClientBuilderFactory(eventHubProperties.getProcessor().getCheckpointStore()); + return new BlobServiceClientBuilderFactory(getCheckpointStoreProperties(eventHubProperties)); } private AzureEventHubProperties.Processor.BlobCheckpointStore getCheckpointStoreProperties( diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubMessagingAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubMessagingAutoConfiguration.java index 1ea42cf70a9bf..89667415f34d2 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubMessagingAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubMessagingAutoConfiguration.java @@ -28,6 +28,8 @@ import org.springframework.context.annotation.Import; import reactor.util.function.Tuple2; +import static com.azure.spring.core.properties.AzurePropertiesUtils.copyAzureCommonProperties; + /** * An auto-configuration for Event Hub, which provides {@link EventHubsTemplate} and {@link * EventHubProcessorContainer}. @@ -49,6 +51,7 @@ public class AzureEventHubMessagingAutoConfiguration { public NamespaceProperties eventHubNamespaceProperties(AzureEventHubProperties properties) { NamespaceProperties namespaceProperties = new NamespaceProperties(); BeanUtils.copyProperties(properties, namespaceProperties); + copyAzureCommonProperties(properties, namespaceProperties); return namespaceProperties; } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfiguration.java index 3c2daef287ec4..aad0f5d2a37a4 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfiguration.java @@ -3,6 +3,7 @@ package com.azure.spring.cloud.autoconfigure.resourcemanager; +import com.azure.messaging.eventhubs.EventHubClientBuilder; import com.azure.resourcemanager.AzureResourceManager; import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnMissingProperty; import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubProperties; @@ -11,6 +12,7 @@ import com.azure.spring.eventhubs.provisioning.arm.DefaultEventHubProvisioner; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -19,7 +21,8 @@ /** * */ -@ConditionalOnProperty(value = "spring.cloud.azure.eventhubs.enabled", havingValue = "true", matchIfMissing = true) +@ConditionalOnProperty(prefix = AzureEventHubProperties.PREFIX, value = "enabled", havingValue = "true", matchIfMissing = true) +@ConditionalOnClass(EventHubClientBuilder.class) @ConditionalOnBean(AzureResourceManager.class) @AutoConfigureAfter(AzureResourceManagerAutoConfiguration.class) public class AzureEventHubResourceManagerAutoConfiguration extends AzureServiceResourceManagerConfigurationBase { @@ -34,8 +37,8 @@ public AzureEventHubResourceManagerAutoConfiguration(AzureResourceManager azureR @Bean @ConditionalOnMissingBean - @ConditionalOnProperty("spring.cloud.azure.eventhubs.namespace") - @ConditionalOnMissingProperty("spring.cloud.azure.eventhubs.connection-string") + @ConditionalOnProperty(prefix = AzureEventHubProperties.PREFIX, value = "namespace") + @ConditionalOnMissingProperty(prefix = AzureEventHubProperties.PREFIX, value = "connection-string") @Order public EventHubArmConnectionStringProvider eventHubArmConnectionStringProvider() { diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfiguration.java index 92becb03eac64..4c03dee18189f 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfiguration.java @@ -3,6 +3,7 @@ package com.azure.spring.cloud.autoconfigure.resourcemanager; +import com.azure.messaging.servicebus.ServiceBusClientBuilder; import com.azure.resourcemanager.AzureResourceManager; import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnMissingProperty; import com.azure.spring.cloud.autoconfigure.servicebus.properties.AzureServiceBusProperties; @@ -13,6 +14,7 @@ import com.azure.spring.servicebus.provisioning.arm.DefaultServiceBusTopicProvisioner; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -22,7 +24,8 @@ * An auto-configuration for Service Bus * */ -@ConditionalOnProperty(value = "spring.cloud.azure.servicebus.enabled", havingValue = "true", matchIfMissing = true) +@ConditionalOnProperty(prefix = AzureServiceBusProperties.PREFIX, value = "enabled", havingValue = "true", matchIfMissing = true) +@ConditionalOnClass(ServiceBusClientBuilder.class) @ConditionalOnBean(AzureResourceManager.class) @AutoConfigureAfter(AzureResourceManagerAutoConfiguration.class) public class AzureServiceBusResourceManagerAutoConfiguration extends AzureServiceResourceManagerConfigurationBase { @@ -50,7 +53,7 @@ public ServiceBusTopicProvisioner serviceBusTopicProvisioner() { @Bean @ConditionalOnMissingBean @ConditionalOnProperty(prefix = AzureServiceBusProperties.PREFIX, value = "namespace") - @ConditionalOnMissingProperty("spring.cloud.azure.servicebus.connection-string") + @ConditionalOnMissingProperty(prefix = AzureServiceBusProperties.PREFIX, value = "connection-string") @Order public ServiceBusArmConnectionStringProvider serviceBusArmConnectionStringProvider() { return new ServiceBusArmConnectionStringProvider(this.azureResourceManager, diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfiguration.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfiguration.java index 7b7f58d47f3c9..152628917732a 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfiguration.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfiguration.java @@ -6,8 +6,10 @@ import com.azure.resourcemanager.AzureResourceManager; import com.azure.spring.cloud.autoconfigure.storage.queue.AzureStorageQueueProperties; import com.azure.spring.cloud.resourcemanager.connectionstring.StorageQueueArmConnectionStringProvider; +import com.azure.storage.queue.QueueServiceClientBuilder; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -19,6 +21,7 @@ * @author Warren Zhu */ @ConditionalOnProperty(prefix = AzureStorageQueueProperties.PREFIX, value = "enabled", matchIfMissing = true) +@ConditionalOnClass(QueueServiceClientBuilder.class) @ConditionalOnBean(AzureResourceManager.class) @AutoConfigureAfter(AzureResourceManagerAutoConfiguration.class) public class AzureStorageQueueResourceManagerAutoConfiguration extends AzureServiceResourceManagerConfigurationBase { 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 beb5f2e55f458..e826d42b5635c 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 @@ -27,7 +27,7 @@ class AzureBlobCheckpointStoreConfigurationTest { @Test - void configureWithoutBlobCheckpointStore() { + void configureWithoutBlobCheckpointStoreClass() { this.contextRunner .withClassLoader(new FilteredClassLoader(BlobCheckpointStore.class)) .withPropertyValues( @@ -38,17 +38,8 @@ void configureWithoutBlobCheckpointStore() { } @Test - void configureWithoutContainerName() { + void configureWithoutRequiredProperties() { this.contextRunner - .withPropertyValues("spring.cloud.azure.eventhubs.processor.checkpoint-store.account-name=sa") - .run(context -> assertThat(context).doesNotHaveBean(AzureBlobCheckpointStoreConfiguration.class)); - } - - - @Test - void configureWithoutAccountName() { - this.contextRunner - .withPropertyValues("spring.cloud.azure.eventhubs.processor.checkpoint-store.container-name=abc") .run(context -> assertThat(context).doesNotHaveBean(AzureBlobCheckpointStoreConfiguration.class)); } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfigurationTest.java index 840b5c442a3f0..62940bb015fc8 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfigurationTest.java @@ -3,7 +3,50 @@ package com.azure.spring.cloud.autoconfigure.resourcemanager; +import com.azure.messaging.eventhubs.EventHubClientBuilder; +import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubProperties; +import com.azure.spring.cloud.resourcemanager.connectionstring.EventHubArmConnectionStringProvider; +import com.azure.spring.eventhubs.provisioning.EventHubProvisioner; +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 AzureEventHubResourceManagerAutoConfigurationTest { - // TODO (xiada): add tests + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(AzureEventHubResourceManagerAutoConfiguration.class)); + + private final String connectionString = "connection-string=Endpoint=sb://eventhub-test-1" + + ".servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;" + + "SharedAccessKey=ByyyxxxUw="; + + @Test + void testEventHubResourceManagerDisabled() { + this.contextRunner + .withPropertyValues("spring.cloud.azure.eventhubs.enabled=false") + .run(context -> { + assertThat(context).doesNotHaveBean(EventHubArmConnectionStringProvider.class); + assertThat(context).doesNotHaveBean(EventHubProvisioner.class); + }); + } + + @Test + void testEventHubResourceManagerWithoutEventHubClientBuilderClass() { + this.contextRunner + .withClassLoader(new FilteredClassLoader(EventHubClientBuilder.class)) + .run(context -> { + assertThat(context).doesNotHaveBean(EventHubArmConnectionStringProvider.class); + assertThat(context).doesNotHaveBean(EventHubProvisioner.class); + }); + } + + @Test + void testEventHubArmConnectionStringProviderBeanDisabled() { + this.contextRunner + .withPropertyValues(AzureEventHubProperties.PREFIX + "." + connectionString) + .run(context -> assertThat(context).doesNotHaveBean(EventHubArmConnectionStringProvider.class)); + } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureResourceManagerAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureResourceManagerAutoConfigurationTest.java index 034056bc2352c..67763be401e2b 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureResourceManagerAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureResourceManagerAutoConfigurationTest.java @@ -5,13 +5,18 @@ import com.azure.core.management.profile.AzureProfile; import com.azure.resourcemanager.AzureResourceManager; +import com.azure.spring.cloud.autoconfigure.context.AzureGlobalPropertiesAutoConfiguration; import com.azure.spring.core.properties.resource.AzureResourceMetadata; +import org.junit.jupiter.api.Assertions; 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 com.azure.core.management.AzureEnvironment.AZURE; +import static com.azure.core.management.AzureEnvironment.AZURE_CHINA; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; class AzureResourceManagerAutoConfigurationTest { @@ -60,4 +65,44 @@ void testWithoutAzureResourceMetadataClass() { .run(context -> assertThat(context).doesNotHaveBean(AzureProfile.class)); } + @Test + void testAzureProfileWithAzureDefault() { + this.contextRunner + .withUserConfiguration(AzureGlobalPropertiesAutoConfiguration.class) + .withBean(AzureResourceManager.class, AzureResourceManagerExt::getAzureResourceManager) + .withPropertyValues( + "spring.cloud.azure.profile.tenant-id=test-tenant-id", + "spring.cloud.azure.profile.subscription-id=test-subscription-id" + ) + .run(context -> { + assertThat(context).hasSingleBean(AzureProfile.class); + AzureProfile azureProfile = context.getBean(AzureProfile.class); + Assertions.assertEquals(azureProfile.getEnvironment().getActiveDirectoryEndpoint(), + AZURE.getActiveDirectoryEndpoint()); + }); + } + + @Test + void testAzureProfileWithAzureChina() { + this.contextRunner + .withUserConfiguration(AzureGlobalPropertiesAutoConfiguration.class) + .withBean(AzureResourceManager.class, AzureResourceManagerExt::getAzureResourceManager) + .withPropertyValues( + "spring.cloud.azure.profile.tenant-id=test-tenant-id", + "spring.cloud.azure.profile.subscription-id=test-subscription-id", + "spring.cloud.azure.profile.cloud=AZURE_CHINA" + ) + .run(context -> { + assertThat(context).hasSingleBean(AzureProfile.class); + AzureProfile azureProfile = context.getBean(AzureProfile.class); + Assertions.assertEquals(azureProfile.getEnvironment().getActiveDirectoryEndpoint(), + AZURE_CHINA.getActiveDirectoryEndpoint()); + }); + } + + static class AzureResourceManagerExt { + static AzureResourceManager getAzureResourceManager() { + return mock(AzureResourceManager.class); + } + } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfigurationTest.java new file mode 100644 index 0000000000000..151a0d5938255 --- /dev/null +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfigurationTest.java @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.cloud.autoconfigure.resourcemanager; + +import com.azure.messaging.servicebus.ServiceBusClientBuilder; +import com.azure.spring.cloud.autoconfigure.servicebus.properties.AzureServiceBusProperties; +import com.azure.spring.cloud.resourcemanager.connectionstring.ServiceBusArmConnectionStringProvider; +import com.azure.spring.servicebus.provisioning.ServiceBusQueueProvisioner; +import com.azure.spring.servicebus.provisioning.ServiceBusTopicProvisioner; +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 AzureServiceBusResourceManagerAutoConfigurationTest { + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(AzureServiceBusResourceManagerAutoConfiguration.class)); + + private final String connectionString = "connection-string=Endpoint=sb://test.servicebus.windows.net/;" + + "SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ByyyxxxUw="; + + @Test + void testServiceBusResourceManagerDisabled() { + this.contextRunner + .withPropertyValues("spring.cloud.azure.servicebus.enabled=false") + .run(context -> { + assertThat(context).doesNotHaveBean(ServiceBusTopicProvisioner.class); + assertThat(context).doesNotHaveBean(ServiceBusQueueProvisioner.class); + }); + } + + @Test + void testServiceBusResourceManagerWithoutServiceBusClientBuilderClass() { + this.contextRunner + .withClassLoader(new FilteredClassLoader(ServiceBusClientBuilder.class)) + .run(context -> { + assertThat(context).doesNotHaveBean(ServiceBusTopicProvisioner.class); + assertThat(context).doesNotHaveBean(ServiceBusQueueProvisioner.class); + }); + } + + @Test + void testServiceBusArmConnectionStringProviderBeanDisabled() { + this.contextRunner + .withPropertyValues(AzureServiceBusProperties.PREFIX + "." + connectionString) + .run(context -> assertThat(context).doesNotHaveBean(ServiceBusArmConnectionStringProvider.class)); + } +} diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfigurationTest.java new file mode 100644 index 0000000000000..011ff8661b14d --- /dev/null +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfigurationTest.java @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.cloud.autoconfigure.resourcemanager; + +import com.azure.spring.cloud.autoconfigure.storage.queue.AzureStorageQueueProperties; +import com.azure.spring.cloud.resourcemanager.connectionstring.StorageQueueArmConnectionStringProvider; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AzureStorageQueueResourceManagerAutoConfigurationTest { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(AzureStorageQueueResourceManagerAutoConfiguration.class)); + + @Test + void testStorageQueueResourceManagerDisabled() { + this.contextRunner + .withPropertyValues(AzureStorageQueueProperties.PREFIX + "enabled=false") + .run(context -> assertThat(context).doesNotHaveBean(StorageQueueArmConnectionStringProvider.class)); + } +} From 8f45d16e01ee49a189f3a75c1ca127f6f769ec80 Mon Sep 17 00:00:00 2001 From: Moary Chen Date: Fri, 5 Nov 2021 19:31:25 +0800 Subject: [PATCH 2/3] reset to the default value --- .../eventhubs/AzureBlobCheckpointStoreConfiguration.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 313c5c937e510..7e42ad661dbe6 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 @@ -55,10 +55,10 @@ public BlobCheckpointStore blobCheckpointStore( @ConditionalOnMissingBean public BlobCheckpointStoreContainerInitializer blobCheckpointStoreContainerInitializer() { return containerAsyncClient -> { - if (Boolean.FALSE.equals(containerAsyncClient.exists().block(Duration.ofSeconds(300)))) { + if (Boolean.FALSE.equals(containerAsyncClient.exists().block(Duration.ofSeconds(3)))) { LOGGER.debug("The checkpoint store container [{}] doesn't exist, will create the blob container now.", containerAsyncClient.getBlobContainerName()); - containerAsyncClient.create().block(Duration.ofSeconds(300)); + containerAsyncClient.create().block(Duration.ofSeconds(3)); } }; } From 80f7a785dff1d32e7a979e6f36e213d01cce2c6e Mon Sep 17 00:00:00 2001 From: Moary Chen Date: Tue, 9 Nov 2021 16:10:36 +0800 Subject: [PATCH 3/3] fix comments --- .../properties/AzureGlobalProperties.java | 4 ---- ...ntHubResourceManagerAutoConfigurationTest.java | 12 ++++++++++++ ...AzureResourceManagerAutoConfigurationTest.java | 11 ++--------- ...ceBusResourceManagerAutoConfigurationTest.java | 15 +++++++++++++++ ...QueueResourceManagerAutoConfigurationTest.java | 13 +++++++++++++ .../resourcemanager/TestAzureResourceManager.java | 15 +++++++++++++++ 6 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/TestAzureResourceManager.java diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/properties/AzureGlobalProperties.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/properties/AzureGlobalProperties.java index 0f1532ce6e972..23702c039102b 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/properties/AzureGlobalProperties.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/properties/AzureGlobalProperties.java @@ -9,15 +9,11 @@ import com.azure.spring.cloud.autoconfigure.properties.core.proxy.ProxyCP; import com.azure.spring.cloud.autoconfigure.properties.core.retry.RetryCP; import com.azure.spring.core.properties.AzureProperties; -import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; -import static com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties.PREFIX; - /** * */ -@ConfigurationProperties(PREFIX) public class AzureGlobalProperties implements AzureProperties { public static final String PREFIX = "spring.cloud.azure"; diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfigurationTest.java index 62940bb015fc8..7fbb1d947ff0b 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureEventHubResourceManagerAutoConfigurationTest.java @@ -4,6 +4,8 @@ package com.azure.spring.cloud.autoconfigure.resourcemanager; import com.azure.messaging.eventhubs.EventHubClientBuilder; +import com.azure.resourcemanager.AzureResourceManager; +import com.azure.spring.cloud.autoconfigure.context.AzureGlobalPropertiesAutoConfiguration; import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubProperties; import com.azure.spring.cloud.resourcemanager.connectionstring.EventHubArmConnectionStringProvider; import com.azure.spring.eventhubs.provisioning.EventHubProvisioner; @@ -49,4 +51,14 @@ void testEventHubArmConnectionStringProviderBeanDisabled() { .withPropertyValues(AzureEventHubProperties.PREFIX + "." + connectionString) .run(context -> assertThat(context).doesNotHaveBean(EventHubArmConnectionStringProvider.class)); } + + @Test + void testAzureEventHubResourceManagerAutoConfigurationBeans() { + this.contextRunner + .withUserConfiguration(AzureGlobalPropertiesAutoConfiguration.class, + AzureResourceManagerAutoConfiguration.class) + .withBean(AzureResourceManager.class, TestAzureResourceManager::getAzureResourceManager) + .withBean(AzureEventHubProperties.class, AzureEventHubProperties::new) + .run(context -> assertThat(context).hasSingleBean(EventHubProvisioner.class)); + } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureResourceManagerAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureResourceManagerAutoConfigurationTest.java index 67763be401e2b..91fc42aaa502e 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureResourceManagerAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureResourceManagerAutoConfigurationTest.java @@ -16,7 +16,6 @@ import static com.azure.core.management.AzureEnvironment.AZURE; import static com.azure.core.management.AzureEnvironment.AZURE_CHINA; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; class AzureResourceManagerAutoConfigurationTest { @@ -69,7 +68,7 @@ void testWithoutAzureResourceMetadataClass() { void testAzureProfileWithAzureDefault() { this.contextRunner .withUserConfiguration(AzureGlobalPropertiesAutoConfiguration.class) - .withBean(AzureResourceManager.class, AzureResourceManagerExt::getAzureResourceManager) + .withBean(AzureResourceManager.class, TestAzureResourceManager::getAzureResourceManager) .withPropertyValues( "spring.cloud.azure.profile.tenant-id=test-tenant-id", "spring.cloud.azure.profile.subscription-id=test-subscription-id" @@ -86,7 +85,7 @@ void testAzureProfileWithAzureDefault() { void testAzureProfileWithAzureChina() { this.contextRunner .withUserConfiguration(AzureGlobalPropertiesAutoConfiguration.class) - .withBean(AzureResourceManager.class, AzureResourceManagerExt::getAzureResourceManager) + .withBean(AzureResourceManager.class, TestAzureResourceManager::getAzureResourceManager) .withPropertyValues( "spring.cloud.azure.profile.tenant-id=test-tenant-id", "spring.cloud.azure.profile.subscription-id=test-subscription-id", @@ -99,10 +98,4 @@ void testAzureProfileWithAzureChina() { AZURE_CHINA.getActiveDirectoryEndpoint()); }); } - - static class AzureResourceManagerExt { - static AzureResourceManager getAzureResourceManager() { - return mock(AzureResourceManager.class); - } - } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfigurationTest.java index 151a0d5938255..459e280808bd4 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureServiceBusResourceManagerAutoConfigurationTest.java @@ -4,6 +4,8 @@ package com.azure.spring.cloud.autoconfigure.resourcemanager; import com.azure.messaging.servicebus.ServiceBusClientBuilder; +import com.azure.resourcemanager.AzureResourceManager; +import com.azure.spring.cloud.autoconfigure.context.AzureGlobalPropertiesAutoConfiguration; import com.azure.spring.cloud.autoconfigure.servicebus.properties.AzureServiceBusProperties; import com.azure.spring.cloud.resourcemanager.connectionstring.ServiceBusArmConnectionStringProvider; import com.azure.spring.servicebus.provisioning.ServiceBusQueueProvisioner; @@ -48,4 +50,17 @@ void testServiceBusArmConnectionStringProviderBeanDisabled() { .withPropertyValues(AzureServiceBusProperties.PREFIX + "." + connectionString) .run(context -> assertThat(context).doesNotHaveBean(ServiceBusArmConnectionStringProvider.class)); } + + @Test + void testAzureServiceBusResourceManagerAutoConfigurationBeans() { + this.contextRunner + .withUserConfiguration(AzureGlobalPropertiesAutoConfiguration.class, + AzureResourceManagerAutoConfiguration.class) + .withBean(AzureResourceManager.class, TestAzureResourceManager::getAzureResourceManager) + .withBean(AzureServiceBusProperties.class, AzureServiceBusProperties::new) + .run(context -> { + assertThat(context).hasSingleBean(ServiceBusTopicProvisioner.class); + assertThat(context).hasSingleBean(ServiceBusQueueProvisioner.class); + }); + } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfigurationTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfigurationTest.java index 011ff8661b14d..f037c3135cc5c 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfigurationTest.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/AzureStorageQueueResourceManagerAutoConfigurationTest.java @@ -3,6 +3,8 @@ package com.azure.spring.cloud.autoconfigure.resourcemanager; +import com.azure.resourcemanager.AzureResourceManager; +import com.azure.spring.cloud.autoconfigure.context.AzureGlobalPropertiesAutoConfiguration; import com.azure.spring.cloud.autoconfigure.storage.queue.AzureStorageQueueProperties; import com.azure.spring.cloud.resourcemanager.connectionstring.StorageQueueArmConnectionStringProvider; import org.junit.jupiter.api.Test; @@ -22,4 +24,15 @@ void testStorageQueueResourceManagerDisabled() { .withPropertyValues(AzureStorageQueueProperties.PREFIX + "enabled=false") .run(context -> assertThat(context).doesNotHaveBean(StorageQueueArmConnectionStringProvider.class)); } + + @Test + void testAzureServiceBusResourceManagerAutoConfigurationBeans() { + this.contextRunner + .withUserConfiguration(AzureGlobalPropertiesAutoConfiguration.class, + AzureResourceManagerAutoConfiguration.class) + .withBean(AzureResourceManager.class, TestAzureResourceManager::getAzureResourceManager) + .withBean(AzureStorageQueueProperties.class, AzureStorageQueueProperties::new) + .withPropertyValues(AzureStorageQueueProperties.PREFIX + ".account-name=test-account") + .run(context -> assertThat(context).hasSingleBean(StorageQueueArmConnectionStringProvider.class)); + } } diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/TestAzureResourceManager.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/TestAzureResourceManager.java new file mode 100644 index 0000000000000..2d15ee7542e5f --- /dev/null +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/resourcemanager/TestAzureResourceManager.java @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.cloud.autoconfigure.resourcemanager; + +import com.azure.resourcemanager.AzureResourceManager; + +import static org.mockito.Mockito.mock; + + +class TestAzureResourceManager { + static AzureResourceManager getAzureResourceManager() { + return mock(AzureResourceManager.class); + } +}