Summary

Class:FakeXrmEasy.FakeMessageExecutors.RetrieveRequestExecutor
Assembly:FakeXrmEasy
File(s):C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\RetrieveRequestExecutor.cs
Covered lines:110
Uncovered lines:4
Coverable lines:114
Total lines:169
Line coverage:96.4%
Branch coverage:86.6%

Metrics

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

File(s)

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

#LineLine coverage
 1using FakeXrmEasy.Extensions;
 2using Microsoft.Xrm.Sdk;
 3using Microsoft.Xrm.Sdk.Messages;
 4using Microsoft.Xrm.Sdk.Query;
 5using System;
 6using System.ServiceModel;
 7
 8namespace FakeXrmEasy.FakeMessageExecutors
 9{
 10    public class RetrieveRequestExecutor : IFakeMessageExecutor
 11    {
 12        public bool CanExecute(OrganizationRequest request)
 9613        {
 9614            return request.GetType().Equals(GetResponsibleRequestType());
 9615        }
 16
 17
 18
 19        public OrganizationResponse Execute(OrganizationRequest req, XrmFakedContext context)
 77820        {
 77821            var request = req as RetrieveRequest;
 22
 77823             if (request.Target == null)
 624            {
 625                throw new ArgumentNullException("Target", "RetrieveRequest without Target is invalid.");
 26            }
 27
 77228            var entityName = request.Target.LogicalName;
 77229            var columnSet = request.ColumnSet;
 77230             if (columnSet == null)
 631            {
 632                throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Required field 'Colu
 33            }
 34
 76635            var id = context.GetRecordUniqueId(request.Target);
 36
 37            //Entity logical name exists, so , check if the requested entity exists
 73938             if (context.Data.ContainsKey(entityName) && context.Data[entityName] != null
 73939                && context.Data[entityName].ContainsKey(id))
 70340            {
 41                //Return the subset of columns requested only
 70342                var reflectedType = context.FindReflectedType(entityName);
 43
 44                //Entity found => return only the subset of columns specified or all of them
 70345                var resultEntity = context.Data[entityName][id].Clone(reflectedType, context);
 70346                 if (!columnSet.AllColumns)
 15247                {
 15248                    resultEntity = resultEntity.ProjectAttributes(columnSet, context);
 15249                }
 70350                resultEntity.ApplyDateBehaviour(context);
 51
 70352                 if (request.RelatedEntitiesQuery != null && request.RelatedEntitiesQuery.Count > 0)
 7853                {
 39054                    foreach (var relatedEntitiesQuery in request.RelatedEntitiesQuery)
 8455                    {
 8456                         if (relatedEntitiesQuery.Value == null)
 657                        {
 658                            throw new ArgumentNullException("relateEntitiesQuery.Value",
 659                                string.Format("RelatedEntitiesQuery for \"{0}\" does not contain a Query Expression.",
 660                                    relatedEntitiesQuery.Key.SchemaName));
 61                        }
 62
 7863                        var fakeRelationship = context.GetRelationship(relatedEntitiesQuery.Key.SchemaName);
 7864                         if (fakeRelationship == null)
 665                        {
 666                            throw new Exception(string.Format("Relationship \"{0}\" does not exist in the metadata cache
 667                                relatedEntitiesQuery.Key.SchemaName));
 68                        }
 69
 7270                        var relatedEntitiesQueryValue = (QueryExpression)relatedEntitiesQuery.Value;
 7271                        QueryExpression retrieveRelatedEntitiesQuery = relatedEntitiesQueryValue.Clone();
 72
 7273                         if (fakeRelationship.RelationshipType == XrmFakedRelationship.enmFakeRelationshipType.OneToMany)
 3674                        {
 3675                            var isFrom1to2 = relatedEntitiesQueryValue.EntityName == fakeRelationship.Entity1LogicalName
 3676                                || request.Target.LogicalName != fakeRelationship.Entity1LogicalName
 3677                                || string.IsNullOrWhiteSpace(relatedEntitiesQueryValue.EntityName);
 78
 3679                             if (isFrom1to2)
 3080                            {
 3081                                 var fromAttribute = isFrom1to2 ? fakeRelationship.Entity1Attribute : fakeRelationship.En
 3082                                 var toAttribute = isFrom1to2 ? fakeRelationship.Entity2Attribute : fakeRelationship.Enti
 83
 3084                                var linkEntity = new LinkEntity
 3085                                {
 3086                                    Columns = new ColumnSet(false),
 3087                                    LinkFromAttributeName = fromAttribute,
 3088                                    LinkFromEntityName = retrieveRelatedEntitiesQuery.EntityName,
 3089                                    LinkToAttributeName = toAttribute,
 3090                                    LinkToEntityName = resultEntity.LogicalName
 3091                                };
 92
 3093                                 if (retrieveRelatedEntitiesQuery.Criteria == null)
 094                                {
 095                                    retrieveRelatedEntitiesQuery.Criteria = new FilterExpression();
 096                                }
 97
 3098                                retrieveRelatedEntitiesQuery.Criteria
 3099                                    .AddFilter(LogicalOperator.And)
 30100                                    .AddCondition(linkEntity.LinkFromAttributeName, ConditionOperator.Equal, resultEntit
 30101                            }
 102                            else
 6103                            {
 6104                                var link = retrieveRelatedEntitiesQuery.AddLink(fakeRelationship.Entity1LogicalName, fak
 6105                                link.LinkCriteria.AddCondition(resultEntity.LogicalName + "id", ConditionOperator.Equal,
 6106                            }
 36107                        }
 108                        else
 36109                        {
 36110                            var isFrom1 = fakeRelationship.Entity1LogicalName == retrieveRelatedEntitiesQuery.EntityName
 36111                             var linkAttributeName = isFrom1 ? fakeRelationship.Entity1Attribute : fakeRelationship.Entit
 36112                             var conditionAttributeName = isFrom1 ? fakeRelationship.Entity2Attribute : fakeRelationship.
 113
 36114                            var linkEntity = new LinkEntity
 36115                            {
 36116                                Columns = new ColumnSet(false),
 36117                                LinkFromAttributeName = linkAttributeName,
 36118                                LinkFromEntityName = retrieveRelatedEntitiesQuery.EntityName,
 36119                                LinkToAttributeName = linkAttributeName,
 36120                                LinkToEntityName = fakeRelationship.IntersectEntity,
 36121                                LinkCriteria = new FilterExpression
 36122                                {
 36123                                    Conditions =
 36124                                {
 36125                                    new ConditionExpression(conditionAttributeName , ConditionOperator.Equal, resultEnti
 36126                                }
 36127                                }
 36128                            };
 36129                            retrieveRelatedEntitiesQuery.LinkEntities.Add(linkEntity);
 36130                        }
 131
 72132                        var retrieveRelatedEntitiesRequest = new RetrieveMultipleRequest
 72133                        {
 72134                            Query = retrieveRelatedEntitiesQuery
 72135                        };
 136
 137                        //use of an executor directly; if to use service.RetrieveMultiple then the result will be
 138                        //limited to the number of records per page (somewhere in future release).
 139                        //ALL RECORDS are needed here.
 72140                        var executor = new RetrieveMultipleRequestExecutor();
 72141                        var retrieveRelatedEntitiesResponse = executor
 72142                            .Execute(retrieveRelatedEntitiesRequest, context) as RetrieveMultipleResponse;
 143
 72144                         if (retrieveRelatedEntitiesResponse.EntityCollection.Entities.Count == 0)
 0145                            continue;
 146
 72147                        resultEntity.RelatedEntities
 72148                            .Add(relatedEntitiesQuery.Key, retrieveRelatedEntitiesResponse.EntityCollection);
 72149                    }
 66150                }
 151
 691152                return new RetrieveResponse
 691153                {
 691154                    Results = new ParameterCollection { { "Entity", resultEntity } }
 691155                };
 156            }
 157            else
 36158            {
 159                // Entity not found in the context => FaultException
 36160                throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault() { ErrorCode = unchecke
 161            }
 691162        }
 163
 164        public Type GetResponsibleRequestType()
 4366165        {
 4366166            return typeof(RetrieveRequest);
 4366167        }
 168    }
 169}