public void ToMessage()
        {
            var converter = new NewtonJsonMessageConverter();
            var payload   = new MyBean
            {
                String   = "Foo",
                Number   = 42,
                Fraction = 42F,
                Array    = new string[] { "Foo", "Bar" },
                Bool     = true,
                Bytes    = new byte[] { 0x1, 0x2 }
            };

            var message = converter.ToMessage(payload, null);

            var actual = Encoding.UTF8.GetString((byte[])message.Payload);

            Assert.Contains("\"string\":\"Foo\"", actual);
            Assert.Contains("\"number\":42", actual);
            Assert.Contains("\"fraction\":42.0", actual);
            Assert.Contains("\"array\":[\"Foo\",\"Bar\"]", actual);
            Assert.Contains("\"bool\":true", actual);
            Assert.Contains("\"bytes\":\"AQI=\"", actual);
            Assert.Equal(new MimeType("application", "json", Encoding.UTF8), message.Headers[MessageHeaders.CONTENT_TYPE]);
        }
        public void DefaultConstructor()
        {
            var converter = new NewtonJsonMessageConverter();

            Assert.Contains(new MimeType("application", "json", Encoding.UTF8), converter.SupportedMimeTypes);
            Assert.Equal(MissingMemberHandling.Ignore, converter.Settings.MissingMemberHandling);
        }
        public void MimetypeParametrizedConstructor()
        {
            var mimetype  = new MimeType("application", "xml", Encoding.UTF8);
            var converter = new NewtonJsonMessageConverter(mimetype);

            Assert.Contains(mimetype, converter.SupportedMimeTypes);
            Assert.Equal(MissingMemberHandling.Ignore, converter.Settings.MissingMemberHandling);
        }
        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 GetIMessageGenericType()
 {
     Assert.Null(NewtonJsonMessageConverter.GetIMessageGenericType(typeof(T1)));
     Assert.Equal(typeof(MyBean), NewtonJsonMessageConverter.GetIMessageGenericType(typeof(T2)));
     Assert.Equal(typeof(MyBean), NewtonJsonMessageConverter.GetIMessageGenericType(typeof(T3 <MyBean>)));
     Assert.Equal(typeof(MyBean), NewtonJsonMessageConverter.GetIMessageGenericType(typeof(T4 <MyBean>)));
     Assert.Equal(typeof(MyBean), NewtonJsonMessageConverter.GetIMessageGenericType(typeof(IMessage <MyBean>)));
     Assert.Equal(typeof(MyBean), NewtonJsonMessageConverter.GetIMessageGenericType(typeof(IMyInterface <MyBean>)));
     Assert.Null(NewtonJsonMessageConverter.GetIMessageGenericType(typeof(IMessage)));
 }
        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 ToMessageUtf16String()
        {
            var converter = new NewtonJsonMessageConverter();

            converter.SerializedPayloadClass = typeof(string);
            var encoding    = new UnicodeEncoding(true, false);
            var contentType = new MimeType("application", "json", encoding);
            IDictionary <string, object> map = new Dictionary <string, object>();

            map.Add(MessageHeaders.CONTENT_TYPE, contentType);
            var headers = new MessageHeaders(map);
            var payload = "H\u00e9llo W\u00f6rld";
            var message = converter.ToMessage(payload, headers);

            Assert.Equal("\"" + payload + "\"", message.Payload);
            Assert.Equal(contentType, message.Headers[MessageHeaders.CONTENT_TYPE]);
        }
        public void ToMessageUtf16()
        {
            var e           = Encoding.Default;
            var converter   = new NewtonJsonMessageConverter();
            var encoding    = new UnicodeEncoding(true, false);
            var contentType = new MimeType("application", "json", encoding);
            IDictionary <string, object> map = new Dictionary <string, object>();

            map.Add(MessageHeaders.CONTENT_TYPE, contentType);
            var headers  = new MessageHeaders(map);
            var payload  = "H\u00e9llo W\u00f6rld";
            var message  = converter.ToMessage(payload, headers);
            var actual   = encoding.GetString((byte[])message.Payload);
            var expected = "\"" + payload + "\"";

            Assert.Equal(expected, actual);
            Assert.Equal(contentType, message.Headers[MessageHeaders.CONTENT_TYPE]);
        }
        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);
        }