private void VerifyNonPrimitiveTypeRoundtrip(object value, string propertyName)
        {
            var properties = new[] { new ODataProperty {
                                         Name = propertyName, Value = value
                                     } };
            var entry = new ODataEntry()
            {
                TypeName = "NS.Student", Properties = properties
            };

            ODataMessageWriterSettings settings = new ODataMessageWriterSettings {
                Version = ODataVersion.V4
            };
            MemoryStream stream = new MemoryStream();

            using (ODataJsonLightOutputContext outputContext = new ODataJsonLightOutputContext(
                       ODataFormat.Json,
                       new NonDisposingStream(stream),
                       new ODataMediaType("application", "json"),
                       Encoding.UTF8,
                       settings,
                       /*writingResponse*/ false,
                       /*synchronous*/ true,
                       model,
                       /*urlResolver*/ null))
            {
                var jsonLightWriter = new ODataJsonLightWriter(outputContext, this.studentSet, this.studentInfo, /*writingFeed*/ false);
                jsonLightWriter.WriteStart(entry);
                jsonLightWriter.WriteEnd();
            }

            stream.Position = 0;
            object actualValue = null;

            using (ODataJsonLightInputContext inputContext = new ODataJsonLightInputContext(
                       ODataFormat.Json,
                       stream,
                       JsonLightUtils.JsonLightStreamingMediaType,
                       Encoding.UTF8,
                       new ODataMessageReaderSettings(),
                       /*readingResponse*/ false,
                       /*synchronous*/ true,
                       model,
                       /*urlResolver*/ null))
            {
                var jsonLightReader = new ODataJsonLightReader(inputContext, this.studentSet, this.studentInfo, /*readingFeed*/ false);
                while (jsonLightReader.Read())
                {
                    if (jsonLightReader.State == ODataReaderState.EntryEnd)
                    {
                        ODataEntry entryOut = jsonLightReader.Item as ODataEntry;
                        actualValue = entryOut.Properties.Single(p => p.Name == propertyName).ODataValue;
                    }
                }
            }

            TestUtils.AssertODataValueAreEqual(actualValue as ODataValue, value as ODataValue);
        }
        private static void WriteEntryAndValidatePayload(ODataResource entry, MemoryStream stream, ODataJsonLightWriter writer, string expectedPayload)
        {
            writer.WriteStart(entry);
            writer.WriteEnd();
            writer.Flush();
            stream.Seek(0, SeekOrigin.Begin);
            string payload = (new StreamReader(stream)).ReadToEnd();

            payload.Should().Be(expectedPayload);
        }
示例#3
0
        private void VerifyComplexRoundtrip(string propertyName, ODataResourceSet resourceSet, params ODataResource[] resources)
        {
            var nestedResourceInfo = new ODataNestedResourceInfo()
            {
                Name = propertyName, IsCollection = resourceSet != null
            };
            var entry = new ODataResource()
            {
                TypeName = "NS.Student"
            };

            ODataMessageWriterSettings settings = new ODataMessageWriterSettings {
                Version = ODataVersion.V4
            };
            MemoryStream stream = new MemoryStream();

            var messageInfoForWriter = new ODataMessageInfo
            {
                MessageStream = new NonDisposingStream(stream),
                MediaType     = new ODataMediaType("application", "json"),
                Encoding      = Encoding.UTF8,
                IsResponse    = false,
                IsAsync       = false,
                Model         = model
            };

            using (var outputContext = new ODataJsonLightOutputContext(messageInfoForWriter, settings))
            {
                var jsonLightWriter = new ODataJsonLightWriter(outputContext, this.studentSet, this.studentInfo, /*writingFeed*/ false);
                jsonLightWriter.WriteStart(entry);
                jsonLightWriter.WriteStart(nestedResourceInfo);
                if (resourceSet != null)
                {
                    jsonLightWriter.WriteStart(resourceSet);
                }

                foreach (var value in resources)
                {
                    jsonLightWriter.WriteStart(value);
                    jsonLightWriter.WriteEnd();
                }

                if (resourceSet != null)
                {
                    jsonLightWriter.WriteEnd();
                }

                jsonLightWriter.WriteEnd();
                jsonLightWriter.WriteEnd();
            }

            stream.Position = 0;
            List <ODataResource> actualResources = new List <ODataResource>();

            var messageInfoForReader = new ODataMessageInfo
            {
                Encoding      = Encoding.UTF8,
                IsResponse    = false,
                MediaType     = JsonLightUtils.JsonLightStreamingMediaType,
                IsAsync       = false,
                Model         = model,
                MessageStream = stream
            };

            using (var inputContext = new ODataJsonLightInputContext(messageInfoForReader, new ODataMessageReaderSettings()))
            {
                var jsonLightReader = new ODataJsonLightReader(inputContext, this.studentSet, this.studentInfo, /*readingFeed*/ false);
                while (jsonLightReader.Read())
                {
                    if (jsonLightReader.State == ODataReaderState.ResourceEnd)
                    {
                        actualResources.Add(jsonLightReader.Item as ODataResource);
                    }
                }
            }

            var count = actualResources.Count;

            actualResources.RemoveAt(count - 1);
            TestUtils.AssertODataResourceSetAreEqual(actualResources, resources.ToList());
        }
示例#4
0
        private void VerifyNonPrimitiveTypeRoundtrip(object value, string propertyName)
        {
            var properties = new[] { new ODataProperty {
                                         Name = propertyName, Value = value
                                     } };
            var entry = new ODataResource()
            {
                TypeName = "NS.Student", Properties = properties
            };

            var stream = new MemoryStream();

            var messageInfoForWriter = new ODataMessageInfo
            {
                MessageStream = new NonDisposingStream(stream),
                MediaType     = new ODataMediaType("application", "json"),
                Encoding      = Encoding.UTF8,
                IsResponse    = false,
                IsAsync       = false,
                Model         = model
            };

            var settings = new ODataMessageWriterSettings
            {
                Version     = ODataVersion.V4,
                Validations = ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType
            };

            using (var outputContext = new ODataJsonLightOutputContext(messageInfoForWriter, settings))
            {
                var jsonLightWriter = new ODataJsonLightWriter(outputContext, this.studentSet, this.studentInfo, /*writingFeed*/ false);
                jsonLightWriter.WriteStart(entry);
                jsonLightWriter.WriteEnd();
            }

            stream.Position = 0;
            object actualValue = null;

            var messageInfoForReader = new ODataMessageInfo
            {
                Encoding      = Encoding.UTF8,
                IsResponse    = false,
                MediaType     = JsonLightUtils.JsonLightStreamingMediaType,
                IsAsync       = false,
                Model         = model,
                MessageStream = stream
            };

            using (var inputContext = new ODataJsonLightInputContext(messageInfoForReader, new ODataMessageReaderSettings()))
            {
                var jsonLightReader = new ODataJsonLightReader(inputContext, this.studentSet, this.studentInfo, /*readingFeed*/ false);
                while (jsonLightReader.Read())
                {
                    if (jsonLightReader.State == ODataReaderState.ResourceEnd)
                    {
                        ODataResource entryOut = jsonLightReader.Item as ODataResource;
                        actualValue = entryOut.Properties.Single(p => p.Name == propertyName).ODataValue;
                    }
                }
            }

            TestUtils.AssertODataValueAreEqual(actualValue as ODataValue, value as ODataValue);
        }