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 async Task ConvertAsync_ReturnsExpectedResult_WithSerializedString(string contentType, string value)
        {
            Message message = new Message(value == null ? null : Encoding.UTF8.GetBytes(value));

            message.ContentType = contentType;

            MessageToStringConverter converter = new MessageToStringConverter();
            string result = await converter.ConvertAsync(message, CancellationToken.None);

            Assert.Equal(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>());
        }
Пример #5
0
        public async Task ConvertAsync_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 = await converter.ConvertAsync(message, CancellationToken.None);

            Assert.AreEqual(value, result);
        }
        public async Task ConvertAsync_ReturnsExpectedResult_WithBinarySerializer(string contentType, string value)
        {
            byte[] bytes;
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractBinarySerializer <string> .Instance.WriteObject(ms, value);

                bytes = ms.ToArray();
            }

            Message message = new Message(bytes);

            message.ContentType = contentType;

            MessageToStringConverter converter = new MessageToStringConverter();
            string result = await converter.ConvertAsync(message, CancellationToken.None);

            Assert.Equal(value, result);
        }
Пример #7
0
        public async Task ConvertAsync_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 = await converter.ConvertAsync(message, CancellationToken.None);

            Assert.AreEqual(message.Body.ToString(), result);
        }
        public async Task ConvertAsync_ReturnsExpectedResult_WithSerializedObject()
        {
            byte[] bytes;
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractBinarySerializer <TestObject> .Instance.WriteObject(ms, new TestObject()
                {
                    Text = "Test"
                });

                bytes = ms.ToArray();
            }

            Message message = new Message(bytes);

            MessageToStringConverter converter = new MessageToStringConverter();
            string result = await converter.ConvertAsync(message, CancellationToken.None);

            Assert.Equal(Encoding.UTF8.GetString(message.Body), result);
        }
Пример #9
0
        public async Task ConvertAsync_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 = await converter.ConvertAsync(message, CancellationToken.None);

            // 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);
            }
        }
Пример #10
0
        public async Task ConvertAsync_Throws_WithSerializedObject()
        {
            byte[] bytes;
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractBinarySerializer <TestObject> .Instance.WriteObject(ms, new TestObject()
                {
                    Text = "Test"
                });

                bytes = ms.ToArray();
            }

            Message message = new Message(bytes);

            MessageToStringConverter converter = new MessageToStringConverter();
            var exception = await Assert.ThrowsAsync <InvalidOperationException>(() => converter.ConvertAsync(message, CancellationToken.None));

            Assert.IsType <SerializationException>(exception.InnerException);
            Assert.StartsWith("The Message with ContentType 'null' failed to deserialize to a string with the message:", exception.Message);
        }