diff --git a/UnitTests/SftpRepositoryShould.cs b/UnitTests/SftpRepositoryShould.cs new file mode 100644 index 0000000..57b76d0 --- /dev/null +++ b/UnitTests/SftpRepositoryShould.cs @@ -0,0 +1,166 @@ +using FileRepository; +using FileRepository.Models; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json.Linq; +using NUnit.Framework; +using System; +using System.IO; +using System.Threading.Tasks; +using UnitTests.Helpers; + +namespace UnitTests +{ + public class SftpRepositoryShould + { + private readonly IConfiguration config = InitConfiguration(); + private readonly Factory.RepositoryType repositoryType = Factory.RepositoryType.Sftp; + + [SetUp] + public void Setup() + { + } + + [Test] + public async Task ShouldCreateAFileWithoutThrowing() + { + try + { + await CreateTestFile("createtest.txt"); + } + catch (Exception ex) + { + Assert.Fail(ex.Message); + } + } + + [Test] + public async Task ShouldDeleteAFile() + { + try + { + await DeleteTestFile("createtest.txt"); + } + catch (Exception ex) + { + Assert.Fail(ex.Message); + } + } + + [Test] + public async Task ShouldReadAFile() + { + string filename = "readtest.txt"; + + // Setup + try + { + // Create a file to read + await CreateTestFile(filename); + } + catch (Exception ex) + { + Assert.Fail($"Failed to create a file to read - {ex.Message}"); + } + + try + { + // Read the file + IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config); + + RepoFile repoFile = await fileRepository.ReadFileAsync(filename); + + Assert.NotNull(repoFile); + Assert.NotNull(repoFile.FileName); + Assert.NotNull(repoFile.Content); + Assert.NotNull(repoFile.Created); + } + catch (Exception ex) + { + // Clean up + await DeleteTestFile(filename); + + Assert.Fail(ex.Message); + } + + // Clean up + await DeleteTestFile(filename); + } + + [Test] + public async Task ShouldUpdateAFile() + { + string filename = "updatetest.txt"; + + // Setup file to read + try + { + // Create a file to read + await CreateTestFile(filename); + } + catch (Exception ex) + { + Assert.Fail($"Failed to create a file to read - {ex.Message}"); + } + + // Read the file and update it + try + { + IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config); + RepoFile repoFile = await fileRepository.ReadFileAsync(filename); + + // Modify the content and update the file + string updateTestText = "update test text"; + repoFile.Content = StreamHelper.GenerateStreamFromString(updateTestText); + await fileRepository.UpdateFileAsync(repoFile); + + // Load the file anew to check for changes + RepoFile repoFileUpdated = await fileRepository.ReadFileAsync(filename); + + // Move the pointer back to the beginning of the stream so we can read it here + repoFileUpdated.Content.Seek(0, SeekOrigin.Begin); + + using (StreamReader reader = new StreamReader(repoFileUpdated.Content)) + { + string contents = await reader.ReadToEndAsync(); + Assert.AreEqual(updateTestText, contents); + } + } + catch (Exception ex) + { + // Clean up + await DeleteTestFile(filename); + + Assert.Fail(ex.Message); + } + + // Clean up + await DeleteTestFile(filename); + } + + private async Task CreateTestFile(String filename) + { + IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config); + RepoFile repoFile = new RepoFile(); + Stream contentStream = StreamHelper.GenerateStreamFromString("a,b \n c,d"); + + repoFile = repoFile.GenerateFile(filename, contentStream); + + await fileRepository.CreateFileAsync(repoFile); + } + + private async Task DeleteTestFile(String filename) + { + IFileRepository fileRepository = Factory.GetFileRepository(repositoryType, config); + + await fileRepository.DeleteFileAsync(filename); + } + + private static IConfiguration InitConfiguration() + { + IConfigurationRoot configBuilder = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .Build(); + return configBuilder; + } + } +} diff --git a/UnitTests/UnitTest1.cs b/UnitTests/UnitTest1.cs deleted file mode 100644 index 29a14af..0000000 --- a/UnitTests/UnitTest1.cs +++ /dev/null @@ -1,18 +0,0 @@ -using NUnit.Framework; - -namespace UnitTests -{ - public class Tests - { - [SetUp] - public void Setup() - { - } - - [Test] - public void Test1() - { - Assert.Pass(); - } - } -} \ No newline at end of file diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 87f61e5..818c15c 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -7,10 +7,25 @@ + + + + + + PreserveNewest + + + + + + + + +