Skip to content

Commit

Permalink
Add tests for Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cardillo committed Sep 2, 2023
1 parent 250eeb6 commit dc1056e
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 196 deletions.
51 changes: 40 additions & 11 deletions tests/FileSystem.Tests/Base/IntegrationTestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Serilog;
using Serilog.Sinks.InMemory;
using Xunit;

namespace FileSystem.Tests.Base;

Expand All @@ -26,59 +27,87 @@ public void Init()
_adapter = (A)Activator.CreateInstance(typeof(A), Prefix, RootPath, Client)!;
}

[TestMethod]
[Fact(DisplayName = "Instantiation_Prefix_Should_Return_Correct")]
[TestCategory("IntegrationTest")]
public override void Instantiation_Prefix_Should_Return_Correct() => base.Instantiation_Prefix_Should_Return_Correct();

[TestMethod]
[Fact(DisplayName = "Instantiation_RootPath_Should_Return_Correct")]
[TestCategory("IntegrationTest")]
public override void Instantiation_RootPath_Should_Return_Correct() => base.Instantiation_RootPath_Should_Return_Correct();

[TestMethod]
[Fact(DisplayName = "GetFile_IfSuccess_Should_ReturnFile")]
[TestCategory("IntegrationTest")]
public override void GetFile_IfSuccess_Should_ReturnFile() => base.GetFile_IfSuccess_Should_ReturnFile();

[Fact(DisplayName = "GetFile_IfFileNotExist_Should_Throw_FileNotFoundException")]
[TestCategory("IntegrationTest")]
public override void GetFile_IfFileNotExist_Should_Throw_FileNotFoundException() => base.GetFile_IfFileNotExist_Should_Throw_FileNotFoundException();

[Fact(DisplayName = "GetFileAsync_IfSuccess_Should_ReturnFile")]
[TestCategory("IntegrationTest")]
public override async Task GetFileAsync_IfSuccess_Should_ReturnFile() => await base.GetFileAsync_IfSuccess_Should_ReturnFile();

[Fact(DisplayName = "GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException")]
[TestCategory("IntegrationTest")]
public override async Task GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException() => await base.GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException();

[TestMethod]
[Fact(DisplayName = "GetFiles_IfSuccess_Should_ReturnFiles")]
[TestCategory("IntegrationTest")]
public override void GetFiles_IfSuccess_Should_ReturnFiles() => base.GetFiles_IfSuccess_Should_ReturnFiles();

[Fact(DisplayName = "GetFiles_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException")]
[TestCategory("IntegrationTest")]
public override void GetFiles_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException() => base.GetFiles_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException();

[Fact(DisplayName = "GetFilesAsync_IfSuccess_Should_ReturnFiles")]
[TestCategory("IntegrationTest")]
public override async Task GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCorrectNumberOfFiles() => await base.GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCorrectNumberOfFiles();
public override async Task GetFilesAsync_IfSuccess_Should_ReturnFiles() => await base.GetFilesAsync_IfSuccess_Should_ReturnFiles();

[Fact(DisplayName = "GetFilesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException")]
[TestCategory("IntegrationTest")]
public override async Task GetFilesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException() => await base.GetFilesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException();

[Fact(DisplayName = "CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists")]
[TestMethod]
[TestCategory("IntegrationTest")]
public override async Task CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists() => await base.CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists();

[TestMethod]
[Fact(DisplayName = "CreateDirectoryAsync_IfExists_Should_ThrowDirectoryExistsException")]
[TestCategory("IntegrationTest")]
public override async Task CreateDirectoryAsync_IfExists_Should_ThrowDirectoryExistsException() => await base.CreateDirectoryAsync_IfExists_Should_ThrowDirectoryExistsException();

[Fact(DisplayName = "DeleteFileAsync")]
[TestMethod]
[TestCategory("IntegrationTest")]
public override async Task DeleteFileAsync() => await base.DeleteFileAsync();

[Fact(DisplayName = "DeleteFileAsync_IfNotExists_Should_ThrowFileNotFoundException")]
[TestMethod]
[TestCategory("IntegrationTest")]
public override async Task DeleteFileAsync_IfNotExists_Should_ThrowFileNotFoundException() => await base.DeleteFileAsync_IfNotExists_Should_ThrowFileNotFoundException();

[TestMethod]
[Fact(DisplayName = "DeleteDirectoryAsync")]
[TestCategory("IntegrationTest")]
public override async Task DeleteDirectoryAsync() => await base.DeleteDirectoryAsync();

[Fact(DisplayName = "DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException")]
[TestMethod]
[TestCategory("IntegrationTest")]
public override async Task DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException() => await base.DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException();

[TestMethod]
[Fact(DisplayName = "ReadFile")]
[TestCategory("IntegrationTest")]
public override void ReadFile() => base.ReadFile();

[TestMethod]
[Fact(DisplayName = "ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException")]
[TestCategory("IntegrationTest")]
public override void ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException() => base.ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException();

[TestMethod]
[Fact(DisplayName = "ReadFileAsync")]
[TestCategory("IntegrationTest")]
public override async Task ReadFileAsync() => await base.ReadFileAsync();

[TestMethod]
[Fact(DisplayName = "ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundException")]
[TestCategory("IntegrationTest")]
public override async Task ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundException() => await base.ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundException();

Expand Down
80 changes: 78 additions & 2 deletions tests/FileSystem.Tests/Base/TestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public virtual void Instantiation_RootPath_Should_Return_Correct()
Assert.AreEqual(RootPath, _adapter!.RootPath);
}

public virtual void Connect_ClientExist_Should_Return_Message_ConnectedSuccsefull()
public virtual void Connect_IfSuccess_Should_Return_Message_ConnectedSuccsefull()
{
//Arrange
Log.CloseAndFlush();
Expand All @@ -49,13 +49,80 @@ public virtual void Connect_ClientExist_Should_Return_Message_ConnectedSuccseful
.Once();
}

public virtual void GetFile_IfSuccess_Should_ReturnFile()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);

_adapter!.CreateDirectory(directory);
_adapter!.WriteFile(directory + "/file1", System.Text.Encoding.UTF8.GetBytes("file1"));

//Act
var file = _adapter!.GetFile(directory + "/file1");

//Assert
Assert.IsNotNull(file);
}

public virtual void GetFile_IfFileNotExist_Should_Throw_FileNotFoundException()
{
//Assert
var aggregateException = Assert.ThrowsException<AggregateException>(() => _adapter!.GetFile("test.txt"));
Assert.AreEqual(aggregateException.InnerException.GetType(), typeof(FileNotFoundException));
}

public virtual async Task GetFileAsync_IfSuccess_Should_ReturnFile()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);

_adapter!.CreateDirectory(directory);
_adapter!.WriteFile(directory + "/file1", System.Text.Encoding.UTF8.GetBytes("file1"));

//Act
var file = await _adapter!.GetFileAsync(directory + "/file1", new System.Threading.CancellationToken());

//Assert
Assert.IsNotNull(file);
}

public virtual async Task GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException()
{
//Assert
await Assert.ThrowsExceptionAsync<FileNotFoundException>(async () => await _adapter!.GetFileAsync("test.txt"));
}

public virtual async Task GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCorrectNumberOfFiles()
public virtual void GetFiles_IfSuccess_Should_ReturnFiles()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);

_adapter!.CreateDirectory(directory);
_adapter!.WriteFile(directory + "/file1", System.Text.Encoding.UTF8.GetBytes("file1"));
_adapter!.WriteFile(directory + "/file2", System.Text.Encoding.UTF8.GetBytes("file2"));
_adapter!.WriteFile(directory + "/file3", System.Text.Encoding.UTF8.GetBytes("file3"));
_adapter!.WriteFile(directory + "/file4", System.Text.Encoding.UTF8.GetBytes("file4"));
_adapter!.WriteFile(directory + "/file5", System.Text.Encoding.UTF8.GetBytes("file5"));
_adapter!.WriteFile(directory + "/file1", System.Text.Encoding.UTF8.GetBytes("file1file1"), true);

//Act
var files = _adapter!.GetFiles(directory);

//Assert
Assert.AreEqual(5, files.Count());
}

public virtual void GetFiles_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);

//Assert
var aggregateException = Assert.ThrowsException<AggregateException>(() => _adapter!.GetFiles(directory));
Assert.AreEqual(aggregateException.InnerException.GetType(), typeof(DirectoryNotFoundException));
}

public virtual async Task GetFilesAsync_IfSuccess_Should_ReturnFiles()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);
Expand All @@ -75,6 +142,15 @@ public virtual async Task GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCo
Assert.AreEqual(5, files.Count());
}

public virtual async Task GetFilesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);

//Assert
await Assert.ThrowsExceptionAsync<DirectoryNotFoundException>(async () => await _adapter!.GetFilesAsync(directory));
}

public virtual async Task CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists()
{
//Arrange
Expand Down
26 changes: 25 additions & 1 deletion tests/FileSystem.Tests/Base/UnitTestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,37 @@ public void Init()
[TestCategory("UnitTest")]
public override void Instantiation_RootPath_Should_Return_Correct() => base.Instantiation_RootPath_Should_Return_Correct();

[TestMethod]
[TestCategory("UnitTest")]
public override void GetFile_IfSuccess_Should_ReturnFile() => base.GetFile_IfSuccess_Should_ReturnFile();

[TestMethod]
[TestCategory("UnitTest")]
public override void GetFile_IfFileNotExist_Should_Throw_FileNotFoundException() => base.GetFile_IfFileNotExist_Should_Throw_FileNotFoundException();

[TestMethod]
[TestCategory("UnitTest")]
public override async Task GetFileAsync_IfSuccess_Should_ReturnFile() => await base.GetFileAsync_IfSuccess_Should_ReturnFile();

[TestMethod]
[TestCategory("UnitTest")]
public override async Task GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException() => await base.GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException();

[TestMethod]
[TestCategory("UnitTest")]
public override async Task GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCorrectNumberOfFiles() => await base.GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCorrectNumberOfFiles();
public override void GetFiles_IfSuccess_Should_ReturnFiles() => base.GetFiles_IfSuccess_Should_ReturnFiles();

[TestMethod]
[TestCategory("UnitTest")]
public override void GetFiles_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException() => base.GetFiles_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException();

[TestMethod]
[TestCategory("UnitTest")]
public override async Task GetFilesAsync_IfSuccess_Should_ReturnFiles() => await base.GetFilesAsync_IfSuccess_Should_ReturnFiles();

[TestMethod]
[TestCategory("UnitTest")]
public override async Task GetFilesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException() => await base.GetFilesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException();

[TestMethod]
[TestCategory("UnitTest")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using FileSystem.Tests.Base;
using Serilog;
using Serilog.Sinks.InMemory;

namespace Tests.FileSystem.Adapters.Ftp;

public class FtpAdapterIntegrationTest : IntegrationTestAdapter<FtpAdapter, FtpClient>, IClassFixture<FtpFixture>, IAsyncLifetime
Expand Down Expand Up @@ -38,49 +37,4 @@ public Task InitializeAsync()
_adapter = new FtpAdapter(Prefix, RootPath, ftpClient);
return Task.CompletedTask;
}

[Fact(DisplayName = "FtpAdapter_Instantiation_Prefix_Should_Return_Correct")]
public override void Instantiation_Prefix_Should_Return_Correct() => base.Instantiation_Prefix_Should_Return_Correct();

[Fact(DisplayName = "FtpAdapter_Instantiation_Prefix_Should_Return_Correct")]
public override void Instantiation_RootPath_Should_Return_Correct() => base.Instantiation_RootPath_Should_Return_Correct();

[Fact(DisplayName = "FtpAdapter_Connect_ClientExist_Should_ConnectedSuccsefull")]
public override void Connect_ClientExist_Should_Return_Message_ConnectedSuccsefull() => base.Connect_ClientExist_Should_Return_Message_ConnectedSuccsefull();

[Fact(DisplayName = "FtpAdapter_GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException")]
public override async Task GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException() => await base.GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException();

[Fact(DisplayName = "FtpAdapter_GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCorrectNumberOfFiles")]
public override async Task GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCorrectNumberOfFiles() => await base.GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCorrectNumberOfFiles();

[Fact(DisplayName = "FtpAdapter_CreateDirectoryAsync_Should_Exists")]
public override async Task CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists() => await base.CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists();

[Fact(DisplayName = "FtpAdapter_CreateDirectoryAsync_IfExists_Should_ThrowDirectoryExistsException")]
public override async Task CreateDirectoryAsync_IfExists_Should_ThrowDirectoryExistsException() => await base.CreateDirectoryAsync_IfExists_Should_ThrowDirectoryExistsException();

[Fact(DisplayName = "FtpAdapter_DeleteFileAsync")]
public override async Task DeleteFileAsync() => await base.DeleteFileAsync();

[Fact(DisplayName = "FtpAdapter_DeleteFileAsync_IfNotExists_Should_ThrowFileNotFoundException")]
public override async Task DeleteFileAsync_IfNotExists_Should_ThrowFileNotFoundException() => await base.DeleteFileAsync_IfNotExists_Should_ThrowFileNotFoundException();

[Fact(DisplayName = "FtpAdapter_DeleteDirectoryAsync")]
public override async Task DeleteDirectoryAsync() => await base.DeleteDirectoryAsync();

[Fact(DisplayName = "FtpAdapter_DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException")]
public override async Task DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException() => await base.DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException();

[Fact(DisplayName = "FtpAdapter_ReadFile")]
public override void ReadFile() => base.ReadFile();

[Fact(DisplayName = "FtpAdapter_ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException")]
public override void ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException() => base.ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException();

[Fact(DisplayName = "FtpAdapter_ReadFileAsync")]
public override async Task ReadFileAsync() => await base.ReadFileAsync();

[Fact(DisplayName = "FtpAdapter_ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundException")]
public override async Task ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundException() => await base.ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundException();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,4 @@ public class LocalAdapterTest : UnitTestAdapter<LocalAdapter>
public override string Prefix => "local1";
public override string RootPath => AppDomain.CurrentDomain.BaseDirectory;

[TestMethod("LocalAdapter_Instantiation_Prefix_Should_Return_Correct")]
public override void Instantiation_Prefix_Should_Return_Correct() => base.Instantiation_Prefix_Should_Return_Correct();

[TestMethod("LocalAdapter_Instantiation_RootPath_Should_Return_Correct")]
public override void Instantiation_RootPath_Should_Return_Correct() => base.Instantiation_RootPath_Should_Return_Correct();

[TestMethod("LocalAdapter_Connect_ClientExist_Should_ConnectedSuccsefull")]
public override void Connect_ClientExist_Should_Return_Message_ConnectedSuccsefull() => base.Connect_ClientExist_Should_Return_Message_ConnectedSuccsefull();

[TestMethod("LocalAdapter_GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException")]
public override async Task GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException() => await base.GetFileAsync_IfFileNotExist_Should_Throw_FileNotFoundException();

[TestMethod("LocalAdapter_GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCorrectNumberOfFiles")]
public override async Task GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCorrectNumberOfFiles() => await base.GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCorrectNumberOfFiles();

[TestMethod("LocalAdapter_CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists")]
public override async Task CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists() => await base.CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists();

[TestMethod("LocalAdapter_CreateDirectoryAsync_IfExists_Should_ThrowDirectoryExistsException")]
public override async Task CreateDirectoryAsync_IfExists_Should_ThrowDirectoryExistsException() => await base.CreateDirectoryAsync_IfExists_Should_ThrowDirectoryExistsException();

[TestMethod("LocalAdapter_DeleteFileAsync")]
public override async Task DeleteFileAsync() => await base.DeleteFileAsync();

[TestMethod("LocalAdapter_DeleteFileAsync_IfNotExists_Should_ThrowFileNotFoundException")]
public override async Task DeleteFileAsync_IfNotExists_Should_ThrowFileNotFoundException() => await base.DeleteFileAsync_IfNotExists_Should_ThrowFileNotFoundException();

[TestMethod("LocalAdapter_DeleteDirectoryAsync")]
public override async Task DeleteDirectoryAsync() => await base.DeleteDirectoryAsync();

[TestMethod("LocalAdapter_DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException")]
public override async Task DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException() => await base.DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException();

[TestMethod("LocalAdapter_ReadFile")]
public override void ReadFile() => base.ReadFile();

[TestMethod("LocalAdapter_ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException")]
public override void ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException() => base.ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException();

[TestMethod("LocalAdapter_ReadFileAsync")]
public override async Task ReadFileAsync() => await base.ReadFileAsync();

[TestMethod("LocalAdapter_ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundException")]
public override async Task ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundException() => await base.ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundException();

}
Loading

0 comments on commit dc1056e

Please sign in to comment.