public void WriteObject_Throws_RootElementNameMissing()
        {
            ODataSerializerContext   writeContext = new ODataSerializerContext();
            ODataPrimitiveSerializer serializer   = new ODataPrimitiveSerializer();

            Assert.Throws <ArgumentException>(
                () => serializer.WriteObject(42, typeof(int), ODataTestUtil.GetMockODataMessageWriter(), writeContext),
                "The 'RootElementName' property is required on 'ODataSerializerContext'.\r\nParameter name: writeContext");
        }
        public void WriteObject_EdmPrimitives(object graph, string type, string value)
        {
            // Arrange
            ODataPrimitiveSerializer serializer   = new ODataPrimitiveSerializer();
            ODataSerializerContext   writecontext = new ODataSerializerContext()
            {
                RootElementName = "PropertyName",
                Model           = EdmCoreModel.Instance
            };

            ODataMessageWriterSettings settings = new ODataMessageWriterSettings
            {
                ODataUri = new ODataUri {
                    ServiceRoot = new Uri("http://any/"),
                }
            };

            MemoryStream       stream = new MemoryStream();
            ODataMessageWriter writer = new ODataMessageWriter(
                new ODataMessageWrapper(stream) as IODataResponseMessage, settings);

            string expect = "{\"@odata.context\":\"http://any/$metadata#" + type + "\",";

            if (type == "Edm.Null")
            {
                expect += "\"@odata.null\":" + value + "}";
            }
            else
            {
                expect += "\"value\":" + value + "}";
            }

            // Act
            serializer.WriteObject(graph, typeof(int), writer, writecontext);

            // Assert
            stream.Seek(0, SeekOrigin.Begin);
            string result = new StreamReader(stream).ReadToEnd();

            Assert.Equal(expect, result);
        }
Ejemplo n.º 3
0
        public void WriteObject_EdmPrimitives(object graph)
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));
            var serializer = new ODataPrimitiveSerializer();
            ODataSerializerContext writecontext = new ODataSerializerContext()
            {
                RootElementName = "PropertyName", Model = EdmCoreModel.Instance
            };

            ODataMessageWriterSettings settings = new ODataMessageWriterSettings
            {
                ODataUri = new ODataUri {
                    ServiceRoot = new Uri("http://any/"),
                }
            };
            ODataMessageWriter writer = new ODataMessageWriter(
                new ODataMessageWrapper(new MemoryStream()) as IODataResponseMessage,
                settings);

            Assert.DoesNotThrow(() => serializer.WriteObject(graph, typeof(int), writer, writecontext));
        }
        public void WriteObject_Throws_RootElementNameMissing()
        {
            ODataSerializerContext writeContext = new ODataSerializerContext();
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();

            Assert.Throws<ArgumentException>(
                () => serializer.WriteObject(42, typeof(int), ODataTestUtil.GetMockODataMessageWriter(), writeContext),
                "The 'RootElementName' property is required on 'ODataSerializerContext'.\r\nParameter name: writeContext");
        }
        public void WriteObject_EdmPrimitives(object graph)
        {
            IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));
            var serializer = new ODataPrimitiveSerializer();
            ODataSerializerContext writecontext = new ODataSerializerContext() { RootElementName = "PropertyName", Model = EdmCoreModel.Instance };

            ODataMessageWriterSettings settings = new ODataMessageWriterSettings
            {
                ODataUri = new ODataUri { ServiceRoot = new Uri("http://any/"), }
            };
            ODataMessageWriter writer = new ODataMessageWriter(
                new ODataMessageWrapper(new MemoryStream()) as IODataResponseMessage,
                settings);

            Assert.DoesNotThrow(() => serializer.WriteObject(graph, typeof(int), writer, writecontext));
        }
        public void Read_DateTimePrimitive(DateTimeOffset expected, DateTime value, TimeZoneInfo timeZoneInfo)
        {
            // Arrange
            IEdmModel model = CreateModel();

            HttpRequestMessage request = new HttpRequestMessage();
            request.SetConfiguration(new HttpConfiguration());
            if (timeZoneInfo != null)
            {
                request.GetConfiguration().SetTimeZoneInfo(timeZoneInfo);
            }
            else
            {
                request.GetConfiguration().SetTimeZoneInfo(TimeZoneInfo.Local);
            }

            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();
            ODataPrimitiveDeserializer deserializer = new ODataPrimitiveDeserializer();

            MemoryStream stream = new MemoryStream();
            ODataMessageWrapper message = new ODataMessageWrapper(stream);

            ODataMessageWriterSettings settings = new ODataMessageWriterSettings
            {
                ODataUri = new ODataUri { ServiceRoot = new Uri("http://any/"), }
            };
            settings.SetContentType(ODataFormat.Json);

            ODataMessageWriter messageWriter = new ODataMessageWriter(message as IODataResponseMessage, settings, model);
            ODataMessageReader messageReader = new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), model);
            ODataSerializerContext writeContext = new ODataSerializerContext { RootElementName = "Property", Model = model, Request = request };
            ODataDeserializerContext readContext = new ODataDeserializerContext { Model = model, Request = request };

            serializer.WriteObject(value, typeof(DateTimeOffset), messageWriter, writeContext);
            stream.Seek(0, SeekOrigin.Begin);

            // Act && Assert
            Assert.Equal(expected, deserializer.Read(messageReader, typeof(DateTimeOffset), readContext));
        }
        public void Read_MappedPrimitive(object obj, object expected)
        {
            // Arrange
            IEdmModel model = CreateModel();
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();
            ODataPrimitiveDeserializer deserializer = new ODataPrimitiveDeserializer();

            MemoryStream stream = new MemoryStream();
            ODataMessageWrapper message = new ODataMessageWrapper(stream);

            ODataMessageWriterSettings settings = new ODataMessageWriterSettings
            {
                ODataUri = new ODataUri { ServiceRoot = new Uri("http://any/"), }
            };
            settings.SetContentType(ODataFormat.Json);

            ODataMessageWriter messageWriter = new ODataMessageWriter(message as IODataResponseMessage, settings, model);
            ODataMessageReader messageReader = new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), model);
            ODataSerializerContext writeContext = new ODataSerializerContext { RootElementName = "Property", Model = model };
            ODataDeserializerContext readContext = new ODataDeserializerContext { Model = model };

            Type type = obj == null ? typeof(int) : expected.GetType();
            serializer.WriteObject(obj, type, messageWriter, writeContext);
            stream.Seek(0, SeekOrigin.Begin);

            // Act && Assert
            Assert.Equal(expected, deserializer.Read(messageReader, type, readContext));
        }
        public void WriteObject_EdmPrimitives(object graph, string type, string value)
        {
            // Arrange
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();
            ODataSerializerContext writecontext = new ODataSerializerContext()
            {
                RootElementName = "PropertyName",
                Model = EdmCoreModel.Instance
            };

            ODataMessageWriterSettings settings = new ODataMessageWriterSettings
            {
                ODataUri = new ODataUri { ServiceRoot = new Uri("http://any/"), }
            };

            MemoryStream stream = new MemoryStream();
            ODataMessageWriter writer = new ODataMessageWriter(
                new ODataMessageWrapper(stream) as IODataResponseMessage, settings);

            string expect = "{\"@odata.context\":\"http://any/$metadata#" + type + "\",";
            if (type == "Edm.Null")
            {
                expect += "\"@odata.null\":" + value + "}";
            }
            else
            {
                expect += "\"value\":" + value + "}";
            }

            // Act
            serializer.WriteObject(graph, typeof(int), writer, writecontext);

            // Assert
            stream.Seek(0, SeekOrigin.Begin);
            string result = new StreamReader(stream).ReadToEnd();
            Assert.Equal(expect, result);
        }
        public void Read_Primitive(object obj)
        {
            // Arrange
            IEdmModel model = EdmCoreModel.Instance;
            ODataPrimitiveSerializer serializer = new ODataPrimitiveSerializer();
            ODataPrimitiveDeserializer deserializer = new ODataPrimitiveDeserializer();

            MemoryStream stream = new MemoryStream();
            ODataMessageWrapper message = new ODataMessageWrapper(stream);
            ODataMessageWriterSettings settings = new ODataMessageWriterSettings();
            settings.SetServiceDocumentUri(new Uri("http://any/"));
            settings.SetContentType(ODataFormat.Atom);
            ODataMessageWriter messageWriter = new ODataMessageWriter(message as IODataResponseMessage, settings, model);
            ODataMessageReader messageReader = new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), model);
            ODataSerializerContext writeContext = new ODataSerializerContext { RootElementName = "Property", Model = model };
            ODataDeserializerContext readContext = new ODataDeserializerContext { Model = model };

            serializer.WriteObject(obj, typeof(int), messageWriter, writeContext);
            stream.Seek(0, SeekOrigin.Begin);

            // Act & Assert
            Assert.Equal(obj, deserializer.Read(messageReader, typeof(int), readContext));
        }