/// <summary>
        /// Copies the Message instance's properties to the AmqpMessage instance.
        /// </summary>
        public static void UpdateAmqpMessageHeadersAndProperties(AmqpMessage amqpMessage, Message data, bool copyUserProperties = true)
        {
            amqpMessage.Properties.MessageId = data.MessageId;

            if (data.To != null)
            {
                amqpMessage.Properties.To = data.To;
            }

            if (!data.ExpiryTimeUtc.Equals(default(DateTime)))
            {
                amqpMessage.Properties.AbsoluteExpiryTime = data.ExpiryTimeUtc;
            }

            if (data.CorrelationId != null)
            {
                amqpMessage.Properties.CorrelationId = data.CorrelationId;
            }

            if (data.UserId != null)
            {
                amqpMessage.Properties.UserId = new ArraySegment <byte>(Encoding.UTF8.GetBytes(data.UserId));
            }

            if (amqpMessage.ApplicationProperties == null)
            {
                amqpMessage.ApplicationProperties = new ApplicationProperties();
            }

            object propertyValue;

            if (data.SystemProperties.TryGetValue(MessageSystemPropertyNames.Ack, out propertyValue))
            {
                amqpMessage.ApplicationProperties.Map["iothub-ack"] = (string)propertyValue;
            }

            if (data.SystemProperties.TryGetValue(MessageSystemPropertyNames.MessageSchema, out propertyValue))
            {
                amqpMessage.ApplicationProperties.Map[MessageSystemPropertyNames.MessageSchema] = (string)propertyValue;
            }

            if (data.SystemProperties.TryGetValue(MessageSystemPropertyNames.CreationTimeUtc, out propertyValue))
            {
                amqpMessage.ApplicationProperties.Map[MessageSystemPropertyNames.CreationTimeUtc] = ((DateTime)propertyValue).ToString("o");    // Convert to string that complies with ISO 8601
            }

            if (data.SystemProperties.TryGetValue(MessageSystemPropertyNames.ContentType, out propertyValue))
            {
                amqpMessage.Properties.ContentType = (string)propertyValue;
            }

            if (data.SystemProperties.TryGetValue(MessageSystemPropertyNames.ContentEncoding, out propertyValue))
            {
                amqpMessage.Properties.ContentEncoding = (string)propertyValue;
            }

            if (data.SystemProperties.TryGetValue(MessageSystemPropertyNames.OutputName, out propertyValue))
            {
                amqpMessage.ApplicationProperties.Map[MessageSystemPropertyNames.OutputName] = (string)propertyValue;
            }

            if (data.SystemProperties.TryGetValue(MessageSystemPropertyNames.InterfaceId, out propertyValue))
            {
                amqpMessage.MessageAnnotations.Map[MessageSystemPropertyNames.InterfaceId] = (string)propertyValue;
            }

            if (copyUserProperties && data.Properties.Count > 0)
            {
                foreach (var pair in data.Properties)
                {
                    object amqpObject;
                    if (TryGetAmqpObjectFromNetObject(pair.Value, MappingType.ApplicationProperty, out amqpObject))
                    {
                        amqpMessage.ApplicationProperties.Map[pair.Key] = amqpObject;
                    }
                }
            }

            if (IoTHubClientDiagnostic.HasDiagnosticProperties(data))
            {
                amqpMessage.MessageAnnotations.Map[AmqpDiagIdKey] = data.SystemProperties[MessageSystemPropertyNames.DiagId];
                amqpMessage.MessageAnnotations.Map[AmqpDiagCorrelationContextKey] = data.SystemProperties[MessageSystemPropertyNames.DiagCorrelationContext];
            }
        }
        public void IoTHubClientDiagnostic_SamplingPercentage_Test()
        {
            int percentage   = 0;
            int count        = 0;
            int messageCount = 0;

            for (int i = 1; i <= 100; i++)
            {
                Message message = CreateMessage();
                IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, percentage, ref messageCount);
                if (IoTHubClientDiagnostic.HasDiagnosticProperties(message))
                {
                    count++;
                }
            }
            Assert.AreEqual(count, 0);

            count        = 0;
            percentage   = 10;
            messageCount = 0;
            for (int i = 1; i <= 100; i++)
            {
                Message message = CreateMessage();
                IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, percentage, ref messageCount);
                if (IoTHubClientDiagnostic.HasDiagnosticProperties(message))
                {
                    Assert.IsTrue(i % 10 == 1);
                    count++;
                }
            }
            Assert.AreEqual(count, 10);

            count        = 0;
            percentage   = 20;
            messageCount = 0;
            for (int i = 1; i <= 100; i++)
            {
                Message message = CreateMessage();
                IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, percentage, ref messageCount);
                if (IoTHubClientDiagnostic.HasDiagnosticProperties(message))
                {
                    Assert.IsTrue(i % 5 == 1);
                    count++;
                }
            }
            Assert.AreEqual(count, 20);


            count        = 0;
            percentage   = 50;
            messageCount = 0;
            for (int i = 1; i <= 100; i++)
            {
                Message message = CreateMessage();
                IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, percentage, ref messageCount);
                if (IoTHubClientDiagnostic.HasDiagnosticProperties(message))
                {
                    Assert.IsTrue(i % 2 == 1);
                    count++;
                }
            }
            Assert.AreEqual(count, 50);

            count        = 0;
            percentage   = 70;
            messageCount = 0;
            for (int i = 1; i <= 100; i++)
            {
                Message message = CreateMessage();
                IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, percentage, ref messageCount);
                if (IoTHubClientDiagnostic.HasDiagnosticProperties(message))
                {
                    count++;
                }
            }
            Assert.AreEqual(count, 70);

            count        = 0;
            percentage   = 100;
            messageCount = 0;
            for (int i = 1; i <= 100; i++)
            {
                Message message = CreateMessage();
                IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, percentage, ref messageCount);
                if (IoTHubClientDiagnostic.HasDiagnosticProperties(message))
                {
                    count++;
                }
            }
            Assert.AreEqual(count, 100);
        }