public void ShortIntegrationTestToValidateEntryShouldBeRead()
        {
            var odataEntry = new ODataResource()
            {
                Id = new Uri("http://services.odata.org/OData/OData.svc/Customers(0)")
            };

            odataEntry.Properties = new ODataProperty[] { new ODataProperty()
                                                          {
                                                              Name = "ID", Value = 0
                                                          }, new ODataProperty()
                                                          {
                                                              Name = "Description", Value = "Simple Stuff"
                                                          } };

            var clientEdmModel = new ClientEdmModel(ODataProtocolVersion.V4);
            var context        = new DataServiceContext();

            MaterializerEntry.CreateEntry(odataEntry, ODataFormat.Json, true, clientEdmModel);
            var materializerContext = new TestMaterializerContext()
            {
                Model = clientEdmModel, Context = context
            };
            var             adapter    = new EntityTrackingAdapter(new TestEntityTracker(), MergeOption.OverwriteChanges, clientEdmModel, context);
            QueryComponents components = new QueryComponents(new Uri("http://foo.com/Service"), new Version(4, 0), typeof(Customer), null, new Dictionary <Expression, Expression>());

            var entriesMaterializer = new ODataEntriesEntityMaterializer(new ODataResource[] { odataEntry }, materializerContext, adapter, components, typeof(Customer), null, ODataFormat.Json);

            var customersRead = new List <Customer>();

            // This line will call ODataEntityMaterializer.ReadImplementation() which will reconstruct the entity, and will get non-public setter called.
            while (entriesMaterializer.Read())
            {
                customersRead.Add(entriesMaterializer.CurrentValue as Customer);
            }

            customersRead.Should().HaveCount(1);
            customersRead[0].ID.Should().Be(0);
            customersRead[0].Description.Should().Be("Simple Stuff");
        }
Пример #2
0
        public void MaterializeEntityShouldWork()
        {
            var odataEntry = new OData.ODataResource()
            {
                Id = new Uri("http://www.odata.org/service.svc/entitySet(1)")
            };

            odataEntry.Properties = new OData.ODataProperty[]
            {
                new OData.ODataProperty {
                    Name = "keyProp", Value = 1
                },
                new OData.ODataProperty {
                    Name = "colorProp", Value = new OData.ODataEnumValue("blue")
                },
                new OData.ODataProperty {
                    Name  = "primitiveCollection",
                    Value = new OData.ODataCollectionValue
                    {
                        TypeName = "Edm.Int32",
                        Items    = new List <object> {
                            1, 2, 3
                        }
                    }
                },
                new OData.ODataProperty {
                    Name  = "enumCollection",
                    Value = new OData.ODataCollectionValue
                    {
                        TypeName = "color",
                        Items    = new List <OData.ODataEnumValue> {
                            new OData.ODataEnumValue("white"), new OData.ODataEnumValue("blue")
                        }
                    }
                }
            };

            var complexP = new OData.ODataNestedResourceInfo()
            {
                Name         = "complexProp",
                IsCollection = false
            };

            var complexResource = new OData.ODataResource
            {
                Properties = new OData.ODataProperty[]
                {
                    new OData.ODataProperty {
                        Name = "age", Value = 11
                    },
                    new OData.ODataProperty {
                        Name = "name", Value = "June"
                    }
                }
            };

            var complexColP = new OData.ODataNestedResourceInfo
            {
                Name         = "complexCollection",
                IsCollection = true
            };

            var complexColResourceSet = new OData.ODataResourceSet();

            var items = new List <OData.ODataResource>
            {
                new OData.ODataResource
                {
                    Properties = new OData.ODataProperty[]
                    {
                        new OData.ODataProperty {
                            Name = "name", Value = "Aug"
                        },
                        new OData.ODataProperty {
                            Name = "age", Value = 8
                        },
                        new OData.ODataProperty {
                            Name = "color", Value = new OData.ODataEnumValue("white")
                        }
                    }
                },
                new OData.ODataResource
                {
                    Properties = new OData.ODataProperty[]
                    {
                        new OData.ODataProperty {
                            Name = "name", Value = "Sep"
                        },
                        new OData.ODataProperty {
                            Name = "age", Value = 9
                        },
                        new OData.ODataProperty {
                            Name = "color", Value = new OData.ODataEnumValue("blue")
                        }
                    }
                }
            };

            var clientEdmModel    = new ClientEdmModel(ODataProtocolVersion.V4);
            var context           = new DataServiceContext();
            var materializerEntry = MaterializerEntry.CreateEntry(odataEntry, OData.ODataFormat.Json, true, clientEdmModel);

            MaterializerNavigationLink.CreateLink(complexP, MaterializerEntry.CreateEntry(complexResource, OData.ODataFormat.Json, true, clientEdmModel));
            MaterializerFeed.CreateFeed(complexColResourceSet, items);
            MaterializerNavigationLink.CreateLink(complexColP, complexColResourceSet);

            var materializerContext = new TestMaterializerContext()
            {
                Model = clientEdmModel, Context = context
            };
            var             adapter    = new EntityTrackingAdapter(new TestEntityTracker(), MergeOption.OverwriteChanges, clientEdmModel, context);
            QueryComponents components = new QueryComponents(new Uri("http://foo.com/Service"), new Version(4, 0), typeof(EntityType), null, new Dictionary <Expression, Expression>());

            var entriesMaterializer = new ODataEntriesEntityMaterializer(new OData.ODataResource[] { odataEntry }, materializerContext, adapter, components, typeof(EntityType), null, OData.ODataFormat.Json);

            var customersRead = new List <EntityType>();

            while (entriesMaterializer.Read())
            {
                customersRead.Add(entriesMaterializer.CurrentValue as EntityType);
            }

            customersRead.Should().HaveCount(1);
            customersRead[0].KeyProp.Should().Be(1);
            customersRead[0].ComplexProp.Should().Equals(new ComplexType {
                Age = 11, Name = "June"
            });
            customersRead[0].ColorProp.Should().Equals(Color.Blue);
            customersRead[0].PrimitiveCollection.Should().Equals(new List <int> {
                1, 2, 3
            });
            ComplexType complex1 = new ComplexType {
                Name = "Aug", Age = 8, Color = Color.White
            };
            ComplexType complex2 = new ComplexType {
                Name = "Sep", Age = 9, Color = Color.Blue
            };

            customersRead[0].ComplexCollection.Should().Equals(new List <ComplexType> {
                complex1, complex2
            });
            customersRead[0].EnumCollection.Should().Equals(new List <Color> {
                Color.White, Color.Blue
            });
        }