Summary

Class:FakeXrmEasy.FakeMessageExecutors.AddUserToRecordTeamRequestExecutor
Assembly:FakeXrmEasy
File(s):F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\AddUserToRecordTeamRequestExecutor.cs
Covered lines:51
Uncovered lines:10
Coverable lines:61
Total lines:98
Line coverage:83.6%
Branch coverage:50%

Metrics

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

File(s)

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

#LineLine coverage
 1#if FAKE_XRM_EASY_2013 || FAKE_XRM_EASY_2015 || FAKE_XRM_EASY_2016 || FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9
 2using System;
 3using System.Linq;
 4using Microsoft.Xrm.Sdk;
 5using System.ServiceModel;
 6using Microsoft.Crm.Sdk.Messages;
 7
 8namespace FakeXrmEasy.FakeMessageExecutors
 9{
 10    public class AddUserToRecordTeamRequestExecutor : IFakeMessageExecutor
 11    {
 12        public bool CanExecute(OrganizationRequest request)
 513        {
 514            return request is AddUserToRecordTeamRequest;
 515        }
 16
 17        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 518        {
 519            AddUserToRecordTeamRequest addReq = (AddUserToRecordTeamRequest)request;
 20
 521            EntityReference target = addReq.Record;
 522            Guid systemuserId = addReq.SystemUserId;
 523            Guid teamTemplateId = addReq.TeamTemplateId;
 24
 525             if (target == null)
 026            {
 027                throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not add to team 
 28            }
 29
 530             if (systemuserId == Guid.Empty)
 031            {
 032                throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not add to team 
 33            }
 34
 535             if (teamTemplateId == Guid.Empty)
 036            {
 037                throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not add to team 
 38            }
 39
 540            IOrganizationService service = ctx.GetOrganizationService();
 41
 542            Entity teamTemplate = ctx.CreateQuery("teamtemplate").FirstOrDefault(p => p.Id == teamTemplateId);
 543             if (teamTemplate == null)
 044            {
 045                throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Team template with i
 46            }
 47
 548            Entity user = ctx.CreateQuery("systemuser").FirstOrDefault(p => p.Id == systemuserId);
 549             if (user == null)
 050            {
 051                throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "User with id=" + tea
 52            }
 53
 54
 555            Entity team = ctx.CreateQuery("team").FirstOrDefault(p => ((EntityReference)p["teamtemplateid"]).Id == teamT
 556             if (team == null)
 557            {
 558                team = new Entity("team")
 559                {
 560                    ["teamtemplateid"] = new EntityReference("teamtemplate", teamTemplateId)
 561                };
 562                team.Id = service.Create(team);
 563            }
 64
 565            Entity tm = new Entity("teammembership")
 566            {
 567                ["systemuserid"] = systemuserId,
 568                ["teamid"] = team.Id
 569            };
 570            tm.Id = service.Create(tm);
 71
 572             Entity poa = new Entity("principalobjectaccess")
 573            {
 574                ["objectid"] = target.Id,
 575                ["principalid"] = team.Id,
 576                ["accessrightsmask"] = teamTemplate.Contains("defaultaccessrightsmask") ? teamTemplate["defaultaccessrig
 577            };
 578            poa.Id = service.Create(poa);
 79
 580            ctx.AccessRightsRepository.GrantAccessTo(target, new PrincipalAccess
 581            {
 582                Principal = user.ToEntityReference(),
 583                AccessMask = (AccessRights)poa["accessrightsmask"]
 584            });
 85
 586            return new AddUserToRecordTeamResponse
 587            {
 588                ResponseName = "AddUserToRecordTeam"
 589            };
 590        }
 91
 92        public Type GetResponsibleRequestType()
 359293        {
 359294            return typeof(AddUserToRecordTeamRequest);
 359295        }
 96    }
 97}
 98#endif