Summary

Class:FakeXrmEasy.FakeMessageExecutors.WhoAmIRequestExecutor
Assembly:FakeXrmEasy
File(s):F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\WhoAmIRequestExecutor.cs
Covered lines:43
Uncovered lines:0
Coverable lines:43
Total lines:69
Line coverage:100%
Branch coverage:100%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
CanExecute(...)1100100
Execute(...)2100100
GetResponsibleRequestType()1100100
GetBusinessUnitId(...)3100100
GetOrganizationId(...)6100100

File(s)

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

#LineLine coverage
 1using Microsoft.Crm.Sdk.Messages;
 2using Microsoft.Xrm.Sdk;
 3using System;
 4using System.Linq;
 5
 6namespace FakeXrmEasy.FakeMessageExecutors
 7{
 8    public class WhoAmIRequestExecutor : IFakeMessageExecutor
 9    {
 10        public bool CanExecute(OrganizationRequest request)
 3011        {
 3012            return request is WhoAmIRequest;
 3013        }
 14
 15        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 3016        {
 3017            var req = request as WhoAmIRequest;
 18
 3019            var callerId = ctx.CallerId.Id;
 20
 3021            var results = new ParameterCollection {
 3022              { "UserId", callerId }
 3023            };
 24
 3025            var user = ctx.CreateQuery("systemuser")
 3026                          .Where(u => u.Id == callerId)
 3027                          .SingleOrDefault();
 28
 5429             if(user != null) {
 2430              var buId = GetBusinessUnitId(user);
 2431              results.Add("BusinessUnitId", buId);
 32
 2433              var orgId = GetOrganizationId(ctx, user, buId);
 2434              results.Add("OrganizationId", orgId);
 2435            }
 36
 3037            var response = new WhoAmIResponse
 3038            {
 3039                Results = results
 3040            };
 3041            return response;
 3042        }
 43
 44        public Type GetResponsibleRequestType()
 426445        {
 426446            return typeof(WhoAmIRequest);
 426447        }
 48
 2449        private static Guid GetBusinessUnitId(Entity user) {
 2450          var buRef = user.GetAttributeValue<EntityReference>("businessunitid");
 2451           var buId = buRef != null ? buRef.Id : Guid.Empty;
 2452          return buId;
 2453        }
 54
 2455        private static Guid GetOrganizationId(XrmFakedContext ctx, Entity user, Guid buId) {
 2456           var orgId = user.GetAttributeValue<Guid?>("organizationid") ?? Guid.Empty;
 3657           if(orgId == Guid.Empty) {
 1258            var bu = ctx.CreateQuery("businessunit")
 1259                        .Where(b => b.Id == buId)
 1260                        .SingleOrDefault();
 1261            var orgRef = bu.GetAttributeValue<EntityReference>("organizationid");
 1262             orgId = orgRef?.Id ?? Guid.Empty;
 1263          }
 64
 2465          return orgId;
 2466        }
 67
 68    }
 69}