Skip to content

Commit

Permalink
Merge pull request #37 from DynamicsValue/feature/2-6-improvements
Browse files Browse the repository at this point in the history
Adding fake message executors for DynamicsValue/fake-xrm-easy#157
  • Loading branch information
jordimontana82 authored Aug 24, 2024
2 parents 8a1708c + 3adc0b8 commit ec1649f
Show file tree
Hide file tree
Showing 16 changed files with 872 additions and 36 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Implements the CommitFileBlocksUploadRequest https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.commitfileblocksuploadrequest?view=dataverse-sdk-latest
/// </summary>
public class CommitFileBlocksUploadRequestExecutor: IFakeMessageExecutor
{
/// <summary>
/// Returns true when the request is a CommitFileBlocksUploadRequest
/// </summary>
/// <param name="request">The request to check if it can be executed by this executor</param>
/// <returns></returns>
public bool CanExecute(OrganizationRequest request)
{
return request is CommitFileBlocksUploadRequest;
}

/// <summary>
/// Executes a CommitFileBlocksUploadRequest
/// </summary>
/// <param name="request">The request to execute</param>
/// <param name="ctx">The IXrmFakedContext that this request will be executed against</param>
/// <returns>CommitFileBlocksUploadResponse</returns>
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 }
}
};
}

/// <summary>
/// The type of CommitFileBlocksUploadRequest
/// </summary>
/// <returns></returns>
public Type GetResponsibleRequestType()
{
return typeof(CommitFileBlocksUploadRequest);
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Implements the DeleteFileRequest message https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.deletefilerequest?view=dataverse-sdk-latest
/// </summary>
public class DeleteFileRequestExecutor: IFakeMessageExecutor
{
/// <summary>
/// Returns true if the request is a DeleteFileRequest message
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool CanExecute(OrganizationRequest request)
{
return request is DeleteFileRequest;
}

/// <summary>
/// Implements a fake DeleteFileRequest message
/// </summary>
/// <param name="request"></param>
/// <param name="ctx"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
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();
}

/// <summary>
/// Returns the type of DeleteFileRequest
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public Type GetResponsibleRequestType()
{
return typeof(DeleteFileRequest);
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Implements the DownloadBlockRequest message https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.downloadblockrequest?view=dataverse-sdk-latest
/// </summary>
public class DownloadBlockRequestExecutor: IFakeMessageExecutor
{
/// <summary>
/// Returns true if the request is a DownloadBlockRequest
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool CanExecute(OrganizationRequest request)
{
return request is DownloadBlockRequest;
}

/// <summary>
/// Executes a fake implementation of DownloadBlockRequest
/// </summary>
/// <param name="request"></param>
/// <param name="ctx"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
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 }
}
};
}

/// <summary>
/// Returns the DownloadBlockRequest type
/// </summary>
/// <returns></returns>
public Type GetResponsibleRequestType()
{
return typeof(DownloadBlockRequest);
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Implements the InitializeFileBlocksDownloadRequest message https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.initializefileblocksdownloadrequest?view=dataverse-sdk-latest
/// </summary>
public class InitializeFileBlocksDownloadRequestExecutor: IFakeMessageExecutor
{
/// <summary>
/// Returns true if InitializeFileBlocksDownloadRequest can be executed
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool CanExecute(OrganizationRequest request)
{
return request is InitializeFileBlocksDownloadRequest;
}

/// <summary>
/// Executes a fake implementation of InitializeFileBlocksDownloadRequest
/// </summary>
/// <param name="request"></param>
/// <param name="ctx"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
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 },
}
};
}

/// <summary>
/// Returns the type of InitializeFileBlocksDownloadRequest
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public Type GetResponsibleRequestType()
{
return typeof(InitializeFileBlocksDownloadRequest);
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Implements the InitializeFileBlocksUploadRequest message: https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.initializefileblocksuploadrequest?view=dataverse-sdk-latest
/// </summary>
public class InitializeFileBlocksUploadRequestExecutor: IFakeMessageExecutor
{
/// <summary>
/// Returns true when asked if it can execute an InitializeFileBlocksUploadRequest message
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public bool CanExecute(OrganizationRequest request)
{
return request is InitializeFileBlocksUploadRequest;
}

/// <summary>
/// Executes a fake implementation of the request using an In-Memory File Storage mechanism
/// </summary>
/// <param name="request">The request to execute</param>
/// <param name="ctx">The context to store the files against</param>
/// <returns>InitializeFileBlocksUploadResponse</returns>
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 }
}
};
}

/// <summary>
/// Returns the type of InitializeFileBlocksUploadResponse
/// </summary>
/// <returns></returns>
public Type GetResponsibleRequestType()
{
return typeof(InitializeFileBlocksUploadRequest);
}
}
}
#endif
Loading

0 comments on commit ec1649f

Please sign in to comment.