示例#1
0
        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_WriteStartExpandedFeedCalledInInvalidState("DeltaFeed"));
        }
示例#2
0
        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_InvalidTransitionToExpandedNavigationProperty("DeltaFeed", "ExpandedNavigationProperty"));
        }
示例#3
0
        public void WriteEmptyDeltaFeed()
        {
            this.TestInit();

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

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

            string payload = this.TestFinish();

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

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

            writer.WriteStart(deltaFeed);
            writer.WriteStart(customerEntry);
            writer.WriteStart(productBeingViewedNavigationLink);
            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(); // productBeingViewedNavigationLink
            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\"," +
                "\"ProductBeingViewed\":" +
                "{" +
                "\"@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 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.WriteStart(shippingAddressInfo);
            writer.WriteStart(shippingAddress);
            writer.WriteEnd(); // shippingAddress
            writer.WriteEnd(); // shippingAddressInfo
            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\"" +
                "}" +
                "}" +
                "]" +
                "}" +
                "]" +
                "}");
        }
        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"));
        }
示例#7
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();
        }
示例#8
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 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);
            }
        }
示例#10
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 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.WriteStart(shippingAddressInfo);
            writer.WriteStart(shippingAddress);
            writer.WriteEnd(); // shippingAddress
            writer.WriteEnd(); // shippingAddressInfo
            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 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\"}]}");
        }
示例#13
0
        public void WriteContainedEntityInDeltaFeed()
        {
            this.TestInit(this.GetModel());

            ODataDeltaFeed feed           = new ODataDeltaFeed();
            ODataEntry     containedEntry = new ODataEntry()
            {
                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 ODataFeedAndEntrySerializationInfo()
            {
                NavigationSourceEntityTypeName = "MyNS.ProductDetail",
                NavigationSourceName           = "Products(1)/Details",
                NavigationSourceKind           = EdmNavigationSourceKind.ContainedEntitySet
            });

            ODataEntry containedInContainedEntity = new ODataEntry()
            {
                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 ODataFeedAndEntrySerializationInfo()
            {
                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();

            string payload = this.TestFinish();

            payload.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\"}]}");
        }