From 0f2e664e9fb50e294631cce8d5325811859d98f0 Mon Sep 17 00:00:00 2001 From: Richard Randak Date: Mon, 4 May 2020 09:41:08 +0200 Subject: [PATCH] Fix issues in ArchiveConnector --- .../GeneratedTests/ArchiveTests.cs | 63 ++++++++++++++++++- .../SupplierInvoiceFileConnectionTests.cs | 2 +- .../Generated/Connectors/ArchiveConnector.cs | 54 +++++++++++++--- .../Generated/Entities/Archive/ArchiveFile.cs | 5 ++ 4 files changed, 113 insertions(+), 11 deletions(-) diff --git a/FortnoxAPILibrary.Tests/GeneratedTests/ArchiveTests.cs b/FortnoxAPILibrary.Tests/GeneratedTests/ArchiveTests.cs index b1569ecf..f1aaca3f 100644 --- a/FortnoxAPILibrary.Tests/GeneratedTests/ArchiveTests.cs +++ b/FortnoxAPILibrary.Tests/GeneratedTests/ArchiveTests.cs @@ -74,8 +74,69 @@ public void Test_Folder_Delete_ByPath() MyAssert.HasNoError(connector); Assert.AreEqual(randomFolderName, retrievedFolder.Name); - connector.DeleteFolder(testRootFolder.Name+ @"\" + retrievedFolder.Name); + connector.DeleteFolder(testRootFolder.Name + @"\" + retrievedFolder.Name); MyAssert.HasNoError(connector); } + + [TestMethod] + public void Test_GetSupplierFolder() + { + IArchiveConnector connector1 = new InboxConnector(); + var folder1 = connector1.GetFolder(StaticFolders.SupplierInvoices); + Assert.AreEqual("inbox_s", folder1.Id); + + IArchiveConnector connector2 = new ArchiveConnector(); + var folder2 = connector2.GetFolder(StaticFolders.SupplierInvoices); + Assert.AreEqual("inbox_s", folder1.Id); + } + + [TestMethod] + public void Test_Upload_Download_Delete_From_Static_Folder() + { + IArchiveConnector connector = new ArchiveConnector(); + + var data = Resource.fortnox_image; + var randomFileName = TestUtils.RandomString() + ".txt"; + + var fortnoxFile = connector.UploadFile(randomFileName, data, StaticFolders.SupplierInvoices); + MyAssert.HasNoError(connector); + + var fileData = connector.DownloadFile(fortnoxFile.Id); + MyAssert.HasNoError(connector); + CollectionAssert.AreEqual(data, fileData); + + connector.DeleteFile(fortnoxFile.Id); + MyAssert.HasNoError(connector); + } + + [TestMethod] + public void Test_ManyRequests() + { + IArchiveConnector connector = new ArchiveConnector(); + + for (int i = 0; i < 20; i++) + { + var data = Resource.fortnox_image; + var randomFileName = TestUtils.RandomString() + ".txt"; + + var fortnoxFile = connector.UploadFile(randomFileName, data, StaticFolders.Root); + MyAssert.HasNoError(connector); + + var fileData = connector.DownloadFile(fortnoxFile.Id); + MyAssert.HasNoError(connector); + CollectionAssert.AreEqual(data, fileData); + + connector.DeleteFile(fortnoxFile.Id); + MyAssert.HasNoError(connector); + } + } + + [TestMethod] + public void Test_Get_Root() + { + IArchiveConnector connector = new ArchiveConnector(); + var rootFolder = connector.GetRoot(); + Assert.AreEqual("root", rootFolder.Id); + } } } diff --git a/FortnoxAPILibrary.Tests/GeneratedTests/SupplierInvoiceFileConnectionTests.cs b/FortnoxAPILibrary.Tests/GeneratedTests/SupplierInvoiceFileConnectionTests.cs index 319ebc0d..35c4a0f3 100644 --- a/FortnoxAPILibrary.Tests/GeneratedTests/SupplierInvoiceFileConnectionTests.cs +++ b/FortnoxAPILibrary.Tests/GeneratedTests/SupplierInvoiceFileConnectionTests.cs @@ -41,7 +41,7 @@ public void Test_SupplierInvoiceFileConnection_CRUD() new SupplierInvoiceRow(){ ArticleNumber = tmpArticle.ArticleNumber, Quantity = 20, Price = 100} } }); - var tmpFile = new InboxConnector().UploadFile("tmpImage.png", Resource.fortnox_image, "inbox_s"); + var tmpFile = new InboxConnector().UploadFile("tmpImage.png", Resource.fortnox_image, StaticFolders.SupplierInvoices); #endregion Arrange ISupplierInvoiceFileConnectionConnector connector = new SupplierInvoiceFileConnectionConnector(); diff --git a/FortnoxAPILibrary/Generated/Connectors/ArchiveConnector.cs b/FortnoxAPILibrary/Generated/Connectors/ArchiveConnector.cs index ad0aadde..af0b58b2 100644 --- a/FortnoxAPILibrary/Generated/Connectors/ArchiveConnector.cs +++ b/FortnoxAPILibrary/Generated/Connectors/ArchiveConnector.cs @@ -3,7 +3,6 @@ using System.IO; using System.Linq; using System.Web; -using FortnoxAPILibrary; using FortnoxAPILibrary.Entities; using System.Threading.Tasks; @@ -61,14 +60,15 @@ public ArchiveFile UploadFile(string name, byte[] data, string folderPathOrId = if (!Path.HasExtension(name)) throw new ArgumentException("File name with extention must be set."); + if (string.IsNullOrEmpty(folderPathOrId)) + folderPathOrId = "root"; + var urlParams = new Dictionary(); - if (folderPathOrId != null) - { - if (IsArchiveId(folderPathOrId)) - urlParams.Add("folderId", folderPathOrId); - else - urlParams.Add("path", folderPathOrId); - } + + if (IsArchiveId(folderPathOrId) || IsPredefinedFolder(folderPathOrId)) + urlParams.Add("folderid", folderPathOrId); + else + urlParams.Add("path", folderPathOrId); return BaseUpload(name, data, urlParams); } @@ -100,7 +100,10 @@ public void DeleteFile(string id) /// public ArchiveFolder GetFolder(string pathOrId = null) { - if (IsArchiveId(pathOrId)) + if (string.IsNullOrEmpty(pathOrId)) + pathOrId = "root"; + + if (IsArchiveId(pathOrId) || IsPredefinedFolder(pathOrId)) return BaseGet(pathOrId).Result; else { @@ -227,5 +230,38 @@ private static bool IsArchiveId(string str) return Guid.TryParse(str, out _); } + private static bool IsPredefinedFolder(string str) + { + switch (str) + { + case StaticFolders.Root: + case StaticFolders.AssetRegister: + case StaticFolders.DailyTakings: + case StaticFolders.SupplierInvoices: + case StaticFolders.Vouchers: + case StaticFolders.BankFiles: + case StaticFolders.Salary: + case StaticFolders.CustomerInvoices: + case StaticFolders.Orders: + case StaticFolders.Offers: + return true; + default: + return false; + } + } + } + + public class StaticFolders + { + public const string Root = "root"; + public const string AssetRegister = "inbox_a"; + public const string DailyTakings = "inbox_d"; + public const string SupplierInvoices = "inbox_s"; + public const string Vouchers = "inbox_v"; + public const string BankFiles = "inbox_b"; + public const string Salary = "inbox_l"; + public const string CustomerInvoices = "inbox_kf"; + public const string Orders = "inbox_o"; + public const string Offers = "inbox_of"; //"quotes" } } diff --git a/FortnoxAPILibrary/Generated/Entities/Archive/ArchiveFile.cs b/FortnoxAPILibrary/Generated/Entities/Archive/ArchiveFile.cs index 08699b0f..ce0733a0 100644 --- a/FortnoxAPILibrary/Generated/Entities/Archive/ArchiveFile.cs +++ b/FortnoxAPILibrary/Generated/Entities/Archive/ArchiveFile.cs @@ -39,5 +39,10 @@ public class ArchiveFile [ReadOnly] [JsonProperty] public string Size { get; private set; } + + /// Id of the file in Archive. Only defined if obtained from Inbox connector + [ReadOnly] + [JsonProperty] + public string ArchiveFileId { get; set; } } } \ No newline at end of file