public void Equals_SameSettings_TrueReturned()
        {
            var serializer1 = new NewtonsoftJsonMessageSerializer <TestEventOne>
            {
                Settings = new JsonSerializerSettings
                {
                    MaxDepth          = 42,
                    NullValueHandling = NullValueHandling.Ignore
                }
            };

            var serializer2 = new NewtonsoftJsonMessageSerializer <TestEventOne>
            {
                Settings = new JsonSerializerSettings
                {
                    MaxDepth          = 42,
                    NullValueHandling = NullValueHandling.Ignore
                }
            };

            // ReSharper disable once EqualExpressionComparison
            var result = Equals(serializer1, serializer2);

            result.Should().BeTrue();
        }
Esempio n. 2
0
        public async Task SerializeUsing_NewtonsoftJsonSerializer_PayloadSerialized()
        {
            var message = new TestEventOne {
                Content = "Hello MQTT!"
            };
            var serializer = new NewtonsoftJsonMessageSerializer <TestEventOne>
            {
                Encoding = MessageEncoding.Unicode,
                Settings = new JsonSerializerSettings
                {
                    Formatting = Formatting.Indented
                }
            };
            var messageBytes = (await serializer.SerializeAsync(
                                    message,
                                    new MessageHeaderCollection(),
                                    MessageSerializationContext.Empty)).ReadAll();
            var builder = new MqttLastWillMessageBuilder();

            builder
            .ProduceTo("testaments")
            .SerializeUsing(serializer)
            .Message(message);

            var willMessage = builder.Build();

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

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

            serialized.Should().BeNull();
        }
        public async Task DeserializeAsync_NullMessage_NullObjectReturned()
        {
            var serializer = new NewtonsoftJsonMessageSerializer();

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

            deserializedObject.Should().BeNull();
        }
        public void Equals_SameInstance_TrueReturned()
        {
            var serializer = new NewtonsoftJsonMessageSerializer();

            // ReSharper disable once EqualExpressionComparison
            var result = Equals(serializer, serializer);

            result.Should().BeTrue();
        }
        public async Task DeserializeAsync_NullMessage_TypeReturned()
        {
            var serializer = new NewtonsoftJsonMessageSerializer <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 NewtonsoftJsonMessageSerializer();
            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 Equals_DefaultSettings_TrueReturned()
        {
            var serializer1 = new NewtonsoftJsonMessageSerializer <TestEventOne>();
            var serializer2 = new NewtonsoftJsonMessageSerializer <TestEventOne>();

            // ReSharper disable once EqualExpressionComparison
            var result = Equals(serializer1, serializer2);

            result.Should().BeTrue();
        }
        public void DeserializeAsync_MissingTypeHeader_ExceptionThrown()
        {
            var serializer = new NewtonsoftJsonMessageSerializer();
            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_NullObjectReturned()
        {
            var serializer = new NewtonsoftJsonMessageSerializer <TestEventOne>();

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

            deserializedObject.Should().BeNull();
        }
        public async Task DeserializeAsync_EmptyStream_TypeReturned()
        {
            var serializer = new NewtonsoftJsonMessageSerializer();

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

            type.Should().Be(typeof(TestEventOne));
        }
        public async Task SerializeAsync_ByteArray_ReturnedUnmodified()
        {
            var messageBytes = Encoding.UTF8.GetBytes("test");

            var serializer = new NewtonsoftJsonMessageSerializer <TestEventOne>();

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

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

            var serializer = new NewtonsoftJsonMessageSerializer();

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

            serialized.ReadAll().Should().BeEquivalentTo(Encoding.UTF8.GetBytes("{\"Content\":\"the message\"}"));
        }
        public async Task SerializeAsync_Stream_ReturnedUnmodified()
        {
            var stream = new MemoryStream(Encoding.UTF8.GetBytes("test"));

            var serializer = new NewtonsoftJsonMessageSerializer <TestEventOne>();

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

            serialized.Should().BeSameAs(stream);
        }
        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 async Task SerializeAsync_Message_TypeHeaderAdded()
        {
            var message = new TestEventOne {
                Content = "the message"
            };
            var headers = new MessageHeaderCollection();

            var serializer = new NewtonsoftJsonMessageSerializer();

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

            headers.Should().ContainEquivalentOf(
                new MessageHeader("x-message-type", typeof(TestEventOne).AssemblyQualifiedName));
        }
        public void UsesDefaultSerializerSettings_WhenNoSettingsProvided()
        {
            var testee = new NewtonsoftJsonMessageSerializer();

            var messageNotSerializableByDefaultConverters = Message.Create("Text");

            using (var target = new MemoryStream())
            {
                testee.Serialize(messageNotSerializableByDefaultConverters, target);
                // ReSharper disable once AccessToDisposedClosure delegate is executed inside the using clause
                Action act = () => testee.Deserialize(target, typeof(Message));

                AssertThatDefaultSerializerSettingsAreUsed(act);
            }
        }
        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 void UsesProvidedSerializerSettings()
        {
            var settings = new JsonSerializerSettings
            {
                Converters = new List <JsonConverter> {
                    new MessageConverter()
                }
            };

            var testee = new NewtonsoftJsonMessageSerializer(() => settings);

            var message = Message.Create("Text");

            using (var target = new MemoryStream())
            {
                testee.Serialize(message, target);
                var result = (Message)testee.Deserialize(target, typeof(Message));

                result.ShouldBeEquivalentTo(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);
        }
        public void Equals_DifferentSettings_FalseReturned()
        {
            var serializer1 = new NewtonsoftJsonMessageSerializer
            {
                Settings = new JsonSerializerSettings
                {
                    MaxDepth          = 42,
                    NullValueHandling = NullValueHandling.Ignore
                }
            };

            var serializer2 = new NewtonsoftJsonMessageSerializer
            {
                Settings = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Include
                }
            };

            // ReSharper disable once EqualExpressionComparison
            var result = Equals(serializer1, serializer2);

            result.Should().BeFalse();
        }