Summary

Class:FakeXrmEasy.FakeMessageExecutors.RetrieveMultipleRequestExecutor
Assembly:FakeXrmEasy
File(s):C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\RetrieveMultipleRequestExecutor.cs
Covered lines:146
Uncovered lines:2
Coverable lines:148
Total lines:216
Line coverage:98.6%
Branch coverage:92.8%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
CanExecute(...)1100100
Execute(...)2498.0493.55
PopulateFormattedValues(...)6100100
GetFormattedValueForValue(...)510085.71
GetResponsibleRequestType()1100100
GetDistinctEntities(...)4100100

File(s)

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

#LineLine coverage
 1using FakeXrmEasy.Extensions;
 2using FakeXrmEasy.Extensions.FetchXml;
 3using Microsoft.Xrm.Sdk;
 4using Microsoft.Xrm.Sdk.Messages;
 5using Microsoft.Xrm.Sdk.Query;
 6using System;
 7using System.Collections.Generic;
 8using System.Linq;
 9
 10namespace FakeXrmEasy.FakeMessageExecutors
 11{
 12    public class RetrieveMultipleRequestExecutor : IFakeMessageExecutor
 13    {
 14        public bool CanExecute(OrganizationRequest request)
 73515        {
 73516            return request is RetrieveMultipleRequest;
 73517        }
 18
 19        public OrganizationResponse Execute(OrganizationRequest req, XrmFakedContext ctx)
 237320        {
 237321            var request = req as RetrieveMultipleRequest;
 237322            List<Entity> list = null;
 237323            PagingInfo pageInfo = null;
 24            QueryExpression qe;
 25
 237326            string entityName = null;
 27
 237328             if (request.Query is QueryExpression)
 158729            {
 158730                qe = (request.Query as QueryExpression).Clone();
 158731                entityName = qe.EntityName;
 32
 158733                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe);
 157234                list = linqQuery.ToList();
 157235            }
 78636             else if (request.Query is FetchExpression)
 66737            {
 66738                var fetchXml = (request.Query as FetchExpression).Query;
 66739                var xmlDoc = XrmFakedContext.ParseFetchXml(fetchXml);
 66740                qe = XrmFakedContext.TranslateFetchXmlDocumentToQueryExpression(ctx, xmlDoc);
 64941                entityName = qe.EntityName;
 42
 64943                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe);
 63744                list = linqQuery.ToList();
 45
 63746                 if (xmlDoc.IsAggregateFetchXml())
 14447                {
 14448                    list = XrmFakedContext.ProcessAggregateFetchXml(ctx, xmlDoc, list);
 14449                }
 63750            }
 11951             else if (request.Query is QueryByAttribute)
 11952            {
 53                // We instantiate a QueryExpression to be executed as we have the implementation done already
 11954                var query = request.Query as QueryByAttribute;
 11955                qe = new QueryExpression(query.EntityName);
 11956                entityName = qe.EntityName;
 57
 11958                qe.ColumnSet = query.ColumnSet;
 11959                qe.Criteria = new FilterExpression();
 58460                 for (var i = 0; i < query.Attributes.Count; i++)
 17361                {
 17362                    qe.Criteria.AddCondition(new ConditionExpression(query.Attributes[i], ConditionOperator.Equal, query
 17363                }
 64
 36965                foreach (var order in query.Orders)
 666                {
 667                    qe.AddOrder(order.AttributeName, order.OrderType);
 668                }
 69
 11970                qe.PageInfo = query.PageInfo;
 11971                qe.TopCount = query.TopCount;
 72
 73                // QueryExpression now done... execute it!
 11974                var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe);
 11975                list = linqQuery.ToList();
 11976            }
 77            else
 078            {
 079                throw PullRequestException.NotImplementedOrganizationRequest(request.Query.GetType());
 80            }
 81
 232882             if (qe.Distinct)
 4283            {
 4284                list = GetDistinctEntities(list);
 4285            }
 86
 87            // Handle the top count before taking paging into account
 232888             if (qe.TopCount != null && qe.TopCount.Value < list.Count)
 689            {
 690                list = list.Take(qe.TopCount.Value).ToList();
 691            }
 92
 93            // Handle TotalRecordCount here?
 232894            int totalRecordCount = -1;
 232895             if (qe?.PageInfo?.ReturnTotalRecordCount == true)
 3096            {
 3097                totalRecordCount = list.Count;
 3098            }
 99
 100            // Handle paging
 2328101            var pageSize = ctx.MaxRetrieveCount;
 2328102            pageInfo = qe.PageInfo;
 2328103            int pageNumber = 1;
 2328104             if (pageInfo != null && pageInfo.PageNumber > 0)
 1305105            {
 1305106                pageNumber = pageInfo.PageNumber;
 1305107                 pageSize = pageInfo.Count == 0 ? ctx.MaxRetrieveCount : pageInfo.Count;
 1305108            }
 109
 110            // Figure out where in the list we need to start and how many items we need to grab
 2328111            int numberToGet = pageSize;
 2328112            int startPosition = 0;
 113
 2328114             if (pageNumber != 1)
 36115            {
 36116                startPosition = (pageNumber - 1) * pageSize;
 36117            }
 118
 2328119             if (list.Count < pageSize)
 2124120            {
 2124121                numberToGet = list.Count;
 2124122            }
 204123             else if (list.Count - pageSize * (pageNumber - 1) < pageSize)
 18124            {
 18125                numberToGet = list.Count - (pageSize * (pageNumber - 1));
 18126            }
 127
 2328128            var recordsToReturn = startPosition + numberToGet > list.Count ? new List<Entity>() : list.GetRange(startPos
 129
 18083130            recordsToReturn.ForEach(e => e.ApplyDateBehaviour(ctx));
 18083131            recordsToReturn.ForEach(e => PopulateFormattedValues(e));
 132
 2328133            var response = new RetrieveMultipleResponse
 2328134            {
 2328135                Results = new ParameterCollection
 2328136                                 {
 2328137                                    { "EntityCollection", new EntityCollection(recordsToReturn) }
 2328138                                 }
 2328139            };
 2328140            response.EntityCollection.EntityName = entityName;
 2328141            response.EntityCollection.MoreRecords = (list.Count - pageSize * pageNumber) > 0;
 2328142            response.EntityCollection.TotalRecordCount = totalRecordCount;
 143
 2328144             if (response.EntityCollection.MoreRecords)
 66145            {
 66146                var first = response.EntityCollection.Entities.First();
 66147                var last = response.EntityCollection.Entities.Last();
 66148                response.EntityCollection.PagingCookie = $"<cookie page=\"{pageNumber}\"><{first.LogicalName}id last=\"{
 66149            }
 150
 2328151            return response;
 2328152        }
 153
 154        /// <summary>
 155        /// Populates the formmated values property of this entity record based on the proxy types
 156        /// </summary>
 157        /// <param name="e"></param>
 158        protected void PopulateFormattedValues(Entity e)
 15755159        {
 160            // Iterate through attributes and retrieve formatted values based on type
 80763161            foreach (var attKey in e.Attributes.Keys)
 16749162            {
 16749163                var value = e[attKey];
 16749164                string formattedValue = "";
 16749165                 if (!e.FormattedValues.ContainsKey(attKey) && (value != null))
 16731166                {
 167                    bool bShouldAdd;
 16731168                    formattedValue = this.GetFormattedValueForValue(value, out bShouldAdd);
 16731169                     if (bShouldAdd)
 6170                    {
 6171                        e.FormattedValues.Add(attKey, formattedValue);
 6172                    }
 16731173                }
 16749174            }
 15755175        }
 176
 177        protected string GetFormattedValueForValue(object value, out bool bShouldAddFormattedValue)
 20827178        {
 20827179            bShouldAddFormattedValue = false;
 20827180            var sFormattedValue = string.Empty;
 181
 20827182             if (value is Enum)
 6183            {
 184                // Retrieve the enum type
 6185                sFormattedValue = Enum.GetName(value.GetType(), value);
 6186                bShouldAddFormattedValue = true;
 6187            }
 20821188             else if (value is AliasedValue)
 4096189            {
 4096190                 return this.GetFormattedValueForValue((value as AliasedValue)?.Value, out bShouldAddFormattedValue);
 191            }
 192
 16731193            return sFormattedValue;
 20827194        }
 195
 196        public Type GetResponsibleRequestType()
 4270197        {
 4270198            return typeof(RetrieveMultipleRequest);
 4270199        }
 200
 201        private static List<Entity> GetDistinctEntities(IEnumerable<Entity> input)
 42202        {
 42203            var output = new List<Entity>();
 204
 258205            foreach (var entity in input)
 66206            {
 90207                 if (!output.Any(i => i.LogicalName == entity.LogicalName && i.Attributes.SequenceEqual(entity.Attributes
 48208                {
 48209                    output.Add(entity);
 48210                }
 66211            }
 212
 42213            return output;
 42214        }
 215    }
 216}