public void FromMessageMatchingInstance()
        {
            var      myBean    = new MyBean();
            var      converter = new NewtonJsonMessageConverter();
            IMessage message   = MessageBuilder <MyBean> .WithPayload(myBean).Build();

            Assert.Same(myBean, converter.FromMessage(message, typeof(MyBean)));
        }
        public void FromMessageInvalidJson()
        {
            var      converter = new NewtonJsonMessageConverter();
            var      payload   = "FooBar";
            var      bytes     = Encoding.UTF8.GetBytes(payload);
            IMessage message   = MessageBuilder <byte[]> .WithPayload(bytes).Build();

            Assert.Throws <MessageConversionException>(() => converter.FromMessage <MyBean>(message));
        }
        public void FromMessageValidJsonWithUnknownProperty()
        {
            var converter = new NewtonJsonMessageConverter();
            var payload   = "{\"string\":\"string\",\"unknownProperty\":\"value\"}";
            var bytes     = Encoding.UTF8.GetBytes(payload);
            var message   = MessageBuilder.WithPayload(bytes).Build();
            var myBean    = converter.FromMessage <MyBean>(message);

            Assert.Equal("string", myBean.String);
        }
        public void FromMessageToMessageWithPojo()
        {
            var      converter = new NewtonJsonMessageConverter();
            var      payload   = "{\"string\":\"foo\"}";
            var      bytes     = Encoding.UTF8.GetBytes(payload);
            IMessage message   = MessageBuilder <byte[]> .WithPayload(bytes).Build();

            var info   = GetType().GetMethod("HandleMessage", BindingFlags.Instance | BindingFlags.NonPublic).GetParameters()[0];
            var actual = converter.FromMessage(message, typeof(MyBean), info);

            Assert.IsType <MyBean>(actual);
            Assert.Equal("foo", ((MyBean)actual).String);
        }
        public void FromMessageToList()
        {
            var      converter = new NewtonJsonMessageConverter();
            var      payload   = "[1, 2, 3, 4, 5, 6, 7, 8, 9]";
            var      bytes     = Encoding.UTF8.GetBytes(payload);
            IMessage message   = MessageBuilder <byte[]> .WithPayload(bytes).Build();

            var info   = GetType().GetMethod("HandleList", BindingFlags.Instance | BindingFlags.NonPublic).GetParameters()[0];
            var actual = converter.FromMessage(message, typeof(List <long>), info);

            Assert.NotNull(actual);
            Assert.Equal(new List <long>()
            {
                1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L
            }, actual);
        }
        public void FromMessageUntyped()
        {
            var converter = new NewtonJsonMessageConverter();
            var payload   = "{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],"
                            + "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
            var      bytes   = Encoding.UTF8.GetBytes(payload);
            IMessage message = MessageBuilder <byte[]> .WithPayload(bytes).Build();

            var actual = converter.FromMessage <Dictionary <string, object> >(message);

            Assert.Equal("Foo", actual["string"]);
            Assert.Equal(42L, actual["number"]);
            Assert.Equal(42D, (double)actual["fraction"]);
            Assert.Equal(new string[] { "Foo", "Bar" }, actual["array"]);
            Assert.Equal(true, actual["bool"]);
            Assert.Equal("AQI=", actual["bytes"]);
        }
        public void FromMessage()
        {
            var converter = new NewtonJsonMessageConverter();
            var payload   = "{" +
                            "\"bytes\":\"AQI=\"," +
                            "\"array\":[\"Foo\",\"Bar\"]," +
                            "\"number\":42," +
                            "\"string\":\"Foo\"," +
                            "\"bool\":true," +
                            "\"fraction\":42.0}";
            var bytes   = Encoding.UTF8.GetBytes(payload);
            var message = MessageBuilder.WithPayload(bytes).Build();
            var actual  = (MyBean)converter.FromMessage(message, typeof(MyBean));

            Assert.Equal("Foo", actual.String);
            Assert.Equal(42, actual.Number);
            Assert.Equal(42F, actual.Fraction);
            Assert.Equal(new string[] { "Foo", "Bar" }, actual.Array);
            Assert.True(actual.Bool);
            Assert.Equal(new byte[] { 0x1, 0x2 }, actual.Bytes);
        }