public async Task DeserializeAsync_NullMessage_NullObjectReturned()
        {
            var serializer = new JsonMessageSerializer <TestEventOne>();

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

            deserializedObject.Should().BeNull();
        }
        public async Task DeserializeAsync_NullMessage_TypeReturned()
        {
            var serializer = new JsonMessageSerializer <TestEventOne>();

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

            type.Should().Be(typeof(TestEventOne));
        }
        public async Task DeserializeAsync_MessageWithIncompleteTypeHeader_TypeReturned()
        {
            var serializer = new JsonMessageSerializer();
            var rawMessage = new MemoryStream(Encoding.UTF8.GetBytes("{\"Content\":\"the message\"}"));

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

            type.Should().Be(typeof(TestEventOne));
        }
        public void DeserializeAsync_MissingTypeHeader_ExceptionThrown()
        {
            var serializer = new JsonMessageSerializer();
            var rawMessage = new MemoryStream(Encoding.UTF8.GetBytes("{\"Content\":\"the message\"}"));
            var headers    = new MessageHeaderCollection();

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

            act.Should().Throw <MessageSerializerException>();
        }
        public async Task DeserializeAsync_EmptyStream_TypeReturned()
        {
            var serializer = new JsonMessageSerializer();

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

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

            var(deserializedObject, _) = await serializer
                                         .DeserializeAsync(
                new MemoryStream(),
                TestEventOneMessageTypeHeaders,
                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 JsonMessageSerializer <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 JsonMessageSerializer();
            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 rawContent = new MemoryStream(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 });
            var headers    = new MessageHeaderCollection
            {
                {
                    "x-message-type",
                    "Bad.TestEventOne, Silverback.Integration.Tests"
                }
            };
            var serializer = new JsonMessageSerializer();

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

            act.Should().Throw <TypeLoadException>();
        }
        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.Tests"
                }
            };
            var serializer = new JsonMessageSerializer();

            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 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);
        }