Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autoconfiguration for file share directoryclient #26645

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down