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 3b64f5d73cf55..874faf6a4ac2e 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 @@ -15,6 +15,8 @@ import com.azure.spring.service.implementation.storage.fileshare.ShareServiceClientBuilderFactory; import com.azure.storage.file.share.ShareAsyncClient; import com.azure.storage.file.share.ShareClient; +import com.azure.storage.file.share.ShareDirectoryAsyncClient; +import com.azure.storage.file.share.ShareDirectoryClient; import com.azure.storage.file.share.ShareFileAsyncClient; import com.azure.storage.file.share.ShareFileClient; import com.azure.storage.file.share.ShareServiceAsyncClient; @@ -79,33 +81,49 @@ ShareServiceClientBuilder shareServiceClientBuilder(ShareServiceClientBuilderFac @Bean @ConditionalOnMissingBean - @ConditionalOnProperty(prefix = AzureStorageFileShareProperties.PREFIX, name = "file-name") - public ShareFileAsyncClient shareFileAsyncClient(AzureStorageFileShareProperties properties, - ShareAsyncClient shareAsyncClient) { - return shareAsyncClient.getFileClient(properties.getFileName()); + @ConditionalOnProperty(prefix = AzureStorageFileShareProperties.PREFIX, name = "share-name") + public ShareAsyncClient shareAsyncClient(AzureStorageFileShareProperties properties, + ShareServiceAsyncClient shareServiceAsyncClient) { + return shareServiceAsyncClient.getShareAsyncClient(properties.getShareName()); } @Bean @ConditionalOnMissingBean @ConditionalOnProperty(prefix = AzureStorageFileShareProperties.PREFIX, name = "share-name") - public ShareAsyncClient shareAsyncClient(AzureStorageFileShareProperties properties, - ShareServiceAsyncClient shareServiceAsyncClient) { - return shareServiceAsyncClient.getShareAsyncClient(properties.getShareName()); + public ShareClient shareClient(AzureStorageFileShareProperties properties, ShareServiceClient shareServiceClient) { + return shareServiceClient.getShareClient(properties.getShareName()); + } + + @Bean + @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = AzureStorageFileShareProperties.PREFIX, name = "file-path") + public ShareFileAsyncClient shareFileAsyncClient(AzureStorageFileShareProperties properties, + ShareAsyncClient shareAsyncClient) { + return shareAsyncClient.getFileClient(properties.getFilePath()); } @Bean @ConditionalOnMissingBean - @ConditionalOnProperty(prefix = AzureStorageFileShareProperties.PREFIX, name = "file-name") + @ConditionalOnProperty(prefix = AzureStorageFileShareProperties.PREFIX, name = "file-path") public ShareFileClient shareFileClient(AzureStorageFileShareProperties properties, ShareClient shareClient) { - return shareClient.getFileClient(properties.getFileName()); + return shareClient.getFileClient(properties.getFilePath()); } @Bean @ConditionalOnMissingBean - @ConditionalOnProperty(prefix = AzureStorageFileShareProperties.PREFIX, name = "share-name") - public ShareClient shareClient(AzureStorageFileShareProperties properties, ShareServiceClient shareServiceClient) { - return shareServiceClient.getShareClient(properties.getShareName()); + @ConditionalOnProperty(prefix = AzureStorageFileShareProperties.PREFIX, name = "directory-path") + public ShareDirectoryAsyncClient shareDirectoryAsyncClient(AzureStorageFileShareProperties properties, + ShareAsyncClient shareAsyncClient) { + return shareAsyncClient.getDirectoryClient(properties.getDirectoryPath()); + } + + @Bean + @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = AzureStorageFileShareProperties.PREFIX, name = "directory-path") + public ShareDirectoryClient shareDirectoryClient(AzureStorageFileShareProperties properties, + ShareClient shareClient) { + return shareClient.getDirectoryClient(properties.getDirectoryPath()); } @Bean diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/properties/AzureStorageFileShareProperties.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/properties/AzureStorageFileShareProperties.java index 13ccd0a6d541c..a23b55bd9775d 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/properties/AzureStorageFileShareProperties.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/storage/fileshare/properties/AzureStorageFileShareProperties.java @@ -24,9 +24,14 @@ public class AzureStorageFileShareProperties extends AzureStorageProperties impl */ private String shareName; /** - * Name of the file. + * Path to the file. For instance, 'directory1/file1'. */ - private String fileName; + private String filePath; + /** + * Path to the directory. For instance, 'directory1/directory2'. + */ + private String directoryPath; + public String getEndpoint() { return endpoint == null ? buildEndpointFromAccountName() : endpoint; @@ -52,11 +57,19 @@ public void setShareName(String shareName) { this.shareName = shareName; } - public String getFileName() { - return fileName; + public String getFilePath() { + return filePath; + } + + public void setFilePath(String filePath) { + this.filePath = filePath; + } + + public String getDirectoryPath() { + return directoryPath; } - public void setFileName(String fileName) { - this.fileName = fileName; + public void setDirectoryPath(String directoryPath) { + this.directoryPath = directoryPath; } } 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 4264d7e2456bc..482420bc6d407 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 @@ -10,6 +10,10 @@ import com.azure.spring.service.implementation.storage.fileshare.ShareServiceClientBuilderFactory; import com.azure.storage.file.share.ShareAsyncClient; import com.azure.storage.file.share.ShareClient; +import com.azure.storage.file.share.ShareDirectoryAsyncClient; +import com.azure.storage.file.share.ShareDirectoryClient; +import com.azure.storage.file.share.ShareFileAsyncClient; +import com.azure.storage.file.share.ShareFileClient; import com.azure.storage.file.share.ShareServiceAsyncClient; import com.azure.storage.file.share.ShareServiceClient; import com.azure.storage.file.share.ShareServiceClientBuilder; @@ -97,6 +101,102 @@ void shareNameNotSetShouldNotConfigureShareClient() { }); } + @Test + void filePathSetShouldConfigureFileClient() { + ShareServiceClient shareServiceClient = mock(ShareServiceClient.class); + ShareClient shareClient = mock(ShareClient.class); + when(shareServiceClient.getShareClient("share1")).thenReturn(shareClient); + when(shareClient.getFileClient("directory1/file1")).thenReturn(mock(ShareFileClient.class)); + + ShareServiceAsyncClient shareServiceAsyncClient = mock(ShareServiceAsyncClient.class); + ShareAsyncClient shareAsyncClient = mock(ShareAsyncClient.class); + when(shareServiceAsyncClient.getShareAsyncClient("share1")).thenReturn(shareAsyncClient); + when(shareAsyncClient.getFileClient("directory1/file1")).thenReturn(mock(ShareFileAsyncClient.class)); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.storage.fileshare.account-name=sa", + "spring.cloud.azure.storage.fileshare.share-name=share1", + "spring.cloud.azure.storage.fileshare.file-path=directory1/file1" + ) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean(ShareServiceClient.class, () -> shareServiceClient) + .withBean(ShareServiceAsyncClient.class, () -> shareServiceAsyncClient) + .run(context -> { + assertThat(context).hasSingleBean(ShareFileClient.class); + assertThat(context).hasSingleBean(ShareFileAsyncClient.class); + }); + } + + @Test + void filePathNotSetShouldNotConfigureFileClient() { + ShareServiceClient shareServiceClient = mock(ShareServiceClient.class); + when(shareServiceClient.getShareClient("share1")).thenReturn(mock(ShareClient.class)); + + ShareServiceAsyncClient shareServiceAsyncClient = mock(ShareServiceAsyncClient.class); + when(shareServiceAsyncClient.getShareAsyncClient("share1")).thenReturn(mock(ShareAsyncClient.class)); + + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.storage.fileshare.account-name=sa", + "spring.cloud.azure.storage.fileshare.share-name=share1" + ) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean(ShareServiceClient.class, () -> mock(ShareServiceClient.class)) + .withBean(ShareServiceAsyncClient.class, () -> mock(ShareServiceAsyncClient.class)) + .run(context -> { + assertThat(context).doesNotHaveBean(ShareFileClient.class); + assertThat(context).doesNotHaveBean(ShareFileAsyncClient.class); + }); + } + + @Test + void directoryPathSetShouldConfigureDirectoryClient() { + ShareServiceClient shareServiceClient = mock(ShareServiceClient.class); + ShareClient shareClient = mock(ShareClient.class); + when(shareServiceClient.getShareClient("share1")).thenReturn(shareClient); + when(shareClient.getDirectoryClient("directory1/directory2")).thenReturn(mock(ShareDirectoryClient.class)); + + ShareServiceAsyncClient shareServiceAsyncClient = mock(ShareServiceAsyncClient.class); + ShareAsyncClient shareAsyncClient = mock(ShareAsyncClient.class); + when(shareServiceAsyncClient.getShareAsyncClient("share1")).thenReturn(shareAsyncClient); + when(shareAsyncClient.getDirectoryClient("directory1/directory2")).thenReturn(mock(ShareDirectoryAsyncClient.class)); + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.storage.fileshare.account-name=sa", + "spring.cloud.azure.storage.fileshare.share-name=share1", + "spring.cloud.azure.storage.fileshare.directory-path=directory1/directory2" + ) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean(ShareServiceClient.class, () -> shareServiceClient) + .withBean(ShareServiceAsyncClient.class, () -> shareServiceAsyncClient) + .run(context -> { + assertThat(context).hasSingleBean(ShareDirectoryClient.class); + assertThat(context).hasSingleBean(ShareDirectoryAsyncClient.class); + }); + } + + @Test + void directoryNameNotSetShouldNotConfigureDirectoryClient() { + ShareServiceClient shareServiceClient = mock(ShareServiceClient.class); + when(shareServiceClient.getShareClient("share1")).thenReturn(mock(ShareClient.class)); + + ShareServiceAsyncClient shareServiceAsyncClient = mock(ShareServiceAsyncClient.class); + when(shareServiceAsyncClient.getShareAsyncClient("share1")).thenReturn(mock(ShareAsyncClient.class)); + + this.contextRunner + .withPropertyValues( + "spring.cloud.azure.storage.fileshare.account-name=sa", + "spring.cloud.azure.storage.fileshare.share-name=share1" + ) + .withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) + .withBean(ShareServiceClient.class, () -> mock(ShareServiceClient.class)) + .withBean(ShareServiceAsyncClient.class, () -> mock(ShareServiceAsyncClient.class)) + .run(context -> { + assertThat(context).doesNotHaveBean(ShareDirectoryClient.class); + assertThat(context).doesNotHaveBean(ShareDirectoryAsyncClient.class); + }); + } + @Test void customizerShouldBeCalled() { ShareServiceClientBuilderCustomizer customizer = new ShareServiceClientBuilderCustomizer();