public void IoTHubClientDiagnostic_CopyDiagnosticPropertiesToAmqpAnnotations_Test()
        {
            Message   message = CreateMessage();
            const int DiagPercentageWithDiagnostic = 100;
            int       messageCount = 0;

            IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, DiagPercentageWithDiagnostic, ref messageCount);
            AmqpMessage amqpMessage = AmqpIoTMessageConverter.MessageToAmqpMessage(message);

            if (amqpMessage.MessageAnnotations.Map.TryGetValue("Diagnostic-Id", out string diagId))
            {
                Assert.IsTrue(message.SystemProperties[MessageSystemPropertyNames.DiagId] as string == diagId);
            }
            else
            {
                throw new AssertFailedException("Diagnostic-Id mismatch");
            }

            if (amqpMessage.MessageAnnotations.Map.TryGetValue("Correlation-Context", out string diagCorrelationContext))
            {
                Assert.IsTrue(message.SystemProperties[MessageSystemPropertyNames.DiagCorrelationContext] as string == diagCorrelationContext);
            }
            else
            {
                throw new AssertFailedException("Correlation-Context mismatch");
            }
        }
        public void IoTHubClientDiagnostic_DiagId_Test()
        {
            Message   message = CreateMessage();
            const int DiagPercentageWithDiagnostic = 100;
            int       messageCount = 0;

            IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, DiagPercentageWithDiagnostic, ref messageCount);
            string diagId = message.SystemProperties[MessageSystemPropertyNames.DiagId].ToString();
            var    r      = new Regex("^[a-zA-Z0-9]{8}$");

            Assert.IsTrue(r.IsMatch(diagId));
        }
        public void IoTHubClientDiagnostic_CopyDiagnosticPropertiesToAmqpAnnotations_Test()
        {
            Message   message = CreateMessage();
            const int DiagPercentageWithDiagnostic = 100;
            int       messageCount = 0;

            IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, DiagPercentageWithDiagnostic, ref messageCount);
            AmqpMessage amqpMessage = message.ToAmqpMessage();

            Assert.IsTrue(message.SystemProperties[MessageSystemPropertyNames.DiagId] == amqpMessage.MessageAnnotations.Map["Diagnostic-Id"]);
            Assert.IsTrue(message.SystemProperties[MessageSystemPropertyNames.DiagCorrelationContext] == amqpMessage.MessageAnnotations.Map["Correlation-Context"]);
        }
        public void IoTHubClientDiagnostic_AddDiagnosticInfoIfNecessary_Test()
        {
            Message   message = CreateMessage();
            const int DiagPercentageWithoutDiagnostic = 0;
            const int DiagPercentageWithDiagnostic    = 100;

            int messageCount = 0;

            IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, DiagPercentageWithoutDiagnostic, ref messageCount);

            Assert.IsFalse(message.SystemProperties.ContainsKey(MessageSystemPropertyNames.DiagId));
            Assert.IsFalse(message.SystemProperties.ContainsKey(MessageSystemPropertyNames.DiagCorrelationContext));

            message      = CreateMessage();
            messageCount = 0;
            IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, DiagPercentageWithDiagnostic, ref messageCount);
            Assert.IsTrue(message.SystemProperties.ContainsKey(MessageSystemPropertyNames.DiagId));
            Assert.IsTrue(message.SystemProperties.ContainsKey(MessageSystemPropertyNames.DiagCorrelationContext));
        }
        public void IoTHubClientDiagnostic_CorrelationContext_Test()
        {
            Message   message = CreateMessage();
            const int DiagPercentageWithDiagnostic = 100;
            int       messageCount = 0;

            IoTHubClientDiagnostic.AddDiagnosticInfoIfNecessary(message, DiagPercentageWithDiagnostic, ref messageCount);

            string diagctx = message.SystemProperties[MessageSystemPropertyNames.DiagCorrelationContext].ToString();

            var r = new Regex("^creationtimeutc=\\d*\\.\\d*$");

            Assert.IsTrue(r.IsMatch(diagctx));

            NameValueCollection properties = HttpUtility.ParseQueryString(diagctx.Replace(",", "&"));
            string   second       = properties["creationtimeutc"];
            double   epochTime    = double.Parse(second, CultureInfo.InvariantCulture);
            DateTime creationTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(epochTime);

            Assert.IsTrue((DateTime.UtcNow - creationTime).TotalSeconds < 60);
        }
        /// <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);
        }