Skip to content

Commit

Permalink
Added logic to also save a new FileAttachment record DynamicsValue/fa…
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed Aug 8, 2024
1 parent 4f226f0 commit ff7f54b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/FakeXrmEasy.Core/FileStorage/Db/InMemoryFileDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using FakeXrmEasy.Core.Db;
using FakeXrmEasy.Core.FileStorage.Db.Exceptions;
using Microsoft.Xrm.Sdk;

namespace FakeXrmEasy.Core.FileStorage.Db
{
Expand All @@ -12,6 +13,8 @@ internal class InMemoryFileDb : IInMemoryFileDbInternal
private readonly ConcurrentDictionary<string, FileUploadSession> _uncommittedFileUploads;
private readonly ConcurrentDictionary<string, FileAttachment> _files;
private readonly InMemoryDb _db;

internal const string FILE_ATTACHMENT_TABLE_NAME = "fileattachment";

internal InMemoryFileDb(InMemoryDb db)
{
Expand Down Expand Up @@ -101,8 +104,32 @@ public string CommitFileUploadSession(CommitFileUploadSessionProperties commitPr
_files.TryRemove(fileAttachment.Id, out var removedFileAttachment);
}

SaveFileAttachment(fileUploadSession, fileAttachment);

return fileAttachment.Id;
}
}

private void SaveFileAttachment(FileUploadSession fileUploadSession, FileAttachment fileAttachment)
{
//Add file attachment
if (!_db.ContainsTable(FILE_ATTACHMENT_TABLE_NAME))
{
InMemoryTable fileAttachmentTable;
_db.AddTable(FILE_ATTACHMENT_TABLE_NAME, out fileAttachmentTable);
}

var filesTable = _db.GetTable(FILE_ATTACHMENT_TABLE_NAME);
filesTable.Add(new Entity(FILE_ATTACHMENT_TABLE_NAME)
{
Id = new Guid(fileAttachment.Id)
});

//Update associated record
var table = _db.GetTable(fileUploadSession.Properties.Target.LogicalName);
var record = table.GetById(fileUploadSession.Properties.Target.Id);
record[fileUploadSession.Properties.FileAttributeName] = fileAttachment.Id;
record[$"{fileUploadSession.Properties.FileAttributeName}_name"] = fileAttachment.FileName;
}
}
}
18 changes: 15 additions & 3 deletions tests/FakeXrmEasy.Core.Tests/FileStorage/Db/InMemoryFileDbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ public void Should_init_and_commit_file()
Assert.Equal(commitProperties.FileName, createdFile.FileName);
Assert.Equal(commitProperties.MimeType, createdFile.MimeType);
Assert.Equal(new byte[] { 1, 2, 3, 4 }, createdFile.Content);

//Uncommited session is removed
Assert.Null(_fileDb.GetFileUploadSession(fileContinuationToken));

//File attribute is updated with file id and file name
var entityAfter = _db.GetTable(_entity.LogicalName).GetById(_entity.Id);
Assert.Equal(createdFile.Id, entityAfter[_fileUploadProperties.FileAttributeName]);
Assert.Equal(createdFile.FileName, entityAfter[$"{_fileUploadProperties.FileAttributeName}_name"]);

//New file attachment record is created
var fileAttachment = _db
.GetTable(InMemoryFileDb.FILE_ATTACHMENT_TABLE_NAME)
.GetById(new Guid(createdFile.Id));

Assert.NotNull(fileAttachment);
}

[Fact]
Expand Down Expand Up @@ -148,9 +163,6 @@ public void Should_init_and_upload_multiple_blocks_concurrently_and_commit_file(
Assert.Equal(Convert.ToByte(30 + i + 1), createdFile.Content[i * 4 + 2]);
Assert.Equal(Convert.ToByte(40 + i + 1), createdFile.Content[i * 4 + 3]);
}

//Uncommited session is removed
Assert.Null(_fileDb.GetFileUploadSession(fileContinuationToken));
}

[Fact]
Expand Down

0 comments on commit ff7f54b

Please sign in to comment.