Пример #1
0
        public void WriteObject_Throws_RootElementNameMissing()
        {
            ODataSerializerContext   writeContext = new ODataSerializerContext();
            ODataPrimitiveSerializer serializer   = new ODataPrimitiveSerializer();

#if NETCOREAPP3_1
            ExceptionAssert.Throws <ArgumentException>(
                () => serializer.WriteObject(42, typeof(int), ODataTestUtil.GetMockODataMessageWriter(), writeContext),
                "The 'RootElementName' property is required on 'ODataSerializerContext'. (Parameter 'writeContext')");
#else
            ExceptionAssert.Throws <ArgumentException>(
                () => serializer.WriteObject(42, typeof(int), ODataTestUtil.GetMockODataMessageWriter(), writeContext),
                "The 'RootElementName' property is required on 'ODataSerializerContext'.\r\nParameter name: writeContext");
#endif
        }
        public void Read_MappedPrimitive(object obj, object expected)
        {
            // Arrange
            IEdmPrimitiveTypeReference primitive = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));

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

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

            serializer.WriteObject(
                obj,
                new ODataMessageWriter(message as IODataResponseMessage, new ODataMessageWriterSettings(), EdmCoreModel.Instance),
                new ODataSerializerContext {
                ServiceOperationName = "Property"
            });
            stream.Seek(0, SeekOrigin.Begin);

            // Act && Assert
            Assert.Equal(
                expected,
                deserializer.Read(
                    new ODataMessageReader(message as IODataResponseMessage, new ODataMessageReaderSettings(), EdmCoreModel.Instance),
                    new ODataDeserializerContext()));
        }
        public void Read_MappedPrimitive(object obj, object expected)
        {
            // Arrange
            IEdmPrimitiveTypeReference primitive = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(int));
            IEdmModel model = EdmCoreModel.Instance;
            ODataPrimitiveSerializer   serializer   = new ODataPrimitiveSerializer();
            ODataPrimitiveDeserializer deserializer = new ODataPrimitiveDeserializer();

            MemoryStream           stream        = new MemoryStream();
            ODataMessageWrapper    message       = new ODataMessageWrapper(stream);
            ODataMessageWriter     messageWriter = new ODataMessageWriter(message as IODataResponseMessage, new ODataMessageWriterSettings(), 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(expected, deserializer.Read(messageReader, typeof(int), readContext));
        }
        public void WriteObject_Throws_RootElementNameMissing()
        {
            // Arrange
            ODataSerializerContext   writeContext = new ODataSerializerContext();
            ODataPrimitiveSerializer serializer   = new ODataPrimitiveSerializer();

            // Act & Assert
            ExceptionAssert.Throws <ArgumentException>(
                () => serializer.WriteObject(42, typeof(int), ODataTestUtil.GetMockODataMessageWriter(), writeContext),
                "The 'RootElementName' property is required on 'ODataSerializerContext'. (Parameter 'writeContext')");
        }
        public void Read_DateTimePrimitive(DateTimeOffset expected, DateTime value, TimeZoneInfo timeZoneInfo)
        {
            // Arrange
            IEdmModel model = CreateModel();

            var config  = RoutingConfigurationFactory.CreateWithRootContainer("OData");
            var request = RequestFactory.Create(config, "OData");

            if (timeZoneInfo != null)
            {
                config.SetTimeZoneInfo(timeZoneInfo);
            }
            else
            {
                config.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 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, string edmType, string value)
        {
            // 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) : obj.GetType();

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

            // Act & Assert
            Assert.NotNull(edmType);
            Assert.NotNull(value);
            Assert.Equal(obj, deserializer.Read(messageReader, type, readContext));
        }