Summary

Class:FakeXrmEasy.FakeMessageExecutors.AddMemberListRequestExecutor
Assembly:FakeXrmEasy
File(s):F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\AddMemberListRequestExecutor.cs
Covered lines:46
Uncovered lines:10
Coverable lines:56
Total lines:104
Line coverage:82.1%
Branch coverage:82.3%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
CanExecute(...)1100100
Execute(...)1278.2683.33
GetResponsibleRequestType()1100100

File(s)

F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\AddMemberListRequestExecutor.cs

#LineLine coverage
 1using Microsoft.Crm.Sdk.Messages;
 2using Microsoft.Xrm.Sdk;
 3using System;
 4using System.Linq;
 5using System.ServiceModel;
 6
 7namespace FakeXrmEasy.FakeMessageExecutors
 8{
 9    public class AddMemberListRequestExecutor : IFakeMessageExecutor
 10    {
 11        public enum ListCreatedFromCode
 12        {
 13            Account = 1,
 14            Contact = 2,
 15            Lead = 4
 16        }
 17
 18        public bool CanExecute(OrganizationRequest request)
 4819        {
 4820            return request is AddMemberListRequest;
 4821        }
 22
 23        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 4824        {
 4825            var req = (AddMemberListRequest)request;
 26
 4827             if ( req.ListId == Guid.Empty)
 628            {
 629        FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "ListId parameter is required");
 030            }
 31
 4232             if ( req.EntityId == Guid.Empty)
 633            {
 634        FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "EntityId parameter is required");
 035            }
 36
 3637            var service = ctx.GetOrganizationService();
 38
 39            //Find the list
 3640            var list = ctx.CreateQuery("list")
 3641                        .Where(e => e.Id == req.ListId)
 3642                        .FirstOrDefault();
 43
 3644             if (list == null)
 645            {
 646        FakeOrganizationServiceFault.Throw(ErrorCodes.IsvAborted, string.Format("List with Id {0} wasn't found", req.Lis
 047            }
 48
 49            //Find the member
 3050             if (!list.Attributes.ContainsKey("createdfromcode"))
 651            {
 652        FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a Created
 053            }
 54
 2455             if (list["createdfromcode"] != null && !(list["createdfromcode"] is OptionSetValue))
 056            {
 057        FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a Created
 058            }
 59
 2460            var createdFromCodeValue = (list["createdfromcode"] as OptionSetValue).Value;
 2461            string memberEntityName = "";
 2462             switch (createdFromCodeValue)
 63            {
 64                case (int)ListCreatedFromCode.Account:
 1265                    memberEntityName = "account";
 1266                    break;
 67
 68                case (int)ListCreatedFromCode.Contact:
 669                    memberEntityName = "contact";
 670                    break;
 71
 72                case (int)ListCreatedFromCode.Lead:
 673                    memberEntityName = "lead";
 674                    break;
 75
 76                default:
 077          FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a suppo
 078          break;
 79      }
 80
 2481            var member = ctx.CreateQuery(memberEntityName)
 2482                        .Where(e => e.Id == req.EntityId)
 2483                        .FirstOrDefault();
 84
 2485             if (member == null)
 686            {
 687        FakeOrganizationServiceFault.Throw(ErrorCodes.IsvAborted, string.Format("Member with Id {0} wasn't found", req.E
 088            }
 89
 90            //create member list
 1891            var listmember = new Entity("listmember");
 1892            listmember["listid"] = new EntityReference("list", req.ListId);
 1893            listmember["entityid"] = new EntityReference(memberEntityName, req.EntityId);
 1894            service.Create(listmember);
 95
 1896            return new AddMemberListResponse();
 1897        }
 98
 99        public Type GetResponsibleRequestType()
 4264100        {
 4264101            return typeof(AddMemberListRequest);
 4264102        }
 103    }
 104}