示例#1
0
 public async Task SerializeUsingNewSerializer()
 {
     await _serializer.SerializeAsync(
         WeekWhetherForecastsEvent.Sample,
         _messageHeaderCollection,
         _messageSerializationContext);
 }
示例#2
0
        public async Task SerializeAsJson_Action_PayloadSerialized()
        {
            var message = new TestEventOne {
                Content = "Hello MQTT!"
            };
            var serializer = new JsonMessageSerializer <TestEventOne>
            {
                Options = new JsonSerializerOptions
                {
                    WriteIndented = true
                }
            };
            var messageBytes = (await serializer.SerializeAsync(
                                    message,
                                    new MessageHeaderCollection(),
                                    MessageSerializationContext.Empty)).ReadAll();
            var builder = new MqttLastWillMessageBuilder();

            builder
            .ProduceTo("testaments")
            .SerializeAsJson(
                serializerBuilder => serializerBuilder
                .WithOptions(
                    new JsonSerializerOptions
            {
                WriteIndented = true
            }))
            .Message(message);

            var willMessage = builder.Build();

            willMessage.Payload.Should().NotBeNullOrEmpty();
            willMessage.Payload.Should().BeEquivalentTo(messageBytes);
        }
        public async Task SerializeAsync_NullMessage_NullReturned()
        {
            var serializer = new JsonMessageSerializer <TestEventOne>();

            var serialized = await serializer
                             .SerializeAsync(null, new MessageHeaderCollection(), MessageSerializationContext.Empty);

            serialized.Should().BeNull();
        }
        public async Task SerializeAsync_ByteArray_ReturnedUnmodified()
        {
            var messageBytes = Encoding.UTF8.GetBytes("test");

            var serializer = new JsonMessageSerializer <TestEventOne>();

            var serialized = await serializer.SerializeAsync(
                messageBytes,
                new MessageHeaderCollection(),
                MessageSerializationContext.Empty);

            serialized.ReadAll().Should().BeEquivalentTo(messageBytes);
        }
        public async Task SerializeAsync_Stream_ReturnedUnmodified()
        {
            var messageStream = new MemoryStream(Encoding.UTF8.GetBytes("test"));

            var serializer = new JsonMessageSerializer <TestEventOne>();

            var serialized = await serializer.SerializeAsync(
                messageStream,
                new MessageHeaderCollection(),
                MessageSerializationContext.Empty);

            serialized.Should().BeSameAs(messageStream);
        }
        public async Task SerializeAsync_Message_TypeHeaderAdded()
        {
            var message = new TestEventOne {
                Content = "the message"
            };
            var headers = new MessageHeaderCollection();

            var serializer = new JsonMessageSerializer();

            await serializer.SerializeAsync(message, headers, MessageSerializationContext.Empty);

            headers.Should().ContainEquivalentOf(
                new MessageHeader("x-message-type", typeof(TestEventOne).AssemblyQualifiedName));
        }
        public async Task SerializeAsync_WithDefaultSettings_CorrectlySerialized()
        {
            var message = new TestEventOne {
                Content = "the message"
            };
            var headers = new MessageHeaderCollection();

            var serializer = new JsonMessageSerializer();

            var serialized = await serializer.SerializeAsync(message, headers, MessageSerializationContext.Empty);

            var expected = Encoding.UTF8.GetBytes("{\"Content\":\"the message\"}");

            serialized.ReadAll().Should().BeEquivalentTo(expected);
        }
        public async Task SerializeDeserializeAsync_Message_CorrectlyDeserialized()
        {
            var message = new TestEventOne {
                Content = "the message"
            };
            var headers = new MessageHeaderCollection();

            var serializer = new JsonMessageSerializer();

            var serialized = await serializer.SerializeAsync(message, headers, MessageSerializationContext.Empty);

            var(deserialized, _) = await serializer
                                   .DeserializeAsync(serialized, headers, MessageSerializationContext.Empty);

            var message2 = deserialized as TestEventOne;

            message2.Should().NotBeNull();
            message2.Should().BeEquivalentTo(message);
        }
        public async Task SerializeDeserializeAsync_Message_CorrectlyDeserialized()
        {
            var message = new TestEventOne {
                Content = "the message"
            };
            var headers = new MessageHeaderCollection();

            var serializer = new JsonMessageSerializer <TestEventOne>();

            var serialized = (await serializer.SerializeAsync(message, headers, MessageSerializationContext.Empty)) !;

            Encoding.UTF8.GetString(serialized.ReadAll() !).Should().NotContain("TestEventOne");

            serialized.Position = 0;

            var(deserialized, _) = await serializer
                                   .DeserializeAsync(serialized, headers, MessageSerializationContext.Empty);

            var message2 = deserialized as TestEventOne;

            message2.Should().NotBeNull();
            message2.Should().BeEquivalentTo(message);
        }