Skip to content

Commit

Permalink
Fix issues in ArchiveConnector
Browse files Browse the repository at this point in the history
  • Loading branch information
richardrandak committed May 4, 2020
1 parent ac13f3a commit 0f2e664
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 11 deletions.
63 changes: 62 additions & 1 deletion FortnoxAPILibrary.Tests/GeneratedTests/ArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
54 changes: 45 additions & 9 deletions FortnoxAPILibrary/Generated/Connectors/ArchiveConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using System.Linq;
using System.Web;
using FortnoxAPILibrary;
using FortnoxAPILibrary.Entities;

using System.Threading.Tasks;
Expand Down Expand Up @@ -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<string, string>();
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);
}
Expand Down Expand Up @@ -100,7 +100,10 @@ public void DeleteFile(string id)
/// <returns></returns>
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
{
Expand Down Expand Up @@ -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"
}
}
5 changes: 5 additions & 0 deletions FortnoxAPILibrary/Generated/Entities/Archive/ArchiveFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@ public class ArchiveFile
[ReadOnly]
[JsonProperty]
public string Size { get; private set; }

///<summary> Id of the file in Archive. Only defined if obtained from Inbox connector</summary>
[ReadOnly]
[JsonProperty]
public string ArchiveFileId { get; set; }
}
}

0 comments on commit 0f2e664

Please sign in to comment.