public void Convert_ValueBodyMessage_String()
        {
            ServiceBusReceivedMessage message = ServiceBusModelFactory.ServiceBusReceivedMessage();

            message.GetRawAmqpMessage().Body = AmqpMessageBody.FromValue("value");

            MessageToStringConverter converter = new MessageToStringConverter();
            string result = converter.Convert(message);

            Assert.AreEqual("value", result);
        }
        public void Convert_SequenceBodyMessage_Throws()
        {
            ServiceBusReceivedMessage message = ServiceBusModelFactory.ServiceBusReceivedMessage();

            message.GetRawAmqpMessage().Body = AmqpMessageBody.FromSequence(new IList <object>[] { new object[] { "sequence" } });

            MessageToStringConverter converter = new MessageToStringConverter();

            Assert.That(
                () => converter.Convert(message),
                Throws.InstanceOf <NotSupportedException>());
        }
        public void Convert_ValueBodyMessage_NonStringThrows()
        {
            ServiceBusReceivedMessage message = ServiceBusModelFactory.ServiceBusReceivedMessage();

            message.GetRawAmqpMessage().Body = AmqpMessageBody.FromValue(5);

            MessageToStringConverter converter = new MessageToStringConverter();

            Assert.That(
                () => converter.Convert(message),
                Throws.InstanceOf <NotSupportedException>());
        }
        public void Convert_ReturnsExpectedResult_WithBinarySerializer(string contentType, string value)
        {
            byte[] bytes;
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractBinarySerializer <string> .Instance.WriteObject(ms, value);

                bytes = ms.ToArray();
            }

            ServiceBusReceivedMessage message = ServiceBusModelFactory.ServiceBusReceivedMessage(body: new BinaryData(bytes), contentType: contentType);

            MessageToStringConverter converter = new MessageToStringConverter();
            string result = converter.Convert(message);

            Assert.AreEqual(value, result);
        }
        public void Convert_ReturnsExpectedResult_WithSerializedObject()
        {
            byte[] bytes;
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractBinarySerializer <TestObject> .Instance.WriteObject(ms, new TestObject()
                {
                    Text = "Test"
                });

                bytes = ms.ToArray();
            }

            ServiceBusReceivedMessage message = ServiceBusModelFactory.ServiceBusReceivedMessage(body: new BinaryData(bytes));

            MessageToStringConverter converter = new MessageToStringConverter();
            string result = converter.Convert(message);

            Assert.AreEqual(message.Body.ToString(), result);
        }
        public void Convert_ReturnsExpectedResult_WithSerializedString(string contentType, string value)
        {
            ServiceBusReceivedMessage message = ServiceBusModelFactory.ServiceBusReceivedMessage(
                body: value == null ? null : new BinaryData(value),
                contentType: contentType);

            MessageToStringConverter converter = new MessageToStringConverter();
            string result = converter.Convert(message);

            // A received message will never have a null body as a body section is required when sending even if it
            // is empty. This was true in Track 1 as well, but in Track 1 the actual body property could be null when
            // constructing the message, but in practice it wouldn't be null when receiving.
            if (value == null)
            {
                Assert.AreEqual("", result);
            }
            else
            {
                Assert.AreEqual(value, result);
            }
        }