public async Task DeserializeAsync_MissingTypeHeader_TypeReturned()
        {
            var rawMessage = new MemoryStream(Encoding.UTF8.GetBytes("{\"Content\":\"the message\"}"));
            var headers    = new MessageHeaderCollection();

            var serializer = new NewtonsoftJsonMessageSerializer <TestEventOne>();

            var(_, type) = await serializer
                           .DeserializeAsync(rawMessage, headers, MessageSerializationContext.Empty);

            type.Should().Be(typeof(TestEventOne));
        }
        public async Task DeserializeAsync_EmptyStream_TypeReturned()
        {
            var serializer = new NewtonsoftJsonMessageSerializer <TestEventOne>();

            var(_, type) = await serializer
                           .DeserializeAsync(
                new MemoryStream(),
                new MessageHeaderCollection(),
                MessageSerializationContext.Empty);

            type.Should().Be(typeof(TestEventOne));
        }
        public async Task DeserializeAsync_EmptyStream_NullObjectReturned()
        {
            var serializer = new NewtonsoftJsonMessageSerializer <TestEventOne>();

            var(deserializedObject, _) = await serializer
                                         .DeserializeAsync(
                new MemoryStream(),
                new MessageHeaderCollection(),
                MessageSerializationContext.Empty);

            deserializedObject.Should().BeNull();
        }
        public async Task DeserializeAsync_MissingTypeHeader_Deserialized()
        {
            var rawMessage = new MemoryStream(Encoding.UTF8.GetBytes("{\"Content\":\"the message\"}"));
            var headers    = new MessageHeaderCollection();

            var serializer = new NewtonsoftJsonMessageSerializer <TestEventOne>();

            var(deserializedObject, _) = await serializer
                                         .DeserializeAsync(rawMessage, headers, MessageSerializationContext.Empty);

            deserializedObject.Should().NotBeNull();
            deserializedObject.Should().BeOfType <TestEventOne>();
            deserializedObject.As <TestEventOne>().Content.Should().Be("the message");
        }
        public async Task DeserializeAsync_MessageWithIncompleteTypeHeader_Deserialized()
        {
            var serializer = new NewtonsoftJsonMessageSerializer();
            var rawMessage = new MemoryStream(Encoding.UTF8.GetBytes("{\"Content\":\"the message\"}"));

            var(deserializedObject, _) = await serializer
                                         .DeserializeAsync(rawMessage, TestEventOneMessageTypeHeaders, MessageSerializationContext.Empty);

            var message = deserializedObject as TestEventOne;

            message.Should().NotBeNull();
            message.Should().BeOfType <TestEventOne>();
            message.As <TestEventOne>().Content.Should().Be("the message");
        }
        public void DeserializeAsync_BadTypeHeader_ExceptionThrown()
        {
            var rawMessage = new MemoryStream(Encoding.UTF8.GetBytes("{\"Content\":\"the message\"}"));
            var headers    = new MessageHeaderCollection
            {
                {
                    "x-message-type",
                    "Bad.TestEventOne, Silverback.Integration.Newtonsoft.Tests"
                }
            };
            var serializer = new NewtonsoftJsonMessageSerializer();

            Func <Task> act = async() => await serializer
                              .DeserializeAsync(rawMessage, headers, MessageSerializationContext.Empty);

            act.Should().Throw <TypeLoadException>();
        }
        public async Task SerializeDeserializeAsync_Message_CorrectlyDeserialized()
        {
            var message = new TestEventOne {
                Content = "the message"
            };
            var headers = new MessageHeaderCollection();

            var serializer = new NewtonsoftJsonMessageSerializer();

            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 NewtonsoftJsonMessageSerializer <TestEventOne>();

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

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

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

            var message2 = deserialized as TestEventOne;

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