FakeXrmEasy Late-bound custom actions error for version 2.3.3

45 Views Asked by At

I'm trying to test the behavior of FakeXrmEasy and want to call a custom action in a CodeActivity. For this I use FakeXrmEasy version 2.3.3.

However, something seems to be missing in my code as my custom executor seems to be unavailable and produces the following error when calling Execute for the custom request.

FakeXrmEasy.Abstractions.Exceptions.NonCommercialUnsupportedException HResult=0x80131500 Message=Exception occurred: The organization request type 'Microsoft.Xrm.Sdk.OrganizationRequest' is not yet supported...

This is my code:

using FakeXrmEasy.Abstractions;
using FakeXrmEasy.Abstractions.Enums;
using FakeXrmEasy.Abstractions.FakeMessageExecutors;
using FakeXrmEasy.Middleware;
using FakeXrmEasy.Middleware.Crud;
using FakeXrmEasy.Middleware.Messages;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Xunit;

namespace TestFakeXrmEasyCustomActions
{
    public class FakeXrmEasyCustomActionTestsBase
    {
        protected readonly IXrmFakedContext _context;
        protected readonly IOrganizationService _service;

        public FakeXrmEasyCustomActionTestsBase()
        {
            _context = MiddlewareBuilder
                             .New()

                             .AddCrud()
                             .AddGenericFakeMessageExecutors(Assembly.GetAssembly(typeof(SalesOrderCustomActionExecutor)))
                             .UseCrud()
                             .UseMessages()

                             // Here we are saying we're using FakeXrmEasy (FXE) in compliance with the Modified PolyForm Non-Commercial license 1.0.0

                             // For more info please refer to the license at https://dynamicsvalue.github.io/fake-xrm-easy-docs/licensing/license/
                             // And the  _licensing FAQ at https://dynamicsvalue.github.io/fake-xrm-easy-docs/licensing/faq/
                             .SetLicense(FakeXrmEasyLicense.NonCommercial)
                             .Build();

            _service = _context.GetOrganizationService();
        }
    }

    public class ExportSalesOrderTestActionRequest : OrganizationRequest
    {
        public int EntityTypeCode
        {
            get { return (int)this["EntityTypeCode"]; }
            set { this["EntityTypeCode"] = value; }
        }

        public EntityReference SelectedOrder
        {
            get { return (EntityReference)this["SelectedOrder"]; }
            set { this["SelectedOrder"] = value; }
        }

        public string SalesOrderIdString
        {
            get { return (string)this["SalesOrderIdString"]; }
            set { this["SalesOrderIdString"] = value; }
        }
    }

    public class SalesOrderCustomActionExecutor : IFakeMessageExecutor
    {
        public static readonly string RequestName = "ExportSalesOrderTestActionRequest";
        public bool CanExecute(OrganizationRequest request)
        {
            return request.RequestName.Equals(RequestName);
        }

        public OrganizationResponse Execute(OrganizationRequest request, IXrmFakedContext ctx)
        {
            EntityReference selectedOrder = (EntityReference)request.Parameters["SelectedOrder"];
            if (selectedOrder == null) throw new Exception("SelectedOrder is a required parameter.");


            var response = new OrganizationResponse()
            {
                ResponseName = RequestName,
                Results = new ParameterCollection()
            };

            response.Results["SalesOrderIdString"] = selectedOrder.Id.ToString();
            return response;
        }

        public Type GetResponsibleRequestType()
        {
            return typeof(OrganizationRequest);
        }

        public string GetRequestName()
        {
            return RequestName;
        }

    }

    public class CustomActionUnitTests : FakeXrmEasyCustomActionTestsBase
    {

        [Fact]
        public void TestFakeXrmEasyCustomAction()
        {
            var saorder = new Entity("salesorder")
            {
                Id = Guid.NewGuid()
            };
            _service.Create(saorder);

            IQueryable<Entity> data = new List<Entity>().AsQueryable();

            data.Append(saorder);

            _context.Initialize(data);

            OrganizationRequest exportSalesOrderRequest = new OrganizationRequest("ExportSalesOrderTestActionRequest");

            exportSalesOrderRequest["EntityTypeCode"] = 1088;
            exportSalesOrderRequest["SelectedOrder"] = new EntityReference("salesorder", (Guid)saorder.Id);

            OrganizationResponse salesorderId = (OrganizationResponse)_service.Execute(exportSalesOrderRequest);

            Assert.NotEmpty((System.Collections.IEnumerable)salesorderId);

        }
    }
}
0

There are 0 best solutions below