Summary

Class:FakeXrmEasy.FakeMessageExecutors.ExecuteFetchRequestExecutor
Assembly:FakeXrmEasy
File(s):F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\FakeMessageExecutors\ExecuteFetchRequestExecutor.cs
Covered lines:130
Uncovered lines:20
Coverable lines:150
Total lines:207
Line coverage:86.6%
Branch coverage:74.5%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
CanExecute(...)1100100
Execute(...)893.3371.43
CreateXmlResult(...)987.8872.73
AttributeValueToFetchResult(...)1981.8275.76
GetResponsibleRequestType()1100100
.ctor()1100100

File(s)

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

#LineLine coverage
 1using Microsoft.Crm.Sdk.Messages;
 2using Microsoft.Xrm.Sdk;
 3using Microsoft.Xrm.Sdk.Messages;
 4using Microsoft.Xrm.Sdk.Query;
 5using System;
 6using System.Collections.Generic;
 7using System.Linq;
 8using System.ServiceModel;
 9using System.Xml.Linq;
 10
 11namespace FakeXrmEasy.FakeMessageExecutors
 12{
 13    public class ExecuteFetchRequestExecutor : IFakeMessageExecutor
 14    {
 428915        private Dictionary<string, int?> _typeCodes = new Dictionary<string, int?>();
 16
 17        public bool CanExecute(OrganizationRequest request)
 4218        {
 4219            return request is ExecuteFetchRequest;
 4220        }
 21
 22        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
 4223        {
 4224            var executeFetchRequest = (ExecuteFetchRequest)request;
 25
 4226             if (executeFetchRequest.FetchXml == null)
 027            {
 028                throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "You need to provide 
 29            }
 30
 4231            var service = ctx.GetFakedOrganizationService();
 32
 4233            var retrieveMultiple = new RetrieveMultipleRequest()
 4234            {
 4235                Query = new FetchExpression(executeFetchRequest.FetchXml)
 4236            };
 4237            var queryResult = (service.Execute(retrieveMultiple) as RetrieveMultipleResponse).EntityCollection;
 38
 4239            XDocument doc = new XDocument(new XElement("resultset",
 4240                new XAttribute("morerecords", Convert.ToInt16(queryResult.MoreRecords))));
 4241             if (queryResult.PagingCookie != null)
 1842            {
 1843                doc.Root.Add(new XAttribute("paging-cookie", queryResult.PagingCookie));
 1844            }
 45
 4246            var allowedAliases = new string[0];
 47
 4248            var fetchXmlDocument = XDocument.Parse(executeFetchRequest.FetchXml).Root;
 4249             if (fetchXmlDocument != null)
 4250            {
 6651                var linkedEntityName = fetchXmlDocument.Descendants("link-entity").Attributes("name").Select(a => a.Valu
 4852                allowedAliases = linkedEntityName.Concat(fetchXmlDocument.Descendants("link-entity").Attributes("alias")
 4253            }
 54
 51055            foreach (var row in queryResult.Entities)
 19256            {
 19257                doc.Root.Add(CreateXmlResult(row, ctx, allowedAliases));
 19258            }
 59
 4260            var response = new ExecuteFetchResponse
 4261            {
 4262                Results = new ParameterCollection
 4263                                 {
 4264                                    { "FetchXmlResult", doc.ToString() }
 4265                                 }
 4266            };
 67
 4268            return response;
 4269        }
 70
 71        private XElement CreateXmlResult(Entity entity, XrmFakedContext ctx, string[] allowedAliases)
 19272        {
 19273            var row = new XElement("result");
 19274            var formattedValues = entity.FormattedValues;
 75
 115276            foreach (var entAtt in entity.Attributes)
 28877            {
 28878                var attribute = entAtt;
 79
 80                // Depricated ExecuteFetch doesn't use implicitly numbered enitity aliases
 28881                 if (attribute.Key.Contains("."))
 2482                {
 2483                    var alias = attribute.Key.Substring(0, attribute.Key.IndexOf(".", StringComparison.Ordinal));
 2484                     if (!allowedAliases.Contains(alias))
 1885                    {
 86                        // The maximum amount of linked entities is 10,
 1887                         var newAlias = alias.Substring(0, alias.Length - (!alias.EndsWith("10") ? 1 : 2));
 1888                         if (allowedAliases.Contains(newAlias))
 1889                        {
 1890                            var newKey = attribute.Key.Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries);
 1891                            newKey[0] = newAlias;
 1892                            attribute = new KeyValuePair<string, object>(string.Join(".", newKey), attribute.Value);
 1893                        }
 94                        else
 095                        {
 96                            // unknow alias, just leave it
 097                        }
 1898                    }
 2499                }
 100
 288101                var attributeValueElement = AttributeValueToFetchResult(attribute, formattedValues, ctx);
 288102                 if (attributeValueElement == null)
 0103                {
 0104                    continue;
 105                }
 106
 288107                row.Add(attributeValueElement);
 288108            }
 109
 192110            return row;
 192111        }
 112
 113        public XElement AttributeValueToFetchResult(KeyValuePair<string, object> entAtt, FormattedValueCollection format
 343114        {
 115            XElement attributeValueElement;
 343116             if (entAtt.Value == null)
 0117                return null;
 343118             if (entAtt.Value is DateTime?)
 18119            {
 18120                attributeValueElement = XElement.Parse(String.Format("<{0} date=\"{1:yyyy-MM-dd}\" time=\"{1:hh:mm tt}\"
 18121            }
 325122             else if (entAtt.Value is EntityReference)
 24123            {
 24124                var entRef = (EntityReference)entAtt.Value;
 24125                 if (!_typeCodes.ContainsKey(entRef.LogicalName))
 24126                {
 24127                    var entType = RetrieveEntityRequestExecutor.GetEntityProxyType(entRef.LogicalName, ctx);
 24128                    var typeCode = entType.GetField("EntityTypeCode").GetValue(null);
 129
 24130                    _typeCodes.Add(entRef.LogicalName, (int?)typeCode);
 24131                }
 132
 24133                attributeValueElement = XElement.Parse(String.Format("<{0} dsc=\"0\" yomi=\"{1}\" name=\"{1}\" type=\"{3
 24134            }
 301135             else if (entAtt.Value is bool?)
 6136            {
 6137                var boolValue = (bool?)entAtt.Value;
 138
 6139                var formattedValue = boolValue.ToString();
 6140                 if (formattedValues.ContainsKey(entAtt.Key))
 0141                    formattedValue = formattedValues[entAtt.Key];
 6142                attributeValueElement = XElement.Parse(String.Format("<{0} name=\"{1}\">{2}</{0}>", entAtt.Key, formatte
 6143            }
 295144             else if (entAtt.Value is OptionSetValue)
 12145            {
 12146                var osValue = (OptionSetValue)entAtt.Value;
 147
 12148                var formattedValue = osValue.Value.ToString();
 12149                 if (formattedValues.ContainsKey(entAtt.Key))
 6150                    formattedValue = formattedValues[entAtt.Key];
 12151                attributeValueElement = XElement.Parse(String.Format("<{0} name=\"{1}\" formattedvalue=\"{2}\">{2}</{0}>
 12152            }
 283153             else if (entAtt.Value is Enum)
 0154            {
 0155                var osValue = (Enum)entAtt.Value;
 156
 0157                var formattedValue = osValue.ToString();
 0158                 if (formattedValues.ContainsKey(entAtt.Key))
 0159                    formattedValue = formattedValues[entAtt.Key];
 0160                attributeValueElement = XElement.Parse(String.Format("<{0} name=\"{1}\" formattedvalue=\"{2}\">{2}</{0}>
 0161            }
 283162             else if (entAtt.Value is Money)
 6163            {
 6164                var moneyValue = (Money)entAtt.Value;
 165
 6166                var formattedValue = moneyValue.Value.ToString();
 6167                 if (formattedValues.ContainsKey(entAtt.Key))
 0168                    formattedValue = formattedValues[entAtt.Key];
 6169                attributeValueElement = XElement.Parse(String.Format("<{0} formattedvalue=\"{1}\">{2:0.##}</{0}>", entAt
 6170            }
 277171             else if (entAtt.Value is decimal?)
 0172            {
 0173                var decimalVal = (decimal?)entAtt.Value;
 174
 0175                attributeValueElement = XElement.Parse(String.Format("<{0}>{1:0.####}</{0}>", entAtt.Key, decimalVal.Val
 0176            }
 277177             else if (entAtt.Value is AliasedValue)
 30178            {
 30179                var alliasedVal = entAtt.Value as AliasedValue;
 30180                attributeValueElement = AttributeValueToFetchResult(new KeyValuePair<string, object>(entAtt.Key, alliase
 30181            }
 247182             else if (entAtt.Value is Guid)
 192183            {
 384184                attributeValueElement = XElement.Parse(String.Format("<{0}>{1}</{0}>", entAtt.Key, entAtt.Value.ToString
 192185            }
 186#if FAKE_XRM_EASY_9
 10187             else if (entAtt.Value is OptionSetValueCollection)
 1188            {
 1189                var optionSetValueCollectionVal = entAtt.Value as OptionSetValueCollection;
 5190                var values = String.Join(",", optionSetValueCollectionVal.Select(o => o.Value).OrderBy(v => v));
 1191                var serializedCollection = String.Join(",", "[-1", values, "-1]");
 1192                attributeValueElement = XElement.Parse(String.Format("<{0} name=\"{1}\">{1}</{0}>", entAtt.Key, serializ
 1193            }
 194#endif
 195            else
 54196            {
 54197                attributeValueElement = XElement.Parse(String.Format("<{0}>{1}</{0}>", entAtt.Key, entAtt.Value));
 54198            }
 343199            return attributeValueElement;
 343200        }
 201
 202        public Type GetResponsibleRequestType()
 4264203        {
 4264204            return typeof(ExecuteFetchRequest);
 4264205        }
 206    }
 207}