Skip to content

Commit

Permalink
Merge pull request #39 from FrendsPlatform/issue-19
Browse files Browse the repository at this point in the history
FTP.DownloadFiles - Modified operations log
  • Loading branch information
Svenskapojkarna authored Jan 16, 2024
2 parents 090dee8 + 4d459bd commit 5c73c0b
Show file tree
Hide file tree
Showing 12 changed files with 615 additions and 605 deletions.
4 changes: 4 additions & 0 deletions Frends.FTP.DownloadFiles/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [1.1.2] - 2024-01-16
### Improved
- Improved Operations log by adding more logging steps.

## [1.1.1] - 2024-01-04
### Added
- Added setup for FtpClient.ReadTimeout, FtpClient.DataConnectionConnectTimeout and FtpClient.DataConnectionReadTimeout which were all defaulting to 15 seconds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ public void DownloadFTPS_IncorrectFingerprint()
// Test and assert
var ex = Assert.Throws<AggregateException>(() =>
{
var result = FTP.DownloadFiles(source, destination, connection, new Options(), new Info(),
new CancellationToken());
FTP.DownloadFiles(source, destination, connection, new Options(), new Info(), default);

});

Expand Down Expand Up @@ -208,8 +207,7 @@ public void DownloadFTPS_CurrentUserHasNoCertificates()

var ex = Assert.Throws<AggregateException>(() =>
{
var result = FTP.DownloadFiles(source, destination, connection, new Options(), new Info(),
new CancellationToken());
FTP.DownloadFiles(source, destination, connection, new Options(), new Info(), default);

});

Expand All @@ -221,7 +219,7 @@ public void DownloadFTPS_CurrentUserHasNoCertificates()
[Test]
public void DownloadFTP_LargeFiles()
{
FtpHelper.CreateLargeFileOnFTP(FtpDir, 5);
FtpHelper.CreateLargeFileOnFTP(FtpDir, 1);
var source = new Source { Directory = FtpDir, FileName = "*.bin", Operation = SourceOperation.Delete };
var destination = new Destination { Directory = LocalDirFullPath, Action = DestinationAction.Overwrite };
var connection = new Connection
Expand All @@ -235,7 +233,7 @@ public void DownloadFTP_LargeFiles()
// Test and assert
var result = FTP.DownloadFiles(source, destination, connection, new Options(), new Info(), new CancellationToken());
Assert.IsTrue(result.Success, result.UserResultMessage);
Assert.AreEqual(5, result.SuccessfulTransferCount);
Assert.AreEqual(1, result.SuccessfulTransferCount);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void OneTimeSetUp()
public void OneTimeTearDown()
{
FtpHelper.Dispose();
FtpHelper.RemoveLocalTestFiles();
}

protected bool LocalFileExists(string fileName, string subDir = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ private FtpClient Client
get
{
client ??= new FtpClient(FtpHost, FtpPort, FtpUsername, FtpPassword);
//client.connConnect();
return client;
}
}
Expand Down Expand Up @@ -118,4 +117,13 @@ public void DeleteDirectoryOnFTP(string ftpDir)
{
client.DeleteDirectory(ftpDir, FtpListOption.Recursive);
}

public static void RemoveLocalTestFiles()
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../DockerVolumes/", "data");
foreach (var dir in Directory.GetDirectories(path))
{
Directory.Delete(dir, true);
}
}
}
168 changes: 84 additions & 84 deletions Frends.FTP.DownloadFiles/Frends.FTP.DownloadFiles.Tests/MacrosTests.cs
Original file line number Diff line number Diff line change
@@ -1,109 +1,109 @@
using System;
using System.IO;
using System.Threading;
using Frends.FTP.DownloadFiles.Enums;
using Frends.FTP.DownloadFiles.TaskConfiguration;
using Frends.FTP.DownloadFiles.TaskResult;
using Frends.FTP.DownloadFiles.Tests.Lib;
using NUnit.Framework;

namespace Frends.FTP.DownloadFiles.Tests;

/// <summary>
/// We have several places that support macros:
/// - source dir
/// - destination dir
/// - destination file name
///
/// NOTE:
/// - source file name does not support macros for some reason in Cobalt, currently not implementing because
/// need to understand reasoning better
/// </summary>
[TestFixture]
public class MacrosTests : DownloadFilesTestBase
{
[Test]
public void MacrosWorkInSourceDirectory()
{
// Setup
var year = DateTime.Now.Year;
using System;
using System.IO;
using System.Threading;
using Frends.FTP.DownloadFiles.Enums;
using Frends.FTP.DownloadFiles.TaskConfiguration;
using Frends.FTP.DownloadFiles.TaskResult;
using Frends.FTP.DownloadFiles.Tests.Lib;
using NUnit.Framework;

namespace Frends.FTP.DownloadFiles.Tests;

/// <summary>
/// We have several places that support macros:
/// - source dir
/// - destination dir
/// - destination file name
///
/// NOTE:
/// - source file name does not support macros for some reason in Cobalt, currently not implementing because
/// need to understand reasoning better
/// </summary>
[TestFixture]
public class MacrosTests : DownloadFilesTestBase
{
[Test]
public void MacrosWorkInSourceDirectory()
{
// Setup
var year = DateTime.Now.Year;
FtpHelper.CreateFileOnFTP($"dir{year}", "file1.txt");

var result = CallDownloadFiles(
"dir%Year%",
"file*.txt",
var result = CallDownloadFiles(
"dir%Year%",
"file*.txt",
LocalDirFullPath);

Assert.IsTrue(result.Success, result.UserResultMessage);
Assert.AreEqual(1, result.SuccessfulTransferCount);
Assert.IsTrue(LocalFileExists("file1.txt"));
Assert.IsTrue(result.Success, result.UserResultMessage);
Assert.AreEqual(1, result.SuccessfulTransferCount);
Assert.IsTrue(LocalFileExists("file1.txt"));
}

[Test]
public void MacrosWorkInDestinationDirectory()
{
var year = DateTime.Now.Year;
var guid = Guid.NewGuid().ToString();
FtpHelper.CreateFileOnFTP(FtpDir, "file1.txt");
var destinationDirWithMacros = Path.Combine(Path.GetTempPath(), $"transfer-%Year%-{guid}");
[Test]
public void MacrosWorkInDestinationDirectory()
{
var year = DateTime.Now.Year;
var guid = Guid.NewGuid().ToString();
FtpHelper.CreateFileOnFTP(FtpDir, "file1.txt");
var destinationDirWithMacros = Path.Combine(Path.GetTempPath(), $"transfer-%Year%-{guid}");
var destinationDirWithMacrosExpanded = Path.Combine(Path.GetTempPath(), $"transfer-{year}-{guid}");

var result = CallDownloadFiles(
FtpDir,
"file1.txt",
var result = CallDownloadFiles(
FtpDir,
"file1.txt",
destinationDirWithMacros);

Assert.IsTrue(result.Success, result.UserResultMessage);
Assert.AreEqual(1, result.SuccessfulTransferCount);
Assert.IsTrue(File.Exists(Path.Combine(destinationDirWithMacrosExpanded, "file1.txt")), result.UserResultMessage);
Assert.IsTrue(result.Success, result.UserResultMessage);
Assert.AreEqual(1, result.SuccessfulTransferCount);
Assert.IsTrue(File.Exists(Path.Combine(destinationDirWithMacrosExpanded, "file1.txt")), result.UserResultMessage);
}

[Test]
public void MacrosWorkInDestinationFilename()
{
var year = DateTime.Now.Year;
var guid = Guid.NewGuid().ToString();
FtpHelper.CreateFileOnFTP(FtpDir, "file1.txt");
var destinationFileNameWithMacros = $"f-%Year%-%SourceFileName%-{guid}";
[Test]
public void MacrosWorkInDestinationFilename()
{
var year = DateTime.Now.Year;
var guid = Guid.NewGuid().ToString();
FtpHelper.CreateFileOnFTP(FtpDir, "file1.txt");
var destinationFileNameWithMacros = $"f-%Year%-%SourceFileName%-{guid}";
var destinationFileNameWithMacrosExpanded = $"f-{year}-file1-{guid}";

var result = CallDownloadFiles(
FtpDir,
"file1.txt",
LocalDirFullPath,
var result = CallDownloadFiles(
FtpDir,
"file1.txt",
LocalDirFullPath,
destinationFileNameWithMacros);

Assert.IsTrue(result.Success, result.UserResultMessage);
Assert.AreEqual(1, result.SuccessfulTransferCount);
Assert.IsTrue(File.Exists(Path.Combine(LocalDirFullPath, destinationFileNameWithMacrosExpanded)), result.UserResultMessage);
Assert.IsTrue(result.Success, result.UserResultMessage);
Assert.AreEqual(1, result.SuccessfulTransferCount);
Assert.IsTrue(File.Exists(Path.Combine(LocalDirFullPath, destinationFileNameWithMacrosExpanded)), result.UserResultMessage);
}

private Result CallDownloadFiles(
string sourceDirectory,
string sourceFileName,
string targetDirectory,
string targetFileName = null,
string moveToDir = null,
string renameTo = null)
{
var source = new Source
{
private static Result CallDownloadFiles(
string sourceDirectory,
string sourceFileName,
string targetDirectory,
string targetFileName = null,
string moveToDir = null,
string renameTo = null)
{
var source = new Source
{
Directory = sourceDirectory,
FileName = sourceFileName,
Operation = SourceOperation.Delete,
DirectoryToMoveAfterTransfer = moveToDir,
FileNameAfterTransfer = renameTo
};
FileName = sourceFileName,
Operation = SourceOperation.Delete,
DirectoryToMoveAfterTransfer = moveToDir,
FileNameAfterTransfer = renameTo
};
var destination = new Destination
{
Directory = targetDirectory,
Action = DestinationAction.Overwrite,
FileName = targetFileName
};
var options = new Options { CreateDestinationDirectories = true, RenameSourceFileBeforeTransfer = true };
var connection = FtpHelper.GetFtpsConnection();

var result = FTP.DownloadFiles(source, destination, connection, options, new Info(), new CancellationToken());
return result;
}
};
var options = new Options { CreateDestinationDirectories = true, RenameSourceFileBeforeTransfer = true };
var connection = FtpHelper.GetFtpsConnection();

var result = FTP.DownloadFiles(source, destination, connection, options, new Info(), new CancellationToken());
return result;
}
}
Loading

0 comments on commit 5c73c0b

Please sign in to comment.