| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Text; |
| | 4 | | using Microsoft.Xrm.Sdk; |
| | 5 | | using System.Configuration; |
| | 6 | | using System.IO; |
| | 7 | |
|
| | 8 | | using System.Xml.Linq; |
| | 9 | | using System.Linq; |
| | 10 | |
|
| | 11 | | using System.IO.Compression; |
| | 12 | | using System.Runtime.Serialization; |
| | 13 | |
|
| | 14 | | #if FAKE_XRM_EASY_2016 || FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9 |
| | 15 | | using Microsoft.Xrm.Tooling.Connector; |
| | 16 | | #else |
| | 17 | |
|
| | 18 | | using Microsoft.Xrm.Client; |
| | 19 | | using Microsoft.Xrm.Client.Services; |
| | 20 | |
|
| | 21 | | #endif |
| | 22 | |
|
| | 23 | | namespace 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 | | { |
| 60 | 31 | | public string ConnectionStringName { get; set; } = "fakexrmeasy-connection"; |
| | 32 | |
|
| 24 | 33 | | public XrmRealContext() |
| 24 | 34 | | { |
| | 35 | | //Don't setup fakes in this case. |
| 24 | 36 | | } |
| | 37 | |
|
| 6 | 38 | | public XrmRealContext(string connectionStringName) |
| 6 | 39 | | { |
| 6 | 40 | | ConnectionStringName = connectionStringName; |
| | 41 | | //Don't setup fakes in this case. |
| 6 | 42 | | } |
| | 43 | |
|
| 6 | 44 | | public XrmRealContext(IOrganizationService organizationService) |
| 6 | 45 | | { |
| 6 | 46 | | Service = organizationService; |
| | 47 | | //Don't setup fakes in this case. |
| 6 | 48 | | } |
| | 49 | |
|
| | 50 | | public override IOrganizationService GetOrganizationService() |
| 18 | 51 | | { |
| 18 | 52 | | if (Service != null) |
| 6 | 53 | | return Service; |
| | 54 | |
|
| 12 | 55 | | Service = GetOrgService(); |
| 12 | 56 | | return Service; |
| 18 | 57 | | } |
| | 58 | |
|
| | 59 | | public override void Initialize(IEnumerable<Entity> entities) |
| 6 | 60 | | { |
| | 61 | | //Does nothing... otherwise it would create records in a real org db |
| 6 | 62 | | } |
| | 63 | |
|
| | 64 | | protected IOrganizationService GetOrgService() |
| 12 | 65 | | { |
| 12 | 66 | | var connection = ConfigurationManager.ConnectionStrings[ConnectionStringName]; |
| | 67 | |
|
| | 68 | | // In case of missing connection string in configuration, |
| | 69 | | // use ConnectionStringName as an explicit connection string |
| 12 | 70 | | var connectionString = connection == null ? ConnectionStringName : connection.ConnectionString; |
| | 71 | |
|
| 12 | 72 | | if (string.IsNullOrWhiteSpace(connectionString)) |
| 0 | 73 | | { |
| 0 | 74 | | 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. |
| 6 | 80 | | CrmServiceClient client = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString); |
| 6 | 81 | | return client; |
| | 82 | |
|
| | 83 | | #else |
| 6 | 84 | | CrmConnection crmConnection = CrmConnection.Parse(connectionString); |
| 6 | 85 | | OrganizationService service = new OrganizationService(crmConnection); |
| 6 | 86 | | return service; |
| | 87 | | #endif |
| 12 | 88 | | } |
| | 89 | |
|
| | 90 | | public XrmFakedPluginExecutionContext GetContextFromSerialisedCompressedProfile(string sCompressedProfile) |
| 12 | 91 | | { |
| 12 | 92 | | byte[] data = Convert.FromBase64String(sCompressedProfile); |
| | 93 | |
|
| 12 | 94 | | using (var memStream = new MemoryStream(data)) |
| 12 | 95 | | { |
| 12 | 96 | | using (var decompressedStream = new DeflateStream(memStream, CompressionMode.Decompress, false)) |
| 12 | 97 | | { |
| 12 | 98 | | byte[] buffer = new byte[0x1000]; |
| | 99 | |
|
| 12 | 100 | | using (var tempStream = new MemoryStream()) |
| 12 | 101 | | { |
| 12 | 102 | | int numBytesRead = decompressedStream.Read(buffer, 0, buffer.Length); |
| 108 | 103 | | while (numBytesRead > 0) |
| 96 | 104 | | { |
| 96 | 105 | | tempStream.Write(buffer, 0, numBytesRead); |
| 96 | 106 | | numBytesRead = decompressedStream.Read(buffer, 0, buffer.Length); |
| 96 | 107 | | } |
| | 108 | |
|
| | 109 | | //tempStream has the decompressed plugin context now |
| 12 | 110 | | var decompressedString = Encoding.UTF8.GetString(tempStream.ToArray()); |
| 12 | 111 | | var xlDoc = XDocument.Parse(decompressedString); |
| | 112 | |
|
| 12 | 113 | | var contextElement = xlDoc.Descendants().Elements() |
| 72 | 114 | | .Where(x => x.Name.LocalName.Equals("Context")) |
| 12 | 115 | | .FirstOrDefault(); |
| | 116 | |
|
| 12 | 117 | | var pluginContextString = contextElement.Value; |
| | 118 | |
|
| 12 | 119 | | XrmFakedPluginExecutionContext context = null; |
| 12 | 120 | | using (var reader = new MemoryStream(Encoding.UTF8.GetBytes(pluginContextString))) |
| 12 | 121 | | { |
| 12 | 122 | | var dcSerializer = new DataContractSerializer(typeof(XrmFakedPluginExecutionContext)); |
| 12 | 123 | | context = (XrmFakedPluginExecutionContext)dcSerializer.ReadObject(reader); |
| 12 | 124 | | } |
| | 125 | |
|
| 12 | 126 | | return context; |
| | 127 | | } |
| | 128 | | } |
| | 129 | | } |
| 12 | 130 | | } |
| | 131 | | } |
| | 132 | | } |