private string WriteAndVerifyCarEntry(StreamResponseMessage responseMessage, ODataWriter odataWriter,
                                              bool hasModel, string mimeType)
        {
            var carEntry = WritePayloadHelper.CreateCarEntry(hasModel);

            odataWriter.WriteStart(carEntry);

            // Finish writing the entry.
            odataWriter.WriteEnd();

            // Some very basic verification for the payload.
            bool verifyEntryCalled          = false;
            Action <ODataEntry> verifyEntry = (entry) =>
            {
                Assert.AreEqual(4, entry.Properties.Count(), "entry.Properties.Count");
                Assert.IsNotNull(entry.MediaResource, "entry.MediaResource");
                Assert.IsTrue(entry.EditLink.AbsoluteUri.Contains("Car(11)"), "entry.EditLink");
                Assert.IsTrue(entry.ReadLink == null || entry.ReadLink.AbsoluteUri.Contains("Car(11)"), "entry.ReadLink");
                Assert.AreEqual(1, entry.InstanceAnnotations.Count, "entry.InstanceAnnotations.Count");

                verifyEntryCalled = true;
            };

            Stream stream = responseMessage.GetStream();

            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                stream.Seek(0, SeekOrigin.Begin);
                WritePayloadHelper.ReadAndVerifyFeedEntryMessage(false, responseMessage, WritePayloadHelper.CarSet, WritePayloadHelper.CarType, null, verifyEntry,
                                                                 null);
                Assert.IsTrue(verifyEntryCalled, "Verification action not called.");
            }

            return(WritePayloadHelper.ReadStreamContent(stream));
        }
        private string WriteAndVerifySingleLink(StreamResponseMessage responseMessage, ODataMessageWriter messageWriter, string mimeType)
        {
            var link = new ODataEntityReferenceLink()
            {
                Url = new Uri(this.ServiceUri + "Order(-10)")
            };

            messageWriter.WriteEntityReferenceLink(link);
            var stream = responseMessage.GetStream();

            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                stream.Seek(0, SeekOrigin.Begin);
                var settings = new ODataMessageReaderSettings()
                {
                    BaseUri = this.ServiceUri
                };

                ODataMessageReader messageReader = new ODataMessageReader(responseMessage, settings, WritePayloadHelper.Model);

                ODataEntityReferenceLink linkRead = messageReader.ReadEntityReferenceLink();
                Assert.IsTrue(linkRead.Url.AbsoluteUri.Contains("Order(-10)"), "linkRead.Url");
            }

            return(WritePayloadHelper.ReadStreamContent(stream));
        }
Exemplo n.º 3
0
        public void SpecifySerializationInfoForFeedAndEntry()
        {
            foreach (var mimeType in this.testMimeTypes)
            {
                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri()
                {
                    ServiceRoot = this.ServiceUri
                };

                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataFeedWriter();
                    var feed        = this.CreatePersonFeed();
                    feed.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                    {
                        NavigationSourceName = "Person", NavigationSourceEntityTypeName = NameSpace + "Person"
                    });

                    var entry = new ODataEntry()
                    {
                        Id       = new Uri(ServiceUri + "Person(-5)"),
                        TypeName = NameSpace + "Employee"
                    };

                    var personEntryP1 = new ODataProperty {
                        Name = "PersonId", Value = -5
                    };
                    var personEntryP2 = new ODataProperty
                    {
                        Name  = "Name",
                        Value = "xhsdckkeqzvlnprheujeycqrglfehtdocildrequohlffazfgtvmddyqsaxrojqxrsckohrakdxlrghgmzqnyruzu"
                    };

                    var personEntryP3 = new ODataProperty {
                        Name = "ManagersPersonId", Value = -465010984
                    };

                    entry.Properties = new[] { personEntryP1, personEntryP2, personEntryP3 };
                    entry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                    {
                        NavigationSourceName = "Person", NavigationSourceEntityTypeName = NameSpace + "Person"
                    });

                    odataWriter.WriteStart(feed);
                    odataWriter.WriteStart(entry);
                    odataWriter.WriteEnd();
                    odataWriter.WriteEnd();
                    Stream stream = responseMessageWithoutModel.GetStream();
                    string result = WritePayloadHelper.ReadStreamContent(stream);
                    Assert.IsTrue(result.Contains(NameSpace + "Employee"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        Assert.IsTrue(result.Contains("$metadata#Person"));
                    }
                }
            }
        }
Exemplo n.º 4
0
        private string ReadFeedEntryMessage(bool isFeed, StreamResponseMessage responseMessage, string mimeType, IEdmEntitySet edmEntitySet, IEdmEntityType edmEntityType)
        {
            Stream stream = responseMessage.GetStream();

            stream.Seek(0, SeekOrigin.Begin);
            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                // read the response message using ODL reader, verify the read completed successfully
                WritePayloadHelper.ReadAndVerifyFeedEntryMessage(isFeed, responseMessage, edmEntitySet, edmEntityType, null, null, null);
            }

            return(WritePayloadHelper.ReadStreamContent(stream));
        }
        private string WriteAndVerifyCollection(StreamResponseMessage responseMessage, ODataCollectionWriter odataWriter,
                                                bool hasModel, string mimeType)
        {
            var collectionStart = new ODataCollectionStart()
            {
                Name = "BackupContactInfo", Count = 12, NextPageLink = new Uri("http://localhost")
            };

            if (!hasModel)
            {
                collectionStart.SetSerializationInfo(new ODataCollectionStartSerializationInfo()
                {
                    CollectionTypeName = "Collection(" + NameSpace + "ContactDetails)"
                });
            }

            odataWriter.WriteStart(collectionStart);
            odataWriter.WriteItem(WritePayloadHelper.CreatePrimaryContactODataComplexValue());
            odataWriter.WriteEnd();

            Stream stream = responseMessage.GetStream();

            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                stream.Seek(0, SeekOrigin.Begin);
                var settings = new ODataMessageReaderSettings()
                {
                    BaseUri = this.ServiceUri
                };

                ODataMessageReader    messageReader = new ODataMessageReader(responseMessage, settings, WritePayloadHelper.Model);
                ODataCollectionReader reader        = messageReader.CreateODataCollectionReader(WritePayloadHelper.ContactDetailType);
                bool collectionRead = false;
                while (reader.Read())
                {
                    if (reader.State == ODataCollectionReaderState.CollectionEnd)
                    {
                        collectionRead = true;
                    }
                }

                Assert.IsTrue(collectionRead, "collectionRead");
                Assert.AreEqual(ODataCollectionReaderState.Completed, reader.State);
            }

            return(WritePayloadHelper.ReadStreamContent(stream));
        }
        public void HandCraftEdmType()
        {
            foreach (var mimeType in this.testMimeTypes)
            {
                EdmModel edmModel = new EdmModel();

                EdmEntityType edmEntityType = new EdmEntityType(NameSpace, "Person");
                edmModel.AddElement(edmEntityType);
                var keyProperty = new EdmStructuralProperty(edmEntityType, "PersonId", EdmCoreModel.Instance.GetInt32(false));
                edmEntityType.AddKeys(new IEdmStructuralProperty[] { keyProperty });
                edmEntityType.AddProperty(keyProperty);
                var property = new EdmStructuralProperty(edmEntityType, "Name", EdmCoreModel.Instance.GetString(true));
                edmEntityType.AddKeys(new IEdmStructuralProperty[] { property });
                edmEntityType.AddProperty(property);

                var defaultContainer = new EdmEntityContainer(NameSpace, "DefaultContainer");
                edmModel.AddElement(defaultContainer);

                EdmEntitySet entitySet = new EdmEntitySet(defaultContainer, "Person", edmEntityType);
                defaultContainer.AddElement(entitySet);

                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri()
                {
                    ServiceRoot = this.ServiceUri
                };
                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataResourceWriter(entitySet, edmEntityType);

                    var entry = this.CreatePersonEntryWithoutSerializationInfo();
                    odataWriter.WriteStart(entry);
                    odataWriter.WriteEnd();

                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        Assert.IsTrue(
                            WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream())
                            .Contains("Person/$entity"));
                    }
                }
            }
        }
        private string WriteAndVerifyLinks(StreamResponseMessage responseMessage, ODataMessageWriter messageWriter, string mimeType)
        {
            var links = new ODataEntityReferenceLinks()
            {
                Links = new[]
                {
                    new ODataEntityReferenceLink()
                    {
                        Url = new Uri(this.ServiceUri + "Order(-10)")
                    },
                    new ODataEntityReferenceLink()
                    {
                        Url = new Uri(this.ServiceUri + "Order(-7)")
                    },
                },
                NextPageLink = new Uri(this.ServiceUri + "Customer(-10)/Orders/$ref?$skiptoken=-7")
            };

            messageWriter.WriteEntityReferenceLinks(links);

            Stream stream = responseMessage.GetStream();

            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                stream.Seek(0, SeekOrigin.Begin);
                var settings = new ODataMessageReaderSettings()
                {
                    BaseUri = this.ServiceUri
                };

                ODataMessageReader messageReader = new ODataMessageReader(responseMessage, settings, WritePayloadHelper.Model);

                ODataEntityReferenceLinks linksRead = messageReader.ReadEntityReferenceLinks();
                Assert.AreEqual(2, linksRead.Links.Count(), "linksRead.Links.Count");
                Assert.IsNotNull(linksRead.NextPageLink, "linksRead.NextPageLink");
            }

            return(WritePayloadHelper.ReadStreamContent(stream));
        }
        public void ChangeSerializationInfoAfterWriteStart()
        {
            foreach (var mimeType in this.testMimeTypes)
            {
                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri()
                {
                    ServiceRoot = this.ServiceUri
                };

                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataResourceWriter();
                    var entry       = this.CreatePersonEntryWithoutSerializationInfo();

                    entry.SetSerializationInfo(new ODataResourceSerializationInfo()
                    {
                        NavigationSourceName = "Person", NavigationSourceEntityTypeName = NameSpace + "Person"
                    });
                    odataWriter.WriteStart(entry);
                    entry.SetSerializationInfo(new ODataResourceSerializationInfo()
                    {
                        NavigationSourceName = "Parsen", NavigationSourceEntityTypeName = NameSpace + "Person"
                    });
                    odataWriter.WriteEnd();

                    Stream stream = responseMessageWithoutModel.GetStream();

                    // nometadata option does not write odata.metadata in the payload
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        Assert.IsTrue(WritePayloadHelper.ReadStreamContent(stream).Contains("Person/$entity"));
                    }
                }
            }
        }
Exemplo n.º 9
0
        public void WriteEntryWithWrongSerializationInfoWithModel()
        {
            bool[] autoComputeMetadataBools = new bool[] { true, false, };
            foreach (var mimeType in this.testMimeTypes)
            {
                foreach (var autoComputeMetadata in autoComputeMetadataBools)
                {
                    var settings = new ODataMessageWriterSettings();
                    settings.ODataUri = new ODataUri()
                    {
                        ServiceRoot = this.ServiceUri
                    };
                    settings.AutoComputePayloadMetadataInJson = autoComputeMetadata;

                    var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                    responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                    using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings, WritePayloadHelper.Model))
                    {
                        var odataWriter = messageWriter.CreateODataEntryWriter(WritePayloadHelper.PersonSet, WritePayloadHelper.PersonType);
                        var entry       = this.CreatePersonEntryWithoutSerializationInfo();

                        entry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                        {
                            NavigationSourceName = "Parsen", NavigationSourceEntityTypeName = NameSpace + "Person"
                        });
                        odataWriter.WriteStart(entry);
                        odataWriter.WriteEnd();
                        var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                        Assert.IsTrue(result.Contains("\"PersonId\":-5"));
                        if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                        {
                            // no metadata does not write odata.metadata
                            Assert.IsTrue(result.Contains("$metadata#Parsen/$entity"));
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
        private string WriteAndVerifyEmployeeEntry(StreamResponseMessage responseMessage, ODataWriter odataWriter,
                                                   bool hasExpectedType, string mimeType)
        {
            ODataEntry employeeEntry = WritePayloadHelper.CreateEmployeeEntry(false);
            ODataFeedAndEntrySerializationInfo serializationInfo = new ODataFeedAndEntrySerializationInfo()
            {
                NavigationSourceName           = "Person",
                NavigationSourceEntityTypeName = NameSpace + "Person",
            };

            if (hasExpectedType)
            {
                serializationInfo.ExpectedTypeName = NameSpace + "Employee";
            }

            employeeEntry.SetSerializationInfo(serializationInfo);
            odataWriter.WriteStart(employeeEntry);

            var employeeNavigation1 = new ODataNavigationLink()
            {
                Name         = "PersonMetadata",
                IsCollection = true,
                Url          = new Uri("Person(-3)/" + NameSpace + "Employee" + "/PersonMetadata", UriKind.Relative)
            };

            odataWriter.WriteStart(employeeNavigation1);
            odataWriter.WriteEnd();

            var employeeNavigation2 = new ODataNavigationLink()
            {
                Name         = "Manager",
                IsCollection = false,
                Url          = new Uri("Person(-3)/" + NameSpace + "Employee" + "/Manager", UriKind.Relative)
            };

            odataWriter.WriteStart(employeeNavigation2);
            odataWriter.WriteEnd();

            // Finish writing employeeEntry.
            odataWriter.WriteEnd();

            // Some very basic verification for the payload.
            bool verifyEntryCalled      = false;
            bool verifyNavigationCalled = false;

            Action <ODataEntry> verifyEntry = (entry) =>
            {
                Assert.IsTrue(entry.EditLink.AbsoluteUri.Contains("Person"), "entry.EditLink");
                verifyEntryCalled = true;
            };
            Action <ODataNavigationLink> verifyNavigation = (navigation) =>
            {
                Assert.IsTrue(navigation.Name == "PersonMetadata" || navigation.Name == "Manager", "navigation.Name");
                verifyNavigationCalled = true;
            };

            Stream stream = responseMessage.GetStream();

            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                stream.Seek(0, SeekOrigin.Begin);
                WritePayloadHelper.ReadAndVerifyFeedEntryMessage(false, responseMessage, WritePayloadHelper.PersonSet, WritePayloadHelper.PersonType, null,
                                                                 verifyEntry, verifyNavigation);
                Assert.IsTrue(verifyEntryCalled && verifyNavigationCalled,
                              "Verification action not called.");
            }

            return(WritePayloadHelper.ReadStreamContent(stream));
        }
Exemplo n.º 11
0
        private string WriteAndVerifyPersonFeed(StreamResponseMessage responseMessage, ODataWriter odataWriter,
                                                bool hasModel, string mimeType)
        {
            var personFeed = new ODataFeed()
            {
                Id        = new Uri(this.ServiceUri + "Person"),
                DeltaLink = new Uri(this.ServiceUri + "Person")
            };

            if (!hasModel)
            {
                personFeed.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                {
                    NavigationSourceName = "Person", NavigationSourceEntityTypeName = NameSpace + "Person"
                });
            }

            odataWriter.WriteStart(personFeed);

            ODataEntry personEntry = WritePayloadHelper.CreatePersonEntry(hasModel);

            odataWriter.WriteStart(personEntry);

            var personNavigation = new ODataNavigationLink()
            {
                Name         = "PersonMetadata",
                IsCollection = true,
                Url          = new Uri("Person(-5)/PersonMetadata", UriKind.Relative)
            };

            odataWriter.WriteStart(personNavigation);
            odataWriter.WriteEnd();

            // Finish writing personEntry.
            odataWriter.WriteEnd();

            ODataEntry employeeEntry = WritePayloadHelper.CreateEmployeeEntry(hasModel);

            odataWriter.WriteStart(employeeEntry);

            var employeeNavigation1 = new ODataNavigationLink()
            {
                Name         = "PersonMetadata",
                IsCollection = true,
                Url          = new Uri("Person(-3)/" + NameSpace + "Employee" + "/PersonMetadata", UriKind.Relative)
            };

            odataWriter.WriteStart(employeeNavigation1);
            odataWriter.WriteEnd();

            var employeeNavigation2 = new ODataNavigationLink()
            {
                Name         = "Manager",
                IsCollection = false,
                Url          = new Uri("Person(-3)/" + NameSpace + "Employee" + "/Manager", UriKind.Relative)
            };

            odataWriter.WriteStart(employeeNavigation2);
            odataWriter.WriteEnd();

            // Finish writing employeeEntry.
            odataWriter.WriteEnd();

            ODataEntry specialEmployeeEntry = WritePayloadHelper.CreateSpecialEmployeeEntry(hasModel);

            odataWriter.WriteStart(specialEmployeeEntry);

            var specialEmployeeNavigation1 = new ODataNavigationLink()
            {
                Name         = "PersonMetadata",
                IsCollection = true,
                Url          = new Uri("Person(-10)/" + NameSpace + "SpecialEmployee" + "/PersonMetadata", UriKind.Relative)
            };

            odataWriter.WriteStart(specialEmployeeNavigation1);
            odataWriter.WriteEnd();

            var specialEmployeeNavigation2 = new ODataNavigationLink()
            {
                Name         = "Manager",
                IsCollection = false,
                Url          = new Uri("Person(-10)/" + NameSpace + "SpecialEmployee" + "/Manager", UriKind.Relative)
            };

            odataWriter.WriteStart(specialEmployeeNavigation2);
            odataWriter.WriteEnd();

            var specialEmployeeNavigation3 = new ODataNavigationLink()
            {
                Name         = "Car",
                IsCollection = false,
                Url          = new Uri("Person(-10)/" + NameSpace + "SpecialEmployee" + "/Manager", UriKind.Relative)
            };

            odataWriter.WriteStart(specialEmployeeNavigation3);
            odataWriter.WriteEnd();

            // Finish writing specialEmployeeEntry.
            odataWriter.WriteEnd();

            // Finish writing the feed.
            odataWriter.WriteEnd();

            // Some very basic verification for the payload.
            bool verifyFeedCalled         = false;
            bool verifyEntryCalled        = false;
            bool verifyNavigationCalled   = false;
            Action <ODataFeed> verifyFeed = (feed) =>
            {
                if (mimeType != MimeTypes.ApplicationAtomXml)
                {
                    Assert.IsTrue(feed.DeltaLink.AbsoluteUri.Contains("Person"));
                }
                verifyFeedCalled = true;
            };
            Action <ODataEntry> verifyEntry = (entry) =>
            {
                Assert.IsTrue(entry.EditLink.AbsoluteUri.EndsWith("Person(-5)") ||
                              entry.EditLink.AbsoluteUri.EndsWith("Person(-3)/" + NameSpace + "Employee") ||
                              entry.EditLink.AbsoluteUri.EndsWith("Person(-10)/" + NameSpace + "SpecialEmployee"));
                verifyEntryCalled = true;
            };
            Action <ODataNavigationLink> verifyNavigation = (navigation) =>
            {
                Assert.IsTrue(navigation.Name == "PersonMetadata" || navigation.Name == "Manager" || navigation.Name == "Car");
                verifyNavigationCalled = true;
            };

            Stream stream = responseMessage.GetStream();

            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                stream.Seek(0, SeekOrigin.Begin);
                WritePayloadHelper.ReadAndVerifyFeedEntryMessage(true, responseMessage, WritePayloadHelper.PersonSet, WritePayloadHelper.PersonType, verifyFeed,
                                                                 verifyEntry, verifyNavigation);
                Assert.IsTrue(verifyFeedCalled && verifyEntryCalled && verifyNavigationCalled,
                              "Verification action not called.");
            }

            return(WritePayloadHelper.ReadStreamContent(stream));
        }
Exemplo n.º 12
0
        private string WriteAndVerifyExpandedCustomerEntry(StreamResponseMessage responseMessage,
                                                           ODataWriter odataWriter, bool hasModel, string mimeType)
        {
            ODataEntry customerEntry = WritePayloadHelper.CreateCustomerEntry(hasModel);

            odataWriter.WriteStart(customerEntry);

            // write non-expanded navigations
            foreach (var navigation in WritePayloadHelper.CreateCustomerNavigationLinks())
            {
                odataWriter.WriteStart(navigation);
                odataWriter.WriteEnd();
            }

            // write expanded navigation
            var expandedNavigation = new ODataNavigationLink()
            {
                Name         = "Logins",
                IsCollection = true,
                Url          = new Uri(this.ServiceUri + "Customer(-9)/Logins")
            };

            odataWriter.WriteStart(expandedNavigation);

            var loginFeed = new ODataFeed()
            {
                Id = new Uri(this.ServiceUri + "Customer(-9)/Logins")
            };

            if (!hasModel)
            {
                loginFeed.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                {
                    NavigationSourceName = "Login", NavigationSourceEntityTypeName = NameSpace + "Login"
                });
            }

            odataWriter.WriteStart(loginFeed);

            var loginEntry = WritePayloadHelper.CreateLoginEntry(hasModel);

            odataWriter.WriteStart(loginEntry);

            foreach (var navigation in WritePayloadHelper.CreateLoginNavigationLinks())
            {
                odataWriter.WriteStart(navigation);
                odataWriter.WriteEnd();
            }

            // Finish writing loginEntry.
            odataWriter.WriteEnd();

            // Finish writing the loginFeed.
            odataWriter.WriteEnd();

            // Finish writing expandedNavigation.
            odataWriter.WriteEnd();

            // Finish writing customerEntry.
            odataWriter.WriteEnd();

            // Some very basic verification for the payload.
            bool verifyFeedCalled         = false;
            int  verifyEntryCalled        = 0;
            bool verifyNavigationCalled   = false;
            Action <ODataFeed> verifyFeed = (feed) =>
            {
                verifyFeedCalled = true;
            };

            Action <ODataEntry> verifyEntry = (entry) =>
            {
                if (entry.TypeName.Contains("Customer"))
                {
                    Assert.AreEqual(7, entry.Properties.Count());
                }

                if (entry.TypeName.Contains("Login"))
                {
                    Assert.AreEqual(2, entry.Properties.Count());
                }

                verifyEntryCalled++;
            };

            Action <ODataNavigationLink> verifyNavigation = (navigation) =>
            {
                Assert.IsNotNull(navigation.Name);
                verifyNavigationCalled = true;
            };

            Stream stream = responseMessage.GetStream();

            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                stream.Seek(0, SeekOrigin.Begin);
                WritePayloadHelper.ReadAndVerifyFeedEntryMessage(false, responseMessage, WritePayloadHelper.CustomerSet, WritePayloadHelper.CustomerType,
                                                                 verifyFeed, verifyEntry, verifyNavigation);
                Assert.IsTrue(verifyFeedCalled && verifyEntryCalled == 2 && verifyNavigationCalled,
                              "Verification action not called.");
            }

            return(WritePayloadHelper.ReadStreamContent(stream));
        }
Exemplo n.º 13
0
        public void WriteEntryWithWrongSerializationInfo()
        {
            foreach (var mimeType in this.testMimeTypes)
            {
                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri()
                {
                    ServiceRoot = this.ServiceUri
                };

                // wrong EntitySetName for entry
                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataEntryWriter();
                    var entry       = this.CreatePersonEntryWithoutSerializationInfo();

                    entry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                    {
                        NavigationSourceName = "Parsen", NavigationSourceEntityTypeName = NameSpace + "Person"
                    });
                    odataWriter.WriteStart(entry);
                    odataWriter.WriteEnd();
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains("Person(-5)\",\"PersonId\":-5"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Parsen/$entity"));
                    }
                }

                // wrong EntitySetName for feed
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataFeedWriter();

                    var feed = this.CreatePersonFeed();
                    feed.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                    {
                        NavigationSourceName = "Parsen", NavigationSourceEntityTypeName = NameSpace + "Person"
                    });
                    var entry = this.CreatePersonEntryWithoutSerializationInfo();
                    odataWriter.WriteStart(feed);
                    odataWriter.WriteStart(entry);
                    odataWriter.WriteEnd();
                    odataWriter.WriteEnd();
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains("Person(-5)\",\"PersonId\":-5"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Parsen\""));
                    }
                }

                // wrong collection type name
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter    = messageWriter.CreateODataCollectionWriter();
                    var collctionStart = new ODataCollectionStart()
                    {
                        Name = "BackupContactInfo"
                    };
                    collctionStart.SetSerializationInfo(new ODataCollectionStartSerializationInfo()
                    {
                        CollectionTypeName = "Collection(" + NameSpace + "ContactDETAIL)"
                    });

                    odataWriter.WriteStart(collctionStart);
                    odataWriter.WriteItem(
                        WritePayloadHelper.CreatePrimaryContactODataComplexValue());
                    odataWriter.WriteEnd();
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains("value\":[{\"EmailBag\":["));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Collection(" + NameSpace + "ContactDETAIL)"));
                    }
                }

                // The following two cases of wrong reference link info is no longer active. The ODataEntityReferenceLinkSerializationInfo is ignored when writer writes the payload.
                // Consider removing them.

                // wrong reference link info
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var link = new ODataEntityReferenceLink()
                    {
                        Url = new Uri(this.ServiceUri + "Order(-10)")
                    };
                    messageWriter.WriteEntityReferenceLink(link);
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains(this.ServiceUri + "Order(-10)"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.context
                        Assert.IsTrue(result.Contains("$metadata#$ref"));
                    }
                }

                // wrong reference links info
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var links = new ODataEntityReferenceLinks()
                    {
                        Links = new[]
                        {
                            new ODataEntityReferenceLink()
                            {
                                Url = new Uri(this.ServiceUri + "Order(-10)")
                            }
                        },
                    };

                    messageWriter.WriteEntityReferenceLinks(links);
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains(this.ServiceUri + "Order(-10)"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Collection($ref)"));
                    }
                }
            }
        }
Exemplo n.º 14
0
        private string WriteAndVerifyRequestMessage(ODataMessageWriterSettings settings, string mimeType, bool hasModel)
        {
            // create an order entry to POST
            var order = new ODataEntry()
            {
                TypeName = NameSpace + "Order"
            };

            var orderP1 = new ODataProperty {
                Name = "OrderId", Value = -10
            };
            var orderp2 = new ODataProperty {
                Name = "CustomerId", Value = 8212
            };
            var orderp3 = new ODataProperty {
                Name = "Concurrency", Value = null
            };

            order.Properties = new[] { orderP1, orderp2, orderp3 };
            if (!hasModel)
            {
                order.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                {
                    NavigationSourceName = "Order", NavigationSourceEntityTypeName = NameSpace + "Order"
                });
                orderP1.SetSerializationInfo(new ODataPropertySerializationInfo()
                {
                    PropertyKind = ODataPropertyKind.Key
                });
            }

            Dictionary <string, object> expectedOrderObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.OrderType, "Order(-10)", order, hasModel);

            // write the request message and read using ODL reader
            var requestMessage = new StreamRequestMessage(new MemoryStream(), new Uri(this.ServiceUri + "Order"), "POST");

            requestMessage.SetHeader("Content-Type", mimeType);
            string result = string.Empty;

            using (var messageWriter = this.CreateODataMessageWriter(requestMessage, settings, hasModel))
            {
                var odataWriter = this.CreateODataEntryWriter(messageWriter, WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType, hasModel);
                odataWriter.WriteStart(order);
                odataWriter.WriteEnd();

                Stream stream = requestMessage.GetStream();
                result = WritePayloadHelper.ReadStreamContent(stream);
                if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    var readerSetting = new ODataMessageReaderSettings()
                    {
                        BaseUri = this.ServiceUri
                    };
                    ODataMessageReader messageReader = new ODataMessageReader(requestMessage, readerSetting, WritePayloadHelper.Model);
                    ODataReader        reader        = messageReader.CreateODataEntryReader(WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType);
                    bool verifyEntryCalled           = false;
                    while (reader.Read())
                    {
                        if (reader.State == ODataReaderState.EntryEnd)
                        {
                            ODataEntry entry = reader.Item as ODataEntry;
                            Assert.AreEqual(3, entry.Properties.Count(), "entry.Properties.Count");
                            verifyEntryCalled = true;
                        }
                    }

                    Assert.AreEqual(ODataReaderState.Completed, reader.State);
                    Assert.IsTrue(verifyEntryCalled, "verifyEntryCalled");
                }
            }

            // For Json light, verify the resulting metadata is as expected
            if (mimeType != MimeTypes.ApplicationAtomXml)
            {
                JavaScriptSerializer        jScriptSerializer = new JavaScriptSerializer();
                Dictionary <string, object> resultObject      = jScriptSerializer.DeserializeObject(result) as Dictionary <string, object>;

                // AutoComputePayloadMetadataInJson has no effect on request message metadata
                Assert.AreEqual(this.ServiceUri + "$metadata#Order/$entity", resultObject.Single(e => e.Key == JsonLightConstants.ODataContextAnnotationName).Value);
                resultObject.Remove(JsonLightConstants.ODataContextAnnotationName);

                foreach (var pair in resultObject)
                {
                    Assert.IsFalse(pair.Key.Contains("odata.") || pair.Key.StartsWith("#"));
                }
            }

            return(result);
        }
Exemplo n.º 15
0
        public void WriteWithoutSerializationInfo()
        {
            foreach (var mimeType in this.testMimeTypes)
            {
                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri()
                {
                    ServiceRoot = this.ServiceUri
                };

                // write entry without serialization info
                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter   = messageWriter.CreateODataResourceWriter();
                    var entry         = this.CreatePersonEntryWithoutSerializationInfo();
                    var expectedError = mimeType.Contains(MimeTypes.ODataParameterNoMetadata)
                                               ? null
                                               : "ODataResourceTypeContext_MetadataOrSerializationInfoMissing";
                    AssertThrows <ODataException>(() => odataWriter.WriteStart(entry), expectedError);
                }

                // write feed without serialization info
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataResourceSetWriter();
                    var feed        = this.CreatePersonFeed();
                    var entry       = this.CreatePersonEntryWithoutSerializationInfo();
                    entry.SetSerializationInfo(new ODataResourceSerializationInfo()
                    {
                        NavigationSourceName = "Person", NavigationSourceEntityTypeName = NameSpace + "Person"
                    });
                    var expectedError = mimeType.Contains(MimeTypes.ODataParameterNoMetadata)
                                               ? null
                                               : "ODataResourceTypeContext_MetadataOrSerializationInfoMissing";
                    AssertThrows <ODataException>(() => odataWriter.WriteStart(feed), expectedError);
                }

                // write collection without serialization info
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter     = messageWriter.CreateODataCollectionWriter();
                    var collectionStart = new ODataCollectionStart()
                    {
                        Name = "BackupContactInfo"
                    };
                    var expectedError = mimeType.Contains(MimeTypes.ODataParameterNoMetadata)
                                               ? null
                                               : "ODataContextUriBuilder_TypeNameMissingForTopLevelCollection";
                    AssertThrows <ODataException>(() => odataWriter.WriteStart(collectionStart), expectedError);
                }

                // write a reference link without serialization info
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var link = new ODataEntityReferenceLink()
                    {
                        Url = new Uri(this.ServiceUri + "Order(-10)")
                    };
                    messageWriter.WriteEntityReferenceLink(link);

                    // No exception is expected. Simply verify the writing succeeded.
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        Stream stream = responseMessageWithoutModel.GetStream();
                        Assert.IsTrue(WritePayloadHelper.ReadStreamContent(stream).Contains("$metadata#$ref"));
                    }
                }

                // write reference links without serialization info
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var links = new ODataEntityReferenceLinks()
                    {
                        Links = new[]
                        {
                            new ODataEntityReferenceLink()
                            {
                                Url = new Uri(this.ServiceUri + "Order(-10)")
                            }
                        },
                    };
                    messageWriter.WriteEntityReferenceLinks(links);

                    // No exception is expected. Simply verify the writing succeeded.
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        Stream stream = responseMessageWithoutModel.GetStream();
                        Assert.IsTrue(WritePayloadHelper.ReadStreamContent(stream).Contains("$metadata#Collection($ref)"));
                    }
                }

                // write request message containing an entry
                var requestMessageWithoutModel = new StreamRequestMessage(new MemoryStream(),
                                                                          new Uri(this.ServiceUri + "Person"), "POST");
                requestMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(requestMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataResourceWriter();
                    var entry       = this.CreatePersonEntryWithoutSerializationInfo();
                    odataWriter.WriteStart(entry);
                    odataWriter.WriteEnd();
                    Stream stream = requestMessageWithoutModel.GetStream();
                    Assert.IsTrue(WritePayloadHelper.ReadStreamContent(stream).Contains("Person(-5)\",\"PersonId\":-5,\"Name\":\"xhsdckkeqzvlnprheujeycqrglfehtdocildrequohlffazfgtvmddyqsaxrojqxrsckohrakdxlrghgmzqnyruzu\""));
                }
            }
        }
Exemplo n.º 16
0
        public void WriteEntryWithWrongSerializationInfo()
        {
            foreach (var mimeType in this.testMimeTypes)
            {
                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri()
                {
                    ServiceRoot = this.ServiceUri
                };

                // wrong EntitySetName for entry
                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataResourceWriter();
                    var entry       = this.CreatePersonEntryWithoutSerializationInfo();

                    entry.SetSerializationInfo(new ODataResourceSerializationInfo()
                    {
                        NavigationSourceName = "Parsen", NavigationSourceEntityTypeName = NameSpace + "Person"
                    });
                    odataWriter.WriteStart(entry);
                    odataWriter.WriteEnd();
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains("\"PersonId\":-5"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Parsen/$entity"));
                        Assert.IsTrue(result.Contains("Person(-5)\",\"PersonId\":-5"));
                    }
                    else
                    {
                        Assert.IsFalse(result.Contains("Person(-5)\",\"PersonId\":-5"));
                    }
                }

                // wrong EntitySetName for feed
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataResourceSetWriter();

                    var feed = this.CreatePersonFeed();
                    feed.SetSerializationInfo(new ODataResourceSerializationInfo()
                    {
                        NavigationSourceName = "Parsen", NavigationSourceEntityTypeName = NameSpace + "Person"
                    });
                    var entry = this.CreatePersonEntryWithoutSerializationInfo();
                    odataWriter.WriteStart(feed);
                    odataWriter.WriteStart(entry);
                    odataWriter.WriteEnd();
                    odataWriter.WriteEnd();
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains("\"PersonId\":-5"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Parsen\""));
                        Assert.IsTrue(result.Contains("Person(-5)\",\"PersonId\":-5"));
                    }
                    else
                    {
                        Assert.IsFalse(result.Contains("Person(-5)\",\"PersonId\":-5"));
                    }
                }

                // wrong complex collection type name
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter       = messageWriter.CreateODataResourceSetWriter();
                    var complexCollection = new ODataResourceSetWrapper()
                    {
                        ResourceSet = new ODataResourceSet(),
                        Resources   = new List <ODataResourceWrapper>()
                        {
                            WritePayloadHelper.CreatePrimaryContactODataWrapper()
                        }
                    };

                    complexCollection.ResourceSet.SetSerializationInfo(new ODataResourceSerializationInfo()
                    {
                        ExpectedTypeName = NameSpace + "ContactDETAIL"
                    });
                    ODataWriterHelper.WriteResourceSet(odataWriter, complexCollection);
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // [{"@odata.type":"#Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails","*****@*****.**":"#Collection(String)"...
                        Assert.IsTrue(result.Contains("\"value\":[{\"@odata.type\":\"#Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails\",\"EmailBag"));

                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Collection(" + NameSpace + "ContactDETAIL)"));
                    }
                    else
                    {
                        Assert.IsFalse(result.Contains("\"@odata.type\":\"#Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails\""));
                    }
                }

                // The following two cases of wrong reference link info is no longer active. The ODataEntityReferenceLinkSerializationInfo is ignored when writer writes the payload.
                // Consider removing them.

                // wrong reference link info
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var link = new ODataEntityReferenceLink()
                    {
                        Url = new Uri(this.ServiceUri + "Order(-10)")
                    };
                    messageWriter.WriteEntityReferenceLink(link);
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains(this.ServiceUri + "Order(-10)"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.context
                        Assert.IsTrue(result.Contains("$metadata#$ref"));
                    }
                }

                // wrong reference links info
                responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var links = new ODataEntityReferenceLinks()
                    {
                        Links = new[]
                        {
                            new ODataEntityReferenceLink()
                            {
                                Url = new Uri(this.ServiceUri + "Order(-10)")
                            }
                        },
                    };

                    messageWriter.WriteEntityReferenceLinks(links);
                    var result = WritePayloadHelper.ReadStreamContent(responseMessageWithoutModel.GetStream());
                    Assert.IsTrue(result.Contains(this.ServiceUri + "Order(-10)"));
                    if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
                    {
                        // no metadata does not write odata.metadata
                        Assert.IsTrue(result.Contains("$metadata#Collection($ref)"));
                    }
                }
            }
        }
Exemplo n.º 17
0
        private string WriteAndVerifyRequestMessage(StreamRequestMessage requestMessageWithoutModel,
                                                    ODataWriter odataWriter, bool hasModel, string mimeType)
        {
            var order = new ODataEntry()
            {
                Id       = new Uri(this.ServiceUri + "Order(-10)"),
                TypeName = NameSpace + "Order"
            };

            var orderP1 = new ODataProperty {
                Name = "OrderId", Value = -10
            };
            var orderp2 = new ODataProperty {
                Name = "CustomerId", Value = 8212
            };
            var orderp3 = new ODataProperty {
                Name = "Concurrency", Value = null
            };

            order.Properties = new[] { orderP1, orderp2, orderp3 };
            if (!hasModel)
            {
                order.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                {
                    NavigationSourceName = "Order", NavigationSourceEntityTypeName = NameSpace + "Order"
                });
                orderP1.SetSerializationInfo(new ODataPropertySerializationInfo()
                {
                    PropertyKind = ODataPropertyKind.Key
                });
            }

            odataWriter.WriteStart(order);
            odataWriter.WriteEnd();

            Stream stream = requestMessageWithoutModel.GetStream();

            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                stream.Seek(0, SeekOrigin.Begin);
                var settings = new ODataMessageReaderSettings()
                {
                    BaseUri = this.ServiceUri
                };
                ODataMessageReader messageReader = new ODataMessageReader(requestMessageWithoutModel, settings,
                                                                          WritePayloadHelper.Model);
                ODataReader reader            = messageReader.CreateODataEntryReader(WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType);
                bool        verifyEntryCalled = false;
                while (reader.Read())
                {
                    if (reader.State == ODataReaderState.EntryEnd)
                    {
                        ODataEntry entry = reader.Item as ODataEntry;
                        Assert.IsTrue(entry.Id.ToString().Contains("Order(-10)"), "entry.Id");
                        Assert.AreEqual(3, entry.Properties.Count(), "entry.Properties.Count");
                        verifyEntryCalled = true;
                    }
                }

                Assert.AreEqual(ODataReaderState.Completed, reader.State);
                Assert.IsTrue(verifyEntryCalled, "verifyEntryCalled");
            }

            return(WritePayloadHelper.ReadStreamContent(stream));
        }
Exemplo n.º 18
0
        private string WriteAndVerifyOrderFeed(StreamResponseMessage responseMessage, ODataWriter odataWriter,
                                               bool hasModel, string mimeType)
        {
            var orderFeed = new ODataFeed()
            {
                Id           = new Uri(this.ServiceUri + "Order"),
                NextPageLink = new Uri(this.ServiceUri + "Order?$skiptoken=-9"),
            };

            if (!hasModel)
            {
                orderFeed.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo()
                {
                    NavigationSourceName = "Order", NavigationSourceEntityTypeName = NameSpace + "Order"
                });
            }

            odataWriter.WriteStart(orderFeed);

            var orderEntry1 = WritePayloadHelper.CreateOrderEntry1(hasModel);

            odataWriter.WriteStart(orderEntry1);

            var orderEntry1Navigation1 = new ODataNavigationLink()
            {
                Name         = "Customer",
                IsCollection = false,
                Url          = new Uri(this.ServiceUri + "Order(-10)/Customer")
            };

            odataWriter.WriteStart(orderEntry1Navigation1);
            odataWriter.WriteEnd();

            var orderEntry1Navigation2 = new ODataNavigationLink()
            {
                Name         = "Login",
                IsCollection = false,
                Url          = new Uri(this.ServiceUri + "Order(-10)/Login")
            };

            odataWriter.WriteStart(orderEntry1Navigation2);
            odataWriter.WriteEnd();

            // Finish writing orderEntry1.
            odataWriter.WriteEnd();

            var orderEntry2 = WritePayloadHelper.CreateOrderEntry2(hasModel);

            odataWriter.WriteStart(orderEntry2);

            var orderEntry2Navigation1 = new ODataNavigationLink()
            {
                Name         = "Customer",
                IsCollection = false,
                Url          = new Uri(this.ServiceUri + "Order(-9)/Customer")
            };

            odataWriter.WriteStart(orderEntry2Navigation1);
            odataWriter.WriteEnd();

            var orderEntry2Navigation2 = new ODataNavigationLink()
            {
                Name         = "Login",
                IsCollection = false,
                Url          = new Uri(this.ServiceUri + "Order(-9)/Login")
            };

            odataWriter.WriteStart(orderEntry2Navigation2);
            odataWriter.WriteEnd();

            // Finish writing orderEntry2.
            odataWriter.WriteEnd();

            // Finish writing the feed.
            odataWriter.WriteEnd();

            // Some very basic verification for the payload.
            bool verifyFeedCalled         = false;
            bool verifyEntryCalled        = false;
            bool verifyNavigationCalled   = false;
            Action <ODataFeed> verifyFeed = (feed) =>
            {
                Assert.IsNotNull(feed.NextPageLink, "feed.NextPageLink");
                verifyFeedCalled = true;
            };
            Action <ODataEntry> verifyEntry = (entry) =>
            {
                Assert.AreEqual(3, entry.Properties.Count(), "entry.Properties.Count");
                verifyEntryCalled = true;
            };
            Action <ODataNavigationLink> verifyNavigation = (navigation) =>
            {
                Assert.IsTrue(navigation.Name == "Customer" || navigation.Name == "Login", "navigation.Name");
                verifyNavigationCalled = true;
            };

            Stream stream = responseMessage.GetStream();

            if (!mimeType.Contains(MimeTypes.ODataParameterNoMetadata))
            {
                stream.Seek(0, SeekOrigin.Begin);
                WritePayloadHelper.ReadAndVerifyFeedEntryMessage(true, responseMessage, WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType, verifyFeed,
                                                                 verifyEntry, verifyNavigation);
                Assert.IsTrue(verifyFeedCalled && verifyEntryCalled && verifyNavigationCalled,
                              "Verification action not called.");
            }

            return(WritePayloadHelper.ReadStreamContent(stream));
        }