22// Licensed under the MIT License. See License.txt in the project root for license information.
33
44using System ;
5+ using System . Collections . Generic ;
56using System . IO ;
67using System . Text ;
78using System . Threading ;
89using System . Threading . Tasks ;
10+ using Azure . Core . Amqp ;
911using Microsoft . Azure . WebJobs . ServiceBus . Triggers ;
1012using NUnit . Framework ;
1113using Azure . Messaging . ServiceBus ;
@@ -25,7 +27,7 @@ public class MessageToStringConverterTests
2527 [ TestCase ( null , TestJson ) ]
2628 [ TestCase ( "application/xml" , TestJson ) ]
2729 [ TestCase ( ContentTypes . TextPlain , null ) ]
28- public async Task ConvertAsync_ReturnsExpectedResult_WithBinarySerializer ( string contentType , string value )
30+ public void Convert_ReturnsExpectedResult_WithBinarySerializer ( string contentType , string value )
2931 {
3032 byte [ ] bytes ;
3133 using ( MemoryStream ms = new MemoryStream ( ) )
@@ -37,7 +39,7 @@ public async Task ConvertAsync_ReturnsExpectedResult_WithBinarySerializer(string
3739 ServiceBusReceivedMessage message = ServiceBusModelFactory . ServiceBusReceivedMessage ( body : new BinaryData ( bytes ) , contentType : contentType ) ;
3840
3941 MessageToStringConverter converter = new MessageToStringConverter ( ) ;
40- string result = await converter . ConvertAsync ( message , CancellationToken . None ) ;
42+ string result = converter . Convert ( message ) ;
4143
4244 Assert . AreEqual ( value , result ) ;
4345 }
@@ -50,14 +52,14 @@ public async Task ConvertAsync_ReturnsExpectedResult_WithBinarySerializer(string
5052 [ TestCase ( "application/xml" , TestJson ) ]
5153 [ TestCase ( ContentTypes . TextPlain , null ) ]
5254 [ TestCase ( ContentTypes . TextPlain , "" ) ]
53- public async Task ConvertAsync_ReturnsExpectedResult_WithSerializedString ( string contentType , string value )
55+ public void Convert_ReturnsExpectedResult_WithSerializedString ( string contentType , string value )
5456 {
5557 ServiceBusReceivedMessage message = ServiceBusModelFactory . ServiceBusReceivedMessage (
5658 body : value == null ? null : new BinaryData ( value ) ,
5759 contentType : contentType ) ;
5860
5961 MessageToStringConverter converter = new MessageToStringConverter ( ) ;
60- string result = await converter . ConvertAsync ( message , CancellationToken . None ) ;
62+ string result = converter . Convert ( message ) ;
6163 // A received message will never have a null body as a body section is required when sending even if it
6264 // is empty. This was true in Track 1 as well, but in Track 1 the actual body property could be null when
6365 // constructing the message, but in practice it wouldn't be null when receiving.
@@ -72,7 +74,7 @@ public async Task ConvertAsync_ReturnsExpectedResult_WithSerializedString(string
7274 }
7375
7476 [ Test ]
75- public async Task ConvertAsync_ReturnsExpectedResult_WithSerializedObject ( )
77+ public void Convert_ReturnsExpectedResult_WithSerializedObject ( )
7678 {
7779 byte [ ] bytes ;
7880 using ( MemoryStream ms = new MemoryStream ( ) )
@@ -84,10 +86,45 @@ public async Task ConvertAsync_ReturnsExpectedResult_WithSerializedObject()
8486 ServiceBusReceivedMessage message = ServiceBusModelFactory . ServiceBusReceivedMessage ( body : new BinaryData ( bytes ) ) ;
8587
8688 MessageToStringConverter converter = new MessageToStringConverter ( ) ;
87- string result = await converter . ConvertAsync ( message , CancellationToken . None ) ;
89+ string result = converter . Convert ( message ) ;
8890 Assert . AreEqual ( message . Body . ToString ( ) , result ) ;
8991 }
9092
93+ [ Test ]
94+ public void Convert_ValueBodyMessage_String ( )
95+ {
96+ ServiceBusReceivedMessage message = ServiceBusModelFactory . ServiceBusReceivedMessage ( ) ;
97+ message . GetRawAmqpMessage ( ) . Body = AmqpMessageBody . FromValue ( "value" ) ;
98+
99+ MessageToStringConverter converter = new MessageToStringConverter ( ) ;
100+ string result = converter . Convert ( message ) ;
101+ Assert . AreEqual ( "value" , result ) ;
102+ }
103+
104+ [ Test ]
105+ public void Convert_ValueBodyMessage_NonStringThrows ( )
106+ {
107+ ServiceBusReceivedMessage message = ServiceBusModelFactory . ServiceBusReceivedMessage ( ) ;
108+ message . GetRawAmqpMessage ( ) . Body = AmqpMessageBody . FromValue ( 5 ) ;
109+
110+ MessageToStringConverter converter = new MessageToStringConverter ( ) ;
111+ Assert . That (
112+ ( ) => converter . Convert ( message ) ,
113+ Throws . InstanceOf < NotSupportedException > ( ) ) ;
114+ }
115+
116+ [ Test ]
117+ public void Convert_SequenceBodyMessage_Throws ( )
118+ {
119+ ServiceBusReceivedMessage message = ServiceBusModelFactory . ServiceBusReceivedMessage ( ) ;
120+ message . GetRawAmqpMessage ( ) . Body = AmqpMessageBody . FromSequence ( new IList < object > [ ] { new object [ ] { "sequence" } } ) ;
121+
122+ MessageToStringConverter converter = new MessageToStringConverter ( ) ;
123+ Assert . That (
124+ ( ) => converter . Convert ( message ) ,
125+ Throws . InstanceOf < NotSupportedException > ( ) ) ;
126+ }
127+
91128 [ Serializable ]
92129 public class TestObject
93130 {
0 commit comments