/// <summary>
        /// Instantiates a new Serializer class and calls WriteEntry method on it.
        /// </summary>
        /// <param name="dataServiceContext"></param>
        /// <returns></returns>
        private static Person SetupSerializerAndCallWriteEntry(DataServiceContext dataServiceContext)
        {
            Person  person  = new Person();
            Address address = new Address();
            Car     car1    = new Car();

            person.Cars.Add(car1);
            person.HomeAddress = address;

            dataServiceContext.AttachTo("Cars", car1);
            dataServiceContext.AttachTo("Addresses", address);

            var requestInfo      = new RequestInfo(dataServiceContext);
            var serializer       = new Serializer(requestInfo);
            var headers          = new HeaderCollection();
            var clientModel      = new ClientEdmModel(ODataProtocolVersion.V4);
            var entityDescriptor = new EntityDescriptor(clientModel);

            entityDescriptor.State  = EntityStates.Added;
            entityDescriptor.Entity = person;
            var requestMessageArgs         = new BuildingRequestEventArgs("POST", new Uri("http://www.foo.com/Northwind"), headers, entityDescriptor, HttpStack.Auto);
            var linkDescriptors            = new LinkDescriptor[] { new LinkDescriptor(person, "Cars", car1, clientModel), new LinkDescriptor(person, "HomeAddress", address, clientModel) };
            var odataRequestMessageWrapper = ODataRequestMessageWrapper.CreateRequestMessageWrapper(requestMessageArgs, requestInfo);

            serializer.WriteEntry(entityDescriptor, linkDescriptors, odataRequestMessageWrapper);
            return(person);
        }
Ejemplo n.º 2
0
        private ODataRequestMessageWrapper CreateRequestMessageForPost(RequestInfo requestInfo)
        {
            HeaderCollection headers = new HeaderCollection();

            headers.SetHeader("Content-Type", "application/json;odata.metadata=minimal");

            return(ODataRequestMessageWrapper.CreateRequestMessageWrapper(
                       new BuildingRequestEventArgs("POST", new Uri("http://odataService/"), headers, null, HttpStack.Auto), requestInfo));
        }
        public void SerializeEnity_NullableEnumProperty()
        {
            MyEntity1 myEntity1 = new MyEntity1()
            {
                ID                 = 2,
                MyColorValue       = null,
                MyFlagsColorValue  = MyFlagsColor.Blue,
                ComplexValue1Value = new ComplexValue1()
                {
                    MyColorValue = MyColor.Green, MyFlagsColorValue = MyFlagsColor.Red
                },
                MyFlagsColorCollection1 = new List <MyFlagsColor>()
                {
                    MyFlagsColor.Blue, MyFlagsColor.Red, MyFlagsColor.Red
                },
                MyColorCollection = new List <MyColor?> {
                    MyColor.Green, null
                }
            };

            DataServiceContext dataServiceContext = new DataServiceContext(new Uri("http://www.odata.org/service.svc"));

            dataServiceContext.EnableAtom = true;
            dataServiceContext.Format.UseAtom();
            dataServiceContext.AttachTo("MyEntitySet1", myEntity1);

            var requestInfo      = new RequestInfo(dataServiceContext);
            var serializer       = new Serializer(requestInfo);
            var headers          = new HeaderCollection();
            var clientModel      = new ClientEdmModel(ODataProtocolVersion.V4);
            var entityDescriptor = new EntityDescriptor(clientModel);

            entityDescriptor.State  = EntityStates.Added;
            entityDescriptor.Entity = myEntity1;
            var requestMessageArgs         = new BuildingRequestEventArgs("POST", new Uri("http://www.foo.com/Northwind"), headers, entityDescriptor, HttpStack.Auto);
            var linkDescriptors            = new LinkDescriptor[] { };
            var odataRequestMessageWrapper = ODataRequestMessageWrapper.CreateRequestMessageWrapper(requestMessageArgs, requestInfo);

            serializer.WriteEntry(entityDescriptor, linkDescriptors, odataRequestMessageWrapper);

            // read result:
            MemoryStream stream = (MemoryStream)(odataRequestMessageWrapper.CachedRequestStream.Stream);

            stream.Position = 0;

            string payload = (new StreamReader(stream)).ReadToEnd();

            payload = Regex.Replace(payload, "<updated>[^<]*</updated>", "");
            payload.Should().Be(
                "{\"ComplexValue1Value\":{\"MyColorValue\":\"Green\",\"MyFlagsColorValue\":\"Red\",\"StringValue\":null},\"ID\":2,\"MyColorCollection\":[\"Green\",null],\"MyColorValue\":null,\"MyFlagsColorCollection1\":[\"Blue\",\"Red\",\"Red\"],\"MyFlagsColorValue\":\"Blue\"}");
        }
        public void SerializeEnity_TwoNavigationLinksInJsonFormat()
        {
            var person = new Person
            {
                ID   = 100,
                Name = "Bing",
            };

            var car1 = new Car {
                ID = 1001
            };
            var car2 = new Car {
                ID = 1002
            };

            DataServiceContext dataServiceContext = new DataServiceContext(new Uri("http://www.odata.org/service.svc"));

            dataServiceContext.AttachTo("Persons", person);
            dataServiceContext.AttachTo("Cars", car1);
            dataServiceContext.AttachTo("Cars", car2);
            dataServiceContext.AddLink(person, "Cars", car1);
            dataServiceContext.AddLink(person, "Cars", car2);

            var requestInfo      = new RequestInfo(dataServiceContext);
            var serializer       = new Serializer(requestInfo);
            var headers          = new HeaderCollection();
            var clientModel      = new ClientEdmModel(ODataProtocolVersion.V4);
            var entityDescriptor = new EntityDescriptor(clientModel);

            entityDescriptor.State    = EntityStates.Added;
            entityDescriptor.Entity   = person;
            entityDescriptor.EditLink = new Uri("http://www.foo.com/custom");
            var requestMessageArgs         = new BuildingRequestEventArgs("POST", new Uri("http://www.foo.com/Northwind"), headers, entityDescriptor, HttpStack.Auto);
            var linkDescriptors            = new LinkDescriptor[] { new LinkDescriptor(person, "Cars", car1, clientModel), new LinkDescriptor(person, "Cars", car2, clientModel) };
            var odataRequestMessageWrapper = ODataRequestMessageWrapper.CreateRequestMessageWrapper(requestMessageArgs, requestInfo);

            serializer.WriteEntry(entityDescriptor, linkDescriptors, odataRequestMessageWrapper);

            // read result:
            MemoryStream stream = (MemoryStream)(odataRequestMessageWrapper.CachedRequestStream.Stream);

            stream.Position = 0;

            string payload = (new StreamReader(stream)).ReadToEnd();

            payload.Should().Be(
                "{\"ID\":100,\"Name\":\"Bing\",\"[email protected]\":[\"http://www.odata.org/service.svc/Cars(1001)\",\"http://www.odata.org/service.svc/Cars(1002)\"]}");
        }
            internal List <Stream> GetChangeStream()
            {
                List <Stream>    stream  = new List <Stream>();
                HeaderCollection headers = new HeaderCollection();

                headers.SetHeader("Content-Type", "application/json;odata.metadata=minimal");
                for (int i = 0; i < this.ChangedEntries.Count; ++i)
                {
                    ODataRequestMessageWrapper requestMessage = ODataRequestMessageWrapper.CreateRequestMessageWrapper(
                        new BuildingRequestEventArgs("GET", new Uri("http://service.svc/randomuri"), headers, null, HttpStack.Auto),
                        this.RequestInfo);
                    this.CreateChangeData(i, requestMessage);
                    stream.Add(requestMessage.CachedRequestStream.Stream);
                }

                return(stream);
            }
        private string SerializeEntity <T>(string entitySetName, T entityObject)
        {
            this.context.AddObject(entitySetName, entityObject);
            var requestInfo      = new RequestInfo(this.context);
            var serializer       = new Serializer(requestInfo);
            var headers          = new HeaderCollection();
            var entityDescriptor = new EntityDescriptor(this.clientEdmModel);

            entityDescriptor.State  = EntityStates.Added;
            entityDescriptor.Entity = entityObject;
            var requestMessageArgs         = new BuildingRequestEventArgs("POST", new Uri("http://tempuri.org"), headers, entityDescriptor, HttpStack.Auto);
            var odataRequestMessageWrapper = ODataRequestMessageWrapper.CreateRequestMessageWrapper(requestMessageArgs, requestInfo);
            var linkDescriptors            = new LinkDescriptor[0];

            serializer.WriteEntry(entityDescriptor, linkDescriptors, odataRequestMessageWrapper);

            var stream       = (MemoryStream)odataRequestMessageWrapper.CachedRequestStream.Stream;
            var streamReader = new StreamReader(stream);
            var body         = streamReader.ReadToEnd();

            return(body);
        }
        public void SerializeEnity_EnumProperty()
        {
            MyEntity1 myEntity1 = new MyEntity1()
            {
                ID                 = 2,
                MyColorValue       = MyColor.Yellow,
                MyFlagsColorValue  = MyFlagsColor.Blue,
                ComplexValue1Value = new ComplexValue1()
                {
                    MyColorValue = MyColor.Green, MyFlagsColorValue = MyFlagsColor.Red
                },
                MyFlagsColorCollection1 = new List <MyFlagsColor>()
                {
                    MyFlagsColor.Blue, MyFlagsColor.Red, MyFlagsColor.Red
                },
                MyColorCollection = new List <MyColor?>()
            };

            DataServiceContext dataServiceContext = new DataServiceContext(new Uri("http://www.odata.org/service.svc"));

            dataServiceContext.EnableAtom = true;
            dataServiceContext.Format.UseAtom();
            dataServiceContext.AttachTo("MyEntitySet1", myEntity1);

            var requestInfo = new RequestInfo(dataServiceContext);
            var serializer  = new Serializer(requestInfo);
            var headers     = new HeaderCollection();

            headers.SetHeader("Content-Type", "application/atom+xml;odata.metadata=minimal");
            var clientModel      = new ClientEdmModel(ODataProtocolVersion.V4);
            var entityDescriptor = new EntityDescriptor(clientModel);

            entityDescriptor.State  = EntityStates.Added;
            entityDescriptor.Entity = myEntity1;
            var requestMessageArgs         = new BuildingRequestEventArgs("POST", new Uri("http://www.foo.com/Northwind"), headers, entityDescriptor, HttpStack.Auto);
            var linkDescriptors            = new LinkDescriptor[] { };
            var odataRequestMessageWrapper = ODataRequestMessageWrapper.CreateRequestMessageWrapper(requestMessageArgs, requestInfo);

            serializer.WriteEntry(entityDescriptor, linkDescriptors, odataRequestMessageWrapper);

            // read result:
            MemoryStream stream = (MemoryStream)(odataRequestMessageWrapper.CachedRequestStream.Stream);

            stream.Position = 0;

            string payload = (new StreamReader(stream)).ReadToEnd();

            payload = Regex.Replace(payload, "<updated>[^<]*</updated>", "");
            payload.Should().Be(
                "<?xml version=\"1.0\" encoding=\"utf-8\"?><entry xmlns=\"http://www.w3.org/2005/Atom\" " +
                "xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\" " +
                "xmlns:georss=\"http://www.georss.org/georss\" xmlns:gml=\"http://www.opengis.net/gml\">" +
                "<id />" +
                "<title />" +
                //"<updated>2013-11-11T19:29:54Z</updated>" +
                "<author><name /></author>" +
                "<content type=\"application/xml\">" +
                "<m:properties>" +
                "<d:ComplexValue1Value>" +
                "<d:MyColorValue m:type=\"#AstoriaUnitTests.TDD.Tests.Client.ODataWriterWrapperUnitTests_MyColor\">Green</d:MyColorValue>" +
                "<d:MyFlagsColorValue m:type=\"#AstoriaUnitTests.TDD.Tests.Client.ODataWriterWrapperUnitTests_MyFlagsColor\">Red</d:MyFlagsColorValue>" +
                "<d:StringValue m:null=\"true\" />" +
                "</d:ComplexValue1Value>" +
                "<d:ID m:type=\"Int64\">2</d:ID>" +
                "<d:MyColorCollection />" +
                "<d:MyColorValue m:type=\"#AstoriaUnitTests.TDD.Tests.Client.ODataWriterWrapperUnitTests_MyColor\">Yellow</d:MyColorValue>" +
                "<d:MyFlagsColorCollection1>" +
                "<m:element m:type=\"#AstoriaUnitTests.TDD.Tests.Client.ODataWriterWrapperUnitTests+MyFlagsColor\">Blue</m:element>" +
                "<m:element m:type=\"#AstoriaUnitTests.TDD.Tests.Client.ODataWriterWrapperUnitTests+MyFlagsColor\">Red</m:element>" +
                "<m:element m:type=\"#AstoriaUnitTests.TDD.Tests.Client.ODataWriterWrapperUnitTests+MyFlagsColor\">Red</m:element>" +
                "</d:MyFlagsColorCollection1>" +
                "<d:MyFlagsColorValue m:type=\"#AstoriaUnitTests.TDD.Tests.Client.ODataWriterWrapperUnitTests_MyFlagsColor\">Blue</d:MyFlagsColorValue>" +
                "</m:properties>" +
                "</content>" +
                "</entry>");
        }
 /// <summary>
 /// Creates a request message with the given arguments.
 /// </summary>
 /// <param name="requestMessageArgs">The request message args.</param>
 /// <returns>Newly created request message.</returns>
 internal ODataRequestMessageWrapper CreateRequestMessage(BuildingRequestEventArgs requestMessageArgs)
 {
     return(ODataRequestMessageWrapper.CreateRequestMessageWrapper(requestMessageArgs, this.requestInfo));
 }