Summary

Class:FakeXrmEasy.FakeMessageExecutors.InitializeFromRequestExecutor
Assembly:FakeXrmEasy
File(s):C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\InitializeFromRequestExecutor.cs
Covered lines:57
Uncovered lines:2
Coverable lines:59
Total lines:105
Line coverage:96.6%
Branch coverage:81.2%

Metrics

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

File(s)

C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\InitializeFromRequestExecutor.cs

#LineLine coverage
 1using Microsoft.Crm.Sdk.Messages;
 2using Microsoft.Xrm.Sdk;
 3using Microsoft.Xrm.Sdk.Query;
 4using System;
 5using System.Linq;
 6using System.ServiceModel;
 7
 8namespace FakeXrmEasy.FakeMessageExecutors
 9{
 10    public class InitializeFromRequestExecutor : IFakeMessageExecutor
 11    {
 12        public bool CanExecute(OrganizationRequest request)
 4213        {
 4214            return request is InitializeFromRequest;
 4215        }
 16
 17        public Type GetResponsibleRequestType()
 427018        {
 427019            return typeof(InitializeFromRequest);
 427020        }
 21
 22        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 4223        {
 4224            var req = request as InitializeFromRequest;
 4225             if (req == null)
 026                throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Cannot execute Initi
 27
 28            //TODO: Implement logic to filter mapping attributes based on the req.TargetFieldType
 4229             if (req.TargetFieldType != TargetFieldType.All)
 030                throw PullRequestException.PartiallyNotImplementedOrganizationRequest(req.GetType(), "logic for filterin
 31
 4232            var service = ctx.GetOrganizationService();
 4233            var fetchXml = string.Format(FetchMappingsByEntity, req.EntityMoniker.LogicalName, req.TargetEntityName);
 4234            var mapping = service.RetrieveMultiple(new FetchExpression(fetchXml));
 6035            var sourceAttributes = mapping.Entities.Select(a => a.GetAttributeValue<AliasedValue>("attributemap.sourceat
 4236             var columnSet = sourceAttributes.Length == 0 ? new ColumnSet(true) : new ColumnSet(sourceAttributes);
 4237            var source = service.Retrieve(req.EntityMoniker.LogicalName, req.EntityMoniker.Id, columnSet);
 38
 39            // If we are using proxy types, and the appropriate proxy type is found in
 40            // the assembly create an instance of the appropiate class
 41            // Othersise return a simple Entity
 4242            Entity entity = new Entity
 4243            {
 4244                LogicalName = req.TargetEntityName,
 4245                Id = Guid.Empty
 4246            };
 47
 4248             if (ctx.ProxyTypesAssembly != null)
 3649            {
 3650                var subClassType = ctx.FindReflectedType(req.TargetEntityName);
 3651                 if (subClassType != null)
 3652                {
 3653                    var instance = Activator.CreateInstance(subClassType);
 3654                    entity = (Entity) instance;
 3655                }
 3656            }
 57
 4258             if (mapping.Entities.Count > 0)
 1859            {
 11460                foreach (var attr in source.Attributes)
 3061                {
 6062                    var mappingEntity = mapping.Entities.FirstOrDefault(e => e.GetAttributeValue<AliasedValue>("attribut
 4263                     if (mappingEntity == null) continue;
 1864                    var targetAttribute = mappingEntity.GetAttributeValue<AliasedValue>("attributemap.targetattributenam
 1865                    entity[targetAttribute] = attr.Value;
 66
 1867                    var isEntityReference = string.Equals(attr.Key, source.LogicalName + "id", StringComparison.CurrentC
 1868                     if (isEntityReference)
 669                    {
 670                        entity[targetAttribute] = new EntityReference(source.LogicalName, (Guid)attr.Value);
 671                    }
 72                    else
 1273                    {
 1274                        entity[targetAttribute] = attr.Value;
 1275                    }
 1876                }
 1877            }
 78
 4279            var response = new InitializeFromResponse
 4280            {
 4281                Results =
 4282                {
 4283                    ["Entity"] = entity
 4284                }
 4285            };
 86
 4287            return response;
 4288        }
 89
 90        private const string FetchMappingsByEntity = @"<fetch version='1.0' mapping='logical' distinct='false'>
 91                                                           <entity name='entitymap'>
 92                                                              <attribute name='sourceentityname'/>
 93                                                              <attribute name='targetentityname'/>
 94                                                              <link-entity name='attributemap' alias='attributemap' to='
 95                                                                 <attribute name='sourceattributename'/>
 96                                                                 <attribute name='targetattributename'/>
 97                                                              </link-entity>
 98                                                              <filter type='and'>
 99                                                                 <condition attribute='sourceentityname' operator='eq' v
 100                                                                 <condition attribute='targetentityname' operator='eq' v
 101                                                              </filter>
 102                                                           </entity>
 103                                                        </fetch>";
 104    }
 105}