Browse Source

add unit tests

pull/20747/head
liangshiwei 2 years ago
parent
commit
fdcba83c60
  1. 62
      framework/src/Volo.Abp.BlobStoring.Google/Volo/Abp/BlobStoring/Google/GoogleBlobProvider.cs
  2. 66
      framework/test/Volo.Abp.BlobStoring.Google.Tests/Volo/Abp/BlobStoring/Google/AbpBlobStoringGoogleTestModule.cs
  3. 13
      framework/test/Volo.Abp.BlobStoring.Google.Tests/Volo/Abp/BlobStoring/Google/GoogleBlobContainer_Tests.cs
  4. 56
      framework/test/Volo.Abp.BlobStoring.Google.Tests/Volo/Abp/BlobStoring/Google/GoogleBlobNameCalculator_Tests.cs

62
framework/src/Volo.Abp.BlobStoring.Google/Volo/Abp/BlobStoring/Google/GoogleBlobProvider.cs

@ -31,17 +31,13 @@ public class GoogleBlobProvider : BlobProviderBase, ITransientDependency
{ {
throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{GetContainerName(args)}'! Set {nameof(args.OverrideExisting)} if it should be overwritten."); throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{GetContainerName(args)}'! Set {nameof(args.OverrideExisting)} if it should be overwritten.");
} }
if (configuration.CreateContainerIfNotExists) if (configuration.CreateContainerIfNotExists)
{ {
await CreateContainerIfNotExists(args); await CreateContainerIfNotExists(args);
} }
await storageClient.UploadObjectAsync(containerName, blobName, contentType: "application/octet-stream", args.BlobStream, await storageClient.UploadObjectAsync(containerName, blobName, contentType: "application/octet-stream", args.BlobStream);
new UploadObjectOptions
{
IfGenerationMatch = !args.OverrideExisting ? 0 : 1
});
} }
public async override Task<bool> DeleteAsync(BlobProviderDeleteArgs args) public async override Task<bool> DeleteAsync(BlobProviderDeleteArgs args)
@ -49,8 +45,16 @@ public class GoogleBlobProvider : BlobProviderBase, ITransientDependency
var storageClient = await GetStorageClientClientAsync(args); var storageClient = await GetStorageClientClientAsync(args);
var blobName = GoogleBlobNameCalculator.Calculate(args); var blobName = GoogleBlobNameCalculator.Calculate(args);
var containerName = GetContainerName(args); var containerName = GetContainerName(args);
await storageClient.DeleteObjectAsync(containerName, blobName); try
{
await storageClient.DeleteObjectAsync(containerName, blobName);
}
catch (GoogleApiException e) when (e.HttpStatusCode == HttpStatusCode.NotFound)
{
return true;
}
return true; return true;
} }
@ -66,13 +70,12 @@ public class GoogleBlobProvider : BlobProviderBase, ITransientDependency
var storageClient = await GetStorageClientClientAsync(args); var storageClient = await GetStorageClientClientAsync(args);
var blobName = GoogleBlobNameCalculator.Calculate(args); var blobName = GoogleBlobNameCalculator.Calculate(args);
var containerName = GetContainerName(args); var containerName = GetContainerName(args);
var @object = await storageClient.GetObjectAsync(containerName, blobName); if(!await BlobExistsAsync(args, blobName))
if (@object == null)
{ {
return null; return null;
} }
var stream = new MemoryStream(); var stream = new MemoryStream();
await storageClient.DownloadObjectAsync(containerName, blobName, stream); await storageClient.DownloadObjectAsync(containerName, blobName, stream);
@ -92,23 +95,48 @@ public class GoogleBlobProvider : BlobProviderBase, ITransientDependency
protected virtual async Task<bool> BlobExistsAsync(BlobProviderArgs args, string blobName) protected virtual async Task<bool> BlobExistsAsync(BlobProviderArgs args, string blobName)
{ {
var @object = await (await GetStorageClientClientAsync(args)).GetObjectAsync(args.ContainerName, blobName); var storageClient = await GetStorageClientClientAsync(args);
if(!await ContainerExistsAsync(args, storageClient))
return @object != null; {
return false;
}
try
{
await storageClient.GetObjectAsync(GetContainerName(args), blobName);
}
catch (GoogleApiException e) when (e.HttpStatusCode == HttpStatusCode.NotFound)
{
return false;
}
return true;
} }
protected virtual async Task CreateContainerIfNotExists(BlobProviderArgs args) protected virtual async Task CreateContainerIfNotExists(BlobProviderArgs args)
{ {
var storageClient = await GetStorageClientClientAsync(args); var storageClient = await GetStorageClientClientAsync(args);
var configuration = args.Configuration.GetGoogleConfiguration(); var configuration = args.Configuration.GetGoogleConfiguration();
if(await ContainerExistsAsync(args, storageClient))
{
return;
}
await storageClient.CreateBucketAsync(configuration.ProjectId, GetContainerName(args));
}
protected virtual async Task<bool> ContainerExistsAsync(BlobProviderArgs args, StorageClient client)
{
try try
{ {
await storageClient.GetBucketAsync(args.ContainerName); await client.GetBucketAsync(GetContainerName(args));
} }
catch (GoogleApiException e) when (e.HttpStatusCode == HttpStatusCode.NotFound) catch (GoogleApiException e) when (e.HttpStatusCode == HttpStatusCode.NotFound)
{ {
await storageClient.CreateBucketAsync(configuration.ProjectId, GetContainerName(args)); return false;
} }
return true;
} }
protected virtual async Task<StorageClient> GetStorageClientClientAsync(BlobProviderArgs args) protected virtual async Task<StorageClient> GetStorageClientClientAsync(BlobProviderArgs args)

66
framework/test/Volo.Abp.BlobStoring.Google.Tests/Volo/Abp/BlobStoring/Google/AbpBlobStoringGoogleTestModule.cs

@ -1,4 +1,7 @@
using System; using System;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Storage.V1;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
@ -25,9 +28,11 @@ public class AbpBlobStoringGoogleTestModule : AbpModule
{ {
private const string UserSecretsId = "9f0d2c00-80c1-435b-bfab-2c39c8249091"; private const string UserSecretsId = "9f0d2c00-80c1-435b-bfab-2c39c8249091";
private string _connectionString; private string _clientEmail;
private string _projectId;
private string _privateKey;
private readonly string _randomContainerName = "abp-azure-test-container-" + Guid.NewGuid().ToString("N"); private readonly string _randomContainerName = "abp-test-container-" + Guid.NewGuid().ToString("N");
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
@ -36,25 +41,50 @@ public class AbpBlobStoringGoogleTestModule : AbpModule
builder.AddUserSecrets(UserSecretsId); builder.AddUserSecrets(UserSecretsId);
})); }));
// var configuration = context.Services.GetConfiguration(); var configuration = context.Services.GetConfiguration();
// _connectionString = configuration["Azure:ConnectionString"]; _clientEmail = configuration["Google:ClientEmail"];
// _projectId = configuration["Google:ProjectId"];
// Configure<AbpBlobStoringOptions>(options => _privateKey = configuration["Google:PrivateKey"];
// {
// options.Containers.ConfigureAll((containerName, containerConfiguration) => Configure<AbpBlobStoringOptions>(options =>
// { {
// containerConfiguration.UseAzure(azure => options.Containers.ConfigureAll((containerName, containerConfiguration) =>
// { {
// azure.ConnectionString = _connectionString; containerConfiguration.UseGoogle(google =>
// azure.ContainerName = _randomContainerName; {
// azure.CreateContainerIfNotExists = true; google.ClientEmail = _clientEmail;
// }); google.ProjectId = _projectId = "wide-origin-296910";
// }); google.PrivateKey = _privateKey;
// }); google.ContainerName = _randomContainerName;
google.CreateContainerIfNotExists = true;
});
});
});
} }
public override void OnApplicationShutdown(ApplicationShutdownContext context) public override void OnApplicationShutdown(ApplicationShutdownContext context)
{ {
var googleCredential = GoogleCredential.FromServiceAccountCredential(
new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(_clientEmail)
{
ProjectId = _projectId
}
.FromPrivateKey(_privateKey)
));
var client = StorageClient.Create(googleCredential);
try
{
client.DeleteBucket(_randomContainerName, new DeleteBucketOptions
{
DeleteObjects = true
});
}
catch (Exception e)
{
// ignored
}
} }
} }

13
framework/test/Volo.Abp.BlobStoring.Google.Tests/Volo/Abp/BlobStoring/Google/GoogleBlobContainer_Tests.cs

@ -0,0 +1,13 @@
using Xunit;
namespace Volo.Abp.BlobStoring.Google;
/*
//Please set the correct connection string in secrets.json and continue the test.
public class GoogleBlobContainer_Tests : BlobContainer_Tests<AbpBlobStoringGoogleTestModule>
{
public GoogleBlobContainer_Tests()
{
}
}
*/

56
framework/test/Volo.Abp.BlobStoring.Google.Tests/Volo/Abp/BlobStoring/Google/GoogleBlobNameCalculator_Tests.cs

@ -0,0 +1,56 @@
using System;
using Shouldly;
using Volo.Abp.MultiTenancy;
using Xunit;
namespace Volo.Abp.BlobStoring.Google;
public class GoogleBlobNameCalculator_Tests : AbpBlobStoringGoogleTestCommonBase
{
private readonly IGoogleBlobNameCalculator _calculator;
private readonly ICurrentTenant _currentTenant;
private const string AzureContainerName = "/";
private const string AzureSeparator = "/";
public GoogleBlobNameCalculator_Tests()
{
_calculator = GetRequiredService<IGoogleBlobNameCalculator>();
_currentTenant = GetRequiredService<ICurrentTenant>();
}
[Fact]
public void Default_Settings()
{
_calculator.Calculate(
GetArgs("my-container", "my-blob")
).ShouldBe($"host{AzureSeparator}my-blob");
}
[Fact]
public void Default_Settings_With_TenantId()
{
var tenantId = Guid.NewGuid();
using (_currentTenant.Change(tenantId))
{
_calculator.Calculate(
GetArgs("my-container", "my-blob")
).ShouldBe($"tenants{AzureSeparator}{tenantId:D}{AzureSeparator}my-blob");
}
}
private static BlobProviderArgs GetArgs(
string containerName,
string blobName)
{
return new BlobProviderGetArgs(
containerName,
new BlobContainerConfiguration().UseGoogle(x =>
{
x.ContainerName = containerName;
}),
blobName
);
}
}
Loading…
Cancel
Save