From d9892e4bb2f2db6ac8d938b736cc1052faf4f24c Mon Sep 17 00:00:00 2001 From: Mauro van der Gun Date: Thu, 10 Aug 2023 12:54:13 -0400 Subject: [PATCH 1/6] add initial amazon s3 unit tests --- .../src/AmazonS3Adapter.cs | 23 +- .../src/ModelFactory.cs | 16 +- FileSystem/src/Utilities/PathUtilities.cs | 5 + .../AmazonS3AdapterTest.cs | 303 +++++++++++++++++- .../AzureFileStorageAdapterTest.cs | 16 +- .../MicrosoftOneDriveAdapterTest.cs | 6 + 6 files changed, 339 insertions(+), 30 deletions(-) create mode 100644 Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs diff --git a/FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs b/FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs index 67c33f0..6c9bb95 100644 --- a/FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs +++ b/FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs @@ -7,6 +7,7 @@ using Amazon.S3; using Amazon.S3.Model; using SharpGrip.FileSystem.Exceptions; +using SharpGrip.FileSystem.Extensions; using SharpGrip.FileSystem.Models; using DirectoryNotFoundException = SharpGrip.FileSystem.Exceptions.DirectoryNotFoundException; using FileNotFoundException = SharpGrip.FileSystem.Exceptions.FileNotFoundException; @@ -41,7 +42,7 @@ public override async Task GetFileAsync(string virtualPath, CancellationT { using var response = await client.GetObjectAsync(bucketName, path, cancellationToken); - return ModelFactory.CreateFile(response, virtualPath); + return ModelFactory.CreateFile(response, path, virtualPath); } catch (AmazonS3Exception exception) { @@ -69,15 +70,12 @@ public override async Task GetDirectoryAsync(string virtualPath, Can try { - var prefix = ""; - var pathParts = GetPathParts(path); - - if (pathParts.Length > 1) + if (path == "/") { - prefix = string.Join("/", pathParts.Take(pathParts.Length - 1)) + "/"; + return ModelFactory.CreateDirectory(new S3Object {Key = "/"}, virtualPath); } - var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = prefix}; + var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path}; var response = await client.ListObjectsV2Async(request, cancellationToken); if (response.KeyCount == 0) @@ -106,6 +104,11 @@ public override async Task> GetFilesAsync(string virtualPath await GetDirectoryAsync(virtualPath, cancellationToken); var path = GetPath(virtualPath); + if (!path.EndsWith("/")) + { + path += "/"; + } + try { var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path}; @@ -145,7 +148,7 @@ public override async Task> GetDirectoriesAsync(string v foreach (var item in response.S3Objects) { - var itemName = item.Key.Substring(0, item.Key.Length - path.Length); + var itemName = item.Key.Substring(path.Length).RemoveLeadingForwardSlash(); if (item.Key.EndsWith("/") && itemName.Count(c => c.Equals('/')) == 1) { @@ -192,9 +195,9 @@ public override async Task DeleteDirectoryAsync(string virtualPath, Cancellation try { var deleteObjectsRequest = new DeleteObjectsRequest {BucketName = bucketName}; - var listObjectsRequest = new ListObjectsRequest {BucketName = bucketName, Prefix = path}; + var listObjectsRequest = new ListObjectsV2Request {BucketName = bucketName, Prefix = path}; - var response = await client.ListObjectsAsync(listObjectsRequest, cancellationToken); + var response = await client.ListObjectsV2Async(listObjectsRequest, cancellationToken); foreach (S3Object entry in response.S3Objects) { diff --git a/FileSystem.Adapters.AmazonS3/src/ModelFactory.cs b/FileSystem.Adapters.AmazonS3/src/ModelFactory.cs index f8d28a2..800c51c 100644 --- a/FileSystem.Adapters.AmazonS3/src/ModelFactory.cs +++ b/FileSystem.Adapters.AmazonS3/src/ModelFactory.cs @@ -1,18 +1,19 @@ using System; using System.Linq; using Amazon.S3.Model; +using SharpGrip.FileSystem.Extensions; using SharpGrip.FileSystem.Models; namespace SharpGrip.FileSystem.Adapters.AmazonS3 { public static class ModelFactory { - public static FileModel CreateFile(GetObjectResponse file, string virtualPath) + public static FileModel CreateFile(GetObjectResponse file, string path, string virtualPath) { return new FileModel { Name = file.Key.Split('/').Last(), - Path = file.Key, + Path = path, VirtualPath = virtualPath, Length = file.ContentLength, LastModifiedDateTime = file.LastModified @@ -34,7 +35,12 @@ public static FileModel CreateFile(S3Object file, string virtualPath) public static DirectoryModel CreateDirectory(S3Object directory, string virtualPath) { var pathParts = directory.Key.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries); - var name = pathParts.Last(); + var name = pathParts.LastOrDefault(); + + if (name == null) + { + name = "/"; + } if (pathParts.Length == 1) { @@ -43,8 +49,8 @@ public static DirectoryModel CreateDirectory(S3Object directory, string virtualP return new DirectoryModel { - Name = name.Substring(0, name.Length - 1), - Path = directory.Key.Substring(0, name.Length - 1), + Name = name, + Path = directory.Key.RemoveTrailingForwardSlash(), VirtualPath = virtualPath, LastModifiedDateTime = directory.LastModified }; diff --git a/FileSystem/src/Utilities/PathUtilities.cs b/FileSystem/src/Utilities/PathUtilities.cs index 77d2c07..b6cf6f3 100644 --- a/FileSystem/src/Utilities/PathUtilities.cs +++ b/FileSystem/src/Utilities/PathUtilities.cs @@ -27,6 +27,11 @@ public static string GetPrefix(string virtualPath) /// The path. public static string GetPath(string virtualPath, string rootPath) { + if (string.IsNullOrWhiteSpace(rootPath)) + { + return string.Join(PathSeparator, ResolvePrefixAndPath(virtualPath)[1]); + } + return string.Join(PathSeparator, rootPath, ResolvePrefixAndPath(virtualPath)[1]); } diff --git a/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs b/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs index 9f7838e..b8862f7 100644 --- a/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs @@ -1,16 +1,26 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; using System.Threading.Tasks; +using Amazon.Runtime; using Amazon.S3; using Amazon.S3.Model; using NSubstitute; +using NSubstitute.ExceptionExtensions; +using SharpGrip.FileSystem.Adapters; using SharpGrip.FileSystem.Adapters.AmazonS3; -using SharpGrip.FileSystem.Models; +using SharpGrip.FileSystem.Exceptions; using Xunit; namespace Tests.FileSystem.Adapters.AmazonS3 { public class AmazonS3AdapterTest { + private readonly AmazonS3Exception amazonS3NoSuchKeyException = new("NoSuchKey", ErrorType.Receiver, "NoSuchKey", "12345", HttpStatusCode.NotFound); + private readonly AmazonS3Exception amazonS3InvalidAccessKeyIdException = new("InvalidAccessKeyId", ErrorType.Receiver, "InvalidAccessKeyId", "12345", HttpStatusCode.Unauthorized); + private readonly AmazonS3Exception amazonS3InvalidSecurityException = new("InvalidSecurity", ErrorType.Receiver, "InvalidSecurity", "12345", HttpStatusCode.Unauthorized); + [Fact] public void Test_Instantiation() { @@ -25,30 +35,295 @@ public void Test_Instantiation() public async Task Test_Get_File_Async() { var amazonS3Client = Substitute.For(); - var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "/root-path-1", amazonS3Client, "bucket-1"); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); var getObjectResponse = Substitute.For(); - getObjectResponse.Key = "test.txt"; + getObjectResponse.Key = "test1.txt"; getObjectResponse.ContentLength = 1; getObjectResponse.LastModified = new DateTime(1970, 1, 1); - amazonS3Client.GetObjectAsync("bucket-1", "/root-path-1/test.txt").Returns(getObjectResponse); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(amazonS3InvalidSecurityException); + + var file = await fileSystem.GetFileAsync("prefix-1://test1.txt"); + + Assert.Equal("test1.txt", file.Name); + Assert.Equal("root-path-1/test1.txt", file.Path); + Assert.Equal("prefix-1://test1.txt", file.VirtualPath); + Assert.Equal(1, file.Length); + Assert.Equal(new DateTime(1970, 1, 1), file.LastModifiedDateTime); + + await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test2.txt")); + await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test4.txt")); + } + + [Fact] + public async Task Test_Get_Directory_Async() + { + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var listObjectsV2Response1 = Substitute.For(); + var listObjectsV2Response2 = Substitute.For(); + + var s3Object = new S3Object + { + Key = "root-path-1/test1/", + LastModified = new DateTime(1970, 1, 1) + }; + + listObjectsV2Response1.S3Objects.Add(s3Object); + listObjectsV2Response1.KeyCount = listObjectsV2Response1.S3Objects.Count; + + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).Returns(listObjectsV2Response1); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).Returns(listObjectsV2Response2); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test5/")).ThrowsAsync(amazonS3InvalidSecurityException); + + var directory = await fileSystem.GetDirectoryAsync("prefix-1://test1"); + + Assert.Equal("test1", directory.Name); + Assert.Equal("root-path-1/test1", directory.Path); + Assert.Equal("prefix-1://test1", directory.VirtualPath); + Assert.Equal(new DateTime(1970, 1, 1), directory.LastModifiedDateTime); + + await Assert.ThrowsAsync(() => fileSystem.GetDirectoryAsync("prefix-1://test2")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoryAsync("prefix-1://test3")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoryAsync("prefix-1://test4")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoryAsync("prefix-1://test5")); + } + + [Fact] + public async Task Test_Get_Files_Async() + { + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var listObjectsV2Response = Substitute.For(); + + var s3ObjectFile = new S3Object + { + Key = "root-path-1/test1/test.txt", + Size = 1, + LastModified = new DateTime(1970, 1, 1) + }; + var s3ObjectDirectory = new S3Object + { + Key = "root-path-1/test1/", + LastModified = new DateTime(1970, 1, 1) + }; + + listObjectsV2Response.S3Objects.Add(s3ObjectFile); + listObjectsV2Response.S3Objects.Add(s3ObjectDirectory); + listObjectsV2Response.KeyCount = listObjectsV2Response.S3Objects.Count; + + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).Returns(listObjectsV2Response); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(amazonS3InvalidSecurityException); + + var files = await fileSystem.GetFilesAsync("prefix-1://test1"); + var file = files.First(); - var fileModel = new FileModel + Assert.Equal("test.txt", file.Name); + Assert.Equal("root-path-1/test1/test.txt", file.Path); + Assert.Equal("prefix-1://test1/test.txt", file.VirtualPath); + Assert.Equal(1, file.Length); + Assert.Equal(new DateTime(1970, 1, 1), file.LastModifiedDateTime); + + await Assert.ThrowsAsync(() => fileSystem.GetFilesAsync("prefix-1://test2")); + await Assert.ThrowsAsync(() => fileSystem.GetFilesAsync("prefix-1://test3")); + await Assert.ThrowsAsync(() => fileSystem.GetFilesAsync("prefix-1://test4")); + } + + [Fact] + public async Task Test_Get_Directories_Async() + { + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var listObjectsV2Response = Substitute.For(); + + var s3ObjectDirectory1 = new S3Object + { + Key = "root-path-1/", + Size = 1, + LastModified = new DateTime(1970, 1, 1) + }; + var s3ObjectDirectory2 = new S3Object + { + Key = "root-path-1/test1/", + Size = 1, + LastModified = new DateTime(1970, 1, 1) + }; + var s3ObjectDirectory3 = new S3Object { - Name = "test.txt", - Path = "test.txt", - Length = 1, - LastModifiedDateTime = new DateTime(1970, 1, 1) + Key = "root-path-1/test2/", + Size = 1, + LastModified = new DateTime(1970, 1, 1) }; - var result = await amazonS3Adapter.GetFileAsync("prefix-1://test.txt"); + listObjectsV2Response.S3Objects.Add(s3ObjectDirectory1); + listObjectsV2Response.S3Objects.Add(s3ObjectDirectory2); + listObjectsV2Response.S3Objects.Add(s3ObjectDirectory3); + listObjectsV2Response.KeyCount = listObjectsV2Response.S3Objects.Count; + + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/")).Returns(listObjectsV2Response); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3InvalidSecurityException); + + var directories = (await fileSystem.GetDirectoriesAsync("prefix-1://")).ToList(); + var directory1 = directories[0]; + var directory2 = directories[1]; + + Assert.Equal("test1", directory1.Name); + Assert.Equal("root-path-1/test1", directory1.Path); + Assert.Equal("prefix-1://test1/", directory1.VirtualPath); + Assert.Equal(new DateTime(1970, 1, 1), directory1.LastModifiedDateTime); + + Assert.Equal("test2", directory2.Name); + Assert.Equal("root-path-1/test2", directory2.Path); + Assert.Equal("prefix-1://test2/", directory2.VirtualPath); + Assert.Equal(new DateTime(1970, 1, 1), directory2.LastModifiedDateTime); + + await Assert.ThrowsAsync(() => fileSystem.GetDirectoriesAsync("prefix-1://test1")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoriesAsync("prefix-1://test2")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoriesAsync("prefix-1://test3")); + } + + [Fact] + public async Task Test_File_Exists_Async() + { + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var getObjectResponse = Substitute.For(); + + getObjectResponse.Key = "test1.txt"; + getObjectResponse.ContentLength = 1; + getObjectResponse.LastModified = new DateTime(1970, 1, 1); + + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(amazonS3InvalidSecurityException); + + Assert.True(await fileSystem.FileExistsAsync("prefix-1://test1.txt")); + Assert.False(await fileSystem.FileExistsAsync("prefix-1://test2.txt")); + + await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test2.txt")); + await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test4.txt")); + } + + [Fact] + public async Task Test_Directory_Exists_Async() + { + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var listObjectsV2Response1 = Substitute.For(); + var listObjectsV2Response2 = Substitute.For(); + + var s3Object = new S3Object + { + Key = "root-path-1/test1/", + LastModified = new DateTime(1970, 1, 1) + }; + + listObjectsV2Response1.S3Objects.Add(s3Object); + listObjectsV2Response1.KeyCount = listObjectsV2Response1.S3Objects.Count; + + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).Returns(listObjectsV2Response1); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).Returns(listObjectsV2Response2); + + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test5/")).ThrowsAsync(amazonS3InvalidSecurityException); + + Assert.True(await fileSystem.DirectoryExistsAsync("prefix-1://")); + Assert.True(await fileSystem.DirectoryExistsAsync("prefix-1://root-path-1/test1")); + Assert.False(await fileSystem.DirectoryExistsAsync("prefix-1://root-path-1/test2")); + + await Assert.ThrowsAsync(() => fileSystem.DirectoryExistsAsync("prefix-1://root-path-1/test3")); + await Assert.ThrowsAsync(() => fileSystem.DirectoryExistsAsync("prefix-1://root-path-1/test4")); + await Assert.ThrowsAsync(() => fileSystem.DirectoryExistsAsync("prefix-1://root-path-1/test5")); + } + + [Fact] + public async Task Test_Create_Directory_Async() + { + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var listObjectsV2Response1 = Substitute.For(); + var listObjectsV2Response2 = Substitute.For(); + + var s3Object = new S3Object + { + Key = "root-path-1/test1/", + LastModified = new DateTime(1970, 1, 1) + }; + + listObjectsV2Response1.S3Objects.Add(s3Object); + listObjectsV2Response1.KeyCount = listObjectsV2Response1.S3Objects.Count; + + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).Returns(listObjectsV2Response1); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test5/")).Returns(listObjectsV2Response2); + + await Assert.ThrowsAsync(() => fileSystem.CreateDirectoryAsync("prefix-1://test1")); + await Assert.ThrowsAsync(() => fileSystem.CreateDirectoryAsync("prefix-1://test2")); + await Assert.ThrowsAsync(() => fileSystem.CreateDirectoryAsync("prefix-1://test3")); + await Assert.ThrowsAsync(() => fileSystem.CreateDirectoryAsync("prefix-1://test4")); + await fileSystem.CreateDirectoryAsync("prefix-1://test5"); + } + + [Fact] + public async Task Test_Delete_Directory_Async() + { + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var listObjectsV2Response1 = Substitute.For(); + var listObjectsV2Response2 = Substitute.For(); + + var s3Object = new S3Object + { + Key = "root-path-1/test5/", + LastModified = new DateTime(1970, 1, 1) + }; + + listObjectsV2Response2.S3Objects.Add(s3Object); + listObjectsV2Response2.KeyCount = listObjectsV2Response2.S3Objects.Count; + + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).Returns(listObjectsV2Response1); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test5/")).Returns(listObjectsV2Response2); - Assert.Equal(fileModel.Name, result.Name); - Assert.Equal(fileModel.Path, result.Path); - Assert.Equal(fileModel.Length, result.Length); - Assert.Equal(fileModel.LastModifiedDateTime, result.LastModifiedDateTime); + await Assert.ThrowsAsync(() => fileSystem.DeleteDirectoryAsync("prefix-1://test1")); + await Assert.ThrowsAsync(() => fileSystem.DeleteDirectoryAsync("prefix-1://test2")); + await Assert.ThrowsAsync(() => fileSystem.DeleteDirectoryAsync("prefix-1://test3")); + await Assert.ThrowsAsync(() => fileSystem.DeleteDirectoryAsync("prefix-1://test4")); + await fileSystem.DeleteDirectoryAsync("prefix-1://test5"); } } } \ No newline at end of file diff --git a/Tests/src/FileSystem.Adapters.AzureFileStorage/AzureFileStorageAdapterTest.cs b/Tests/src/FileSystem.Adapters.AzureFileStorage/AzureFileStorageAdapterTest.cs index ee4bc7a..dff032c 100644 --- a/Tests/src/FileSystem.Adapters.AzureFileStorage/AzureFileStorageAdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.AzureFileStorage/AzureFileStorageAdapterTest.cs @@ -1,6 +1,20 @@ -namespace Tests.FileSystem.Adapters.AzureFileStorage +using Azure.Storage.Files.Shares; +using NSubstitute; +using SharpGrip.FileSystem.Adapters.AzureFileStorage; +using Xunit; + +namespace Tests.FileSystem.Adapters.AzureFileStorage { public class AzureFileStorageAdapterTest { + [Fact] + public void Test_Instantiation() + { + var shareClient = Substitute.For(); + var azureFileStorageAdapter = new AzureFileStorageAdapter("prefix", "/root-path", shareClient); + + Assert.Equal("prefix", azureFileStorageAdapter.Prefix); + Assert.Equal("/root-path", azureFileStorageAdapter.RootPath); + } } } \ No newline at end of file diff --git a/Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs b/Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs new file mode 100644 index 0000000..20c2efb --- /dev/null +++ b/Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs @@ -0,0 +1,6 @@ +namespace Tests.FileSystem.Adapters.MicrosoftOneDrive +{ + public class MicrosoftOneDriveAdapterTest + { + } +} \ No newline at end of file From bc1288655494decc5fa37959ab29af49d30dfe73 Mon Sep 17 00:00:00 2001 From: Mauro van der Gun Date: Thu, 10 Aug 2023 14:32:57 -0400 Subject: [PATCH 2/6] improve tests --- .../AmazonS3AdapterTest.cs | 56 +++++++++++++- .../MicrosoftOneDriveAdapterTest.cs | 19 ++++- Tests/src/FileSystem/LocalAdapterTest.cs | 74 ++++++++++++++++++- Tests/src/IAdapterTests.cs | 22 ++++++ 4 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 Tests/src/IAdapterTests.cs diff --git a/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs b/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs index b8862f7..4f47cd7 100644 --- a/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs @@ -15,7 +15,7 @@ namespace Tests.FileSystem.Adapters.AmazonS3 { - public class AmazonS3AdapterTest + public class AmazonS3AdapterTest : IAdapterTests { private readonly AmazonS3Exception amazonS3NoSuchKeyException = new("NoSuchKey", ErrorType.Receiver, "NoSuchKey", "12345", HttpStatusCode.NotFound); private readonly AmazonS3Exception amazonS3InvalidAccessKeyIdException = new("InvalidAccessKeyId", ErrorType.Receiver, "InvalidAccessKeyId", "12345", HttpStatusCode.Unauthorized); @@ -31,6 +31,12 @@ public void Test_Instantiation() Assert.Equal("/root-path", amazonS3Adapter.RootPath); } + [Fact] + public async Task Test_Connect() + { + await Task.CompletedTask; + } + [Fact] public async Task Test_Get_File_Async() { @@ -294,6 +300,30 @@ public async Task Test_Create_Directory_Async() await fileSystem.CreateDirectoryAsync("prefix-1://test5"); } + [Fact] + public async Task Test_Delete_File_Async() + { + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var getObjectResponse = Substitute.For(); + + getObjectResponse.Key = "test4.txt"; + getObjectResponse.ContentLength = 1; + getObjectResponse.LastModified = new DateTime(1970, 1, 1); + + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").Returns(getObjectResponse); + + await Assert.ThrowsAsync(() => fileSystem.DeleteFileAsync("prefix-1://test1.txt")); + await Assert.ThrowsAsync(() => fileSystem.DeleteFileAsync("prefix-1://test2.txt")); + await Assert.ThrowsAsync(() => fileSystem.DeleteFileAsync("prefix-1://test3.txt")); + await fileSystem.DeleteFileAsync("prefix-1://test4.txt"); + } + [Fact] public async Task Test_Delete_Directory_Async() { @@ -325,5 +355,29 @@ public async Task Test_Delete_Directory_Async() await Assert.ThrowsAsync(() => fileSystem.DeleteDirectoryAsync("prefix-1://test4")); await fileSystem.DeleteDirectoryAsync("prefix-1://test5"); } + + [Fact] + public async Task Test_Read_File_Async() + { + await Task.CompletedTask; + } + + [Fact] + public async Task Test_Read_Text_File_Async() + { + await Task.CompletedTask; + } + + [Fact] + public async Task Test_Write_File_Async() + { + await Task.CompletedTask; + } + + [Fact] + public async Task Test_Append_File_Async() + { + await Task.CompletedTask; + } } } \ No newline at end of file diff --git a/Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs b/Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs index 20c2efb..badb97e 100644 --- a/Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs @@ -1,6 +1,23 @@ -namespace Tests.FileSystem.Adapters.MicrosoftOneDrive +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Microsoft.Graph; +using NSubstitute; +using SharpGrip.FileSystem.Adapters.MicrosoftOneDrive; +using Xunit; + +namespace Tests.FileSystem.Adapters.MicrosoftOneDrive { public class MicrosoftOneDriveAdapterTest { + [Fact] + public void Test_Instantiation() + { + var delegateAuthenticationProvider = new DelegateAuthenticationProvider(message => Task.FromResult(message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "12345"))); + var graphServiceClient = Substitute.For(delegateAuthenticationProvider, null); + var microsoftOneDriveAdapter = new MicrosoftOneDriveAdapter("prefix", "/root-path", graphServiceClient, "driveId"); + + Assert.Equal("prefix", microsoftOneDriveAdapter.Prefix); + Assert.Equal("/root-path", microsoftOneDriveAdapter.RootPath); + } } } \ No newline at end of file diff --git a/Tests/src/FileSystem/LocalAdapterTest.cs b/Tests/src/FileSystem/LocalAdapterTest.cs index 773693e..7207b20 100644 --- a/Tests/src/FileSystem/LocalAdapterTest.cs +++ b/Tests/src/FileSystem/LocalAdapterTest.cs @@ -5,7 +5,7 @@ namespace Tests.FileSystem { - public class LocalAdapterTest + public class LocalAdapterTest : IAdapterTests { [Fact] public void Test_Instantiation() @@ -16,6 +16,12 @@ public void Test_Instantiation() Assert.Equal("/root-path", localAdapter.RootPath); } + [Fact] + public async Task Test_Connect() + { + await Task.CompletedTask; + } + [Fact] public async Task Test_Get_File_Async() { @@ -47,5 +53,71 @@ public async Task Test_Get_Directories_Async() await Assert.ThrowsAsync(async () => await localAdapter.GetDirectoriesAsync("prefix-1://test")); } + + [Fact] + public async Task Test_File_Exists_Async() + { + var localAdapter = new LocalAdapter("prefix-1", "/root-path-1"); + + Assert.False(await localAdapter.FileExistsAsync("prefix-1://test.txt")); + } + + [Fact] + public async Task Test_Directory_Exists_Async() + { + var localAdapter = new LocalAdapter("prefix-1", "/root-path-1"); + + Assert.False(await localAdapter.DirectoryExistsAsync("prefix-1://test")); + } + + [Fact] + public async Task Test_Create_Directory_Async() + { + await Task.CompletedTask; + } + + [Fact] + public async Task Test_Delete_File_Async() + { + var localAdapter = new LocalAdapter("prefix-1", "/root-path-1"); + + await Assert.ThrowsAsync(async () => await localAdapter.DeleteFileAsync("prefix-1://test.txt")); + } + + [Fact] + public async Task Test_Delete_Directory_Async() + { + var localAdapter = new LocalAdapter("prefix-1", "/root-path-1"); + + await Assert.ThrowsAsync(async () => await localAdapter.DeleteDirectoryAsync("prefix-1://test")); + } + + [Fact] + public async Task Test_Read_File_Async() + { + var localAdapter = new LocalAdapter("prefix-1", "/root-path-1"); + + await Assert.ThrowsAsync(async () => await localAdapter.ReadFileAsync("prefix-1://test.txt")); + } + + [Fact] + public async Task Test_Read_Text_File_Async() + { + var localAdapter = new LocalAdapter("prefix-1", "/root-path-1"); + + await Assert.ThrowsAsync(async () => await localAdapter.ReadTextFileAsync("prefix-1://test.txt")); + } + + [Fact] + public async Task Test_Write_File_Async() + { + await Task.CompletedTask; + } + + [Fact] + public async Task Test_Append_File_Async() + { + await Task.CompletedTask; + } } } \ No newline at end of file diff --git a/Tests/src/IAdapterTests.cs b/Tests/src/IAdapterTests.cs new file mode 100644 index 0000000..726e76b --- /dev/null +++ b/Tests/src/IAdapterTests.cs @@ -0,0 +1,22 @@ +using System.Threading.Tasks; + +namespace Tests; + +public interface IAdapterTests +{ + public void Test_Instantiation(); + public Task Test_Connect(); + public Task Test_Get_File_Async(); + public Task Test_Get_Directory_Async(); + public Task Test_Get_Files_Async(); + public Task Test_Get_Directories_Async(); + public Task Test_File_Exists_Async(); + public Task Test_Directory_Exists_Async(); + public Task Test_Create_Directory_Async(); + public Task Test_Delete_File_Async(); + public Task Test_Delete_Directory_Async(); + public Task Test_Read_File_Async(); + public Task Test_Read_Text_File_Async(); + public Task Test_Write_File_Async(); + public Task Test_Append_File_Async(); +} \ No newline at end of file From a3c3fe84bd33023018d8441959aa22a535156664 Mon Sep 17 00:00:00 2001 From: Mauro van der Gun Date: Thu, 10 Aug 2023 16:47:21 -0400 Subject: [PATCH 3/6] add remaining amazon s3 adapter tests --- FileSystem/src/Adapters/Adapter.cs | 2 +- .../AmazonS3AdapterTest.cs | 171 +++++++++++++++++- Tests/src/FileSystem/LocalAdapterTest.cs | 8 + Tests/src/IAdapterTests.cs | 1 + 4 files changed, 177 insertions(+), 5 deletions(-) diff --git a/FileSystem/src/Adapters/Adapter.cs b/FileSystem/src/Adapters/Adapter.cs index b39a18c..95995a9 100644 --- a/FileSystem/src/Adapters/Adapter.cs +++ b/FileSystem/src/Adapters/Adapter.cs @@ -109,7 +109,7 @@ public async Task ReadFileAsync(string virtualPath, CancellationToken ca try { - using var stream = await ReadFileStreamAsync(virtualPath, cancellationToken); + var stream = await ReadFileStreamAsync(virtualPath, cancellationToken); using var memoryStream = await StreamUtilities.CopyContentsToMemoryStreamAsync(stream, cancellationToken); await stream.CopyToAsync(memoryStream, AdapterConstants.DefaultMemoryStreamBufferSize, cancellationToken); diff --git a/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs b/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs index 4f47cd7..968158e 100644 --- a/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; @@ -12,6 +13,8 @@ using SharpGrip.FileSystem.Adapters.AmazonS3; using SharpGrip.FileSystem.Exceptions; using Xunit; +using DirectoryNotFoundException = SharpGrip.FileSystem.Exceptions.DirectoryNotFoundException; +using FileNotFoundException = SharpGrip.FileSystem.Exceptions.FileNotFoundException; namespace Tests.FileSystem.Adapters.AmazonS3 { @@ -356,28 +359,188 @@ public async Task Test_Delete_Directory_Async() await fileSystem.DeleteDirectoryAsync("prefix-1://test5"); } + [Fact] + public async Task Test_Read_File_Stream_Async() + { + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var getObjectResponse1 = Substitute.For(); + var getObjectResponse2 = Substitute.For(); + + getObjectResponse1.Key = "test1.txt"; + getObjectResponse1.ContentLength = 1; + getObjectResponse1.LastModified = new DateTime(1970, 1, 1); + + getObjectResponse2.Key = "test1.txt"; + getObjectResponse2.ContentLength = 1; + getObjectResponse2.LastModified = new DateTime(1970, 1, 1); + getObjectResponse2.ResponseStream = new MemoryStream("test1"u8.ToArray()); + + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse1, getObjectResponse2); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(amazonS3InvalidSecurityException); + + var fileStream = await fileSystem.ReadFileStreamAsync("prefix-1://test1.txt"); + var streamReader = new StreamReader(fileStream); + + Assert.Equal("test1", await streamReader.ReadToEndAsync()); + await Assert.ThrowsAsync(() => fileSystem.ReadFileStreamAsync("prefix-1://test2.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileStreamAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileStreamAsync("prefix-1://test4.txt")); + } + [Fact] public async Task Test_Read_File_Async() { - await Task.CompletedTask; + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var getObjectResponse1 = Substitute.For(); + var getObjectResponse2 = Substitute.For(); + var getObjectResponse3 = Substitute.For(); + + getObjectResponse1.Key = "test1.txt"; + getObjectResponse1.ContentLength = 1; + getObjectResponse1.LastModified = new DateTime(1970, 1, 1); + + getObjectResponse2.Key = "test1.txt"; + getObjectResponse2.ContentLength = 1; + getObjectResponse2.LastModified = new DateTime(1970, 1, 1); + + getObjectResponse3.Key = "test1.txt"; + getObjectResponse3.ContentLength = 1; + getObjectResponse3.LastModified = new DateTime(1970, 1, 1); + getObjectResponse3.ResponseStream = new MemoryStream("test1"u8.ToArray()); + + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse1, getObjectResponse2, getObjectResponse3); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(amazonS3InvalidSecurityException); + + var fileContents = await fileSystem.ReadFileAsync("prefix-1://test1.txt"); + + Assert.Equal("test1", System.Text.Encoding.UTF8.GetString(fileContents)); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test2.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test4.txt")); } [Fact] public async Task Test_Read_Text_File_Async() { - await Task.CompletedTask; + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var getObjectResponse1 = Substitute.For(); + var getObjectResponse2 = Substitute.For(); + var getObjectResponse3 = Substitute.For(); + + getObjectResponse1.Key = "test1.txt"; + getObjectResponse1.ContentLength = 1; + getObjectResponse1.LastModified = new DateTime(1970, 1, 1); + + getObjectResponse2.Key = "test1.txt"; + getObjectResponse2.ContentLength = 1; + getObjectResponse2.LastModified = new DateTime(1970, 1, 1); + + getObjectResponse3.Key = "test1.txt"; + getObjectResponse3.ContentLength = 1; + getObjectResponse3.LastModified = new DateTime(1970, 1, 1); + getObjectResponse3.ResponseStream = new MemoryStream("test1"u8.ToArray()); + + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse1, getObjectResponse2, getObjectResponse3); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(amazonS3InvalidSecurityException); + + var fileContents = await fileSystem.ReadTextFileAsync("prefix-1://test1.txt"); + + Assert.Equal("test1", fileContents); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test2.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test4.txt")); } [Fact] public async Task Test_Write_File_Async() { - await Task.CompletedTask; + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var getObjectResponse = Substitute.For(); + + getObjectResponse.Key = "test1.txt"; + getObjectResponse.ContentLength = 1; + getObjectResponse.LastModified = new DateTime(1970, 1, 1); + + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").Returns(getObjectResponse); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").Returns(getObjectResponse); + amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test2.txt")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test3.txt")).ThrowsAsync(amazonS3InvalidSecurityException); + + await fileSystem.WriteFileAsync("prefix-1://test1.txt", new MemoryStream(), true); + await Assert.ThrowsAsync(() => fileSystem.WriteFileAsync("prefix-1://test1.txt", new MemoryStream())); + await Assert.ThrowsAsync(() => fileSystem.WriteFileAsync("prefix-1://test2.txt", new MemoryStream(), true)); + await Assert.ThrowsAsync(() => fileSystem.WriteFileAsync("prefix-1://test3.txt", new MemoryStream(), true)); } [Fact] public async Task Test_Append_File_Async() { - await Task.CompletedTask; + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix-1", "root-path-1", amazonS3Client, "bucket-1"); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {amazonS3Adapter}); + + var getObjectResponse1 = Substitute.For(); + var getObjectResponse2 = Substitute.For(); + var getObjectResponse3 = Substitute.For(); + var getObjectResponse4 = Substitute.For(); + var getObjectResponse5 = Substitute.For(); + + getObjectResponse1.Key = "test1.txt"; + getObjectResponse1.ContentLength = 1; + getObjectResponse1.LastModified = new DateTime(1970, 1, 1); + getObjectResponse3.ResponseStream = new MemoryStream("test1"u8.ToArray()); + + getObjectResponse2.Key = "test1.txt"; + getObjectResponse2.ContentLength = 1; + getObjectResponse2.LastModified = new DateTime(1970, 1, 1); + getObjectResponse3.ResponseStream = new MemoryStream("test1"u8.ToArray()); + + getObjectResponse3.Key = "test1.txt"; + getObjectResponse3.ContentLength = 1; + getObjectResponse3.LastModified = new DateTime(1970, 1, 1); + getObjectResponse3.ResponseStream = new MemoryStream("test1"u8.ToArray()); + + getObjectResponse4.Key = "test1.txt"; + getObjectResponse4.ContentLength = 1; + getObjectResponse4.LastModified = new DateTime(1970, 1, 1); + getObjectResponse4.ResponseStream = new MemoryStream("test1"u8.ToArray()); + + getObjectResponse5.Key = "test1.txt"; + getObjectResponse5.ContentLength = 1; + getObjectResponse5.LastModified = new DateTime(1970, 1, 1); + getObjectResponse5.ResponseStream = new MemoryStream("test1"u8.ToArray()); + + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse1, getObjectResponse2, getObjectResponse3, getObjectResponse4, getObjectResponse5); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").Returns(getObjectResponse1, getObjectResponse2, getObjectResponse3, getObjectResponse4, getObjectResponse5); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").Returns(getObjectResponse1, getObjectResponse2, getObjectResponse3, getObjectResponse4, getObjectResponse5); + amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test2.txt")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); + amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test3.txt")).ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test4.txt")).ThrowsAsync(amazonS3InvalidSecurityException); + + await fileSystem.AppendFileAsync("prefix-1://test1.txt", new MemoryStream()); + await Assert.ThrowsAsync(() => fileSystem.AppendFileAsync("prefix-1://test2.txt", new MemoryStream())); + await Assert.ThrowsAsync(() => fileSystem.AppendFileAsync("prefix-1://test3.txt", new MemoryStream())); + await Assert.ThrowsAsync(() => fileSystem.AppendFileAsync("prefix-1://test4.txt", new MemoryStream())); } } } \ No newline at end of file diff --git a/Tests/src/FileSystem/LocalAdapterTest.cs b/Tests/src/FileSystem/LocalAdapterTest.cs index 7207b20..8b3dd23 100644 --- a/Tests/src/FileSystem/LocalAdapterTest.cs +++ b/Tests/src/FileSystem/LocalAdapterTest.cs @@ -92,6 +92,14 @@ public async Task Test_Delete_Directory_Async() await Assert.ThrowsAsync(async () => await localAdapter.DeleteDirectoryAsync("prefix-1://test")); } + [Fact] + public async Task Test_Read_File_Stream_Async() + { + var localAdapter = new LocalAdapter("prefix-1", "/root-path-1"); + + await Assert.ThrowsAsync(async () => await localAdapter.ReadFileAsync("prefix-1://test.txt")); + } + [Fact] public async Task Test_Read_File_Async() { diff --git a/Tests/src/IAdapterTests.cs b/Tests/src/IAdapterTests.cs index 726e76b..3704d4e 100644 --- a/Tests/src/IAdapterTests.cs +++ b/Tests/src/IAdapterTests.cs @@ -15,6 +15,7 @@ public interface IAdapterTests public Task Test_Create_Directory_Async(); public Task Test_Delete_File_Async(); public Task Test_Delete_Directory_Async(); + public Task Test_Read_File_Stream_Async(); public Task Test_Read_File_Async(); public Task Test_Read_Text_File_Async(); public Task Test_Write_File_Async(); From 22c341f514219c9edf93f87c07c0bb74b9b867a3 Mon Sep 17 00:00:00 2001 From: Mauro van der Gun Date: Thu, 10 Aug 2023 17:02:19 -0400 Subject: [PATCH 4/6] remove stream seeking --- FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs | 2 -- FileSystem/src/Adapters/Adapter.cs | 1 - FileSystem/src/Utilities/StreamUtilities.cs | 1 - 3 files changed, 4 deletions(-) diff --git a/FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs b/FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs index 77fff29..ef1441e 100644 --- a/FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs +++ b/FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs @@ -255,8 +255,6 @@ public override async Task WriteFileAsync(string virtualPath, Stream contents, b try { - contents.Seek(0, SeekOrigin.Begin); - var request = new PutObjectRequest { InputStream = contents, diff --git a/FileSystem/src/Adapters/Adapter.cs b/FileSystem/src/Adapters/Adapter.cs index 95995a9..a77f32f 100644 --- a/FileSystem/src/Adapters/Adapter.cs +++ b/FileSystem/src/Adapters/Adapter.cs @@ -130,7 +130,6 @@ public async Task ReadTextFileAsync(string virtualPath, CancellationToke { using var stream = await ReadFileStreamAsync(virtualPath, cancellationToken); using var streamReader = new StreamReader(stream); - stream.Position = 0; return await streamReader.ReadToEndAsync(); } diff --git a/FileSystem/src/Utilities/StreamUtilities.cs b/FileSystem/src/Utilities/StreamUtilities.cs index 04b0ec4..deb903f 100644 --- a/FileSystem/src/Utilities/StreamUtilities.cs +++ b/FileSystem/src/Utilities/StreamUtilities.cs @@ -10,7 +10,6 @@ public static class StreamUtilities public static async Task CopyContentsToMemoryStreamAsync(Stream contents, CancellationToken cancellationToken = default) { var memoryStream = new MemoryStream(); - contents.Seek(0, SeekOrigin.Begin); await contents.CopyToAsync(memoryStream, AdapterConstants.DefaultMemoryStreamBufferSize, cancellationToken); return memoryStream; From 41c616d19662d7f7f0c5b1f5106a47b82246d5a1 Mon Sep 17 00:00:00 2001 From: Mauro van der Gun Date: Thu, 10 Aug 2023 20:39:08 -0400 Subject: [PATCH 5/6] rename exception fields --- .../AmazonS3AdapterTest.cs | 99 ++++++++++--------- 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs b/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs index 968158e..4a37370 100644 --- a/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs @@ -20,9 +20,9 @@ namespace Tests.FileSystem.Adapters.AmazonS3 { public class AmazonS3AdapterTest : IAdapterTests { - private readonly AmazonS3Exception amazonS3NoSuchKeyException = new("NoSuchKey", ErrorType.Receiver, "NoSuchKey", "12345", HttpStatusCode.NotFound); - private readonly AmazonS3Exception amazonS3InvalidAccessKeyIdException = new("InvalidAccessKeyId", ErrorType.Receiver, "InvalidAccessKeyId", "12345", HttpStatusCode.Unauthorized); - private readonly AmazonS3Exception amazonS3InvalidSecurityException = new("InvalidSecurity", ErrorType.Receiver, "InvalidSecurity", "12345", HttpStatusCode.Unauthorized); + private readonly AmazonS3Exception noSuchKeyException = new("NoSuchKey", ErrorType.Receiver, "NoSuchKey", "12345", HttpStatusCode.NotFound); + private readonly AmazonS3Exception invalidAccessKeyIdException = new("InvalidAccessKeyId", ErrorType.Receiver, "InvalidAccessKeyId", "12345", HttpStatusCode.Unauthorized); + private readonly AmazonS3Exception invalidSecurityException = new("InvalidSecurity", ErrorType.Receiver, "InvalidSecurity", "12345", HttpStatusCode.Unauthorized); [Fact] public void Test_Instantiation() @@ -35,9 +35,14 @@ public void Test_Instantiation() } [Fact] - public async Task Test_Connect() + public Task Test_Connect() { - await Task.CompletedTask; + var amazonS3Client = Substitute.For(); + var amazonS3Adapter = new AmazonS3Adapter("prefix", "/root-path", amazonS3Client, "bucket"); + + amazonS3Adapter.Connect(); + + return Task.CompletedTask; } [Fact] @@ -54,9 +59,9 @@ public async Task Test_Get_File_Async() getObjectResponse.LastModified = new DateTime(1970, 1, 1); amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(noSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(invalidSecurityException); var file = await fileSystem.GetFileAsync("prefix-1://test1.txt"); @@ -92,9 +97,9 @@ public async Task Test_Get_Directory_Async() amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).Returns(listObjectsV2Response1); amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).Returns(listObjectsV2Response2); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test5/")).ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(noSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test5/")).ThrowsAsync(invalidSecurityException); var directory = await fileSystem.GetDirectoryAsync("prefix-1://test1"); @@ -135,9 +140,9 @@ public async Task Test_Get_Files_Async() listObjectsV2Response.KeyCount = listObjectsV2Response.S3Objects.Count; amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).Returns(listObjectsV2Response); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(noSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(invalidSecurityException); var files = await fileSystem.GetFilesAsync("prefix-1://test1"); var file = files.First(); @@ -187,9 +192,9 @@ public async Task Test_Get_Directories_Async() listObjectsV2Response.KeyCount = listObjectsV2Response.S3Objects.Count; amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/")).Returns(listObjectsV2Response); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).ThrowsAsync(noSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(invalidSecurityException); var directories = (await fileSystem.GetDirectoriesAsync("prefix-1://")).ToList(); var directory1 = directories[0]; @@ -224,9 +229,9 @@ public async Task Test_File_Exists_Async() getObjectResponse.LastModified = new DateTime(1970, 1, 1); amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(noSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(invalidSecurityException); Assert.True(await fileSystem.FileExistsAsync("prefix-1://test1.txt")); Assert.False(await fileSystem.FileExistsAsync("prefix-1://test2.txt")); @@ -258,9 +263,9 @@ public async Task Test_Directory_Exists_Async() amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).Returns(listObjectsV2Response1); amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).Returns(listObjectsV2Response2); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test5/")).ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(noSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test5/")).ThrowsAsync(invalidSecurityException); Assert.True(await fileSystem.DirectoryExistsAsync("prefix-1://")); Assert.True(await fileSystem.DirectoryExistsAsync("prefix-1://root-path-1/test1")); @@ -291,9 +296,9 @@ public async Task Test_Create_Directory_Async() listObjectsV2Response1.KeyCount = listObjectsV2Response1.S3Objects.Count; amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).Returns(listObjectsV2Response1); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(noSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(invalidSecurityException); amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test5/")).Returns(listObjectsV2Response2); await Assert.ThrowsAsync(() => fileSystem.CreateDirectoryAsync("prefix-1://test1")); @@ -316,9 +321,9 @@ public async Task Test_Delete_File_Async() getObjectResponse.ContentLength = 1; getObjectResponse.LastModified = new DateTime(1970, 1, 1); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").ThrowsAsync(noSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(invalidSecurityException); amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").Returns(getObjectResponse); await Assert.ThrowsAsync(() => fileSystem.DeleteFileAsync("prefix-1://test1.txt")); @@ -347,9 +352,9 @@ public async Task Test_Delete_Directory_Async() listObjectsV2Response2.KeyCount = listObjectsV2Response2.S3Objects.Count; amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test1/")).Returns(listObjectsV2Response1); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test2/")).ThrowsAsync(noSuchKeyException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test3/")).ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test4/")).ThrowsAsync(invalidSecurityException); amazonS3Client.ListObjectsV2Async(Arg.Is(x => x.Prefix == "root-path-1/test5/")).Returns(listObjectsV2Response2); await Assert.ThrowsAsync(() => fileSystem.DeleteDirectoryAsync("prefix-1://test1")); @@ -379,9 +384,9 @@ public async Task Test_Read_File_Stream_Async() getObjectResponse2.ResponseStream = new MemoryStream("test1"u8.ToArray()); amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse1, getObjectResponse2); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(noSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(invalidSecurityException); var fileStream = await fileSystem.ReadFileStreamAsync("prefix-1://test1.txt"); var streamReader = new StreamReader(fileStream); @@ -417,9 +422,9 @@ public async Task Test_Read_File_Async() getObjectResponse3.ResponseStream = new MemoryStream("test1"u8.ToArray()); amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse1, getObjectResponse2, getObjectResponse3); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(noSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(invalidSecurityException); var fileContents = await fileSystem.ReadFileAsync("prefix-1://test1.txt"); @@ -454,9 +459,9 @@ public async Task Test_Read_Text_File_Async() getObjectResponse3.ResponseStream = new MemoryStream("test1"u8.ToArray()); amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse1, getObjectResponse2, getObjectResponse3); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(noSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").ThrowsAsync(invalidSecurityException); var fileContents = await fileSystem.ReadTextFileAsync("prefix-1://test1.txt"); @@ -482,8 +487,8 @@ public async Task Test_Write_File_Async() amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse); amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").Returns(getObjectResponse); amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").Returns(getObjectResponse); - amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test2.txt")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test3.txt")).ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test2.txt")).ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test3.txt")).ThrowsAsync(invalidSecurityException); await fileSystem.WriteFileAsync("prefix-1://test1.txt", new MemoryStream(), true); await Assert.ThrowsAsync(() => fileSystem.WriteFileAsync("prefix-1://test1.txt", new MemoryStream())); @@ -530,12 +535,12 @@ public async Task Test_Append_File_Async() getObjectResponse5.ResponseStream = new MemoryStream("test1"u8.ToArray()); amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test1.txt").Returns(getObjectResponse1, getObjectResponse2, getObjectResponse3, getObjectResponse4, getObjectResponse5); - amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(amazonS3NoSuchKeyException); + amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test2.txt").ThrowsAsync(noSuchKeyException); amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test3.txt").Returns(getObjectResponse1, getObjectResponse2, getObjectResponse3, getObjectResponse4, getObjectResponse5); amazonS3Client.GetObjectAsync("bucket-1", "root-path-1/test4.txt").Returns(getObjectResponse1, getObjectResponse2, getObjectResponse3, getObjectResponse4, getObjectResponse5); - amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test2.txt")).ThrowsAsync(amazonS3InvalidAccessKeyIdException); - amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test3.txt")).ThrowsAsync(amazonS3InvalidSecurityException); - amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test4.txt")).ThrowsAsync(amazonS3InvalidSecurityException); + amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test2.txt")).ThrowsAsync(invalidAccessKeyIdException); + amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test3.txt")).ThrowsAsync(invalidSecurityException); + amazonS3Client.PutObjectAsync(Arg.Is(x => x.BucketName == "bucket-1" && x.Key == "root-path-1/test4.txt")).ThrowsAsync(invalidSecurityException); await fileSystem.AppendFileAsync("prefix-1://test1.txt", new MemoryStream()); await Assert.ThrowsAsync(() => fileSystem.AppendFileAsync("prefix-1://test2.txt", new MemoryStream())); From c3285438a6d027fcc94896491e2bbf69f377ebdf Mon Sep 17 00:00:00 2001 From: Mauro van der Gun Date: Thu, 10 Aug 2023 21:30:34 -0400 Subject: [PATCH 6/6] implement other tests --- .../AzureBlobStorageAdapterTest.cs | 14 +++++++++++++- .../AzureFileStorageAdapterTest.cs | 14 +++++++++++++- .../MicrosoftOneDriveAdapterTest.cs | 12 ++++++++++++ .../FileSystem.Adapters.Sftp/SftpAdapterTest.cs | 12 +----------- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/Tests/src/FileSystem.Adapters.AzureBlobStorage/AzureBlobStorageAdapterTest.cs b/Tests/src/FileSystem.Adapters.AzureBlobStorage/AzureBlobStorageAdapterTest.cs index 72b76a5..0e57584 100644 --- a/Tests/src/FileSystem.Adapters.AzureBlobStorage/AzureBlobStorageAdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.AzureBlobStorage/AzureBlobStorageAdapterTest.cs @@ -1,4 +1,5 @@ -using Azure.Storage.Blobs; +using System.Threading.Tasks; +using Azure.Storage.Blobs; using NSubstitute; using SharpGrip.FileSystem.Adapters.AzureBlobStorage; using Xunit; @@ -16,5 +17,16 @@ public void Test_Instantiation() Assert.Equal("prefix", azureBlobStorageAdapter.Prefix); Assert.Equal("/root-path", azureBlobStorageAdapter.RootPath); } + + [Fact] + public Task Test_Connect() + { + var blobContainerClient = Substitute.For(); + var azureBlobStorageAdapter = new AzureBlobStorageAdapter("prefix", "/root-path", blobContainerClient); + + azureBlobStorageAdapter.Connect(); + + return Task.CompletedTask; + } } } \ No newline at end of file diff --git a/Tests/src/FileSystem.Adapters.AzureFileStorage/AzureFileStorageAdapterTest.cs b/Tests/src/FileSystem.Adapters.AzureFileStorage/AzureFileStorageAdapterTest.cs index dff032c..c8ed7c4 100644 --- a/Tests/src/FileSystem.Adapters.AzureFileStorage/AzureFileStorageAdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.AzureFileStorage/AzureFileStorageAdapterTest.cs @@ -1,4 +1,5 @@ -using Azure.Storage.Files.Shares; +using System.Threading.Tasks; +using Azure.Storage.Files.Shares; using NSubstitute; using SharpGrip.FileSystem.Adapters.AzureFileStorage; using Xunit; @@ -16,5 +17,16 @@ public void Test_Instantiation() Assert.Equal("prefix", azureFileStorageAdapter.Prefix); Assert.Equal("/root-path", azureFileStorageAdapter.RootPath); } + + [Fact] + public Task Test_Connect() + { + var shareClient = Substitute.For(); + var azureFileStorageAdapter = new AzureFileStorageAdapter("prefix", "/root-path", shareClient); + + azureFileStorageAdapter.Connect(); + + return Task.CompletedTask; + } } } \ No newline at end of file diff --git a/Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs b/Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs index badb97e..451b67d 100644 --- a/Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.MicrosoftOneDrive/MicrosoftOneDriveAdapterTest.cs @@ -19,5 +19,17 @@ public void Test_Instantiation() Assert.Equal("prefix", microsoftOneDriveAdapter.Prefix); Assert.Equal("/root-path", microsoftOneDriveAdapter.RootPath); } + + [Fact] + public Task Test_Connect() + { + var delegateAuthenticationProvider = new DelegateAuthenticationProvider(message => Task.FromResult(message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "12345"))); + var graphServiceClient = Substitute.For(delegateAuthenticationProvider, null); + var microsoftOneDriveAdapter = new MicrosoftOneDriveAdapter("prefix", "/root-path", graphServiceClient, "driveId"); + + microsoftOneDriveAdapter.Connect(); + + return Task.CompletedTask; + } } } \ No newline at end of file diff --git a/Tests/src/FileSystem.Adapters.Sftp/SftpAdapterTest.cs b/Tests/src/FileSystem.Adapters.Sftp/SftpAdapterTest.cs index a23a60c..ae30675 100644 --- a/Tests/src/FileSystem.Adapters.Sftp/SftpAdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.Sftp/SftpAdapterTest.cs @@ -1,5 +1,4 @@ -using System.Threading.Tasks; -using NSubstitute; +using NSubstitute; using Renci.SshNet; using SharpGrip.FileSystem.Adapters.Sftp; using SharpGrip.FileSystem.Exceptions; @@ -27,14 +26,5 @@ public void Test_Connect() Assert.Throws(() => sftpAdapter.Connect()); } - - [Fact] - public async Task Test_Get_File_Async() - { - var sftpClient = Substitute.For("hostName", "userName", "password"); - var sftpAdapter = new SftpAdapter("prefix-1", "/root-path-1", sftpClient); - - await Assert.ThrowsAsync(async () => await sftpAdapter.GetFileAsync("prefix-1://test.txt")); - } } } \ No newline at end of file