Skip to content

Commit

Permalink
Code Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cardillo committed Sep 2, 2023
1 parent 81e8d85 commit 2f5b14f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Project is not a test project -->
<SonarQubeTestProject>true</SonarQubeTestProject>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 2 additions & 6 deletions src/FileSystem.Adapters.Ftp/src/FtpAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,9 @@ public override void Connect()
{
try
{
if (client.IsConnected)
{
Logger?.Information("{Adapter} - Connected succsefull", nameof(FtpAdapter));
return;
}
if (!client.IsConnected)
client.Connect();

client.Connect();
Logger?.Information("{Adapter} - Connected succsefull", nameof(FtpAdapter));
}
catch (Exception exception)
Expand Down
1 change: 0 additions & 1 deletion src/FileSystem/src/Adapters/LocalAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down
101 changes: 19 additions & 82 deletions src/FileSystem/src/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,15 @@ protected virtual void Dispose(bool disposing)
public IAdapter GetAdapter(string prefix)
{
if (Adapters.Count == 0)
{
throw new NoAdaptersRegisteredException();
}

var duplicateAdapters = Adapters.GroupBy(adapter => adapter.Prefix).Where(grouping => grouping.Count() > 1).ToList();

if (duplicateAdapters.Any())
{
throw new DuplicateAdapterPrefixException(duplicateAdapters, Adapters);
}

if (Adapters.All(adapter => adapter.Prefix != prefix))
{
throw new AdapterNotFoundException(prefix, Adapters);
}

return Adapters.First(adapter => adapter.Prefix == prefix);
}
Expand All @@ -93,10 +87,7 @@ public IAdapter GetAdapter(string prefix)
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="FileNotFoundException">Thrown if the file does not exists at the given path.</exception>
public IFile GetFile(string path)
{
return GetFileAsync(path).Result;
}
public IFile GetFile(string path) => GetFileAsync(path).Result;

/// <summary>
/// Return a file.
Expand Down Expand Up @@ -133,10 +124,7 @@ public Task<IFile> GetFileAsync(string path, CancellationToken cancellationToken
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="DirectoryNotFoundException">Thrown if the directory does not exists at the given path.</exception>
public IDirectory GetDirectory(string path)
{
return GetDirectoryAsync(path).Result;
}
public IDirectory GetDirectory(string path) => GetDirectoryAsync(path).Result;

/// <summary>
/// Returns a directory.
Expand Down Expand Up @@ -173,10 +161,7 @@ public Task<IDirectory> GetDirectoryAsync(string path, CancellationToken cancell
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="DirectoryNotFoundException">Thrown if the directory does not exists at the given path.</exception>
public IEnumerable<IFile> GetFiles(string path = "")
{
return GetFilesAsync(path).Result;
}
public IEnumerable<IFile> GetFiles(string path = "") => GetFilesAsync(path).Result;

/// <summary>
/// Returns files present at the provided path.
Expand Down Expand Up @@ -213,10 +198,7 @@ public Task<IEnumerable<IFile>> GetFilesAsync(string path = "", CancellationToke
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="DirectoryNotFoundException">Thrown if the directory does not exists at the given path.</exception>
public IEnumerable<IDirectory> GetDirectories(string path = "")
{
return GetDirectoriesAsync(path).Result;
}
public IEnumerable<IDirectory> GetDirectories(string path = "") => GetDirectoriesAsync(path).Result;

/// <summary>
/// Returns directories present at the provided path.
Expand Down Expand Up @@ -252,10 +234,7 @@ public Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string path = "", Cance
/// <exception cref="DuplicateAdapterPrefixException">Thrown when multiple adapters are registered with the same prefix.</exception>
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
public bool FileExists(string path)
{
return FileExistsAsync(path).Result;
}
public bool FileExists(string path) => FileExistsAsync(path).Result;

/// <summary>
/// Checks if a file exists at the provided path.
Expand Down Expand Up @@ -290,10 +269,7 @@ public async Task<bool> FileExistsAsync(string path, CancellationToken cancellat
/// <exception cref="DuplicateAdapterPrefixException">Thrown when multiple adapters are registered with the same prefix.</exception>
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
public bool DirectoryExists(string path)
{
return DirectoryExistsAsync(path).Result;
}
public bool DirectoryExists(string path) => DirectoryExistsAsync(path).Result;

/// <summary>
/// Checks if a directory exists at the provided path.
Expand Down Expand Up @@ -328,10 +304,7 @@ public async Task<bool> DirectoryExistsAsync(string path, CancellationToken canc
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="DirectoryExistsException">Thrown if the directory exists at the given path.</exception>
public void CreateDirectory(string path)
{
CreateDirectoryAsync(path).Wait();
}
public void CreateDirectory(string path) => CreateDirectoryAsync(path).Wait();

/// <summary>
/// Creates a directory at the provided path.
Expand Down Expand Up @@ -366,10 +339,7 @@ public async Task CreateDirectoryAsync(string path, CancellationToken cancellati
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="FileNotFoundException">Thrown if the file does not exists at the given path.</exception>
public void DeleteFile(string path)
{
DeleteFileAsync(path).Wait();
}
public void DeleteFile(string path) => DeleteFileAsync(path).Wait();

/// <summary>
/// Deletes a file at the provided path.
Expand Down Expand Up @@ -404,10 +374,7 @@ public async Task DeleteFileAsync(string path, CancellationToken cancellationTok
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="DirectoryNotFoundException">Thrown if the directory does not exists at the given path.</exception>
public void DeleteDirectory(string path)
{
DeleteDirectoryAsync(path).Wait();
}
public void DeleteDirectory(string path) => DeleteDirectoryAsync(path).Wait();

/// <summary>
/// Deletes a directory at the provided path.
Expand Down Expand Up @@ -443,10 +410,7 @@ public async Task DeleteDirectoryAsync(string path, CancellationToken cancellati
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="FileNotFoundException">Thrown if the file does not exists at the given path.</exception>
public byte[] ReadFile(string path)
{
return ReadFileAsync(path).Result;
}
public byte[] ReadFile(string path) => ReadFileAsync(path).Result;

/// <summary>
/// Reads a file at the provided path.
Expand Down Expand Up @@ -483,10 +447,7 @@ public async Task<byte[]> ReadFileAsync(string path, CancellationToken cancellat
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="FileNotFoundException">Thrown if the file does not exists at the given path.</exception>
public string ReadTextFile(string path)
{
return ReadTextFileAsync(path).Result;
}
public string ReadTextFile(string path) => ReadTextFileAsync(path).Result;

/// <summary>
/// Reads a text file at the provided path.
Expand Down Expand Up @@ -525,10 +486,7 @@ public async Task<string> ReadTextFileAsync(string path, CancellationToken cance
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="FileNotFoundException">Thrown if the file does not exists at the given path.</exception>
/// <exception cref="FileExistsException">Thrown if the file exists at the given path and parameter "overwrite" = false.</exception>
public void CopyFile(string sourcePath, string destinationPath, bool overwrite = false)
{
CopyFileAsync(sourcePath, destinationPath, overwrite).Wait();
}
public void CopyFile(string sourcePath, string destinationPath, bool overwrite = false) => CopyFileAsync(sourcePath, destinationPath, overwrite).Wait();

/// <summary>
/// Copies a file from a source path to a destination path.
Expand Down Expand Up @@ -580,10 +538,7 @@ public async Task CopyFileAsync(
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="FileNotFoundException">Thrown if the file does not exists at the given path.</exception>
/// <exception cref="FileExistsException">Thrown if the file exists at the given path and parameter "overwrite" = false.</exception>
public void MoveFile(string sourcePath, string destinationPath, bool overwrite = false)
{
MoveFileAsync(sourcePath, destinationPath, overwrite).Wait();
}
public void MoveFile(string sourcePath, string destinationPath, bool overwrite = false) => MoveFileAsync(sourcePath, destinationPath, overwrite).Wait();

/// <summary>
/// Moves a file from a source path to a destination path.
Expand Down Expand Up @@ -635,10 +590,7 @@ public async Task MoveFileAsync(
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="FileExistsException">Thrown if the file exists at the given path and parameter "overwrite" = false.</exception>
public void WriteFile(string path, byte[] contents, bool overwrite = false)
{
WriteFileAsync(path, contents, overwrite).Wait();
}
public void WriteFile(string path, byte[] contents, bool overwrite = false) => WriteFileAsync(path, contents, overwrite).Wait();

/// <summary>
/// Writes string contents to a file at the provided path.
Expand All @@ -653,10 +605,7 @@ public void WriteFile(string path, byte[] contents, bool overwrite = false)
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="FileExistsException">Thrown if the file exists at the given path and parameter "overwrite" = false.</exception>
public void WriteFile(string path, string contents, bool overwrite = false)
{
WriteFileAsync(path, contents, overwrite).Wait();
}
public void WriteFile(string path, string contents, bool overwrite = false) => WriteFileAsync(path, contents, overwrite).Wait();

/// <summary>
/// Writes byte array contents to a file at the provided path.
Expand Down Expand Up @@ -718,10 +667,7 @@ public async Task WriteFileAsync(string path, string contents, bool overwrite =
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="FileNotFoundException">Thrown if the file does not exists at the given path.</exception>
public void AppendFile(string path, byte[] contents)
{
AppendFileAsync(path, contents).Wait();
}
public void AppendFile(string path, byte[] contents) => AppendFileAsync(path, contents).Wait();

/// <summary>
/// Writes string contents to a file at the provided path.
Expand All @@ -735,10 +681,7 @@ public void AppendFile(string path, byte[] contents)
/// <exception cref="AdapterNotFoundException">Thrown when an adapter could not be found via the provided prefix.</exception>
/// <exception cref="PrefixNotFoundInPathException">Thrown when a prefix in the provided path could not be found.</exception>
/// <exception cref="FileNotFoundException">Thrown if the file does not exists at the given path.</exception>
public void AppendFile(string path, string contents)
{
AppendFileAsync(path, contents).Wait();
}
public void AppendFile(string path, string contents) => AppendFileAsync(path, contents).Wait();

/// <summary>
/// Writes byte array contents to a file at the provided path.
Expand Down Expand Up @@ -791,20 +734,14 @@ public async Task AppendFileAsync(string path, string contents, CancellationToke
/// </summary>
/// <param name="path">The prefixed path.</param>
/// <returns>The prefix.</returns>
private static string GetPrefix(string path)
{
return ResolvePrefixAndPath(path)[0];
}
private static string GetPrefix(string path) => ResolvePrefixAndPath(path)[0];

/// <summary>
/// Returns the path from a prefixed path.
/// </summary>
/// <param name="path">The prefixed path.</param>
/// <returns>The path.</returns>
private static string GetPath(string path)
{
return ResolvePrefixAndPath(path)[1];
}
private static string GetPath(string path) => ResolvePrefixAndPath(path)[1];

/// <summary>
/// Resolves the prefix and path from a prefixed path.
Expand Down
2 changes: 1 addition & 1 deletion tests/FileSystem.Tests/Base/IntegrationTestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Init()

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

[TestMethod]
[TestCategory("IntegrationTest")]
Expand Down
2 changes: 1 addition & 1 deletion tests/FileSystem.Tests/Base/TestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public virtual async Task GetFilesAsync_IfWriteFileSameDirectory_Should_ReturnCo
Assert.AreEqual(5, files.Count());
}

public virtual async Task CreateDirectoryAsync_Should_Exists()
public virtual async Task CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists()
{
//Arrange
var directory = faker.Database.Random.AlphaNumeric(30);
Expand Down
2 changes: 1 addition & 1 deletion tests/FileSystem.Tests/Base/UnitTestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Init()

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

[TestMethod]
[TestCategory("UnitTest")]
Expand Down

0 comments on commit 2f5b14f

Please sign in to comment.