Summary

Class:FakeXrmEasy.FakeMessageExecutors.AddListMembersListRequestExecutor
Assembly:FakeXrmEasy
File(s):F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\AddListMembersListRequestExecutor.cs
Covered lines:47
Uncovered lines:12
Coverable lines:59
Total lines:107
Line coverage:79.6%
Branch coverage:76.4%

Metrics

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

File(s)

F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\AddListMembersListRequestExecutor.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 AddListMembersListRequestExecutor : IFakeMessageExecutor
 10    {
 11        public enum ListCreatedFromCode
 12        {
 13            Account = 1,
 14            Contact = 2,
 15            Lead = 4
 16        }
 17
 18        public bool CanExecute(OrganizationRequest request)
 5419        {
 5420            return request is AddListMembersListRequest;
 5421        }
 22
 23        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 5424        {
 5425            var req = (AddListMembersListRequest)request;
 26
 5427       if (req.MemberIds == null)
 028      {
 029        FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "Required field 'MemberIds' is missing");
 030      }
 31
 5432       if (req.ListId == Guid.Empty)
 633            {
 634        FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "Expected non-empty Guid.");
 035            }
 36
 4837            var service = ctx.GetOrganizationService();
 38
 39            //Find the list
 4840            var list = ctx.CreateQuery("list")
 4841                        .Where(e => e.Id == req.ListId)
 4842                        .FirstOrDefault();
 43
 4844             if (list == null)
 1245            {
 1246        FakeOrganizationServiceFault.Throw(ErrorCodes.IsvAborted, string.Format("List with Id {0} wasn't found", req.Lis
 047            }
 48
 49            //Find the member
 3650             if (!list.Attributes.ContainsKey("createdfromcode"))
 651            {
 652        FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a Created
 053            }
 54
 3055             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
 3060            var createdFromCodeValue = (list["createdfromcode"] as OptionSetValue).Value;
 3061            string memberEntityName = "";
 3062             switch (createdFromCodeValue)
 63            {
 64                case (int)ListCreatedFromCode.Account:
 1865                    memberEntityName = "account";
 1866                    break;
 67
 68                case (int)ListCreatedFromCode.Contact:
 669                    memberEntityName = "contact";
 670                    break;
 71
 72                case (int)ListCreatedFromCode.Lead:
 673                    memberEntityName = "lead";
 674                    break;
 75                default:
 076          FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a suppo
 077          break;
 78            }
 79
 15080            foreach (var memberId in req.MemberIds)
 3681            {
 3682                var member = ctx.CreateQuery(memberEntityName)
 3683            .Where(e => e.Id == memberId)
 3684            .FirstOrDefault();
 85
 3686                 if (member == null)
 1287                {
 1288          FakeOrganizationServiceFault.Throw(ErrorCodes.IsvAborted, string.Format("Member of type {0} with Id {1} wasn't
 089                }
 90
 91                //create member list
 2492                var listmember = new Entity("listmember");
 2493                listmember["listid"] = new EntityReference("list", req.ListId);
 2494                listmember["entityid"] = new EntityReference(memberEntityName, memberId);
 95
 2496                service.Create(listmember);
 2497            }
 98
 1899            return new AddListMembersListResponse();
 18100        }
 101
 102        public Type GetResponsibleRequestType()
 4264103        {
 4264104            return typeof(AddListMembersListRequest);
 4264105        }
 106    }
 107}