public void WriteDerivedEntity()
        {
            this.TestInit(this.GetModel());

            ODataDeltaResourceSet feed          = new ODataDeltaResourceSet();
            ODataResource         derivedEntity = new ODataResource()
            {
                TypeName   = "MyNS.PhysicalProduct",
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "Id", Value = new ODataPrimitiveValue(1)
                    },
                    new ODataProperty {
                        Name = "Name", Value = new ODataPrimitiveValue("car")
                    },
                    new ODataProperty {
                        Name = "Material", Value = new ODataPrimitiveValue("gold")
                    },
                },
            };

            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetProducts(), this.GetProductType());

            writer.WriteStart(feed);
            writer.WriteStart(derivedEntity);
            writer.WriteEnd();
            writer.WriteEnd();
            writer.Flush();

            this.TestPayload().Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Products/$delta\",\"value\":[{\"@odata.type\":\"#MyNS.PhysicalProduct\",\"Id\":1,\"Name\":\"car\",\"Material\":\"gold\"}]}");
        }
        public void WriteContainedEntityInDeltaFeed()
        {
            this.TestInit(this.GetModel());

            ODataDeltaResourceSet feed           = new ODataDeltaResourceSet();
            ODataResource         containedEntry = new ODataResource()
            {
                TypeName   = "MyNS.ProductDetail",
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "Id", Value = new ODataPrimitiveValue(1)
                    },
                    new ODataProperty {
                        Name = "Detail", Value = new ODataPrimitiveValue("made in china")
                    },
                },
            };

            containedEntry.SetSerializationInfo(new ODataResourceSerializationInfo()
            {
                NavigationSourceEntityTypeName = "MyNS.ProductDetail",
                NavigationSourceName           = "Products(1)/Details",
                NavigationSourceKind           = EdmNavigationSourceKind.ContainedEntitySet
            });

            ODataResource containedInContainedEntity = new ODataResource()
            {
                TypeName   = "MyNS.ProductDetailItem",
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "ItemId", Value = new ODataPrimitiveValue(1)
                    },
                    new ODataProperty {
                        Name = "Description", Value = new ODataPrimitiveValue("made by HCC")
                    },
                },
            };

            containedInContainedEntity.SetSerializationInfo(new ODataResourceSerializationInfo()
            {
                NavigationSourceEntityTypeName = "MyNS.ProductDetailItem",
                NavigationSourceName           = "Products(1)/Details(1)/Items",
                NavigationSourceKind           = EdmNavigationSourceKind.ContainedEntitySet
            });

            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetProducts(), this.GetProductType());

            writer.WriteStart(feed);
            writer.WriteStart(containedEntry);
            writer.WriteEnd();
            writer.WriteStart(containedInContainedEntity);
            writer.WriteEnd();
            writer.WriteEnd();
            writer.Flush();

            this.TestPayload().Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Products/$delta\",\"value\":[{\"@odata.context\":\"http://host/service/$metadata#Products(1)/Details/$entity\",\"Id\":1,\"Detail\":\"made in china\"},{\"@odata.context\":\"http://host/service/$metadata#Products(1)/Details(1)/Items/$entity\",\"ItemId\":1,\"Description\":\"made by HCC\"}]}");
        }
        public void WriteEntityInDeltaFeedWithSelectExpand()
        {
            this.TestInit(this.GetModel());

            ODataDeltaResourceSet feed = new ODataDeltaResourceSet();

            ODataResource orderEntry = new ODataResource()
            {
                SerializationInfo = new ODataResourceSerializationInfo
                {
                    NavigationSourceEntityTypeName = "Order",
                    NavigationSourceKind           = EdmNavigationSourceKind.EntitySet,
                    NavigationSourceName           = "Orders"
                },
            };

            ODataNestedResourceInfo shippingAddressInfo = new ODataNestedResourceInfo
            {
                Name         = "ShippingAddress",
                IsCollection = false
            };

            ODataResource shippingAddress = new ODataResource
            {
                Properties = new List <ODataProperty>
                {
                    new ODataProperty {
                        Name = "City", Value = "Shanghai"
                    },
                }
            };

            var result = new ODataQueryOptionParser(this.GetModel(), this.GetCustomerType(), this.GetCustomers(), new Dictionary <string, string> {
                { "$expand", "Orders($select=ShippingAddress)" }, { "$select", "ContactName" }
            }).ParseSelectAndExpand();

            ODataUri odataUri = new ODataUri()
            {
                ServiceRoot     = new Uri("http://host/service"),
                SelectAndExpand = result
            };

            var outputContext = CreateJsonLightOutputContext(this.stream, this.GetModel(), false, odataUri);
            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetProducts(), this.GetProductType());

            writer.WriteStart(feed);
            writer.WriteStart(orderEntry);
            writer.WriteStart(shippingAddressInfo);
            writer.WriteStart(shippingAddress);
            writer.WriteEnd();
            writer.WriteEnd();
            writer.WriteEnd();
            writer.WriteEnd();
            writer.Flush();

            this.TestPayload().Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Products(ContactName,Orders(ShippingAddress))/$delta\",\"value\":[{\"@odata.context\":\"http://host/service/$metadata#Orders/$entity\",\"ShippingAddress\":{\"City\":\"Shanghai\"}}]}");
        }
        public void CannotWriteExpandedFeedOutsideDeltaEntry()
        {
            this.TestInit(this.GetModel());

            Action writeAction = () =>
            {
                ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetCustomers(), this.GetCustomerType());
                writer.WriteStart(deltaFeed);
                writer.WriteStart(ordersFeed);
            };

            writeAction.ShouldThrow <ODataException>().WithMessage(Strings.ODataJsonLightDeltaWriter_WriteStartExpandedResourceSetCalledInInvalidState("DeltaResourceSet"));
        }
        public void CannotWriteExpandedNavigationPropertyOutsideDeltaEntry()
        {
            this.TestInit(this.GetModel());

            Action writeAction = () =>
            {
                ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetCustomers(), this.GetCustomerType());
                writer.WriteStart(deltaFeed);
                writer.WriteStart(ordersNavigationLink);
            };

            writeAction.ShouldThrow <ODataException>().WithMessage(Strings.ODataJsonLightDeltaWriter_InvalidTransitionToNestedResource("DeltaResourceSet", "NestedResource"));
        }
Exemple #6
0
        public void WriteMultipleExpandedFeeds()
        {
            this.TestInit(this.GetModel());

            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetCustomers(), this.GetCustomerType());

            writer.WriteStart(deltaFeed);
            writer.WriteStart(customerEntry);
            writer.WriteStart(ordersNavigationLink);
            writer.WriteStart(ordersFeed);
            writer.WriteStart(orderEntry);
            writer.WriteEnd(); // orderEntry
            writer.WriteEnd(); // ordersFeed
            writer.WriteEnd(); // ordersNavigationLink
            writer.WriteStart(favouriteProductsNavigationLink);
            writer.WriteStart(favouriteProductsFeed);
            writer.WriteStart(productEntry);
            writer.WriteEnd(); // productEntry
            writer.WriteEnd(); // favouriteProductsFeed
            writer.WriteEnd(); // favouriteProductsNavigationLink
            writer.WriteEnd(); // customerEntry
            writer.WriteEnd(); // deltaFeed
            writer.Flush();

            this.TestPayload().Should().Be(
                "{" +
                "\"@odata.context\":\"http://host/service/$metadata#Customers/$delta\"," +
                "\"value\":" +
                "[" +
                "{" +
                "\"@odata.id\":\"http://host/service/Customers('BOTTM')\"," +
                "\"ContactName\":\"Susan Halvenstern\"," +
                "\"Orders\":" +
                "[" +
                "{" +
                "\"@odata.id\":\"http://host/service/Orders(10643)\"," +
                "\"Id\":10643," +
                "\"ShippingAddress\":" +
                "{" +
                "\"Street\":\"23 Tsawassen Blvd.\"," +
                "\"City\":\"Tsawassen\"," +
                "\"Region\":\"BC\"," +
                "\"PostalCode\":\"T2F 8M4\"" +
                "}" +
                "}" +
                "]," +
                "\"FavouriteProducts\":" +
                "[" +
                "{" +
                "\"@odata.id\":\"http://host/service/Product(1)\"," +
                "\"Id\":1," +
                "\"Name\":\"Car\"" +
                "}" +
                "]" +
                "}" +
                "]" +
                "}");
        }
        public void CannotWriteDeltaItemWhileWritingExpandedNavigationProperty()
        {
            this.TestInit(this.GetModel());

            Action writeAction = () =>
            {
                ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetCustomers(), this.GetCustomerType());
                writer.WriteStart(deltaFeed);
                writer.WriteStart(customerEntry);
                writer.WriteStart(ordersNavigationLink);
                writer.WriteStart(ordersFeed);
                writer.WriteDeltaDeletedEntry(customerDeleted);
            };

            writeAction.ShouldThrow <ODataException>().WithMessage(Strings.ODataJsonLightDeltaWriter_InvalidTransitionFromNestedResource("NestedResource", "DeltaDeletedEntry"));
        }
Exemple #8
0
        private void WriteExpandedFeedWithModelImplementation()
        {
            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetCustomers(), this.GetCustomerType());

            writer.WriteStart(deltaFeed);
            writer.WriteStart(customerEntry);
            writer.WriteStart(ordersNavigationLink);
            writer.WriteStart(ordersFeed);
            writer.WriteStart(orderEntry);
            writer.WriteEnd(); // orderEntry
            writer.WriteEnd(); // ordersFeed
            writer.WriteEnd(); // ordersNavigationLink
            writer.WriteEnd(); // customerEntry
            writer.WriteEnd(); // deltaFeed
            writer.Flush();
        }
Exemple #9
0
        public void WriteExpandedFeedWithSerializationInfoMinimalMetadata()
        {
            this.TestInit();

            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, null, null);

            writer.WriteStart(deltaFeedWithInfo);
            writer.WriteStart(customerEntryWithInfo);
            writer.WriteStart(ordersNavigationLink);
            writer.WriteStart(ordersFeed);
            writer.WriteStart(orderEntryWithInfo);
            writer.WriteEnd(); // orderEntryWithInfo
            writer.WriteEnd(); // ordersFeed
            writer.WriteEnd(); // ordersNavigationLink
            writer.WriteEnd(); // customerEntry
            writer.WriteEnd(); // deltaFeedWithInfo
            writer.Flush();

            this.TestPayload().Should().Be(
                "{" +
                "\"@odata.context\":\"http://host/service/$metadata#Customers/$delta\"," +
                "\"value\":" +
                "[" +
                "{" +
                "\"@odata.id\":\"http://host/service/Customers('BOTTM')\"," +
                "\"ContactName\":\"Susan Halvenstern\"," +
                "\"Orders\":" +
                "[" +
                "{" +
                "\"@odata.id\":\"http://host/service/Orders(10643)\"," +
                "\"Id\":10643," +
                "\"ShippingAddress\":" +
                "{" +
                "\"Street\":\"23 Tsawassen Blvd.\"," +
                "\"City\":\"Tsawassen\"," +
                "\"Region\":\"BC\"," +
                "\"PostalCode\":\"T2F 8M4\"" +
                "}" +
                "}" +
                "]" +
                "}" +
                "]" +
                "}");
        }
Exemple #10
0
        public void WriteExample30FromV4SpecWithoutModel()
        {
            this.TestInit();

            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, null, null);

            writer.WriteStart(feed);
            writer.WriteStart(customerUpdated);
            writer.WriteEnd();
            writer.WriteDeltaDeletedLink(linkToOrder10643);
            writer.WriteDeltaLink(linkToOrder10645);
            writer.WriteStart(order10643);
            writer.WriteEnd();
            writer.WriteDeltaDeletedEntry(customerDeleted);
            writer.WriteEnd();
            writer.Flush();

            this.TestPayload().Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Customers/$delta\",\"@odata.count\":5,\"@odata.deltaLink\":\"Customers?$expand=Orders&$deltatoken=8015\",\"value\":[{\"@odata.id\":\"Customers('BOTTM')\",\"ContactName\":\"Susan Halvenstern\"},{\"@odata.context\":\"http://host/service/$metadata#Customers/$deletedLink\",\"source\":\"Customers('ALFKI')\",\"relationship\":\"Orders\",\"target\":\"Orders('10643')\"},{\"@odata.context\":\"http://host/service/$metadata#Customers/$link\",\"source\":\"Customers('BOTTM')\",\"relationship\":\"Orders\",\"target\":\"Orders('10645')\"},{\"@odata.context\":\"http://host/service/$metadata#Orders/$entity\",\"@odata.id\":\"Orders(10643)\",\"ShippingAddress\":{\"Street\":\"23 Tsawassen Blvd.\",\"City\":\"Tsawassen\",\"Region\":\"BC\",\"PostalCode\":\"T2F 8M4\"}},{\"@odata.context\":\"http://host/service/$metadata#Customers/$deletedEntity\",\"id\":\"Customers('ANTON')\",\"reason\":\"deleted\"}]}");
        }
        public void WriteContainmentExpandedFeeds()
        {
            this.TestInit(this.GetModel());

            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetCustomers(), this.GetCustomerType());

            writer.WriteStart(deltaFeed);
            writer.WriteStart(customerEntry);
            writer.WriteStart(favouriteProductsNavigationLink);
            writer.WriteStart(favouriteProductsFeed);
            writer.WriteStart(productEntry);
            writer.WriteStart(detailsNavigationLink);
            writer.WriteStart(detailsFeed);
            writer.WriteStart(productDetailEntry);
            writer.WriteEnd(); // productDetailEntry
            writer.WriteEnd(); // detailsFeed
            writer.WriteEnd(); // detailsNavigationLink
            writer.WriteEnd(); // productEntry
            writer.WriteEnd(); // favouriteProductsFeed
            writer.WriteEnd(); // favouriteProductsNavigationLink
            writer.WriteEnd(); // customerEntry
            writer.WriteEnd(); // deltaFeed
            writer.Flush();

            this.TestPayload().Should().Be(
                "{" +
                "\"@odata.context\":\"http://host/service/$metadata#Customers/$delta\"," +
                "\"value\":" +
                "[" +
                "{" +
                "\"@odata.id\":\"http://host/service/Customers('BOTTM')\"," +
                "\"ContactName\":\"Susan Halvenstern\"," +
                "\"FavouriteProducts\":" +
                "[" +
                "{" +
                "\"@odata.id\":\"http://host/service/Product(1)\"," +
                "\"Id\":1," +
                "\"Name\":\"Car\"," +
                "\"[email protected]\":\"http://host/service/$metadata#Products(1)/Details\"," +
                "\"Details\":" +
                "[" +
                "{" +
                "\"Id\":1," +
                "\"Detail\":\"made in china\"" +
                "}" +
                "]" +
                "}" +
                "]" +
                "}" +
                "]" +
                "}");
        }
        public void JsonLightDeltaResourceScopePropertyAccessors()
        {
            this.TestInit(this.GetModel());

            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetCustomers(), this.GetCustomerType());
            BindingFlags bindFlags           = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
            Assembly     assembly            = typeof(ODataAction).GetTypeInfo().Assembly;
            Type         typeWriter          = assembly.GetType("Microsoft.OData.JsonLight.ODataJsonLightDeltaWriter");

            writer = new ODataJsonLightDeltaWriter(outputContext, this.GetCustomers(), this.GetCustomerType());
            writer.WriteStart(feedWithoutInfo);
            writer.WriteStart(customerUpdated);

            // Get the CurrentDeltaResourceScope obj and type, and then exercise the setters and getters for its properties.
            PropertyInfo propCurrentDeltaResourceScope = typeWriter.GetProperty("CurrentDeltaResourceScope", bindFlags);
            object       objCurrentDeltaResourceScope  = propCurrentDeltaResourceScope.GetValue(writer);
            Type         typeCurrentDeltaResourceScope = typeWriter.GetNestedType("JsonLightDeltaResourceScope", bindFlags);

            //ODataJsonLightDeltaWriter.JsonLightDeltaResourceScope properties
            string[] propertyNames = new string[]
            {
                "ReadLinkWritten",
                "MediaEditLinkWritten",
                "MediaReadLinkWritten",
                "MediaContentTypeWritten",
                "MediaETagWritten",
            };

            foreach (string propertyName in propertyNames)
            {
                PropertyInfo currentPropertyInfo = typeCurrentDeltaResourceScope.GetProperty(propertyName, bindFlags);

                object currentProperty = currentPropertyInfo.GetValue(objCurrentDeltaResourceScope);
                currentProperty.Should().Be(false);

                // value should only be set from false to true.
                currentPropertyInfo.SetValue(objCurrentDeltaResourceScope, true);
                currentProperty = currentPropertyInfo.GetValue(objCurrentDeltaResourceScope);
                currentProperty.Should().Be(true);
            }
        }
Exemple #13
0
        public void WriteDerivedEntityWithSerilizationInfo()
        {
            this.TestInit(this.GetModel());

            ODataDeltaFeed feed          = new ODataDeltaFeed();
            ODataEntry     derivedEntity = new ODataEntry()
            {
                TypeName   = "MyNS.PhysicalProduct",
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "Id", Value = new ODataPrimitiveValue(1)
                    },
                    new ODataProperty {
                        Name = "Name", Value = new ODataPrimitiveValue("car")
                    },
                    new ODataProperty {
                        Name = "Material", Value = new ODataPrimitiveValue("gold")
                    },
                },

                SerializationInfo = new ODataFeedAndEntrySerializationInfo()
                {
                    ExpectedTypeName = "MyNS.Product",
                    NavigationSourceEntityTypeName = "MyNS.Product",
                    NavigationSourceName           = "Products",
                }
            };

            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetCustomers(), this.GetCustomerType());

            writer.WriteStart(feed);
            writer.WriteStart(derivedEntity);
            writer.WriteEnd();
            writer.WriteEnd();
            writer.Flush();

            string payload = this.TestFinish();

            payload.Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Customers/$delta\",\"value\":[{\"@odata.context\":\"http://host/service/$metadata#Products/$entity\",\"@odata.type\":\"#MyNS.PhysicalProduct\",\"Id\":1,\"Name\":\"car\",\"Material\":\"gold\"}]}");
        }
        public void WriteEmptyDeltaFeed()
        {
            this.TestInit();

            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, null, null);

            writer.WriteStart(feed);
            writer.WriteEnd();
            writer.Flush();

            this.TestPayload().Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Customers/$delta\",\"@odata.count\":5,\"@odata.deltaLink\":\"Customers?$expand=Orders&$deltatoken=8015\",\"value\":[]}");
        }
        public void WriteContainedEntityInDeltaFeedWithSelectExpand()
        {
            this.TestInit(this.GetModel());

            ODataDeltaResourceSet feed = new ODataDeltaResourceSet();

            ODataResource entry = new ODataResource()
            {
                TypeName   = "MyNS.Product",
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "Id", Value = new ODataPrimitiveValue(1)
                    },
                    new ODataProperty {
                        Name = "Name", Value = new ODataPrimitiveValue("Car")
                    },
                },
            };

            ODataResource containedEntry = new ODataResource()
            {
                TypeName   = "MyNS.ProductDetail",
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "Id", Value = new ODataPrimitiveValue(1)
                    },
                    new ODataProperty {
                        Name = "Detail", Value = new ODataPrimitiveValue("made in china")
                    },
                },
            };

            containedEntry.SetSerializationInfo(new ODataResourceSerializationInfo()
            {
                NavigationSourceEntityTypeName = "MyNS.ProductDetail",
                NavigationSourceName           = "Products(1)/Details",
                NavigationSourceKind           = EdmNavigationSourceKind.ContainedEntitySet
            });


            var result = new ODataQueryOptionParser(this.GetModel(), this.GetProductType(), this.GetProducts(), new Dictionary <string, string> {
                { "$expand", "Details($select=Detail)" }, { "$select", "Name" }
            }).ParseSelectAndExpand();

            ODataUri odataUri = new ODataUri()
            {
                ServiceRoot     = new Uri("http://host/service"),
                SelectAndExpand = result
            };

            var outputContext = CreateJsonLightOutputContext(this.stream, this.GetModel(), false, odataUri);
            ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetProducts(), this.GetProductType());

            writer.WriteStart(feed);
            writer.WriteStart(containedEntry);
            writer.WriteEnd();
            writer.WriteStart(entry);
            writer.WriteEnd();
            writer.WriteEnd();
            writer.Flush();

            this.TestPayload().Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Products(Name,Details(Detail))/$delta\",\"value\":[{\"@odata.context\":\"http://host/service/$metadata#Products(1)/Details/$entity\",\"Id\":1,\"Detail\":\"made in china\"},{\"Id\":1,\"Name\":\"Car\"}]}");
        }