Browse Source

Completed integration tests for the file blob store

pull/4127/head
Halil İbrahim Kalkan 6 years ago
parent
commit
ed82af47dd
  1. 1
      framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProvider.cs
  2. 57
      framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerExtensions.cs
  3. 16
      framework/src/Volo.Abp.Core/System/IO/AbpStreamExtensions.cs
  4. 15
      framework/src/Volo.Abp.Core/Volo/Abp/AbpException.cs
  5. 15
      framework/src/Volo.Abp.Core/Volo/Abp/AbpInitializationException.cs
  6. 31
      framework/src/Volo.Abp.Core/Volo/Abp/AbpShutdownException.cs
  7. 28
      framework/src/Volo.Abp.Core/Volo/Abp/IO/DirectoryHelper.cs
  8. 4
      framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleManager.cs
  9. 18
      framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/AbpBlobStoringFileSystemTestModule.cs
  10. 57
      framework/test/Volo.Abp.BlobStoring.Tests/Volo/Abp/BlobStoring/BlobContainer_Tests.cs

1
framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProvider.cs

@ -33,7 +33,6 @@ namespace Volo.Abp.BlobStoring.FileSystem
await args.BlobStream.CopyToAsync(
fileStream,
81920, //this is already the default value, but needed to set to be able to pass the cancellationToken
args.CancellationToken
);

57
framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerExtensions.cs

@ -0,0 +1,57 @@
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Volo.Abp.BlobStoring
{
public static class BlobContainerExtensions
{
public static async Task SaveAsync(
this IBlobContainer container,
string name,
byte[] bytes,
bool overrideExisting = false,
CancellationToken cancellationToken = default
)
{
using (var memoryStream = new MemoryStream(bytes))
{
await container.SaveAsync(
name,
memoryStream,
overrideExisting,
cancellationToken
);
}
}
public static async Task<byte[]> GetAllBytesAsync(
this IBlobContainer container,
string name,
CancellationToken cancellationToken = default)
{
using (var stream = await container.GetAsync(name, cancellationToken))
{
return await stream.GetAllBytesAsync(cancellationToken);
}
}
public static async Task<byte[]> GetAllBytesOrNullAsync(
this IBlobContainer container,
string name,
CancellationToken cancellationToken = default)
{
var stream = await container.GetOrNullAsync(name, cancellationToken);
if (stream == null)
{
return null;
}
using (stream)
{
return await stream.GetAllBytesAsync(cancellationToken);
}
}
}
}

16
framework/src/Volo.Abp.Core/System/IO/AbpStreamExtensions.cs

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
namespace System.IO
{
@ -13,13 +14,22 @@ namespace System.IO
}
}
public static async Task<byte[]> GetAllBytesAsync(this Stream stream)
public static async Task<byte[]> GetAllBytesAsync(this Stream stream, CancellationToken cancellationToken = default)
{
using (var memoryStream = new MemoryStream())
{
await stream.CopyToAsync(memoryStream);
await stream.CopyToAsync(memoryStream, cancellationToken);
return memoryStream.ToArray();
}
}
public static Task CopyToAsync(this Stream stream, Stream destination, CancellationToken cancellationToken)
{
return stream.CopyToAsync(
destination,
81920, //this is already the default value, but needed to set to be able to pass the cancellationToken
cancellationToken
);
}
}
}

15
framework/src/Volo.Abp.Core/Volo/Abp/AbpException.cs

@ -8,38 +8,23 @@ namespace Volo.Abp
/// </summary>
public class AbpException : Exception
{
/// <summary>
/// Creates a new <see cref="AbpException"/> object.
/// </summary>
public AbpException()
{
}
/// <summary>
/// Creates a new <see cref="AbpException"/> object.
/// </summary>
/// <param name="message">Exception message</param>
public AbpException(string message)
: base(message)
{
}
/// <summary>
/// Creates a new <see cref="AbpException"/> object.
/// </summary>
/// <param name="message">Exception message</param>
/// <param name="innerException">Inner exception</param>
public AbpException(string message, Exception innerException)
: base(message, innerException)
{
}
/// <summary>
/// Constructor for serializing.
/// </summary>
public AbpException(SerializationInfo serializationInfo, StreamingContext context)
: base(serializationInfo, context)
{

15
framework/src/Volo.Abp.Core/Volo/Abp/AbpInitializationException.cs

@ -5,38 +5,23 @@ namespace Volo.Abp
{
public class AbpInitializationException : AbpException
{
/// <summary>
/// Creates a new <see cref="AbpException"/> object.
/// </summary>
public AbpInitializationException()
{
}
/// <summary>
/// Creates a new <see cref="AbpException"/> object.
/// </summary>
/// <param name="message">Exception message</param>
public AbpInitializationException(string message)
: base(message)
{
}
/// <summary>
/// Creates a new <see cref="AbpException"/> object.
/// </summary>
/// <param name="message">Exception message</param>
/// <param name="innerException">Inner exception</param>
public AbpInitializationException(string message, Exception innerException)
: base(message, innerException)
{
}
/// <summary>
/// Constructor for serializing.
/// </summary>
public AbpInitializationException(SerializationInfo serializationInfo, StreamingContext context)
: base(serializationInfo, context)
{

31
framework/src/Volo.Abp.Core/Volo/Abp/AbpShutdownException.cs

@ -0,0 +1,31 @@
using System;
using System.Runtime.Serialization;
namespace Volo.Abp
{
public class AbpShutdownException : AbpException
{
public AbpShutdownException()
{
}
public AbpShutdownException(string message)
: base(message)
{
}
public AbpShutdownException(string message, Exception innerException)
: base(message, innerException)
{
}
public AbpShutdownException(SerializationInfo serializationInfo, StreamingContext context)
: base(serializationInfo, context)
{
}
}
}

28
framework/src/Volo.Abp.Core/Volo/Abp/IO/DirectoryHelper.cs

@ -16,7 +16,23 @@ namespace Volo.Abp.IO
Directory.CreateDirectory(directory);
}
}
public static void DeleteIfExists(string directory)
{
if (Directory.Exists(directory))
{
Directory.Delete(directory);
}
}
public static void DeleteIfExists(string directory, bool recursive)
{
if (Directory.Exists(directory))
{
Directory.Delete(directory, recursive);
}
}
public static void CreateIfNotExists(DirectoryInfo directory)
{
if (!directory.Exists)
@ -36,7 +52,8 @@ namespace Volo.Abp.IO
);
}
public static bool IsSubDirectoryOf([NotNull] DirectoryInfo parentDirectory, [NotNull] DirectoryInfo childDirectory)
public static bool IsSubDirectoryOf([NotNull] DirectoryInfo parentDirectory,
[NotNull] DirectoryInfo childDirectory)
{
Check.NotNull(parentDirectory, nameof(parentDirectory));
Check.NotNull(childDirectory, nameof(childDirectory));
@ -66,10 +83,7 @@ namespace Volo.Abp.IO
Directory.SetCurrentDirectory(targetDirectory);
return new DisposeAction(() =>
{
Directory.SetCurrentDirectory(currentDirectory);
});
return new DisposeAction(() => { Directory.SetCurrentDirectory(currentDirectory); });
}
}
}
}

4
framework/src/Volo.Abp.Core/Volo/Abp/Modularity/ModuleManager.cs

@ -44,7 +44,7 @@ namespace Volo.Abp.Modularity
}
catch (Exception ex)
{
throw new AbpInitializationException($"An error occurred during the initialize {contributor.GetType().FullName} phase of the module {module.Type.AssemblyQualifiedName}. See the inner exception for details.", ex);
throw new AbpInitializationException($"An error occurred during the initialize {contributor.GetType().FullName} phase of the module {module.Type.AssemblyQualifiedName}: {ex.Message}. See the inner exception for details.", ex);
}
}
}
@ -76,7 +76,7 @@ namespace Volo.Abp.Modularity
}
catch (Exception ex)
{
throw new AbpInitializationException($"An error occurred during the shutdown {contributor.GetType().FullName} phase of the module {module.Type.AssemblyQualifiedName}. See the inner exception for details.", ex);
throw new AbpShutdownException($"An error occurred during the shutdown {contributor.GetType().FullName} phase of the module {module.Type.AssemblyQualifiedName}: {ex.Message}. See the inner exception for details.", ex);
}
}
}

18
framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/AbpBlobStoringFileSystemTestModule.cs

@ -1,5 +1,6 @@
using System;
using System.IO;
using Volo.Abp.IO;
using Volo.Abp.Modularity;
namespace Volo.Abp.BlobStoring.FileSystem
@ -10,6 +11,16 @@ namespace Volo.Abp.BlobStoring.FileSystem
)]
public class AbpBlobStoringFileSystemTestModule : AbpModule
{
private readonly string _testDirectoryPath;
public AbpBlobStoringFileSystemTestModule()
{
_testDirectoryPath = Path.Combine(
Path.GetTempPath(),
Guid.NewGuid().ToString("N")
);
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpBlobStoringOptions>(options =>
@ -18,10 +29,15 @@ namespace Volo.Abp.BlobStoring.FileSystem
{
containerConfiguration.UseFileSystem(fileSystem =>
{
fileSystem.BasePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
fileSystem.BasePath = _testDirectoryPath;
});
});
});
}
public override void OnApplicationShutdown(ApplicationShutdownContext context)
{
DirectoryHelper.DeleteIfExists(_testDirectoryPath, true);
}
}
}

57
framework/test/Volo.Abp.BlobStoring.Tests/Volo/Abp/BlobStoring/BlobContainer_Tests.cs

@ -1,5 +1,4 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Shouldly;
@ -25,21 +24,53 @@ namespace Volo.Abp.BlobStoring
options.UseAutofac();
}
[Fact]
public async Task SaveAsync()
[Theory]
[InlineData("test-blob-1")]
[InlineData("test-blob-1.txt")]
[InlineData("test-folder/test-blob-1")]
public async Task Should_Save_And_Get_Blobs(string blobName)
{
var testContent = "test content".GetBytes();
using (var memoryStream = new MemoryStream(testContent))
{
await Container.SaveAsync("test-blob-1", memoryStream);
}
await Container.SaveAsync(blobName, testContent);
using (var stream = await Container.GetAsync("test-blob-1"))
{
var result = await stream.GetAllBytesAsync();
result.SequenceEqual(testContent).ShouldBeTrue();
}
var result = await Container.GetAllBytesAsync(blobName);
result.SequenceEqual(testContent).ShouldBeTrue();
}
[Theory]
[InlineData("test-blob-1")]
[InlineData("test-blob-1.txt")]
[InlineData("test-folder/test-blob-1")]
public async Task Should_Delete_Saved_Blobs(string blobName)
{
await Container.SaveAsync(blobName, "test content".GetBytes());
(await Container.GetAllBytesAsync(blobName)).ShouldNotBeNull();
await Container.DeleteAsync(blobName);
(await Container.GetAllBytesOrNullAsync(blobName)).ShouldBeNull();
}
[Theory]
[InlineData("test-blob-1")]
[InlineData("test-blob-1.txt")]
[InlineData("test-folder/test-blob-1")]
public async Task Saved_Blobs_Should_Exists(string blobName)
{
await Container.SaveAsync(blobName, "test content".GetBytes());
(await Container.ExistsAsync(blobName)).ShouldBeTrue();
await Container.DeleteAsync(blobName);
(await Container.ExistsAsync(blobName)).ShouldBeFalse();
}
[Theory]
[InlineData("test-blob-1")]
[InlineData("test-blob-1.txt")]
[InlineData("test-folder/test-blob-1")]
public async Task Unknown_Blobs_Should_Not_Exists(string blobName)
{
await Container.DeleteAsync(blobName);
(await Container.ExistsAsync(blobName)).ShouldBeFalse();
}
}
}
Loading…
Cancel
Save