public void OrderFeedTest()
        {
            foreach (var mimeType in this.mimeTypes)
            {
                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri() { ServiceRoot = this.ServiceUri };
                string outputWithModel = null;
                string outputWithoutModel = null;

                var responseMessageWithModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithModel, settings, WritePayloadHelper.Model))
                {
                    var odataWriter = messageWriter.CreateODataFeedWriter(WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType);
                    outputWithModel = this.WriteAndVerifyOrderFeed(responseMessageWithModel, odataWriter, true, mimeType);
                }

                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataFeedWriter();
                    outputWithoutModel = this.WriteAndVerifyOrderFeed(responseMessageWithoutModel, odataWriter, false,
                                                                      mimeType);
                }

                WritePayloadHelper.VerifyPayloadString(outputWithModel, outputWithoutModel, mimeType);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Read the response message and perform given verifications
        /// </summary>
        /// <param name="isFeed">Whether the response has a feed</param>
        /// <param name="responseMessage">The response message</param>
        /// <param name="expectedSet">Expected IEdmEntitySet</param>
        /// <param name="expectedType">Expected IEdmEntityType</param>
        /// <param name="verifyFeed">Action to verify the feed</param>
        /// <param name="verifyEntry">Action to verify the entry</param>
        /// <param name="verifyNavigation">Action to verify the navigation</param>
        public static void ReadAndVerifyFeedEntryMessage(bool isFeed, StreamResponseMessage responseMessage,
                                   IEdmEntitySet expectedSet, IEdmEntityType expectedType,
                                   Action<ODataFeed> verifyFeed, Action<ODataEntry> verifyEntry,
                                   Action<ODataNavigationLink> verifyNavigation)
        {
            var settings = new ODataMessageReaderSettings() { BaseUri = ServiceUri };
            settings.ShouldIncludeAnnotation = s => true;
            ODataMessageReader messageReader = new ODataMessageReader(responseMessage, settings, Model);
            ODataReader reader = isFeed
                                     ? messageReader.CreateODataFeedReader(expectedSet, expectedType)
                                     : messageReader.CreateODataEntryReader(expectedSet, expectedType);
            while (reader.Read())
            {
                switch (reader.State)
                {
                    case ODataReaderState.FeedEnd:
                        {
                            if (verifyFeed != null)
                            {
                                verifyFeed((ODataFeed)reader.Item);
                            }

                            break;
                        }
                    case ODataReaderState.EntryEnd:
                        {
                            if (verifyEntry != null)
                            {
                                verifyEntry((ODataEntry)reader.Item);
                            }

                            break;
                        }
                    case ODataReaderState.NavigationLinkEnd:
                        {
                            if (verifyNavigation != null)
                            {
                                verifyNavigation((ODataNavigationLink)reader.Item);
                            }

                            break;
                        }
                }
            }

            Assert.AreEqual(ODataReaderState.Completed, reader.State);
        }
        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);
        }
        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 PersonFeedTest()
        {
            foreach (var mimeType in this.mimeTypes)
            {
                var settings = new ODataMessageWriterSettings() { PayloadBaseUri = this.ServiceUri };
                settings.ODataUri = new ODataUri() { ServiceRoot = this.ServiceUri };

                string outputWithModel = null;
                string outputWithoutModel = null;

                var responseMessageWithModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithModel, settings, WritePayloadHelper.Model))
                {
                    var odataWriter = messageWriter.CreateODataFeedWriter(WritePayloadHelper.PersonSet, WritePayloadHelper.PersonType);
                    outputWithModel = this.WriteAndVerifyPersonFeed(responseMessageWithModel, odataWriter, true,
                                                                   mimeType);
                }

                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataFeedWriter();
                    outputWithoutModel = this.WriteAndVerifyPersonFeed(responseMessageWithoutModel, odataWriter, false,
                                                                       mimeType);
                }

                WritePayloadHelper.VerifyPayloadString(outputWithModel, outputWithoutModel, mimeType);

                if (mimeType.Contains(MimeTypes.ODataParameterMinimalMetadata) || mimeType.Contains(MimeTypes.ODataParameterFullMetadata))
                {
                    Assert.IsTrue(outputWithoutModel.Contains(this.ServiceUri + "$metadata#Person\""));
                }

                if (mimeType.Contains(MimeTypes.ApplicationJsonLight))
                {
                    // odata.type is included in json light payload only if entry typename is different than serialization info
                    Assert.IsFalse(outputWithoutModel.Contains("{\"@odata.type\":\"" + "#" + NameSpace + "Person\","), "odata.type Person");
                    Assert.IsTrue(outputWithoutModel.Contains("{\"@odata.type\":\"" + "#" + NameSpace + "Employee\","), "odata.type Employee");
                    Assert.IsTrue(outputWithoutModel.Contains("{\"@odata.type\":\"" + "#" + NameSpace + "SpecialEmployee\","), "odata.type SpecialEmployee");
                }
            }
        }
        private string WriteAndVerifyOrderFeed(ODataMessageWriterSettings settings, string mimeType, bool hasModel)
        {
            // create a feed with two entries
            var orderFeed = new ODataFeed()
            {
                NextPageLink = new Uri(this.ServiceUri + "Order?$skiptoken=-9"),
                Count = 9999
            };

            if (mimeType == MimeTypes.ApplicationAtomXml)
            {
                orderFeed.Id = new Uri(this.ServiceUri + "Order");
            }

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

            var orderEntry1 = WritePayloadHelper.CreateOrderEntry1NoMetadata(hasModel);
            Dictionary<string, object> expectedOrderObject1 = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.OrderType, "Order(-10)", orderEntry1, hasModel);

            var orderEntry2 = WritePayloadHelper.CreateOrderEntry2NoMetadata(hasModel);
            Dictionary<string, object> expectedOrderObject2 = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.OrderType, "Order(-9)", orderEntry2, hasModel);
            var orderEntry2Navigation = WritePayloadHelper.AddOrderEntryCustomNavigation(orderEntry2, expectedOrderObject2, hasModel);

            // write the response message and read using ODL reader
            var responseMessage = new StreamResponseMessage(new MemoryStream());
            responseMessage.SetHeader("Content-Type", mimeType);
            string result = string.Empty;
            using (var messageWriter = this.CreateODataMessageWriter(responseMessage, settings, hasModel))
            {
                var odataWriter = this.CreateODataFeedWriter(messageWriter, WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType, hasModel);

                odataWriter.WriteStart(orderFeed);

                odataWriter.WriteStart(orderEntry1);
                odataWriter.WriteEnd();

                odataWriter.WriteStart(orderEntry2);
                odataWriter.WriteStart(orderEntry2Navigation);
                odataWriter.WriteEnd();
                odataWriter.WriteEnd();

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

                result = this.ReadFeedEntryMessage(true, responseMessage, mimeType, WritePayloadHelper.OrderSet, WritePayloadHelper.OrderType);
            }

            if (mimeType != MimeTypes.ApplicationAtomXml)
            {
                JavaScriptSerializer jScriptSerializer = new JavaScriptSerializer();
                Dictionary<string, object> resultObject = jScriptSerializer.DeserializeObject(result) as Dictionary<string, object>;

                Assert.AreEqual(this.ServiceUri + "Order?$skiptoken=-9", resultObject.Single(e => e.Key == (JsonLightConstants.ODataNextLinkAnnotationName)).Value, "Feed next link");
                Assert.AreEqual(9999, resultObject.Single(e => e.Key == (JsonLightConstants.ODataCountAnnotationName)).Value, "Feed count");
                resultObject.Remove(JsonLightConstants.ODataNextLinkAnnotationName);
                resultObject.Remove(JsonLightConstants.ODataCountAnnotationName);

                VerifyODataContextAnnotation(this.ServiceUri + "$metadata#Order", resultObject, mimeType);

                VerifyEntry(expectedOrderObject1, orderEntry1, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).First() as Dictionary<string, object>, mimeType, settings.AutoComputePayloadMetadataInJson);
                VerifyEntry(expectedOrderObject2, orderEntry2, new ODataNavigationLink[] { orderEntry2Navigation }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).Last() as Dictionary<string, object>, mimeType, settings.AutoComputePayloadMetadataInJson);
            }

            return result;
        }
 private ODataMessageWriter CreateODataMessageWriter(StreamResponseMessage responseMessage, ODataMessageWriterSettings settings, bool hasModel)
 {
     if (hasModel)
     {
         return new ODataMessageWriter(responseMessage, settings, WritePayloadHelper.Model);
     }
     else
     {
         return new ODataMessageWriter(responseMessage, settings);
     }
 }
        private string WriteAndVerifyCarEntry(ODataMessageWriterSettings settings, string mimeType, bool hasModel)
        {
            // create a car entry
            var carEntry = WritePayloadHelper.CreateCarEntryNoMetadata(hasModel);
            carEntry.MediaResource = new ODataStreamReferenceValue();

            Dictionary<string, object> expectedCarObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.CarType, "Car(11)", carEntry, hasModel);
            WritePayloadHelper.ComputeDefaultExpectedFullMetadataEntryMedia(WritePayloadHelper.CarType, "Car(11)", carEntry, expectedCarObject, true /*hasStream*/, hasModel);

            // write the response message and read using ODL reader
            var responseMessage = new StreamResponseMessage(new MemoryStream());
            responseMessage.SetHeader("Content-Type", mimeType);
            responseMessage.PreferenceAppliedHeader().AnnotationFilter = "foo.*";
            string result = string.Empty;
            using (var messageWriter = this.CreateODataMessageWriter(responseMessage, settings, hasModel))
            {
                var odataWriter = this.CreateODataEntryWriter(messageWriter, WritePayloadHelper.CarSet, WritePayloadHelper.CarType, hasModel);
                odataWriter.WriteStart(carEntry);
                odataWriter.WriteEnd();

                result = this.ReadFeedEntryMessage(false, responseMessage, mimeType, WritePayloadHelper.CarSet, WritePayloadHelper.CarType);
            }

            // 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>;

                VerifyODataContextAnnotation(this.ServiceUri + "$metadata#Car/$entity", resultObject, mimeType);

                VerifyEntry(expectedCarObject, carEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, resultObject, mimeType, settings.AutoComputePayloadMetadataInJson);
            }

            return result;
        }
        private string WriteAndVerifyExpandedCustomerEntry(ODataMessageWriterSettings settings, string mimeType, string expectedProjectionClause, bool hasModel)
        {
            ODataEntry customerEntry = WritePayloadHelper.CreateCustomerEntryNoMetadata(hasModel);
            Dictionary<string, object> expectedCustomerObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.CustomerType, "Customer(-9)", customerEntry, hasModel);
            var thumbnailProperty = WritePayloadHelper.AddCustomerMediaProperty(customerEntry, expectedCustomerObject);

            // order navigation
            var orderNavigation = WritePayloadHelper.CreateCustomerOrderNavigation(expectedCustomerObject);

            // expanded logins navigation containing a Login instance
            var expandedLoginsNavigation = WritePayloadHelper.CreateExpandedCustomerLoginsNavigation(expectedCustomerObject);
            var loginFeed = new ODataFeed();
            if (mimeType == MimeTypes.ApplicationAtomXml)
            {
                loginFeed.Id = new Uri(this.ServiceUri + "Customer(-9)/Logins");
            }

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

            var loginEntry = WritePayloadHelper.CreateLoginEntryNoMetadata(hasModel);
            Dictionary<string, object> expectedLoginObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.LoginType, "Login('2')", loginEntry, hasModel);

            this.RemoveNonSelectedMetadataFromExpected(expectedCustomerObject, expectedLoginObject, hasModel);

            // write the response message and read using ODL reader
            var responseMessage = new StreamResponseMessage(new MemoryStream());
            responseMessage.SetHeader("Content-Type", mimeType);
            string result = string.Empty;
            using (var messageWriter = this.CreateODataMessageWriter(responseMessage, settings, hasModel))
            {
                var odataWriter = this.CreateODataEntryWriter(messageWriter, WritePayloadHelper.CustomerSet, WritePayloadHelper.CustomerType, hasModel);

                odataWriter.WriteStart(customerEntry);

                odataWriter.WriteStart(orderNavigation);
                odataWriter.WriteEnd();

                // write expanded navigation
                odataWriter.WriteStart(expandedLoginsNavigation);

                odataWriter.WriteStart(loginFeed);
                odataWriter.WriteStart(loginEntry);
                odataWriter.WriteEnd();
                odataWriter.WriteEnd();

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

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

                result = this.ReadFeedEntryMessage(false, responseMessage, mimeType, WritePayloadHelper.CustomerSet, WritePayloadHelper.CustomerType);
            }

            // 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>;

                VerifyODataContextAnnotation(this.ServiceUri + "$metadata#Customer(" + expectedProjectionClause + ")/$entity", resultObject, mimeType);

                VerifyEntry(expectedCustomerObject, customerEntry, new ODataNavigationLink[] { orderNavigation, expandedLoginsNavigation }, new ODataProperty[] { thumbnailProperty }, resultObject, mimeType, settings.AutoComputePayloadMetadataInJson);
                VerifyEntry(expectedLoginObject, loginEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject["Logins"] as object[]).Single() as Dictionary<string, object>, mimeType, settings.AutoComputePayloadMetadataInJson);
            }

            return result;
        }
        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);
        }
        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);
        }
        public void SingleLinkTest()
        {
            foreach (var mimeType in this.mimeTypes)
            {
                string testMimeType = mimeType.Contains("xml") ? MimeTypes.ApplicationXml : mimeType;

                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri() { ServiceRoot = this.ServiceUri };

                var responseMessage = new StreamResponseMessage(new MemoryStream());
                responseMessage.SetHeader("Content-Type", testMimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessage, settings, WritePayloadHelper.Model))
                {
                    this.WriteAndVerifySingleLink(responseMessage, messageWriter, testMimeType);
                }
            }
        }
        public void LinksTest()
        {
            foreach (var mimeType in this.mimeTypes)
            {
                if (mimeType.Equals(MimeTypes.ApplicationXml)) { continue; }
                string testMimeType = mimeType;
                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri() { ServiceRoot = this.ServiceUri };

                var responseMessage = new StreamResponseMessage(new MemoryStream());
                responseMessage.SetHeader("Content-Type", testMimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessage, settings, WritePayloadHelper.Model))
                {
                    this.WriteAndVerifyLinks(responseMessage, messageWriter, testMimeType);
                }
            }
        }
        public void CollectionTest()
        {
            foreach (var mimeType in this.mimeTypes)
            {
                string testMimeType = mimeType.Contains("xml") ? MimeTypes.ApplicationXml : mimeType;

                var settings = new ODataMessageWriterSettings() { PayloadBaseUri = this.ServiceUri };
                settings.ODataUri = new ODataUri() { ServiceRoot = this.ServiceUri };
                string outputWithModel = null;
                string outputWithoutModel = null;

                var responseMessageWithModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithModel.SetHeader("Content-Type", testMimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithModel, settings, WritePayloadHelper.Model))
                {
                    var odataWriter = messageWriter.CreateODataCollectionWriter(WritePayloadHelper.ContactDetailType);
                    outputWithModel = this.WriteAndVerifyCollection(responseMessageWithModel, odataWriter, true,
                                                                    testMimeType);
                }

                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", testMimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataCollectionWriter();
                    outputWithoutModel = this.WriteAndVerifyCollection(responseMessageWithoutModel, odataWriter, false,
                                                                       testMimeType);
                }

                Assert.AreEqual(outputWithModel, outputWithoutModel);
            }
        }
        public void EmployeeEntryTest()
        {
            foreach (var mimeType in this.mimeTypes)
            {
                var settings = new ODataMessageWriterSettings() { PayloadBaseUri = this.ServiceUri };
                settings.ODataUri = new ODataUri() { ServiceRoot = this.ServiceUri };
                string outputWithTypeCast = null;
                string outputWithoutTypeCast = null;

                // employee entry as response of person(1)
                var responseMessageWithoutTypeCast = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutTypeCast.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutTypeCast, settings))
                {
                    var odataWriter = messageWriter.CreateODataEntryWriter();
                    outputWithoutTypeCast = this.WriteAndVerifyEmployeeEntry(responseMessageWithoutTypeCast, odataWriter,
                                                                             false, mimeType);
                }

                // employee entry as response of person(1)/EmployeeTyeName, in this case the test sets ExpectedTypeName as Employee in Serialization info
                var responseMessageWithTypeCast = new StreamResponseMessage(new MemoryStream());
                responseMessageWithTypeCast.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithTypeCast, settings))
                {
                    var odataWriter = messageWriter.CreateODataEntryWriter();
                    outputWithTypeCast = this.WriteAndVerifyEmployeeEntry(responseMessageWithTypeCast, odataWriter, true,
                                                                          mimeType);
                }

                if (mimeType.Contains(MimeTypes.ODataParameterMinimalMetadata) || mimeType.Contains(MimeTypes.ODataParameterFullMetadata))
                {
                    // expect type cast in odata.metadata if EntitySetElementTypeName != ExpectedTypeName
                    Assert.IsTrue(outputWithoutTypeCast.Contains(this.ServiceUri + "$metadata#Person/$entity"));
                    Assert.IsTrue(
                        outputWithTypeCast.Contains(this.ServiceUri + "$metadata#Person/" + NameSpace +
                                                    "Employee/$entity"));
                }

                if (mimeType.Contains(MimeTypes.ApplicationJsonLight))
                {
                    // write odata.type if entry TypeName != ExpectedTypeName
                    Assert.IsTrue(outputWithoutTypeCast.Contains("odata.type"));
                    Assert.IsFalse(outputWithTypeCast.Contains("odata.type"));
                }
            }
        }
Esempio n. 16
0
        public void CompileAndVerifyGeneratedCode()
        {
            TestServiceUtil.ServiceUriGenerator = new ServiceUriGenerator();
            foreach (var descriptor in TestServiceDescriptors)
            {
                var serviceWrapper = new DefaultServiceWrapper(descriptor.Value);
                try
                {
                    serviceWrapper.StartService();

                    var edmx = RetrieveServiceModelEdmx(serviceWrapper.ServiceUri);
                    Assert.IsNotNull(edmx);

                    // retrieve the IEdmModel corresponding with the edmx
                    byte[] byteArray = Encoding.UTF8.GetBytes(edmx);
                    var message = new StreamResponseMessage(new MemoryStream(byteArray));
                    message.SetHeader("Content-Type", MimeTypes.ApplicationXml);

                    IEdmModel model = null;
                    using (var messageReader = new ODataMessageReader(message))
                    {
                        model = messageReader.ReadMetadataDocument();
                    }

                    foreach (var languageOption in languageOptions)
                    {
                        foreach (var useDataServiceCollection in useDataServiceCollectionBools)
                        {
                            var generatedCode = this.Generate(edmx, descriptor.Key, languageOption, useDataServiceCollection);
                            this.CompileAndVerify(generatedCode, languageOption == ODataT4CodeGenerator.LanguageOption.CSharp, model);
                        }
                    }
                }
                finally
                {
                    serviceWrapper.StopService();
                }
            }
        }
Esempio n. 17
0
        public static void CustomTestInitialize(Uri serviceUri)
        {
            ServiceUri = serviceUri;

            var metadata = @"<edmx:Edmx xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"" Version=""4.0"">
    <edmx:DataServices xmlns:m=""http://docs.oasis-open.org/odata/ns/metadata"" m:DataServiceVersion=""4.0"" m:MaxDataServiceVersion=""4.0"">
        <Schema xmlns=""http://docs.oasis-open.org/odata/ns/edm"" Namespace=""Microsoft.Test.OData.Services.AstoriaDefaultService"">
            <EntityType Name=""AllSpatialTypes"">
                <Key>
                    <PropertyRef Name=""Id"" />
                </Key>
                <Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Geog"" Type=""Edm.Geography"" SRID=""Variable"" />
                <Property Name=""GeogPoint"" Type=""Edm.GeographyPoint"" SRID=""Variable"" />
                <Property Name=""GeogLine"" Type=""Edm.GeographyLineString"" SRID=""Variable"" />
                <Property Name=""GeogPolygon"" Type=""Edm.GeographyPolygon"" SRID=""Variable"" />
                <Property Name=""GeogCollection"" Type=""Edm.GeographyCollection"" SRID=""Variable"" />
                <Property Name=""GeogMultiPoint"" Type=""Edm.GeographyMultiPoint"" SRID=""Variable"" />
                <Property Name=""GeogMultiLine"" Type=""Edm.GeographyMultiLineString"" SRID=""Variable"" />
                <Property Name=""GeogMultiPolygon"" Type=""Edm.GeographyMultiPolygon"" SRID=""Variable"" />
                <Property Name=""Geom"" Type=""Edm.Geometry"" SRID=""Variable"" />
                <Property Name=""GeomPoint"" Type=""Edm.GeometryPoint"" SRID=""Variable"" />
                <Property Name=""GeomLine"" Type=""Edm.GeometryLineString"" SRID=""Variable"" />
                <Property Name=""GeomPolygon"" Type=""Edm.GeometryPolygon"" SRID=""Variable"" />
                <Property Name=""GeomCollection"" Type=""Edm.GeometryCollection"" SRID=""Variable"" />
                <Property Name=""GeomMultiPoint"" Type=""Edm.GeometryMultiPoint"" SRID=""Variable"" />
                <Property Name=""GeomMultiLine"" Type=""Edm.GeometryMultiLineString"" SRID=""Variable"" />
                <Property Name=""GeomMultiPolygon"" Type=""Edm.GeometryMultiPolygon"" SRID=""Variable"" />
            </EntityType>
            <EntityType Name=""AllSpatialCollectionTypes"" Abstract=""true"">
                <Key>
                    <PropertyRef Name=""Id"" />
                </Key>
                <Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false"" />
            </EntityType>
            <EntityType Name=""Customer"">
                <Key>
                    <PropertyRef Name=""CustomerId"" />
                </Key>
                <Property Name=""Thumbnail"" Type=""Edm.Stream"" Nullable=""false"" />
                <Property Name=""Video"" Type=""Edm.Stream"" Nullable=""false"" />
                <Property Name=""CustomerId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Name"" Type=""Edm.String"" />
                <Property Name=""PrimaryContactInfo"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails"" />
                <Property Name=""BackupContactInfo"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails)"" Nullable=""false"" />
                <Property Name=""Auditing"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.AuditInfo"" />
                <NavigationProperty Name=""Orders"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.Order)"" Partner=""Customer"" />
                <NavigationProperty Name=""Logins"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.Login)"" Partner=""Customer"" />
                <NavigationProperty Name=""Husband"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Customer"" Partner=""Husband"" />
                <NavigationProperty Name=""Wife"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Customer"" Partner=""Wife"" />
                <NavigationProperty Name=""Info"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.CustomerInfo"" />
            </EntityType>
            <EntityType Name=""Login"">
                <Key>
                    <PropertyRef Name=""Username"" />
                </Key>
                <Property Name=""Username"" Type=""Edm.String"" Nullable=""false"" />
                <Property Name=""CustomerId"" Type=""Edm.Int32"" Nullable=""false"" />
                <NavigationProperty Name=""Customer"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Customer"" Partner=""Logins"" />
                <NavigationProperty Name=""LastLogin"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.LastLogin"" Partner=""Login"" />
                <NavigationProperty Name=""SentMessages"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.Message)"" Partner=""Sender"" />
                <NavigationProperty Name=""ReceivedMessages"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.Message)"" Partner=""Recipient"" />
                <NavigationProperty Name=""Orders"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.Order)"" Partner=""Login"" />
            </EntityType>
            <EntityType Name=""RSAToken"">
                <Key>
                    <PropertyRef Name=""Serial"" />
                </Key>
                <Property Name=""Serial"" Type=""Edm.String"" Nullable=""false"" />
                <Property Name=""Issued"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
                <NavigationProperty Name=""Login"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Login"" />
            </EntityType>
            <EntityType Name=""PageView"">
                <Key>
                    <PropertyRef Name=""PageViewId"" />
                </Key>
                <Property Name=""PageViewId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Username"" Type=""Edm.String"" />
                <Property Name=""Viewed"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
                <Property Name=""TimeSpentOnPage"" Type=""Edm.Duration"" Nullable=""false"" />
                <Property Name=""PageUrl"" Type=""Edm.String"" />
                <NavigationProperty Name=""Login"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Login"" />
            </EntityType>
            <EntityType Name=""LastLogin"">
                <Key>
                    <PropertyRef Name=""Username"" />
                </Key>
                <Property Name=""Username"" Type=""Edm.String"" Nullable=""false"" />
                <Property Name=""LoggedIn"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
                <Property Name=""LoggedOut"" Type=""Edm.DateTimeOffset"" />
                <Property Name=""Duration"" Type=""Edm.Duration"" Nullable=""false"" />
                <NavigationProperty Name=""Login"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Login"" Partner=""LastLogin"" />
            </EntityType>
            <EntityType Name=""Message"">
                <Key>
                    <PropertyRef Name=""FromUsername"" />
                    <PropertyRef Name=""MessageId"" />
                </Key>
                <Property Name=""MessageId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""FromUsername"" Type=""Edm.String"" Nullable=""false"" />
                <Property Name=""ToUsername"" Type=""Edm.String"" />
                <Property Name=""Sent"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
                <Property Name=""Subject"" Type=""Edm.String"" />
                <Property Name=""Body"" Type=""Edm.String"" />
                <Property Name=""IsRead"" Type=""Edm.Boolean"" Nullable=""false"" />
                <NavigationProperty Name=""Sender"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Login"" Partner=""SentMessages"" />
                <NavigationProperty Name=""Recipient"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Login"" Partner=""ReceivedMessages"" />
                <NavigationProperty Name=""Attachments"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.MessageAttachment)"" />
            </EntityType>
            <EntityType Name=""MessageAttachment"">
                <Key>
                    <PropertyRef Name=""AttachmentId"" />
                </Key>
                <Property Name=""AttachmentId"" Type=""Edm.Guid"" Nullable=""false"" />
                <Property Name=""Attachment"" Type=""Edm.Binary"" />
            </EntityType>
            <EntityType Name=""Order"">
                <Key>
                    <PropertyRef Name=""OrderId"" />
                </Key>
                <Property Name=""OrderId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""CustomerId"" Type=""Edm.Int32"" />
                <Property Name=""Concurrency"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.ConcurrencyInfo"" />
                <NavigationProperty Name=""Login"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Login"" Partner=""Orders"" />
                <NavigationProperty Name=""Customer"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Customer"" Partner=""Orders"" />
            </EntityType>
            <EntityType Name=""OrderLine"">
                <Key>
                    <PropertyRef Name=""OrderId"" />
                    <PropertyRef Name=""ProductId"" />
                </Key>
                <Property Name=""OrderLineStream"" Type=""Edm.Stream"" Nullable=""false"" />
                <Property Name=""OrderId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""ProductId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Quantity"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""ConcurrencyToken"" Type=""Edm.String"" ConcurrencyMode=""Fixed"" />
                <NavigationProperty Name=""Order"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Order"" />
                <NavigationProperty Name=""Product"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Product"" />
            </EntityType>
            <EntityType Name=""Product"">
                <Key>
                    <PropertyRef Name=""ProductId"" />
                </Key>
                <Property Name=""Picture"" Type=""Edm.Stream"" Nullable=""false"" />
                <Property Name=""ProductId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Description"" Type=""Edm.String"" />
                <Property Name=""Dimensions"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Dimensions"" />
                <Property Name=""BaseConcurrency"" Type=""Edm.String"" ConcurrencyMode=""Fixed"" />
                <Property Name=""ComplexConcurrency"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.ConcurrencyInfo"" />
                <Property Name=""NestedComplexConcurrency"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.AuditInfo"" />
                <NavigationProperty Name=""RelatedProducts"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.Product)"" Partner=""RelatedProducts"" />
                <NavigationProperty Name=""Detail"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.ProductDetail"" Partner=""Product"" />
                <NavigationProperty Name=""Reviews"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.ProductReview)"" Partner=""Product"" />
                <NavigationProperty Name=""Photos"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.ProductPhoto)"" />
            </EntityType>
            <EntityType Name=""ProductDetail"">
                <Key>
                    <PropertyRef Name=""ProductId"" />
                </Key>
                <Property Name=""ProductId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Details"" Type=""Edm.String"" />
                <NavigationProperty Name=""Product"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Product"" Partner=""Detail"" />
            </EntityType>
            <EntityType Name=""ProductReview"">
                <Key>
                    <PropertyRef Name=""ProductId"" />
                    <PropertyRef Name=""ReviewId"" />
                    <PropertyRef Name=""RevisionId"" />
                </Key>
                <Property Name=""ProductId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""ReviewId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Review"" Type=""Edm.String"" />
                <Property Name=""RevisionId"" Type=""Edm.String"" Nullable=""false"" />
                <NavigationProperty Name=""Product"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Product"" Partner=""Reviews"" />
            </EntityType>
            <EntityType Name=""ProductPhoto"">
                <Key>
                    <PropertyRef Name=""PhotoId"" />
                    <PropertyRef Name=""ProductId"" />
                </Key>
                <Property Name=""ProductId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""PhotoId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Photo"" Type=""Edm.Binary"" />
            </EntityType>
            <EntityType Name=""CustomerInfo"" HasStream=""true"">
                <Key>
                    <PropertyRef Name=""CustomerInfoId"" />
                </Key>
                <Property Name=""CustomerInfoId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Information"" Type=""Edm.String"" />
            </EntityType>
            <EntityType Name=""Computer"">
                <Key>
                    <PropertyRef Name=""ComputerId"" />
                </Key>
                <Property Name=""ComputerId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Name"" Type=""Edm.String"" />
                <NavigationProperty Name=""ComputerDetail"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.ComputerDetail"" Partner=""Computer"" />
            </EntityType>
            <EntityType Name=""ComputerDetail"">
                <Key>
                    <PropertyRef Name=""ComputerDetailId"" />
                </Key>
                <Property Name=""ComputerDetailId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Manufacturer"" Type=""Edm.String"" />
                <Property Name=""Model"" Type=""Edm.String"" />
                <Property Name=""Serial"" Type=""Edm.String"" />
                <Property Name=""SpecificationsBag"" Type=""Collection(Edm.String)"" Nullable=""false"" />
                <Property Name=""PurchaseDate"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
                <Property Name=""Dimensions"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Dimensions"" />
                <NavigationProperty Name=""Computer"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Computer"" Partner=""ComputerDetail"" />
            </EntityType>
            <EntityType Name=""Driver"">
                <Key>
                    <PropertyRef Name=""Name"" />
                </Key>
                <Property Name=""Name"" Type=""Edm.String"" Nullable=""false"" />
                <Property Name=""BirthDate"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
                <NavigationProperty Name=""License"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.License"" Partner=""Driver"" />
            </EntityType>
            <EntityType Name=""License"">
                <Key>
                    <PropertyRef Name=""Name"" />
                </Key>
                <Property Name=""Name"" Type=""Edm.String"" Nullable=""false"" />
                <Property Name=""LicenseNumber"" Type=""Edm.String"" />
                <Property Name=""LicenseClass"" Type=""Edm.String"" />
                <Property Name=""Restrictions"" Type=""Edm.String"" />
                <Property Name=""ExpirationDate"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
                <NavigationProperty Name=""Driver"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Driver"" Partner=""License"" />
            </EntityType>
            <EntityType Name=""MappedEntityType"">
                <Key>
                    <PropertyRef Name=""Id"" />
                </Key>
                <Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Href"" Type=""Edm.String"" />
                <Property Name=""Title"" Type=""Edm.String"" />
                <Property Name=""HrefLang"" Type=""Edm.String"" />
                <Property Name=""Type"" Type=""Edm.String"" />
                <Property Name=""Length"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""BagOfPrimitiveToLinks"" Type=""Collection(Edm.String)"" Nullable=""false"" />
                <Property Name=""Logo"" Type=""Edm.Binary"" />
                <Property Name=""BagOfDecimals"" Type=""Collection(Edm.Decimal)"" Nullable=""false"" />
                <Property Name=""BagOfDoubles"" Type=""Collection(Edm.Double)"" Nullable=""false"" />
                <Property Name=""BagOfSingles"" Type=""Collection(Edm.Single)"" Nullable=""false"" />
                <Property Name=""BagOfBytes"" Type=""Collection(Edm.Byte)"" Nullable=""false"" />
                <Property Name=""BagOfInt16s"" Type=""Collection(Edm.Int16)"" Nullable=""false"" />
                <Property Name=""BagOfInt32s"" Type=""Collection(Edm.Int32)"" Nullable=""false"" />
                <Property Name=""BagOfInt64s"" Type=""Collection(Edm.Int64)"" Nullable=""false"" />
                <Property Name=""BagOfGuids"" Type=""Collection(Edm.Guid)"" Nullable=""false"" />
                <Property Name=""BagOfDateTimeOffset"" Type=""Collection(Edm.DateTimeOffset)"" Nullable=""false"" />
                <Property Name=""BagOfComplexToCategories"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.ComplexToCategory)"" Nullable=""false"" />
                <Property Name=""ComplexPhone"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Phone"" />
                <Property Name=""ComplexContactDetails"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails"" />
            </EntityType>
            <EntityType Name=""Car"" HasStream=""true"">
                <Key>
                    <PropertyRef Name=""VIN"" />
                </Key>
                <Property Name=""Photo"" Type=""Edm.Stream"" Nullable=""false"" />
                <Property Name=""Video"" Type=""Edm.Stream"" Nullable=""false"" />
                <Property Name=""VIN"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Description"" Type=""Edm.String"" />
            </EntityType>
            <EntityType Name=""Person"">
                <Key>
                    <PropertyRef Name=""PersonId"" />
                </Key>
                <Property Name=""PersonId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Name"" Type=""Edm.String"" />
                <NavigationProperty Name=""PersonMetadata"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.PersonMetadata)"" Partner=""Person"" />
            </EntityType>
            <EntityType Name=""PersonMetadata"">
                <Key>
                    <PropertyRef Name=""PersonMetadataId"" />
                </Key>
                <Property Name=""PersonMetadataId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""PersonId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""PropertyName"" Type=""Edm.String"" />
                <Property Name=""PropertyValue"" Type=""Edm.String"" />
                <NavigationProperty Name=""Person"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Person"" Partner=""PersonMetadata"" />
            </EntityType>
            <ComplexType Name=""ContactDetails"">
                <Property Name=""EmailBag"" Type=""Collection(Edm.String)"" Nullable=""false"" />
                <Property Name=""AlternativeNames"" Type=""Collection(Edm.String)"" Nullable=""false"" />
                <Property Name=""ContactAlias"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Aliases"" />
                <Property Name=""HomePhone"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Phone"" />
                <Property Name=""WorkPhone"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Phone"" />
                <Property Name=""MobilePhoneBag"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.Phone)"" Nullable=""false"" />
            </ComplexType>
            <ComplexType Name=""AuditInfo"">
                <Property Name=""ModifiedDate"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
                <Property Name=""ModifiedBy"" Type=""Edm.String"" />
                <Property Name=""Concurrency"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.ConcurrencyInfo"" />
            </ComplexType>
            <ComplexType Name=""ConcurrencyInfo"">
                <Property Name=""Token"" Type=""Edm.String"" />
                <Property Name=""QueriedDateTime"" Type=""Edm.DateTimeOffset"" />
            </ComplexType>
            <ComplexType Name=""Dimensions"">
                <Property Name=""Width"" Type=""Edm.Decimal"" Nullable=""false"" />
                <Property Name=""Height"" Type=""Edm.Decimal"" Nullable=""false"" />
                <Property Name=""Depth"" Type=""Edm.Decimal"" Nullable=""false"" />
            </ComplexType>
            <ComplexType Name=""ComplexToCategory"">
                <Property Name=""Term"" Type=""Edm.String"" />
                <Property Name=""Scheme"" Type=""Edm.String"" />
                <Property Name=""Label"" Type=""Edm.String"" />
            </ComplexType>
            <ComplexType Name=""Phone"">
                <Property Name=""PhoneNumber"" Type=""Edm.String"" />
                <Property Name=""Extension"" Type=""Edm.String"" />
            </ComplexType>
            <ComplexType Name=""Aliases"">
                <Property Name=""AlternativeNames"" Type=""Collection(Edm.String)"" Nullable=""false"" />
            </ComplexType>
            <EntityType Name=""AllSpatialCollectionTypes_Simple"" BaseType=""Microsoft.Test.OData.Services.AstoriaDefaultService.AllSpatialCollectionTypes"">
                <Property Name=""ManyGeogPoint"" Type=""Collection(Edm.GeographyPoint)"" Nullable=""false"" SRID=""Variable"" />
                <Property Name=""ManyGeogLine"" Type=""Collection(Edm.GeographyLineString)"" Nullable=""false"" SRID=""Variable"" />
                <Property Name=""ManyGeogPolygon"" Type=""Collection(Edm.GeographyPolygon)"" Nullable=""false"" SRID=""Variable"" />
                <Property Name=""ManyGeomPoint"" Type=""Collection(Edm.GeometryPoint)"" Nullable=""false"" SRID=""Variable"" />
                <Property Name=""ManyGeomLine"" Type=""Collection(Edm.GeometryLineString)"" Nullable=""false"" SRID=""Variable"" />
                <Property Name=""ManyGeomPolygon"" Type=""Collection(Edm.GeometryPolygon)"" Nullable=""false"" SRID=""Variable"" />
            </EntityType>
            <EntityType Name=""ProductPageView"" BaseType=""Microsoft.Test.OData.Services.AstoriaDefaultService.PageView"">
                <Property Name=""ProductId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""ConcurrencyToken"" Type=""Edm.String"" ConcurrencyMode=""Fixed"" />
            </EntityType>
            <EntityType Name=""BackOrderLine"" BaseType=""Microsoft.Test.OData.Services.AstoriaDefaultService.OrderLine"" />
            <EntityType Name=""BackOrderLine2"" BaseType=""Microsoft.Test.OData.Services.AstoriaDefaultService.BackOrderLine"" />
            <EntityType Name=""DiscontinuedProduct"" BaseType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Product"">
                <Property Name=""Discontinued"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
                <Property Name=""ReplacementProductId"" Type=""Edm.Int32"" />
                <Property Name=""DiscontinuedPhone"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Phone"" />
                <Property Name=""ChildConcurrencyToken"" Type=""Edm.String"" ConcurrencyMode=""Fixed"" />
            </EntityType>
            <EntityType Name=""Contractor"" BaseType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Person"">
                <Property Name=""ContratorCompanyId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""BillingRate"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""TeamContactPersonId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""JobDescription"" Type=""Edm.String"" />
            </EntityType>
            <EntityType Name=""Employee"" BaseType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Person"">
                <Property Name=""ManagersPersonId"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Salary"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Title"" Type=""Edm.String"" />
                <NavigationProperty Name=""Manager"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Employee"" Partner=""Manager"" />
            </EntityType>
            <EntityType Name=""SpecialEmployee"" BaseType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Employee"">
                <Property Name=""CarsVIN"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Bonus"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""IsFullyVested"" Type=""Edm.Boolean"" Nullable=""false"" />
                <NavigationProperty Name=""Car"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Car"" />
            </EntityType>
            <ComplexType Name=""ComplexWithAllPrimitiveTypes"">
                <Property Name=""Binary"" Type=""Edm.Binary"" />
                <Property Name=""Boolean"" Type=""Edm.Boolean"" Nullable=""false"" />
                <Property Name=""Byte"" Type=""Edm.Byte"" Nullable=""false"" />
                <Property Name=""DateTimeOffset"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
                <Property Name=""Decimal"" Type=""Edm.Decimal"" Nullable=""false"" />
                <Property Name=""Double"" Type=""Edm.Double"" Nullable=""false"" />
                <Property Name=""Int16"" Type=""Edm.Int16"" Nullable=""false"" />
                <Property Name=""Int32"" Type=""Edm.Int32"" Nullable=""false"" />
                <Property Name=""Int64"" Type=""Edm.Int64"" Nullable=""false"" />
                <Property Name=""SByte"" Type=""Edm.SByte"" Nullable=""false"" />
                <Property Name=""String"" Type=""Edm.String"" />
                <Property Name=""Single"" Type=""Edm.Single"" Nullable=""false"" />
                <Property Name=""GeographyPoint"" Type=""Edm.GeographyPoint"" SRID=""Variable"" />
                <Property Name=""GeometryPoint"" Type=""Edm.GeometryPoint"" SRID=""Variable"" />
            </ComplexType>
            <Function Name=""GetPrimitiveString"" m:HttpMethod=""GET"">
                <ReturnType Type=""Edm.String"" />
            </Function>
            <Function Name=""GetSpecificCustomer"" m:HttpMethod=""GET"">
                <Parameter Name=""Name"" Type=""Edm.String"" />
                <ReturnType Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.Customer)"" />
            </Function>
            <Function Name=""GetCustomerCount"" m:HttpMethod=""GET"">
                <ReturnType Type=""Edm.Int32"" Nullable=""false"" />
            </Function>
            <Function Name=""GetArgumentPlusOne"" m:HttpMethod=""GET"">
                <Parameter Name=""arg1"" Type=""Edm.Int32"" Nullable=""false"" />
                <ReturnType Type=""Edm.Int32"" Nullable=""false"" />
            </Function>
            <Function Name=""EntityProjectionReturnsCollectionOfComplexTypes"" m:HttpMethod=""GET"">
                <ReturnType Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails)"" />
            </Function>
            <Action Name=""ResetDataSource"" m:HttpMethod=""POST"" />
            <Function Name=""InStreamErrorGetCustomer"" m:HttpMethod=""GET"">
                <ReturnType Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.Customer)"" />
            </Function>
            <Action Name=""IncreaseSalaries"" IsBound=""true"" m:IsAlwaysBindable=""true"">
                <Parameter Name=""employees"" Type=""Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.Employee)"" />
                <Parameter Name=""n"" Type=""Edm.Int32"" Nullable=""false"" />
            </Action>
            <Action Name=""Sack"" IsBound=""true"" m:IsAlwaysBindable=""true"">
                <Parameter Name=""employee"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Employee"" />
            </Action>
            <Action Name=""GetComputer"" IsBound=""true"" EntitySetPath=""computer"" m:IsAlwaysBindable=""true"">
                <Parameter Name=""computer"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Computer"" />
                <ReturnType Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Computer"" />
            </Action>
            <Action Name=""ChangeCustomerAuditInfo"" IsBound=""true"" m:IsAlwaysBindable=""true"">
                <Parameter Name=""customer"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.Customer"" />
                <Parameter Name=""auditInfo"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.AuditInfo"" />
            </Action>
            <Action Name=""ResetComputerDetailsSpecifications"" IsBound=""true"" m:IsAlwaysBindable=""true"">
                <Parameter Name=""computerDetail"" Type=""Microsoft.Test.OData.Services.AstoriaDefaultService.ComputerDetail"" />
                <Parameter Name=""specifications"" Type=""Collection(Edm.String)"" Nullable=""false"" />
                <Parameter Name=""purchaseTime"" Type=""Edm.DateTimeOffset"" Nullable=""false"" />
            </Action>
            <EntityContainer Name=""DefaultContainer"" m:IsDefaultEntityContainer=""true"">
                <EntitySet Name=""AllGeoTypesSet"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.AllSpatialTypes"" />
                <EntitySet Name=""AllGeoCollectionTypesSet"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.AllSpatialCollectionTypes"" />
                <EntitySet Name=""Customer"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Customer"">
                    <NavigationPropertyBinding Path=""Orders"" Target=""Order"" />
                    <NavigationPropertyBinding Path=""Logins"" Target=""Login"" />
                    <NavigationPropertyBinding Path=""Husband"" Target=""Customer"" />
                    <NavigationPropertyBinding Path=""Wife"" Target=""Customer"" />
                    <NavigationPropertyBinding Path=""Info"" Target=""CustomerInfo"" />
                </EntitySet>
                <EntitySet Name=""Login"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Login"">
                    <NavigationPropertyBinding Path=""Customer"" Target=""Customer"" />
                    <NavigationPropertyBinding Path=""LastLogin"" Target=""LastLogin"" />
                    <NavigationPropertyBinding Path=""SentMessages"" Target=""Message"" />
                    <NavigationPropertyBinding Path=""ReceivedMessages"" Target=""Message"" />
                    <NavigationPropertyBinding Path=""Orders"" Target=""Order"" />
                </EntitySet>
                <EntitySet Name=""RSAToken"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.RSAToken"">
                    <NavigationPropertyBinding Path=""Login"" Target=""Login"" />
                </EntitySet>
                <EntitySet Name=""PageView"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.PageView"">
                    <NavigationPropertyBinding Path=""Login"" Target=""Login"" />
                </EntitySet>
                <EntitySet Name=""LastLogin"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.LastLogin"">
                    <NavigationPropertyBinding Path=""Login"" Target=""Login"" />
                </EntitySet>
                <EntitySet Name=""Message"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Message"">
                    <NavigationPropertyBinding Path=""Sender"" Target=""Login"" />
                    <NavigationPropertyBinding Path=""Recipient"" Target=""Login"" />
                    <NavigationPropertyBinding Path=""Attachments"" Target=""MessageAttachment"" />
                </EntitySet>
                <EntitySet Name=""MessageAttachment"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.MessageAttachment"" />
                <EntitySet Name=""Order"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Order"">
                    <NavigationPropertyBinding Path=""Customer"" Target=""Customer"" />
                    <NavigationPropertyBinding Path=""Login"" Target=""Login"" />
                </EntitySet>
                <EntitySet Name=""OrderLine"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.OrderLine"">
                    <NavigationPropertyBinding Path=""Order"" Target=""Order"" />
                    <NavigationPropertyBinding Path=""Product"" Target=""Product"" />
                </EntitySet>
                <EntitySet Name=""Product"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Product"">
                    <NavigationPropertyBinding Path=""RelatedProducts"" Target=""Product"" />
                    <NavigationPropertyBinding Path=""Detail"" Target=""ProductDetail"" />
                    <NavigationPropertyBinding Path=""Reviews"" Target=""ProductReview"" />
                    <NavigationPropertyBinding Path=""Photos"" Target=""ProductPhoto"" />
                </EntitySet>
                <EntitySet Name=""ProductDetail"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.ProductDetail"">
                    <NavigationPropertyBinding Path=""Product"" Target=""Product"" />
                </EntitySet>
                <EntitySet Name=""ProductReview"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.ProductReview"">
                    <NavigationPropertyBinding Path=""Product"" Target=""Product"" />
                </EntitySet>
                <EntitySet Name=""ProductPhoto"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.ProductPhoto"" />
                <EntitySet Name=""CustomerInfo"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.CustomerInfo"" />
                <EntitySet Name=""Computer"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Computer"">
                    <NavigationPropertyBinding Path=""ComputerDetail"" Target=""ComputerDetail"" />
                </EntitySet>
                <EntitySet Name=""ComputerDetail"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.ComputerDetail"">
                    <NavigationPropertyBinding Path=""Computer"" Target=""Computer"" />
                </EntitySet>
                <EntitySet Name=""Driver"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Driver"">
                    <NavigationPropertyBinding Path=""License"" Target=""License"" />
                </EntitySet>
                <EntitySet Name=""License"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.License"">
                    <NavigationPropertyBinding Path=""Driver"" Target=""Driver"" />
                </EntitySet>
                <EntitySet Name=""MappedEntityType"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.MappedEntityType"" />
                <EntitySet Name=""Car"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Car"" />
                <EntitySet Name=""Person"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.Person"">
                    <NavigationPropertyBinding Path=""Microsoft.Test.OData.Services.AstoriaDefaultService.Employee/Manager"" Target=""Person"" />
                    <NavigationPropertyBinding Path=""Microsoft.Test.OData.Services.AstoriaDefaultService.SpecialEmployee/Car"" Target=""Car"" />
                    <NavigationPropertyBinding Path=""PersonMetadata"" Target=""PersonMetadata"" />
                </EntitySet>
                <EntitySet Name=""PersonMetadata"" EntityType=""Microsoft.Test.OData.Services.AstoriaDefaultService.PersonMetadata"">
                    <NavigationPropertyBinding Path=""Person"" Target=""Person"" />
                </EntitySet>
                <FunctionImport Name=""GetPrimitiveString"" Function=""Microsoft.Test.OData.Services.AstoriaDefaultService.GetPrimitiveString"" IncludeInServiceDocument=""true"" />
                <FunctionImport Name=""GetSpecificCustomer"" Function=""Microsoft.Test.OData.Services.AstoriaDefaultService.GetSpecificCustomer"" EntitySet=""Customer"" IncludeInServiceDocument=""true"" />
                <FunctionImport Name=""GetCustomerCount"" Function=""Microsoft.Test.OData.Services.AstoriaDefaultService.GetCustomerCount"" IncludeInServiceDocument=""true"" />
                <FunctionImport Name=""GetArgumentPlusOne"" Function=""Microsoft.Test.OData.Services.AstoriaDefaultService.GetArgumentPlusOne"" IncludeInServiceDocument=""true"" />
                <FunctionImport Name=""EntityProjectionReturnsCollectionOfComplexTypes"" Function=""Microsoft.Test.OData.Services.AstoriaDefaultService.EntityProjectionReturnsCollectionOfComplexTypes"" IncludeInServiceDocument=""true"" />
                <ActionImport Name=""ResetDataSource"" Action=""Microsoft.Test.OData.Services.AstoriaDefaultService.ResetDataSource"" />
                <FunctionImport Name=""InStreamErrorGetCustomer"" Function=""Microsoft.Test.OData.Services.AstoriaDefaultService.InStreamErrorGetCustomer"" EntitySet=""Customer"" IncludeInServiceDocument=""true"" />
            </EntityContainer>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>";

            byte[] byteArray = Encoding.UTF8.GetBytes(metadata);

            var message = new StreamResponseMessage(new MemoryStream(byteArray));
            message.SetHeader("Content-Type", MimeTypes.ApplicationXml);

            using (var messageReader = new ODataMessageReader(message))
            {
                Model = messageReader.ReadMetadataDocument();
            }
            
            CustomerType = Model.FindDeclaredType(NameSpace + "Customer") as IEdmEntityType;
            CustomerSet = Model.EntityContainer.FindEntitySet("Customer");

            OrderType = Model.FindDeclaredType(NameSpace + "Order") as IEdmEntityType;
            OrderSet = Model.EntityContainer.FindEntitySet("Order");

            CarType = Model.FindDeclaredType(NameSpace + "Car") as IEdmEntityType;
            CarSet = Model.EntityContainer.FindEntitySet("Car");

            PersonType = Model.FindDeclaredType(NameSpace + "Person") as IEdmEntityType;
            EmployeeType = Model.FindDeclaredType(NameSpace + "Employee") as IEdmEntityType;
            SpecialEmployeeType = Model.FindDeclaredType(NameSpace + "SpecialEmployee") as IEdmEntityType;
            PersonSet = Model.EntityContainer.FindEntitySet("Person");

            ContactDetailType =
                new EdmComplexTypeReference((IEdmComplexType)Model.FindDeclaredType(NameSpace + "ContactDetails"), true);

            LoginType = Model.FindDeclaredType(NameSpace + "Login") as IEdmEntityType;
            LoginSet = Model.EntityContainer.FindEntitySet("Login");
        }
        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 WriteAndVerifyPersonFeed(ODataMessageWriterSettings settings, string mimeType, bool hasModel)
        {
            // create a Person feed containing a person, an employee, a special employee
            var personFeed = new ODataFeed();
            if (mimeType == MimeTypes.ApplicationAtomXml)
            {
                personFeed.Id = new Uri(this.ServiceUri + "Person");
            }

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

            ODataEntry personEntry = WritePayloadHelper.CreatePersonEntryNoMetadata(hasModel);
            Dictionary<string, object> expectedPersonObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.PersonType, "Person(-5)", personEntry, hasModel);

            ODataEntry employeeEntry = WritePayloadHelper.CreateEmployeeEntryNoMetadata(hasModel);
            Dictionary<string, object> expectedEmployeeObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.EmployeeType, "Person(-3)", employeeEntry, hasModel, true);

            ODataEntry specialEmployeeEntry = WritePayloadHelper.CreateSpecialEmployeeEntryNoMetadata(hasModel);
            Dictionary<string, object> expectedSpecialEmployeeObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.SpecialEmployeeType, "Person(-10)", specialEmployeeEntry, hasModel, true);

            // write the response message and read using ODL reader
            var responseMessage = new StreamResponseMessage(new MemoryStream());
            responseMessage.SetHeader("Content-Type", mimeType);
            string result = string.Empty;
            using (var messageWriter = this.CreateODataMessageWriter(responseMessage, settings, hasModel))
            {
                var odataWriter = this.CreateODataFeedWriter(messageWriter, WritePayloadHelper.PersonSet, WritePayloadHelper.PersonType, hasModel);
                odataWriter.WriteStart(personFeed);

                odataWriter.WriteStart(personEntry);
                odataWriter.WriteEnd();

                odataWriter.WriteStart(employeeEntry);
                odataWriter.WriteEnd();

                odataWriter.WriteStart(specialEmployeeEntry);
                odataWriter.WriteEnd();

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

                result = this.ReadFeedEntryMessage(true, responseMessage, mimeType, WritePayloadHelper.PersonSet, WritePayloadHelper.PersonType);
            }

            // 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>;

                VerifyODataContextAnnotation(this.ServiceUri + "$metadata#Person", resultObject, mimeType);

                VerifyEntry(expectedPersonObject, personEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).ElementAt(0) as Dictionary<string, object>, mimeType, settings.AutoComputePayloadMetadataInJson);
                VerifyEntry(expectedEmployeeObject, employeeEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).ElementAt(1) as Dictionary<string, object>, mimeType, settings.AutoComputePayloadMetadataInJson);
                VerifyEntry(expectedSpecialEmployeeObject, specialEmployeeEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).ElementAt(2) as Dictionary<string, object>, mimeType, settings.AutoComputePayloadMetadataInJson);
            }

            return result;
        }
        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);
        }
        private string WriteAndVerifyEmployeeFeed(ODataMessageWriterSettings settings, string mimeType, bool hasModel)
        {
            // create a feed with two entries
            var employeeFeed = new ODataFeed();

            if (mimeType == MimeTypes.ApplicationAtomXml)
            {
                employeeFeed.Id = new Uri(this.ServiceUri + "Person/" + NameSpace + "Employee");
            }

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

            ODataEntry employeeEntry = WritePayloadHelper.CreateEmployeeEntryNoMetadata(false);
            ODataEntry specialEmployeeEntry = WritePayloadHelper.CreateSpecialEmployeeEntryNoMetadata(false);

            // expected result with AutoGeneratedUrlsShouldPutKeyValueInDedicatedSegment
            Dictionary<string, object> expectedEmployeeObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.EmployeeType, "Person/-3", employeeEntry, hasModel, true);
            Dictionary<string, object> expectedSpecialEmployeeObject = WritePayloadHelper.ComputeExpectedFullMetadataEntryObject(WritePayloadHelper.SpecialEmployeeType, "Person/-10", specialEmployeeEntry, hasModel, true);

            // write the response message and read using ODL reader
            var responseMessage = new StreamResponseMessage(new MemoryStream());
            responseMessage.SetHeader("Content-Type", mimeType);
            string result = string.Empty;
            using (var messageWriter = this.CreateODataMessageWriter(responseMessage, settings, hasModel))
            {
                var odataWriter = this.CreateODataFeedWriter(messageWriter, WritePayloadHelper.PersonSet, WritePayloadHelper.EmployeeType, hasModel);
                odataWriter.WriteStart(employeeFeed);

                odataWriter.WriteStart(employeeEntry);
                odataWriter.WriteEnd();

                // toggle AutoComputePayloadMetadataInJson, this should not affect the writing result
                settings.AutoComputePayloadMetadataInJson = !settings.AutoComputePayloadMetadataInJson;

                odataWriter.WriteStart(specialEmployeeEntry);
                odataWriter.WriteEnd();

                odataWriter.WriteEnd();

                result = this.ReadFeedEntryMessage(true, responseMessage, mimeType, WritePayloadHelper.PersonSet, WritePayloadHelper.PersonType);
            }

            // 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>;

                VerifyODataContextAnnotation(this.ServiceUri + "$metadata#Person/" + NameSpace + "Employee", resultObject, mimeType);

                settings.AutoComputePayloadMetadataInJson = !settings.AutoComputePayloadMetadataInJson;
                VerifyEntry(expectedEmployeeObject, employeeEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).First() as Dictionary<string, object>, mimeType, settings.AutoComputePayloadMetadataInJson);
                VerifyEntry(expectedSpecialEmployeeObject, specialEmployeeEntry, new ODataNavigationLink[] { }, new ODataProperty[] { }, (resultObject.Last().Value as object[]).Last() as Dictionary<string, object>, mimeType, settings.AutoComputePayloadMetadataInJson);
            }

            return result;
        }
        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);
        }
        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 ExpandedCustomerEntryTest()
        {
            foreach (var mimeType in this.mimeTypes)
            {
                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri() { ServiceRoot = this.ServiceUri };
                string outputWithModel = null;
                string outputWithoutModel = null;

                var responseMessageWithModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithModel, settings, WritePayloadHelper.Model))
                {
                    var odataWriter = messageWriter.CreateODataEntryWriter(WritePayloadHelper.CustomerSet, WritePayloadHelper.CustomerType);
                    outputWithModel = this.WriteAndVerifyExpandedCustomerEntry(responseMessageWithModel, odataWriter,
                                                                               true, mimeType);
                }

                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataEntryWriter();
                    outputWithoutModel = this.WriteAndVerifyExpandedCustomerEntry(responseMessageWithoutModel,
                                                                                  odataWriter, false, mimeType);
                }

                if (mimeType != MimeTypes.ApplicationAtomXml)
                {
                    WritePayloadHelper.VerifyPayloadString(outputWithModel, outputWithoutModel, mimeType);
                }
            }
        }
        public void CarEntryTest()
        {
            foreach (var mimeType in this.mimeTypes)
            {
                var settings = new ODataMessageWriterSettings();
                settings.ODataUri = new ODataUri() { ServiceRoot = this.ServiceUri };
                string outputWithModel = null;
                string outputWithoutModel = null;

                var responseMessageWithModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithModel.SetHeader("Content-Type", mimeType);
                responseMessageWithModel.PreferenceAppliedHeader().AnnotationFilter = "*";
                using (var messageWriter = new ODataMessageWriter(responseMessageWithModel, settings, WritePayloadHelper.Model))
                {
                    var odataWriter = messageWriter.CreateODataEntryWriter(WritePayloadHelper.CarSet, WritePayloadHelper.CarType);
                    outputWithModel = this.WriteAndVerifyCarEntry(responseMessageWithModel, odataWriter, true, mimeType);
                }

                var responseMessageWithoutModel = new StreamResponseMessage(new MemoryStream());
                responseMessageWithoutModel.SetHeader("Content-Type", mimeType);
                responseMessageWithoutModel.PreferenceAppliedHeader().AnnotationFilter = "*";
                using (var messageWriter = new ODataMessageWriter(responseMessageWithoutModel, settings))
                {
                    var odataWriter = messageWriter.CreateODataEntryWriter();
                    outputWithoutModel = this.WriteAndVerifyCarEntry(responseMessageWithoutModel, odataWriter, false,
                                                                     mimeType);
                }

                WritePayloadHelper.VerifyPayloadString(outputWithModel, outputWithoutModel, mimeType);
            }
        }