diff --git a/CHANGELOG.md b/CHANGELOG.md index c6c21ef..f614292 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [2.6.0] + +### Added + +- Added new messages for File and Image Upload/Download/Delete - https://github.com/DynamicsValue/fake-xrm-easy/issues/157 + ## [2.5.0] ### Added diff --git a/src/FakeXrmEasy.Messages/FakeMessageExecutors/CommitFileBlocksUploadRequestExecutor.cs b/src/FakeXrmEasy.Messages/FakeMessageExecutors/CommitFileBlocksUploadRequestExecutor.cs new file mode 100644 index 0000000..5a0229d --- /dev/null +++ b/src/FakeXrmEasy.Messages/FakeMessageExecutors/CommitFileBlocksUploadRequestExecutor.cs @@ -0,0 +1,69 @@ +#if FAKE_XRM_EASY_9 +using System; +using System.Linq; +using FakeXrmEasy.Abstractions; +using FakeXrmEasy.Abstractions.FakeMessageExecutors; +using FakeXrmEasy.Core.FileStorage.Upload; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; + +namespace FakeXrmEasy.FakeMessageExecutors +{ + /// + /// Implements the CommitFileBlocksUploadRequest https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.commitfileblocksuploadrequest?view=dataverse-sdk-latest + /// + public class CommitFileBlocksUploadRequestExecutor: IFakeMessageExecutor + { + /// + /// Returns true when the request is a CommitFileBlocksUploadRequest + /// + /// The request to check if it can be executed by this executor + /// + public bool CanExecute(OrganizationRequest request) + { + return request is CommitFileBlocksUploadRequest; + } + + /// + /// Executes a CommitFileBlocksUploadRequest + /// + /// The request to execute + /// The IXrmFakedContext that this request will be executed against + /// CommitFileBlocksUploadResponse + public OrganizationResponse Execute(OrganizationRequest request, IXrmFakedContext ctx) + { + var req = request as CommitFileBlocksUploadRequest; + + var fileDb = (ctx as XrmFakedContext).FileDb; + + var fileId = fileDb.CommitFileUploadSession(new CommitFileUploadSessionProperties() + { + FileName = req.FileName, + MimeType = req.MimeType, + BlockIdsListSequence = req.BlockList, + FileUploadSessionId = req.FileContinuationToken + }); + + var file = fileDb.GetFileById(fileId); + + return new CommitFileBlocksUploadResponse() + { + Results = new ParameterCollection() + { + { "FileId" , new Guid(fileId) }, + { "FileSizeInBytes" , (long) file.Content.Length } + } + }; + } + + /// + /// The type of CommitFileBlocksUploadRequest + /// + /// + public Type GetResponsibleRequestType() + { + return typeof(CommitFileBlocksUploadRequest); + } + } +} +#endif \ No newline at end of file diff --git a/src/FakeXrmEasy.Messages/FakeMessageExecutors/DeleteFileRequestExecutor.cs b/src/FakeXrmEasy.Messages/FakeMessageExecutors/DeleteFileRequestExecutor.cs new file mode 100644 index 0000000..d357c77 --- /dev/null +++ b/src/FakeXrmEasy.Messages/FakeMessageExecutors/DeleteFileRequestExecutor.cs @@ -0,0 +1,54 @@ +#if FAKE_XRM_EASY_9 +using System; +using FakeXrmEasy.Abstractions; +using FakeXrmEasy.Abstractions.FakeMessageExecutors; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; + +namespace FakeXrmEasy.FakeMessageExecutors +{ + /// + /// Implements the DeleteFileRequest message https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.deletefilerequest?view=dataverse-sdk-latest + /// + public class DeleteFileRequestExecutor: IFakeMessageExecutor + { + /// + /// Returns true if the request is a DeleteFileRequest message + /// + /// + /// + /// + public bool CanExecute(OrganizationRequest request) + { + return request is DeleteFileRequest; + } + + /// + /// Implements a fake DeleteFileRequest message + /// + /// + /// + /// + /// + public OrganizationResponse Execute(OrganizationRequest request, IXrmFakedContext ctx) + { + var req = request as DeleteFileRequest; + + var fileDb = (ctx as XrmFakedContext).FileDb; + fileDb.DeleteFile(req.FileId.ToString()); + + return new DeleteFileResponse(); + } + + /// + /// Returns the type of DeleteFileRequest + /// + /// + /// + public Type GetResponsibleRequestType() + { + return typeof(DeleteFileRequest); + } + } +} +#endif \ No newline at end of file diff --git a/src/FakeXrmEasy.Messages/FakeMessageExecutors/DownloadBlockRequestExecutor.cs b/src/FakeXrmEasy.Messages/FakeMessageExecutors/DownloadBlockRequestExecutor.cs new file mode 100644 index 0000000..a70e349 --- /dev/null +++ b/src/FakeXrmEasy.Messages/FakeMessageExecutors/DownloadBlockRequestExecutor.cs @@ -0,0 +1,66 @@ +#if FAKE_XRM_EASY_9 +using System; +using FakeXrmEasy.Abstractions; +using FakeXrmEasy.Abstractions.FakeMessageExecutors; +using FakeXrmEasy.Core.FileStorage.Download; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; + +namespace FakeXrmEasy.FakeMessageExecutors +{ + /// + /// Implements the DownloadBlockRequest message https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.downloadblockrequest?view=dataverse-sdk-latest + /// + public class DownloadBlockRequestExecutor: IFakeMessageExecutor + { + /// + /// Returns true if the request is a DownloadBlockRequest + /// + /// + /// + /// + public bool CanExecute(OrganizationRequest request) + { + return request is DownloadBlockRequest; + } + + /// + /// Executes a fake implementation of DownloadBlockRequest + /// + /// + /// + /// + /// + public OrganizationResponse Execute(OrganizationRequest request, IXrmFakedContext ctx) + { + var req = request as DownloadBlockRequest; + + var fileDb = (ctx as XrmFakedContext).FileDb; + + var data = fileDb.DownloadFileBlock(new DownloadBlockProperties() + { + FileDownloadSessionId = req.FileContinuationToken, + Offset = req.Offset, + BlockLength = req.BlockLength + }); + + return new DownloadBlockResponse() + { + Results = new ParameterCollection() + { + { "Data", data } + } + }; + } + + /// + /// Returns the DownloadBlockRequest type + /// + /// + public Type GetResponsibleRequestType() + { + return typeof(DownloadBlockRequest); + } + } +} +#endif \ No newline at end of file diff --git a/src/FakeXrmEasy.Messages/FakeMessageExecutors/InitializeFileBlocksDownloadRequestExecutor.cs b/src/FakeXrmEasy.Messages/FakeMessageExecutors/InitializeFileBlocksDownloadRequestExecutor.cs new file mode 100644 index 0000000..13a6316 --- /dev/null +++ b/src/FakeXrmEasy.Messages/FakeMessageExecutors/InitializeFileBlocksDownloadRequestExecutor.cs @@ -0,0 +1,71 @@ +#if FAKE_XRM_EASY_9 +using System; +using FakeXrmEasy.Abstractions; +using FakeXrmEasy.Abstractions.FakeMessageExecutors; +using FakeXrmEasy.Core.FileStorage.Download; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; + +namespace FakeXrmEasy.FakeMessageExecutors +{ + /// + /// Implements the InitializeFileBlocksDownloadRequest message https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.initializefileblocksdownloadrequest?view=dataverse-sdk-latest + /// + public class InitializeFileBlocksDownloadRequestExecutor: IFakeMessageExecutor + { + /// + /// Returns true if InitializeFileBlocksDownloadRequest can be executed + /// + /// + /// + /// + public bool CanExecute(OrganizationRequest request) + { + return request is InitializeFileBlocksDownloadRequest; + } + + /// + /// Executes a fake implementation of InitializeFileBlocksDownloadRequest + /// + /// + /// + /// + /// + public OrganizationResponse Execute(OrganizationRequest request, IXrmFakedContext ctx) + { + var req = request as InitializeFileBlocksDownloadRequest; + + var fileDb = (ctx as XrmFakedContext).FileDb; + + var fileContinuationToken = fileDb.InitFileDownloadSession(new FileDownloadProperties() + { + Target = req.Target, + FileAttributeName = req.FileAttributeName + }); + + var fileDownloadSession = fileDb.GetFileDownloadSession(fileContinuationToken); + + return new InitializeFileBlocksDownloadResponse() + { + Results = new ParameterCollection() + { + { "FileContinuationToken", fileContinuationToken }, + { "FileSizeInBytes", (long) fileDownloadSession.File.Content.Length }, + { "FileName", fileDownloadSession.File.FileName }, + { "IsChunkingSupported", true }, + } + }; + } + + /// + /// Returns the type of InitializeFileBlocksDownloadRequest + /// + /// + /// + public Type GetResponsibleRequestType() + { + return typeof(InitializeFileBlocksDownloadRequest); + } + } +} +#endif \ No newline at end of file diff --git a/src/FakeXrmEasy.Messages/FakeMessageExecutors/InitializeFileBlocksUploadRequestExecutor.cs b/src/FakeXrmEasy.Messages/FakeMessageExecutors/InitializeFileBlocksUploadRequestExecutor.cs new file mode 100644 index 0000000..ef54f63 --- /dev/null +++ b/src/FakeXrmEasy.Messages/FakeMessageExecutors/InitializeFileBlocksUploadRequestExecutor.cs @@ -0,0 +1,64 @@ +#if FAKE_XRM_EASY_9 +using System; +using FakeXrmEasy.Abstractions; +using FakeXrmEasy.Abstractions.FakeMessageExecutors; +using FakeXrmEasy.Core.FileStorage.Upload; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; + +namespace FakeXrmEasy.FakeMessageExecutors +{ + /// + /// Implements the InitializeFileBlocksUploadRequest message: https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.initializefileblocksuploadrequest?view=dataverse-sdk-latest + /// + public class InitializeFileBlocksUploadRequestExecutor: IFakeMessageExecutor + { + /// + /// Returns true when asked if it can execute an InitializeFileBlocksUploadRequest message + /// + /// + /// + public bool CanExecute(OrganizationRequest request) + { + return request is InitializeFileBlocksUploadRequest; + } + + /// + /// Executes a fake implementation of the request using an In-Memory File Storage mechanism + /// + /// The request to execute + /// The context to store the files against + /// InitializeFileBlocksUploadResponse + public OrganizationResponse Execute(OrganizationRequest request, IXrmFakedContext ctx) + { + InitializeFileBlocksUploadRequest req = (InitializeFileBlocksUploadRequest)request; + + var fileDb = (ctx as XrmFakedContext).FileDb; + + var fileUploadSession = fileDb.InitFileUploadSession(new FileUploadProperties() + { + Target = req.Target, + FileName = req.FileName, + FileAttributeName = req.FileAttributeName + }); + + return new InitializeFileBlocksUploadResponse() + { + Results = new ParameterCollection() + { + { "FileContinuationToken" , fileUploadSession } + } + }; + } + + /// + /// Returns the type of InitializeFileBlocksUploadResponse + /// + /// + public Type GetResponsibleRequestType() + { + return typeof(InitializeFileBlocksUploadRequest); + } + } +} +#endif \ No newline at end of file diff --git a/src/FakeXrmEasy.Messages/FakeMessageExecutors/UploadBlockRequestExecutor.cs b/src/FakeXrmEasy.Messages/FakeMessageExecutors/UploadBlockRequestExecutor.cs new file mode 100644 index 0000000..eb13c71 --- /dev/null +++ b/src/FakeXrmEasy.Messages/FakeMessageExecutors/UploadBlockRequestExecutor.cs @@ -0,0 +1,61 @@ +#if FAKE_XRM_EASY_9 +using System; +using FakeXrmEasy.Abstractions; +using FakeXrmEasy.Abstractions.FakeMessageExecutors; +using FakeXrmEasy.Core.FileStorage.Upload; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; + +namespace FakeXrmEasy.FakeMessageExecutors +{ + /// + /// Implements the UploadBlockRequest message https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.uploadblockrequest?view=dataverse-sdk-latest + /// + public class UploadBlockRequestExecutor: IFakeMessageExecutor + { + /// + /// Returns true if the request to execute is an UploadBlockRequest + /// + /// + /// + /// + public bool CanExecute(OrganizationRequest request) + { + return request is UploadBlockRequest; + } + + /// + /// Executes the current request + /// + /// + /// + /// + /// + public OrganizationResponse Execute(OrganizationRequest request, IXrmFakedContext ctx) + { + var uploadBlockRequest = request as UploadBlockRequest; + var fileDb = (ctx as XrmFakedContext).FileDb; + + var fileUploadSession = fileDb.GetFileUploadSession(uploadBlockRequest.FileContinuationToken); + fileUploadSession.AddFileBlock(new UploadBlockProperties() + { + FileContinuationToken = uploadBlockRequest.FileContinuationToken, + BlockContents = uploadBlockRequest.BlockData, + BlockId = uploadBlockRequest.BlockId + }); + + return new UploadBlockResponse(); + } + + /// + /// Returns the type of UploadBlockRequest + /// + /// + /// + public Type GetResponsibleRequestType() + { + return typeof(UploadBlockRequest); + } + } +} +#endif \ No newline at end of file diff --git a/src/FakeXrmEasy.Messages/FakeXrmEasy.Messages.csproj b/src/FakeXrmEasy.Messages/FakeXrmEasy.Messages.csproj index 61380b9..16e625c 100644 --- a/src/FakeXrmEasy.Messages/FakeXrmEasy.Messages.csproj +++ b/src/FakeXrmEasy.Messages/FakeXrmEasy.Messages.csproj @@ -8,7 +8,7 @@ net452 net452 FakeXrmEasy.Messages - 2.5.0 + 2.6.0 Jordi Montaña Dynamics Value FakeXrmEasy Messages @@ -102,28 +102,28 @@ - - + + - - + + - - + + - - + + - - + + - - + + diff --git a/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/AddMemberListRequestTests/AddMemberListRequestTests.cs b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/AddMemberListRequestTests/AddMemberListRequestTests.cs index d4e69e2..4e21284 100644 --- a/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/AddMemberListRequestTests/AddMemberListRequestTests.cs +++ b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/AddMemberListRequestTests/AddMemberListRequestTests.cs @@ -41,8 +41,6 @@ public void When_a_member_is_added_to_a_non_existing_list_exception_is_thrown() [Fact] public void When_a_request_is_called_with_an_empty_listid_parameter_exception_is_thrown() { - - AddListMembersListRequest addListMembersListRequest = new AddListMembersListRequest { MemberIds = new[] @@ -59,8 +57,6 @@ public void When_a_request_is_called_with_an_empty_listid_parameter_exception_is [Fact] public void When_a_request_is_called_with_an_empty_memberid_parameter_exception_is_thrown() { - - AddListMembersListRequest addListMembersListRequest = new AddListMembersListRequest { MemberIds = new[] diff --git a/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/CommitFileBlocksUploadRequestTests/CommitFileBlocksUploadRequestTests.cs b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/CommitFileBlocksUploadRequestTests/CommitFileBlocksUploadRequestTests.cs new file mode 100644 index 0000000..a1d0730 --- /dev/null +++ b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/CommitFileBlocksUploadRequestTests/CommitFileBlocksUploadRequestTests.cs @@ -0,0 +1,73 @@ +#if FAKE_XRM_EASY_9 +using System; +using FakeXrmEasy.Core.FileStorage.Db; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; +using Xunit; + +namespace FakeXrmEasy.Messages.Tests.FakeMessageExecutors.CommitFileBlocksUploadRequestTests +{ + public class CommitFileBlocksUploadRequestTests: FakeXrmEasyTestsBase + { + private readonly InMemoryFileDb _fileDb; + private readonly Entity _entity; + + private readonly InitializeFileBlocksUploadRequest _initFileUploadRequest; + private readonly CommitFileBlocksUploadRequest _commitFileBlocksUploadRequest; + private readonly UploadBlockRequest _uploadBlockRequest; + + public CommitFileBlocksUploadRequestTests() + { + _fileDb = (_context as XrmFakedContext).FileDb; + + _entity = new Entity("dv_test") { Id = Guid.NewGuid() }; + + _initFileUploadRequest = new InitializeFileBlocksUploadRequest() + { + FileName = "Test.pdf", + Target = _entity.ToEntityReference(), + FileAttributeName = "dv_file" + }; + + _uploadBlockRequest = new UploadBlockRequest() + { + BlockData = new byte[] { 1, 2, 3, 4 }, + BlockId = new Guid().ToString(), + }; + + _commitFileBlocksUploadRequest = new CommitFileBlocksUploadRequest() + { + FileName = "Test.pdf", + BlockList = new[] { _uploadBlockRequest.BlockId }, + MimeType = "application/pdf" + }; + } + + [Fact] + public void Should_commit_file_block_upload() + { + _context.Initialize(_entity); + + var initFileUploadResponse = _service.Execute(_initFileUploadRequest) as InitializeFileBlocksUploadResponse; + + _uploadBlockRequest.FileContinuationToken = initFileUploadResponse.FileContinuationToken; + _service.Execute(_uploadBlockRequest); + + _commitFileBlocksUploadRequest.FileContinuationToken = initFileUploadResponse.FileContinuationToken; + var response = _service.Execute(_commitFileBlocksUploadRequest); + Assert.NotNull(response); + Assert.IsType(response); + + var commitResponse = response as CommitFileBlocksUploadResponse; + Assert.Equal(4L, commitResponse.FileSizeInBytes); + Assert.NotEqual(Guid.Empty, commitResponse.FileId); + + var file = _fileDb.GetFileById(commitResponse.FileId.ToString()); + Assert.NotNull(file); + + Assert.Equal(_commitFileBlocksUploadRequest.MimeType, file.MimeType); + Assert.Equal(_uploadBlockRequest.BlockData, file.Content); + } + } +} +#endif \ No newline at end of file diff --git a/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/DeleteFileRequestTests/DeleteFileRequestTests.cs b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/DeleteFileRequestTests/DeleteFileRequestTests.cs new file mode 100644 index 0000000..65c1f6c --- /dev/null +++ b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/DeleteFileRequestTests/DeleteFileRequestTests.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using FakeXrmEasy.Core.FileStorage.Db; +using FakeXrmEasy.Extensions; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; +using Microsoft.Xrm.Sdk.Metadata; +using Xunit; + +#if FAKE_XRM_EASY_9 +namespace FakeXrmEasy.Messages.Tests.FakeMessageExecutors.DeleteFileRequestTests +{ + public class DeleteFileRequestTests: FakeXrmEasyTestsBase + { + private readonly InMemoryFileDb _fileDb; + private readonly Entity _entity; + private readonly DeleteFileRequest _request; + + private readonly FileAttachment _file; + private readonly EntityMetadata _entityMetadata; + + private const string FILE_ENTITY_NAME = "dv_test"; + private const string FILE_ATTRIBUTE_NAME = "dv_file"; + + public DeleteFileRequestTests() + { + _fileDb = (_context as XrmFakedContext).FileDb; + + _entity = new Entity(FILE_ENTITY_NAME) { Id = Guid.NewGuid() }; + + _file = new FileAttachment() + { + Id = Guid.NewGuid().ToString(), + MimeType = "application/pdf", + FileName = "TestFile.pdf", + Target = _entity.ToEntityReference(), + AttributeName = FILE_ATTRIBUTE_NAME, + Content = new byte[] { 1, 2, 3, 4 } + }; + + _entity[FILE_ATTRIBUTE_NAME] = _file.Id; + + var fileAttributeMetadata = new FileAttributeMetadata() + { + LogicalName = FILE_ATTRIBUTE_NAME + }; + fileAttributeMetadata.MaxSizeInKB = 1; //1 KB + + _entityMetadata = new EntityMetadata() + { + LogicalName = FILE_ENTITY_NAME + }; + _entityMetadata.SetAttributeCollection(new List() + { + fileAttributeMetadata + }); + + _request = new DeleteFileRequest() + { + FileId = new Guid(_file.Id) + }; + } + + [Fact] + public void Should_delete_existing_file() + { + _context.InitializeMetadata(_entityMetadata); + _context.Initialize(_entity); + _context.InitializeFiles(new [] { _file }); + + var response = _service.Execute(_request); + + Assert.NotNull(response); + Assert.IsType(response); + + var deletedFile = _fileDb.CreateQuery() + .FirstOrDefault(f => _request.FileId != new Guid(f.Id)); + + Assert.Null(deletedFile); + } + } +} +#endif \ No newline at end of file diff --git a/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/DownloadBlockRequestTests/DownloadBlockRequestTests.cs b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/DownloadBlockRequestTests/DownloadBlockRequestTests.cs new file mode 100644 index 0000000..f6abd7e --- /dev/null +++ b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/DownloadBlockRequestTests/DownloadBlockRequestTests.cs @@ -0,0 +1,90 @@ +#if FAKE_XRM_EASY_9 +using System; +using System.Collections.Generic; +using FakeXrmEasy.Core.FileStorage.Db; +using FakeXrmEasy.Extensions; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; +using Microsoft.Xrm.Sdk.Metadata; +using Xunit; + +namespace FakeXrmEasy.Messages.Tests.FakeMessageExecutors.DownloadBlockRequestTests +{ + public class DownloadBlockRequestTests: FakeXrmEasyTestsBase + { + private readonly InMemoryFileDb _fileDb; + private readonly Entity _entity; + private readonly DownloadBlockRequest _request; + + private readonly FileAttachment _file; + private readonly EntityMetadata _entityMetadata; + + private const string FILE_ENTITY_NAME = "dv_test"; + private const string FILE_ATTRIBUTE_NAME = "dv_file"; + + public DownloadBlockRequestTests() + { + _fileDb = (_context as XrmFakedContext).FileDb; + + _entity = new Entity(FILE_ENTITY_NAME) { Id = Guid.NewGuid() }; + + _file = new FileAttachment() + { + Id = Guid.NewGuid().ToString(), + MimeType = "application/pdf", + FileName = "TestFile.pdf", + Target = _entity.ToEntityReference(), + AttributeName = FILE_ATTRIBUTE_NAME, + Content = new byte[] { 1, 2, 3, 4 } + }; + + _request = new DownloadBlockRequest() + { + Offset = 0, + BlockLength = _file.Content.Length + }; + _entity[FILE_ATTRIBUTE_NAME] = _file.Id; + + var fileAttributeMetadata = new FileAttributeMetadata() + { + LogicalName = FILE_ATTRIBUTE_NAME + }; + fileAttributeMetadata.MaxSizeInKB = 1; //1 KB + + _entityMetadata = new EntityMetadata() + { + LogicalName = FILE_ENTITY_NAME + }; + _entityMetadata.SetAttributeCollection(new List() + { + fileAttributeMetadata + }); + } + + [Fact] + public void Should_download_file_block_when_executing_a_download_block_request() + { + _context.InitializeMetadata(_entityMetadata); + _context.Initialize(_entity); + _context.InitializeFiles(new [] { _file }); + + var initFileDownloadRequest = new InitializeFileBlocksDownloadRequest() + { + Target = _entity.ToEntityReference(), + FileAttributeName = FILE_ATTRIBUTE_NAME + }; + + var initFileDownloadResponse = _service.Execute(initFileDownloadRequest) as InitializeFileBlocksDownloadResponse; + _request.FileContinuationToken = initFileDownloadResponse.FileContinuationToken; + + var response = _service.Execute(_request); + + Assert.NotNull(response); + Assert.IsType(response); + + var downloadBlockResponse = response as DownloadBlockResponse; + Assert.Equal(_file.Content, downloadBlockResponse.Data); + } + } +} +#endif diff --git a/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/InitializeFileBlocksDownloadRequestTests/InitializeFileBlocksDownloadRequestTests.cs b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/InitializeFileBlocksDownloadRequestTests/InitializeFileBlocksDownloadRequestTests.cs new file mode 100644 index 0000000..d43a72c --- /dev/null +++ b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/InitializeFileBlocksDownloadRequestTests/InitializeFileBlocksDownloadRequestTests.cs @@ -0,0 +1,88 @@ +#if FAKE_XRM_EASY_9 +using System; +using System.Collections.Generic; +using FakeXrmEasy.Core.FileStorage.Db; +using FakeXrmEasy.Extensions; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; +using Microsoft.Xrm.Sdk.Metadata; +using Xunit; + +namespace FakeXrmEasy.Messages.Tests.FakeMessageExecutors.InitializeFileBlocksDownloadRequestTests +{ + public class InitializeFileBlocksDownloadRequestTests: FakeXrmEasyTestsBase + { + private readonly InMemoryFileDb _fileDb; + private readonly Entity _entity; + private readonly InitializeFileBlocksDownloadRequest _request; + + private readonly FileAttachment _file; + private readonly EntityMetadata _entityMetadata; + + private const string FILE_ENTITY_NAME = "dv_test"; + private const string FILE_ATTRIBUTE_NAME = "dv_file"; + + public InitializeFileBlocksDownloadRequestTests() + { + _fileDb = (_context as XrmFakedContext).FileDb; + + _entity = new Entity(FILE_ENTITY_NAME) { Id = Guid.NewGuid() }; + + _request = new InitializeFileBlocksDownloadRequest() + { + Target = _entity.ToEntityReference(), + FileAttributeName = FILE_ATTRIBUTE_NAME + }; + + _file = new FileAttachment() + { + Id = Guid.NewGuid().ToString(), + MimeType = "application/pdf", + FileName = "TestFile.pdf", + Target = _entity.ToEntityReference(), + AttributeName = FILE_ATTRIBUTE_NAME, + Content = new byte[] { 1, 2, 3, 4 } + }; + + _entity[FILE_ATTRIBUTE_NAME] = _file.Id; + + var fileAttributeMetadata = new FileAttributeMetadata() + { + LogicalName = FILE_ATTRIBUTE_NAME + }; + fileAttributeMetadata.MaxSizeInKB = 1; //1 KB + + _entityMetadata = new EntityMetadata() + { + LogicalName = FILE_ENTITY_NAME + }; + _entityMetadata.SetAttributeCollection(new List() + { + fileAttributeMetadata + }); + } + + [Fact] + public void Should_initiate_file_download_request_with_correct_parameters() + { + _context.InitializeMetadata(_entityMetadata); + _context.Initialize(_entity); + _context.InitializeFiles(new [] { _file }); + + var response = _service.Execute(_request); + + Assert.NotNull(response); + Assert.IsType(response); + + var downloadResponse = response as InitializeFileBlocksDownloadResponse; + Assert.Equal(_file.FileName, downloadResponse.FileName); + Assert.Equal((long) _file.Content.Length, downloadResponse.FileSizeInBytes); + Assert.True(downloadResponse.IsChunkingSupported); + Assert.NotNull(downloadResponse.FileContinuationToken); + + var fileDownloadSession = _fileDb.GetFileDownloadSession(downloadResponse.FileContinuationToken); + Assert.NotNull(fileDownloadSession); + } + } +} +#endif \ No newline at end of file diff --git a/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/InitializeFileBlocksUploadRequestTests/InitializeFileBlocksUploadRequestTests.cs b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/InitializeFileBlocksUploadRequestTests/InitializeFileBlocksUploadRequestTests.cs new file mode 100644 index 0000000..ab56528 --- /dev/null +++ b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/InitializeFileBlocksUploadRequestTests/InitializeFileBlocksUploadRequestTests.cs @@ -0,0 +1,53 @@ +#if FAKE_XRM_EASY_9 +using System; +using FakeItEasy; +using FakeXrmEasy.Core.Db; +using FakeXrmEasy.Core.FileStorage.Db; +using FakeXrmEasy.Core.FileStorage.Upload; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; +using Xunit; + +namespace FakeXrmEasy.Messages.Tests.FakeMessageExecutors.InitializeFileBlocksUploadRequestTests +{ + public class InitializeFileBlocksUploadRequestTests: FakeXrmEasyTestsBase + { + private readonly InMemoryFileDb _fileDb; + private readonly Entity _entity; + private readonly InitializeFileBlocksUploadRequest _request; + + public InitializeFileBlocksUploadRequestTests(): base() + { + _fileDb = (_context as XrmFakedContext).FileDb; + + _entity = new Entity("dv_test") { Id = Guid.NewGuid() }; + + _request = new InitializeFileBlocksUploadRequest() + { + FileName = "Test.pdf", + Target = _entity.ToEntityReference(), + FileAttributeName = "dv_file" + }; + } + + [Fact] + public void Should_call_file_upload_session_with_correct_parameters() + { + _context.Initialize(_entity); + + var response = _service.Execute(_request) as InitializeFileBlocksUploadResponse; + Assert.NotNull(response); + Assert.NotNull(response.FileContinuationToken); + + var fileSession = _fileDb.GetFileUploadSession(response.FileContinuationToken); + Assert.NotNull(fileSession); + + Assert.Equal(_request.FileName, fileSession.Properties.FileName); + Assert.Equal(_request.Target.LogicalName, fileSession.Properties.Target.LogicalName); + Assert.Equal(_request.Target.Id, fileSession.Properties.Target.Id); + Assert.Equal(_request.FileAttributeName, fileSession.Properties.FileAttributeName); + + } + } +} +#endif \ No newline at end of file diff --git a/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/UploadBlockRequestTests/UploadBlockRequestTests.cs b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/UploadBlockRequestTests/UploadBlockRequestTests.cs new file mode 100644 index 0000000..3fd5e10 --- /dev/null +++ b/tests/FakeXrmEasy.Messages.Tests/FakeMessageExecutors/UploadBlockRequestTests/UploadBlockRequestTests.cs @@ -0,0 +1,61 @@ +#if FAKE_XRM_EASY_9 +using System; +using System.Linq; +using FakeXrmEasy.Core.FileStorage.Db; +using Microsoft.Crm.Sdk.Messages; +using Microsoft.Xrm.Sdk; +using Xunit; + +namespace FakeXrmEasy.Messages.Tests.FakeMessageExecutors.UploadBlockRequestTests +{ + public class UploadBlockRequestTests: FakeXrmEasyTestsBase + { + private readonly InMemoryFileDb _fileDb; + private readonly Entity _entity; + private readonly InitializeFileBlocksUploadRequest _request; + + public UploadBlockRequestTests() + { + _fileDb = (_context as XrmFakedContext).FileDb; + + _entity = new Entity("dv_test") { Id = Guid.NewGuid() }; + + _request = new InitializeFileBlocksUploadRequest() + { + FileName = "Test.pdf", + Target = _entity.ToEntityReference(), + FileAttributeName = "dv_file" + }; + } + + [Fact] + public void Should_call_upload_blob_request_with_correct_parameters() + { + _context.Initialize(_entity); + + var initFileUploadResponse = _service.Execute(_request) as InitializeFileBlocksUploadResponse; + + var request = new UploadBlockRequest() + { + FileContinuationToken = initFileUploadResponse.FileContinuationToken, + BlockData = new byte[] { 1, 2, 3, 4 }, + BlockId = new Guid().ToString(), + }; + + var response = _service.Execute(request); + Assert.NotNull(response); + Assert.IsType(response); + + //Check the blob was added + var fileUploadSession = _fileDb.GetFileUploadSession(initFileUploadResponse.FileContinuationToken); + var fileBlocks = fileUploadSession.GetAllBlocks(); + + Assert.Single(fileBlocks); + + var uploadedFileBlock = fileBlocks.FirstOrDefault(); + Assert.Equal(request.BlockData, uploadedFileBlock.Content); + Assert.Equal(request.BlockId, uploadedFileBlock.BlockId); + } + } +} +#endif \ No newline at end of file diff --git a/tests/FakeXrmEasy.Messages.Tests/FakeXrmEasy.Messages.Tests.csproj b/tests/FakeXrmEasy.Messages.Tests/FakeXrmEasy.Messages.Tests.csproj index 150d92a..db30c6b 100644 --- a/tests/FakeXrmEasy.Messages.Tests/FakeXrmEasy.Messages.Tests.csproj +++ b/tests/FakeXrmEasy.Messages.Tests/FakeXrmEasy.Messages.Tests.csproj @@ -11,7 +11,7 @@ true FakeXrmEasy.MessagesTests - 2.5.0 + 2.6.0 Jordi Montaña Dynamics Value S.L. Internal Unit test suite for FakeXrmEasy.Messages package @@ -109,28 +109,28 @@ - - + + - - + + - - + + - - + + - - + + - - + + @@ -138,22 +138,22 @@ - + - + - + - + - + - +