示例#1
0
        public async Task ReadDeletedLinkInDeltaResourceSetWorksAsExpected2()
        {
            // Arrange
            string payload = "{\"@context\":\"http://example.com/$metadata#Customers/$delta\"," +
                             "\"value\":[" +
                             "{" +
                             "\"@removed\":{\"reason\":\"changed\"}," +
                             "\"CustomerID\":1," +
                             "\"Order@delta\":{" +
                             "\"@context\":\"#Orders/$deletedEntity\"," +
                             "\"@removed\":{\"reason\":\"deleted\"}," +
                             "\"OrderId\":10" +
                             "}" +
                             "}" +
                             "]" +
                             "}";

            IEdmEntitySet customers = Model.EntityContainer.FindEntitySet("Customers");

            Assert.NotNull(customers); // Guard

            // Act
            Func <ODataMessageReader, Task <ODataReader> > func = mr => mr.CreateODataDeltaResourceSetReaderAsync(customers, customers.EntityType());
            ODataItemWrapper item = await ReadPayloadAsync(payload, Model, func, ODataVersion.V401);

            // Assert
            Assert.NotNull(item);

            // --- DeltaResourceSet
            //      |--- DeleteResource (Changed)
            //         |--- NestedResourceInfo
            //            |--- DeleteResource (Deleted)
            ODataDeltaResourceSetWrapper deltaResourceSet       = Assert.IsType <ODataDeltaResourceSetWrapper>(item);
            ODataItemWrapper             deltaItem              = Assert.Single(deltaResourceSet.DeltaItems);
            ODataResourceWrapper         deletedResourceWrapper = Assert.IsType <ODataResourceWrapper>(deltaItem);
            ODataDeletedResource         deletedResource        = Assert.IsType <ODataDeletedResource>(deletedResourceWrapper.Resource);

            Assert.Equal(DeltaDeletedEntryReason.Changed, deletedResource.Reason);

            ODataNestedResourceInfoWrapper nestedResourceInfo = Assert.Single(deletedResourceWrapper.NestedResourceInfos);

            Assert.Equal("Order", nestedResourceInfo.NestedResourceInfo.Name);
            Assert.False(nestedResourceInfo.NestedResourceInfo.IsCollection);

            ODataItemWrapper     nestedItem    = Assert.Single(nestedResourceInfo.NestedItems);
            ODataResourceWrapper orderResource = Assert.IsType <ODataResourceWrapper>(nestedItem);

            Assert.True(orderResource.IsDeletedResource);
            ODataDeletedResource innerDeletedResource = Assert.IsType <ODataDeletedResource>(orderResource.Resource);

            Assert.Equal(DeltaDeletedEntryReason.Deleted, innerDeletedResource.Reason);

            ODataProperty property = Assert.Single(innerDeletedResource.Properties);

            Assert.Equal("OrderId", property.Name);
            Assert.Equal(10, property.Value);
        }
        public async Task ReadResourceSetWithNestedResourceSetWorksAsExpected()
        {
            // Arrange
            const string payload =
                "{" +
                "\"@odata.context\":\"http://localhost/$metadata#Customers\"," +
                "\"value\": [" +
                "{" +
                "\"CustomerID\": 7," +
                "\"Location\": { \"Street\":\"154TH AVE\"}," +
                "\"Order\": {\"OrderId\": 8, \"Price\": 82 }," +
                "\"Orders\": [" +
                "{\"OrderId\": 8, \"Price\": 82 }," +
                "{\"@odata.type\": \"#NS.VipOrder\",\"OrderId\": 9, \"Price\": 42, \"Email\": \"[email protected]\" }" +
                "]" +
                "}" +
                "]" +
                "}";

            IEdmEntitySet customers = Model.EntityContainer.FindEntitySet("Customers");

            Assert.NotNull(customers); // Guard

            // Act
            Func <ODataMessageReader, Task <ODataReader> > func = mr => mr.CreateODataResourceSetReaderAsync(customers, customers.EntityType());
            ODataItemWrapper item = await ReadPayloadAsync(payload, Model, func);

            // Assert
            Assert.NotNull(item);
            ODataResourceSetWrapper resourceSet = Assert.IsType <ODataResourceSetWrapper>(item);
            ODataResourceWrapper    resource    = Assert.Single(resourceSet.Resources);

            Assert.Equal(new[] { "Location", "Order", "Orders" }, resource.NestedResourceInfos.Select(n => n.NestedResourceInfo.Name));

            ODataNestedResourceInfoWrapper orders = resource.NestedResourceInfos.First(n => n.NestedResourceInfo.Name == "Orders");

            Assert.Null(orders.NestedResource); // not a child resource
            Assert.Null(orders.NestedLinks);    // not a child reference link(s)
            Assert.NotNull(orders.NestedResourceSet);

            ODataResourceSetWrapper ordersSet = Assert.IsType <ODataResourceSetWrapper>(orders.NestedResourceSet);

            Assert.Equal(2, ordersSet.Resources.Count);
            Assert.Collection(ordersSet.Resources,
                              r =>
            {
                Assert.Equal("NS.Order", r.Resource.TypeName);
                Assert.Equal(82, r.Resource.Properties.First(p => p.Name == "Price").Value);
            },
                              r =>
            {
                Assert.Equal("NS.VipOrder", r.Resource.TypeName);
                Assert.Equal("*****@*****.**", r.Resource.Properties.First(p => p.Name == "Email").Value);
            });
        }
        public async Task ReadEntityReferenceLinksSetWorksAsExpected_V401()
        {
            // Arrange
            string payload = "{" + // -> ResourceStart
                             "\"@odata.context\":\"http://localhost/$metadata#Customers/$entity\"," +
                             "\"CustomerID\": 7," +
                             "\"Orders\":[" + // -> NestedResourceInfoStart
                             "{ \"@id\": \"http://svc/Orders(2)\" }," +
                             "{ \"@id\": \"http://svc/Orders(3)\" }," +
                             "{ \"@id\": \"http://svc/Orders(4)\" }" +
                             "]" +
                             "}";

            IEdmEntitySet customers = Model.EntityContainer.FindEntitySet("Customers");

            Assert.NotNull(customers); // Guard

            // Act
            Func <ODataMessageReader, Task <ODataReader> > func = mr => mr.CreateODataResourceReaderAsync(customers, customers.EntityType());
            ODataItemWrapper item = await ReadPayloadAsync(payload, Model, func, ODataVersion.V401);

            // Assert
            Assert.NotNull(item);

            // --- Resource
            //     |--- NestedResourceInfo
            //        |--- NestedResourceSet
            //              |--- Resource (2)
            //              |--- Resource (3)
            //              |--- Resource (4)
            ODataResourceWrapper resource = Assert.IsType <ODataResourceWrapper>(item);

            ODataNestedResourceInfoWrapper nestedResourceInfo = Assert.Single(resource.NestedResourceInfos);

            Assert.Equal("Orders", nestedResourceInfo.NestedResourceInfo.Name);
            Assert.True(nestedResourceInfo.NestedResourceInfo.IsCollection);

            ODataResourceSetWrapper ordersResourceSet = Assert.IsType <ODataResourceSetWrapper>(nestedResourceInfo.NestedResourceSet);

            Assert.Equal(3, ordersResourceSet.Resources.Count);
            Assert.Collection(ordersResourceSet.Resources,
                              r =>
            {
                Assert.Equal("http://svc/Orders(2)", r.Resource.Id.OriginalString);
            },
                              r =>
            {
                Assert.Equal("http://svc/Orders(3)", r.Resource.Id.OriginalString);
            },
                              r =>
            {
                Assert.Equal("http://svc/Orders(4)", r.Resource.Id.OriginalString);
            });
        }
        public static void WriteNestedResourceInfo(ODataWriter writer, ODataNestedResourceInfoWrapper nestedResourceInfo)
        {
            writer.WriteStart(nestedResourceInfo.NestedResourceInfo);

            if (nestedResourceInfo.NestedResourceOrResourceSet != null)
            {
                WriteItem(writer, nestedResourceInfo.NestedResourceOrResourceSet);
            }

            writer.WriteEnd();
        }
        public async Task ReadSingleEntityReferenceLinkWorksAsExpected(ODataVersion version, string referenceLink)
        {
            // Arrange
            string payload = "{" + // -> ResourceStart
                             "\"@odata.context\":\"http://localhost/$metadata#Customers/$entity\"," +
                             "\"CustomerID\": 7," +
                             referenceLink +
                             "}";

            IEdmEntitySet customers = Model.EntityContainer.FindEntitySet("Customers");

            Assert.NotNull(customers); // Guard

            // Act
            Func <ODataMessageReader, Task <ODataReader> > func = mr => mr.CreateODataResourceReaderAsync(customers, customers.EntityType());
            ODataItemWrapper item = await ReadPayloadAsync(payload, Model, func, version);

            // Assert
            Assert.NotNull(item);
            ODataResourceWrapper           resource           = Assert.IsType <ODataResourceWrapper>(item);
            ODataNestedResourceInfoWrapper nestedResourceInfo = Assert.Single(resource.NestedResourceInfos);

            Assert.Equal("Order", nestedResourceInfo.NestedResourceInfo.Name);
            if (version == ODataVersion.V401)
            {
                // --- Resource
                //     |--- NestedResourceInfo (Order)
                //          |--- Resource
                ODataResourceWrapper order = Assert.IsType <ODataResourceWrapper>(nestedResourceInfo.NestedResource);
                Assert.Equal("http://svc/Orders(8)", order.Resource.Id.OriginalString);
                Assert.Empty(order.Resource.Properties);
            }
            else
            {
                // --- Resource
                //     |--- NestedResourceInfo (Order)
                //          |--- EntityReferenceLink
                ODataEntityReferenceLinkWrapper orderLink = Assert.IsType <ODataEntityReferenceLinkWrapper>(Assert.Single(nestedResourceInfo.NestedLinks));
                Assert.Equal("http://svc/Orders(7)", orderLink.EntityReferenceLink.Url.OriginalString);
            }
        }
        public async Task ReadDeltaResourceSetWorksAsExpected()
        {
            // Arrange
            string payload = "{\"@context\":\"http://example.com/$metadata#Customers/$delta\"," +
                             "\"value\":[" +
                             "{" +
                             "\"@removed\":{\"reason\":\"changed\"}," +
                             "\"CustomerID\":1," +
                             "\"Orders@delta\":[" +
                             "{" +
                             "\"@removed\":{\"reason\":\"deleted\"}," +
                             "\"OrderId\":10" +
                             "}," +
                             "{" +
                             "\"@type\":\"#NS.VipOrder\"," +
                             "\"OrderId\":9," +
                             "\"Email\":\"[email protected]\"" +
                             "}" +
                             "]" +
                             "}" +
                             "]" +
                             "}";

            IEdmEntitySet customers = Model.EntityContainer.FindEntitySet("Customers");

            Assert.NotNull(customers); // Guard

            // Act
            Func <ODataMessageReader, Task <ODataReader> > func = mr => mr.CreateODataDeltaResourceSetReaderAsync(customers, customers.EntityType());
            ODataItemWrapper item = await ReadPayloadAsync(payload, Model, func, ODataVersion.V401);

            // Assert
            Assert.NotNull(item);

            // --- DeltaResourceSet
            //      |--- DeleteResource (1)
            //          |--- NestedResourceInfo (1-1)
            //               |--- DeltaResourceSet
            //                     |--- DelteResource
            //                     |--- Normal Resource
            ODataDeltaResourceSetWrapper deltaResourceSet = Assert.IsType <ODataDeltaResourceSetWrapper>(item);
            ODataResourceBaseWrapper     resourceBase     = Assert.Single(deltaResourceSet.ResourceBases);
            ODataDeletedResourceWrapper  deletedResource  = Assert.IsType <ODataDeletedResourceWrapper>(resourceBase);

            Assert.Equal(DeltaDeletedEntryReason.Changed, deletedResource.DeletedResource.Reason);

            ODataNestedResourceInfoWrapper nestedResourceInfo = Assert.Single(deletedResource.NestedResourceInfos);

            Assert.Equal("Orders", nestedResourceInfo.NestedResourceInfo.Name);
            Assert.True(nestedResourceInfo.NestedResourceInfo.IsCollection);

            ODataDeltaResourceSetWrapper ordersDeltaResourceSet = Assert.IsType <ODataDeltaResourceSetWrapper>(nestedResourceInfo.NestedResourceSet);

            Assert.Equal(2, ordersDeltaResourceSet.ResourceBases.Count);
            ODataDeletedResourceWrapper deletedResource1 = Assert.IsType <ODataDeletedResourceWrapper>(ordersDeltaResourceSet.ResourceBases.ElementAt(0));

            Assert.Equal(DeltaDeletedEntryReason.Deleted, deletedResource1.DeletedResource.Reason);

            ODataResourceWrapper resource2 = Assert.IsType <ODataResourceWrapper>(ordersDeltaResourceSet.ResourceBases.ElementAt(1));

            Assert.Equal("NS.VipOrder", resource2.Resource.TypeName);
            Assert.Collection(resource2.Resource.Properties,
                              p =>
            {
                Assert.Equal("OrderId", p.Name);
                Assert.Equal(9, p.Value);
            },
                              p =>
            {
                Assert.Equal("Email", p.Name);
                Assert.Equal("*****@*****.**", p.Value);
            });
        }
示例#7
0
        public static void TestReadResource()
        {
            string requestData = @"{
  ""@odata.type"": ""#NS.Customer"",
  ""*****@*****.**"": ""Cloud"",
  ""DisplayName"": ""Group Display Name"",
  ""*****@*****.**"": ""Cloud"",
  ""AssignedLabels"": [
    {
      ""City"": ""City1"",
      ""Street"": ""Street1""
    }
  ]
}";
            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers.Add("Content-Type", "application/json;odata.metadata=minima;odata.streaming=true");
            headers.Add("Accept", "application/json");

            ODataMessageReaderSettings readerSettings = new ODataMessageReaderSettings
            {
                BaseUri = new Uri("http://localhost"),
                ShouldIncludeAnnotation = s => true
            };

            //ODataResourceWrapper topLevelResource = null;

            ODataItemWrapper topLevel = null, currentItem = null;
            IEdmModel        model     = EdmModelBuilder.GetEdmModel();
            IEdmEntitySet    customers = model.EntityContainer.FindEntitySet("Customers");

            //  ODataResource resource = null;
            using (MemoryStream ms = new MemoryStream(Encoding.GetEncoding("iso-8859-1").GetBytes(requestData)))
            {
                ODataMessageWrapper requestMessage = new ODataMessageWrapper(ms);

                ODataMessageReader messageReader = new ODataMessageReader((IODataRequestMessage)requestMessage, readerSettings, model);

                Stack <ODataItemWrapper> resStack = new Stack <ODataItemWrapper>();
                ODataReader odataReader           = messageReader.CreateODataResourceReader(customers, customers.EntityType());

                while (odataReader.Read())
                {
                    Console.WriteLine(odataReader.State);

                    switch (odataReader.State)
                    {
                    case ODataReaderState.ResourceStart:
                        ODataResourceWrapper newResource = new ODataResourceWrapper();
                        newResource.Resource = odataReader.Item as ODataResource;

                        resStack.TryPeek(out currentItem);
                        if (currentItem == null)
                        {
                            // TopLevel, do nothing
                            Debug.Assert(topLevel == null);
                            topLevel = newResource;
                        }
                        else
                        {
                            currentItem.Append(newResource);
                        }

                        resStack.Push(newResource);
                        break;

                    case ODataReaderState.ResourceSetStart:
                        ODataResourceSet        resourceSet = (ODataResourceSet)odataReader.Item;
                        ODataResourceSetWrapper setWrapper  = new ODataResourceSetWrapper
                        {
                            ResourceSet = resourceSet
                        };

                        resStack.TryPeek(out currentItem);
                        if (currentItem == null)
                        {
                            // TopLevel, do nothing
                            Debug.Assert(topLevel == null);
                            topLevel = setWrapper;
                        }
                        else
                        {
                            currentItem.Append(setWrapper);
                        }

                        resStack.Push(setWrapper);
                        break;

                    case ODataReaderState.NestedResourceInfoStart:
                        resStack.TryPeek(out currentItem);
                        Debug.Assert(currentItem != null);

                        ODataItem item = odataReader.Item;
                        ODataNestedResourceInfoWrapper infoWrapper = new ODataNestedResourceInfoWrapper
                        {
                            NestedInfo = item as ODataNestedResourceInfo
                        };

                        currentItem.Append(infoWrapper);
                        resStack.Push(infoWrapper);
                        break;

                    case ODataReaderState.ResourceEnd:
                        //currWrapper = resStack.Peek();
                        //currWrapper.Resource = (ODataResource)odataReader.Item;
                        resStack.Pop();
                        break;

                    case ODataReaderState.ResourceSetEnd:

                        resStack.Pop();
                        break;

                    case ODataReaderState.NestedResourceInfoEnd:

                        item = odataReader.Item;

                        resStack.Pop();
                        break;
                    }
                }
            }

            if (topLevel == null)
            {
                return;
            }

            ODataResourceWrapper topLevelResource = topLevel as ODataResourceWrapper;

            if (topLevelResource != null)
            {
                foreach (var a in topLevelResource.Resource.Properties)
                {
                    Console.WriteLine(a.Name + ": " + a.Value);
                }

                foreach (var a in topLevelResource.NestedResourceInfos)
                {
                    Console.WriteLine(a.NestedInfo.Name);
                    ODataResourceSetWrapper setWrapper      = a.NestedWrapper as ODataResourceSetWrapper;
                    ODataResourceWrapper    resourceWrapper = a.NestedWrapper as ODataResourceWrapper;
                    if (resourceWrapper != null)
                    {
                        foreach (var prop in resourceWrapper.Resource.Properties)
                        {
                            Console.WriteLine(prop.Name + ": " + prop.Value);
                        }
                    }
                }
            }
        }