Summary

Class:FakeXrmEasy.XrmRealContext
Assembly:FakeXrmEasy
File(s):F:\Git\fake-xrm-easy\FakeXrmEasy.Shared\XrmRealContext.cs
Covered lines:59
Uncovered lines:2
Coverable lines:61
Total lines:132
Line coverage:96.7%
Branch coverage:70%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
.ctor()1100100
.ctor(...)1100100
.ctor(...)1100100
GetOrganizationService()2100100
Initialize(...)1100100
GetOrgService()38060
GetContextFromSerialisedCompressedProfile(...)7100100

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using Microsoft.Xrm.Sdk;
 5using System.Configuration;
 6using System.IO;
 7
 8using System.Xml.Linq;
 9using System.Linq;
 10
 11using System.IO.Compression;
 12using System.Runtime.Serialization;
 13
 14#if FAKE_XRM_EASY_2016 || FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9
 15using Microsoft.Xrm.Tooling.Connector;
 16#else
 17
 18using Microsoft.Xrm.Client;
 19using Microsoft.Xrm.Client.Services;
 20
 21#endif
 22
 23namespace FakeXrmEasy
 24{
 25    /// <summary>
 26    /// Reuse unit test syntax to test against a real CRM organisation
 27    /// It uses a real CRM organisation service instance
 28    /// </summary>
 29    public class XrmRealContext : XrmFakedContext, IXrmContext
 30    {
 6031        public string ConnectionStringName { get; set; } = "fakexrmeasy-connection";
 32
 2433        public XrmRealContext()
 2434        {
 35            //Don't setup fakes in this case.
 2436        }
 37
 638        public XrmRealContext(string connectionStringName)
 639        {
 640            ConnectionStringName = connectionStringName;
 41            //Don't setup fakes in this case.
 642        }
 43
 644        public XrmRealContext(IOrganizationService organizationService)
 645        {
 646            Service = organizationService;
 47            //Don't setup fakes in this case.
 648        }
 49
 50        public override IOrganizationService GetOrganizationService()
 1851        {
 1852             if (Service != null)
 653                return Service;
 54
 1255            Service = GetOrgService();
 1256            return Service;
 1857        }
 58
 59        public override void Initialize(IEnumerable<Entity> entities)
 660        {
 61            //Does nothing...  otherwise it would create records in a real org db
 662        }
 63
 64        protected IOrganizationService GetOrgService()
 1265        {
 1266            var connection = ConfigurationManager.ConnectionStrings[ConnectionStringName];
 67
 68            // In case of missing connection string in configuration,
 69            // use ConnectionStringName as an explicit connection string
 1270             var connectionString = connection == null ? ConnectionStringName : connection.ConnectionString;
 71
 1272             if (string.IsNullOrWhiteSpace(connectionString))
 073            {
 074                throw new Exception("The ConnectionStringName property must be either a connection string or a connectio
 75            }
 76
 77#if FAKE_XRM_EASY_2016 || FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9
 78
 79            // Connect to the CRM web service using a connection string.
 680            CrmServiceClient client = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString);
 681            return client;
 82
 83#else
 684            CrmConnection crmConnection = CrmConnection.Parse(connectionString);
 685            OrganizationService service = new OrganizationService(crmConnection);
 686            return service;
 87#endif
 1288        }
 89
 90        public XrmFakedPluginExecutionContext GetContextFromSerialisedCompressedProfile(string sCompressedProfile)
 1291        {
 1292            byte[] data = Convert.FromBase64String(sCompressedProfile);
 93
 1294            using (var memStream = new MemoryStream(data))
 1295            {
 1296                using (var decompressedStream = new DeflateStream(memStream, CompressionMode.Decompress, false))
 1297                {
 1298                    byte[] buffer = new byte[0x1000];
 99
 12100                    using (var tempStream = new MemoryStream())
 12101                    {
 12102                        int numBytesRead = decompressedStream.Read(buffer, 0, buffer.Length);
 108103                         while (numBytesRead > 0)
 96104                        {
 96105                            tempStream.Write(buffer, 0, numBytesRead);
 96106                            numBytesRead = decompressedStream.Read(buffer, 0, buffer.Length);
 96107                        }
 108
 109                        //tempStream has the decompressed plugin context now
 12110                        var decompressedString = Encoding.UTF8.GetString(tempStream.ToArray());
 12111                        var xlDoc = XDocument.Parse(decompressedString);
 112
 12113                        var contextElement = xlDoc.Descendants().Elements()
 72114                            .Where(x => x.Name.LocalName.Equals("Context"))
 12115                            .FirstOrDefault();
 116
 12117                        var pluginContextString = contextElement.Value;
 118
 12119                        XrmFakedPluginExecutionContext context = null;
 12120                        using (var reader = new MemoryStream(Encoding.UTF8.GetBytes(pluginContextString)))
 12121                        {
 12122                            var dcSerializer = new DataContractSerializer(typeof(XrmFakedPluginExecutionContext));
 12123                            context = (XrmFakedPluginExecutionContext)dcSerializer.ReadObject(reader);
 12124                        }
 125
 12126                        return context;
 127                    }
 128                }
 129            }
 12130        }
 131    }
 132}