From 1fee7f08f51cbdb0b22d6c0e732a3bc0e64aca6e Mon Sep 17 00:00:00 2001 From: John Andrews Date: Wed, 28 Aug 2024 08:15:46 +1200 Subject: [PATCH] next cloud unit test --- Nextcloud/FlowElements/UploadToNextcloud.cs | 28 ++++++++++++++++++++- Nextcloud/Helpers/NextcloudUploader.cs | 23 +++++++++++------ Nextcloud/Tests/UploadTest.cs | 24 +++++++++++------- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/Nextcloud/FlowElements/UploadToNextcloud.cs b/Nextcloud/FlowElements/UploadToNextcloud.cs index f1c42fb1..ed8cc1b6 100644 --- a/Nextcloud/FlowElements/UploadToNextcloud.cs +++ b/Nextcloud/FlowElements/UploadToNextcloud.cs @@ -82,7 +82,7 @@ public class UploadToNextcloud : Node args.Logger?.ILog("File: " + local.Value); args.Logger?.ILog("Destination: " + destination); - var uploader = new NextcloudUploader(args.Logger!, settings.Url, settings.Username, settings.Password); + var uploader = GetUploader(args.Logger!, settings.Url, settings.Username, settings.Password); var result = uploader.UploadFile(local.Value, destination); if(result.Failed(out error)) { @@ -94,4 +94,30 @@ public class UploadToNextcloud : Node args.Logger?.ILog("File successfully uploaded"); return 1; } + /// + /// The function to get the nextcloud uploader + /// + private Func? _GetUploader; + + /// + /// Gets the function to get the next cloud uploader used to send a request + /// + internal Func GetUploader + { + get + { + if(_GetUploader == null) + { + _GetUploader = (logger, url, username, password) => + new NextcloudUploader(logger, url, username, password); + } + return _GetUploader; + } +#if(DEBUG) + set + { + _GetUploader = value; + } +#endif + } } \ No newline at end of file diff --git a/Nextcloud/Helpers/NextcloudUploader.cs b/Nextcloud/Helpers/NextcloudUploader.cs index bb9302a6..c744f3e8 100644 --- a/Nextcloud/Helpers/NextcloudUploader.cs +++ b/Nextcloud/Helpers/NextcloudUploader.cs @@ -2,6 +2,20 @@ using System.Net; namespace FileFlows.Nextcloud.Helpers; +/// +/// Nextcloud uploader +/// +public interface INextcloudUploader +{ + /// + /// Uploads a file to Nextcloud + /// + /// The full path to the file on disk to be uploaded. + /// The path in Nextcloud where the file should be uploaded. + /// True if the file was uploaded successfully; otherwise, false. + Result UploadFile(string localFilePath, string remoteFilePath); +} + /// /// Helper class for uploading files to Nextcloud /// @@ -9,7 +23,7 @@ namespace FileFlows.Nextcloud.Helpers; /// The URL of the Nextcloud instance. /// The username for authentication. /// The password for authentication. -public class NextcloudUploader(ILogger logger, string nextcloudUrl, string username, string password) +public class NextcloudUploader(ILogger logger, string nextcloudUrl, string username, string password) : INextcloudUploader { private static HttpClient client; @@ -25,12 +39,7 @@ public class NextcloudUploader(ILogger logger, string nextcloudUrl, string usern } - /// - /// Uploads a file to Nextcloud - /// - /// The full path to the file on disk to be uploaded. - /// The path in Nextcloud where the file should be uploaded. - /// True if the file was uploaded successfully; otherwise, false. + /// public Result UploadFile(string localFilePath, string remoteFilePath) { logger?.ILog("Uploading file: " + localFilePath); diff --git a/Nextcloud/Tests/UploadTest.cs b/Nextcloud/Tests/UploadTest.cs index df6d9c77..0a3c58a2 100644 --- a/Nextcloud/Tests/UploadTest.cs +++ b/Nextcloud/Tests/UploadTest.cs @@ -1,7 +1,9 @@ #if(DEBUG) using FileFlows.Nextcloud.FlowElements; +using FileFlows.Nextcloud.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; using PluginTestLibrary; namespace FileFlows.Nextcloud.Tests; @@ -12,15 +14,19 @@ public class UploadTest : TestBase [TestMethod] public void Test() { - var args = new NodeParameters("/home/john/src/ff-files/test-files/images/heic1.heic", Logger, false, string.Empty, new LocalFileService()); - args.GetPluginSettingsJson = (string input) => - { - return File.ReadAllText("../../../../../nextcloud.json"); - }; - - var ele = new UploadToNextcloud(); - ele.DestinationPath = "ff-test/" + Guid.NewGuid() + ".heic"; - Assert.AreEqual(1, ele.Execute(args)); + var args = GetNodeParameters(TempFile); + args.GetPluginSettingsJson = _ => """{"Username": "user", "Password": "password", "Url": "http://nextcloud.test" }"""; + string destPath = "ff-test/" + Guid.NewGuid() + ".heic"; + Mock mockUploader = new(); + mockUploader.Setup(x => x.UploadFile( + It.Is(y => y == TempFile), + It.Is(y => y == destPath))) + .Returns(true); + + var element = new UploadToNextcloud(); + element.GetUploader = (_, _, _, _) => mockUploader.Object; + element.DestinationPath = destPath; + Assert.AreEqual(1, element.Execute(args)); } } #endif \ No newline at end of file