public void EntryReadAndEditLinkMetadataWriterTest() { Func <XElement, XElement> fragmentExtractor = (e) => e.Element(TestAtomConstants.AtomXNamespace + "link"); // Convert test cases to test descriptions; first for the entry's self link var selfLinkTestDescriptors = linkMetadataTestCases.Select(testCase => { ODataEntry entry = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata(); entry.Atom().SelfLink = testCase.LinkMetadata("self", readLinkHref); return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, entry, testConfiguration => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.ExpectedXml == null ? null : testCase.ExpectedXml("self", readLinkHref, null, null), ExpectedException2 = testCase.ExpectedException == null ? null : testCase.ExpectedException("self", readLinkHref), FragmentExtractor = fragmentExtractor })); }); // now the ones for the entry's edit link var editLinkTestDescriptors = linkMetadataTestCases.Select(testCase => { ODataEntry entry = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata(); entry.ReadLink = null; entry.EditLink = new Uri(editLinkHref); entry.Atom().EditLink = testCase.LinkMetadata("edit", editLinkHref); return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, entry, testConfiguration => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.ExpectedXml == null ? null : testCase.ExpectedXml("edit", editLinkHref, null, null), ExpectedException2 = testCase.ExpectedException == null ? null : testCase.ExpectedException("edit", editLinkHref), FragmentExtractor = fragmentExtractor })); }); var testDescriptors = selfLinkTestDescriptors.Concat(editLinkTestDescriptors); this.CombinatorialEngineProvider.RunCombinations( testDescriptors.PayloadCases(WriterPayloads.EntryPayloads), this.WriterTestConfigurationProvider.AtomFormatConfigurations, (testDescriptor, testConfiguration) => { testConfiguration = testConfiguration.Clone(); testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); TestWriterUtils.WriteAndVerifyODataPayload(testDescriptor, testConfiguration, this.Assert, this.Logger); }); }
public void DefaultStreamEditLinkMetadataWriterTest() { Func <XElement, XElement> fragmentExtractor = (e) => e.Elements(TestAtomConstants.AtomXNamespace + "link").Last(); // NOTE: no self-link test cases since the self link is represented as the <content> element and not customizable through link metadata var testDescriptors = linkMetadataTestCases.Select(testCase => { ODataEntry entry = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata(); ODataStreamReferenceValue streamReferenceValue = new ODataStreamReferenceValue() { ReadLink = new Uri(readLinkHref), EditLink = new Uri(editLinkHref), ContentType = linkMediaType, }; AtomStreamReferenceMetadata streamReferenceMetadata = new AtomStreamReferenceMetadata() { EditLink = testCase.LinkMetadata("edit-media", editLinkHref) }; streamReferenceValue.SetAnnotation <AtomStreamReferenceMetadata>(streamReferenceMetadata); entry.MediaResource = streamReferenceValue; return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, entry, testConfiguration => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.ExpectedXml == null ? null : testCase.ExpectedXml("edit-media", editLinkHref, null, null), ExpectedException2 = testCase.ExpectedException == null ? null : testCase.ExpectedException("edit-media", editLinkHref), FragmentExtractor = fragmentExtractor })); }); this.CombinatorialEngineProvider.RunCombinations( testDescriptors.PayloadCases(WriterPayloads.EntryPayloads), this.WriterTestConfigurationProvider.AtomFormatConfigurations, (testDescriptor, testConfiguration) => { testConfiguration = testConfiguration.Clone(); testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); TestWriterUtils.WriteAndVerifyODataPayload(testDescriptor, testConfiguration, this.Assert, this.Logger); }); }
public void CategoryMetadataOnEntryWriterTest() { var testCases = this.CreateAtomCategoryTestCases(); Func <XElement, XElement> fragmentExtractor = (e) => { var categoryElement = e.Element(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomCategoryElementName); return(categoryElement ?? new XElement("missingCategory")); }; // Convert test cases to test descriptions IEnumerable <PayloadWriterTestDescriptor <ODataItem> > testDescriptors = testCases.Select(testCase => { ODataEntry entry = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata(); AtomEntryMetadata metadata = entry.Atom(); this.Assert.IsNotNull(metadata, "Expected default entry metadata on a default entry."); metadata.Categories = new AtomCategoryMetadata[] { testCase.Category }; return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, entry, testConfiguration => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.Xml, ExpectedException2 = testCase.ExpectedException, FragmentExtractor = fragmentExtractor })); }); // Add tests for category with type name string testTypeName = "TestModel.TestTypeName"; string testLabel = "Test category 1 label"; var categoryWithTypeNameTestCases = new[] { // Clean merge, no conflicts new AtomCategoryTestCase { Category = new AtomCategoryMetadata() { Term = null, Scheme = null, Label = testLabel, }, Xml = @"<category term=""" + testTypeName + @""" scheme=""" + TestAtomConstants.ODataSchemeNamespace + @""" label=""" + testLabel + @""" xmlns=""" + TestAtomConstants.AtomNamespace + @""" />", ExpectedException = null }, // Patch, conflicting values match new AtomCategoryTestCase { Category = new AtomCategoryMetadata() { Term = testTypeName, Scheme = TestAtomConstants.ODataSchemeNamespace, Label = testLabel, }, Xml = @"<category term=""" + testTypeName + @""" scheme=""" + TestAtomConstants.ODataSchemeNamespace + @""" label=""" + testLabel + @""" xmlns=""" + TestAtomConstants.AtomNamespace + @""" />", ExpectedException = null }, // Patch conflict on term new AtomCategoryTestCase { Category = new AtomCategoryMetadata() { Term = testTypeName.ToUpper(), Scheme = TestAtomConstants.ODataSchemeNamespace, Label = testLabel, }, Xml = @"<category term=""" + testTypeName + @""" scheme=""" + TestAtomConstants.ODataSchemeNamespace + @""" label=""" + testLabel + @""" xmlns=""" + TestAtomConstants.AtomNamespace + @""" />", ExpectedException = ODataExpectedExceptions.ODataException("ODataAtomWriterMetadataUtils_CategoryTermsMustMatch", testTypeName, testTypeName.ToUpper()) }, // Patch conflict on scheme new AtomCategoryTestCase { Category = new AtomCategoryMetadata() { Term = testTypeName, Scheme = TestAtomConstants.ODataSchemeNamespace.ToUpper(), Label = testLabel, }, Xml = @"<category term=""" + testTypeName + @""" scheme=""" + TestAtomConstants.ODataSchemeNamespace + @""" label=""" + testLabel + @""" xmlns=""" + TestAtomConstants.AtomNamespace + @""" />", ExpectedException = ODataExpectedExceptions.ODataException("ODataAtomWriterMetadataUtils_CategorySchemesMustMatch", TestAtomConstants.ODataSchemeNamespace, TestAtomConstants.ODataSchemeNamespace.ToUpper()) }, }; testDescriptors = testDescriptors.Concat(categoryWithTypeNameTestCases.Select(testCase => { ODataEntry entry = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata(); entry.TypeName = testTypeName; AtomEntryMetadata metadata = entry.Atom(); this.Assert.IsNotNull(metadata, "Expected default entry metadata on a default entry."); metadata.CategoryWithTypeName = testCase.Category; return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, entry, testConfiguration => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.Xml, ExpectedException2 = testCase.ExpectedException, FragmentExtractor = fragmentExtractor })); })); // Add all the categories as the category with type name on entry which has no type name. // This verifies that the writer won't write the category with type name if there's no type name. testDescriptors = testDescriptors.Concat(testCases.Select(testCase => { ODataEntry entry = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata(); AtomEntryMetadata metadata = entry.Atom(); this.Assert.IsNotNull(metadata, "Expected default entry metadata on a default entry."); metadata.CategoryWithTypeName = testCase.Category; return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, entry, testConfiguration => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = "<missingCategory />", ExpectedException2 = null, FragmentExtractor = fragmentExtractor })); })); this.CombinatorialEngineProvider.RunCombinations( testDescriptors.PayloadCases(WriterPayloads.EntryPayloads), this.WriterTestConfigurationProvider.AtomFormatConfigurations, (testDescriptor, testConfiguration) => { testConfiguration = testConfiguration.Clone(); testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); TestWriterUtils.WriteAndVerifyODataPayload(testDescriptor, testConfiguration, this.Assert, this.Logger); }); }
public void PersonMetadataWriterTest() { string testEmail = "*****@*****.**"; string testName = "Test Author 1"; string testUri = "http://odata.org/authors/1"; var testCases = new[] { new { Person = new AtomPersonMetadata() { Name = testName, Email = testEmail, Uri = new Uri(testUri) }, Xml = string.Join( "$(NL)", @"<author xmlns=""" + TestAtomConstants.AtomNamespace + @""">", @" <name>" + testName + @"</name>", @" <uri>" + testUri + @"</uri>", @" <email>" + testEmail + @"</email>", @"</author>"), ExpectedException = (string)null }, new { Person = new AtomPersonMetadata() { Name = null, Email = testEmail, Uri = new Uri(testUri) }, Xml = string.Join( "$(NL)", @"<author xmlns=""" + TestAtomConstants.AtomNamespace + @""">", @" <name />", @" <uri>" + testUri + @"</uri>", @" <email>" + testEmail + @"</email>", @"</author>"), ExpectedException = (string)null }, new { Person = new AtomPersonMetadata() { Name = testName, Email = null, Uri = null }, Xml = string.Join( "$(NL)", @"<author xmlns=""" + TestAtomConstants.AtomNamespace + @""">", @" <name>" + testName + @"</name>", @"</author>"), ExpectedException = (string)null }, new { Person = (AtomPersonMetadata)testName, Xml = string.Join( "$(NL)", @"<author xmlns=""" + TestAtomConstants.AtomNamespace + @""">", @" <name>" + testName + @"</name>", @"</author>"), ExpectedException = (string)null }, }; Func <XElement, XElement> fragmentExtractor = (e) => e.Element(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomAuthorElementName); // Convert test cases to test descriptions var testDescriptors = testCases.Select(testCase => { ODataEntry entry = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata(); AtomEntryMetadata metadata = entry.Atom(); this.Assert.IsNotNull(metadata, "Expected default entry metadata on a default entry."); metadata.Authors = new AtomPersonMetadata[] { testCase.Person }; return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, entry, testConfiguration => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.Xml, ExpectedODataExceptionMessage = testCase.ExpectedException, FragmentExtractor = fragmentExtractor })); }); this.CombinatorialEngineProvider.RunCombinations( testDescriptors.PayloadCases(WriterPayloads.EntryPayloads), this.WriterTestConfigurationProvider.AtomFormatConfigurations, (testDescriptor, testConfiguration) => { testConfiguration = testConfiguration.Clone(); testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); TestWriterUtils.WriteAndVerifyODataPayload(testDescriptor, testConfiguration, this.Assert, this.Logger); }); }
public void EntryMetadataWriterTest() { const string testPublished = "2010-10-20T20:10:00Z"; AtomTextConstruct testRights = new AtomTextConstruct { Text = "Copyright Data Fx team." }; AtomTextConstruct testSummary = new AtomTextConstruct { Text = "Test summary." }; AtomTextConstruct testTitle = new AtomTextConstruct { Text = "Test title" }; const string testUpdated = "2010-11-01T00:04:00Z"; const string testIcon = "http://odata.org/icon"; const string testSourceId = "http://odata.org/id/random"; const string testLogo = "http://odata.org/logo"; AtomTextConstruct testSubtitle = new AtomTextConstruct { Text = "Test subtitle." }; const string testAuthorName = "Test Author 1"; const string testAuthorEmail = "*****@*****.**"; const string testAuthorUri = "http://odata.org/authors/1"; var testAuthors = new AtomPersonMetadata[] { new AtomPersonMetadata() { Email = testAuthorEmail, Name = testAuthorName, Uri = new Uri(testAuthorUri) } }; var testAuthors2 = new AtomPersonMetadata[0]; const string testCategoryTerm = "Test category 1 term"; const string testCategoryLabel = "Test category 1 label"; const string testCategoryScheme = "http://odata.org/categories/1"; var testCategories = new AtomCategoryMetadata[] { new AtomCategoryMetadata() { Term = testCategoryTerm, Label = testCategoryLabel, Scheme = testCategoryScheme } }; const string testContributorName = "Test Contributor 1"; const string testContributorEmail = "*****@*****.**"; const string testContributorUri = "http://odata.org/contributors/1"; var testContributors = new AtomPersonMetadata[] { new AtomPersonMetadata() { Email = testContributorEmail, Name = testContributorName, Uri = new Uri(testContributorUri) } }; const string testGeneratorName = "Test generator"; const string testGeneratorUri = "http://odata.org/generator"; const string testGeneratorVersion = "3.0"; var testGenerator = new AtomGeneratorMetadata() { Name = testGeneratorName, Uri = new Uri(testGeneratorUri), Version = testGeneratorVersion }; const string testLinkRelation = "http://odata.org/links/1"; const string testLinkTitle = "Test link 1"; const string testLinkHref = "http://odata.org/links/1"; const string testLinkHrefLang = "de-AT"; int? testLinkLength = 999; const string testLinkMediaType = "image/png"; var testLinks = new AtomLinkMetadata[] { new AtomLinkMetadata() { Relation = testLinkRelation, Title = testLinkTitle, Href = new Uri(testLinkHref), HrefLang = testLinkHrefLang, Length = testLinkLength, MediaType = testLinkMediaType } }; AtomFeedMetadata testSource = new AtomFeedMetadata() { Authors = testAuthors, Categories = testCategories, Contributors = testContributors, Generator = testGenerator, Icon = new Uri(testIcon), SourceId = new Uri(testSourceId), Links = testLinks, Logo = new Uri(testLogo), Rights = testRights, Subtitle = testSubtitle, Title = testTitle, Updated = DateTimeOffset.Parse(testUpdated) }; Func <string, Func <XElement, XElement> > fragmentExtractor = (localName) => (e) => e.Element(TestAtomConstants.AtomXNamespace + localName); // TODO, ckerer: specify an Id via metadata if the entry does not specify one; we first have to decide what rules // we want to apply to merging of metadata and ODataLib OM data. var testCases = new[] { new { // specify an author via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Authors = testAuthors), Xml = string.Join( "$(NL)", @"<author xmlns=""" + TestAtomConstants.AtomNamespace + @""">", @" <name>" + testAuthorName + @"</name>", @" <uri>" + testAuthorUri + @"</uri>", @" <email>" + testAuthorEmail + @"</email>", @"</author>"), Extractor = fragmentExtractor(TestAtomConstants.AtomAuthorElementName) }, new { // specify an empty author array via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Authors = testAuthors2), Xml = string.Join( "$(NL)", @"<author xmlns=""" + TestAtomConstants.AtomNamespace + @""">", @" <name />", @"</author>"), Extractor = fragmentExtractor(TestAtomConstants.AtomAuthorElementName) }, new { // specify no authors via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Authors = null), Xml = string.Join( "$(NL)", @"<author xmlns=""" + TestAtomConstants.AtomNamespace + @""">", @"$(Indent)<name />", @"</author>"), Extractor = fragmentExtractor(TestAtomConstants.AtomAuthorElementName) }, new { // specify a category via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Categories = testCategories), Xml = @"<category term=""" + testCategoryTerm + @""" scheme=""" + testCategoryScheme + @""" label=""" + testCategoryLabel + @""" xmlns=""" + TestAtomConstants.AtomNamespace + @""" />", Extractor = fragmentExtractor(TestAtomConstants.AtomCategoryElementName) }, new { // specify a contributor via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Contributors = testContributors), Xml = string.Join( "$(NL)", @"<contributor xmlns=""" + TestAtomConstants.AtomNamespace + @""">", @" <name>" + testContributorName + @"</name>", @" <uri>" + testContributorUri + @"</uri>", @" <email>" + testContributorEmail + @"</email>", @"</contributor>"), Extractor = fragmentExtractor(TestAtomConstants.AtomContributorElementName) }, new { // specify a link via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Links = testLinks), Xml = @"<link rel=""" + testLinkRelation + @""" type = """ + testLinkMediaType + @""" title=""" + testLinkTitle + @""" href=""" + testLinkHref + @""" hreflang=""" + testLinkHrefLang + @""" length=""" + testLinkLength + @""" xmlns=""" + TestAtomConstants.AtomNamespace + @"""/>", Extractor = new Func <XElement, XElement>( (e) => e.Elements(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomLinkElementName) .Where(l => l.Attribute(TestAtomConstants.AtomLinkRelationAttributeName).Value != TestAtomConstants.AtomSelfRelationAttributeValue) .Single()) }, new { // specify a published date via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Published = DateTimeOffset.Parse(testPublished)), Xml = @"<published xmlns=""" + TestAtomConstants.AtomNamespace + @""">" + testPublished + @"</published>", Extractor = fragmentExtractor(TestAtomConstants.AtomPublishedElementName) }, new { // specify rights via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Rights = testRights), Xml = @"<rights type=""text"" xmlns=""" + TestAtomConstants.AtomNamespace + @""">" + testRights.Text + @"</rights>", Extractor = fragmentExtractor(TestAtomConstants.AtomRightsElementName) }, new { // specify a source via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Source = testSource), Xml = string.Join( "$(NL)", @"<source xmlns=""" + TestAtomConstants.AtomNamespace + @""">", @" <id>" + testSourceId + "</id>", @" <title type=""text"">" + testTitle.Text + @"</title>", @" <subtitle type=""text"">" + testSubtitle.Text + @"</subtitle>", @" <updated>" + testUpdated + @"</updated>", @" <link rel=""" + testLinkRelation + @""" type = """ + testLinkMediaType + @""" title=""" + testLinkTitle + @""" href=""" + testLinkHref + @""" hreflang=""" + testLinkHrefLang + @""" length=""" + testLinkLength + @"""/>", @" <category term=""" + testCategoryTerm + @""" scheme=""" + testCategoryScheme + @""" label=""" + testCategoryLabel + @""" />", @" <logo>" + testLogo + @"</logo>", @" <rights type=""text"">" + testRights.Text + @"</rights>", @" <contributor>", @" <name>" + testContributorName + @"</name>", @" <uri>" + testContributorUri + @"</uri>", @" <email>" + testContributorEmail + @"</email>", @" </contributor>", @" <generator uri=""" + testGeneratorUri + @""" version=""" + testGeneratorVersion + @""">" + testGeneratorName + @"</generator>", @" <icon>" + testIcon + @"</icon>", @" <author>", @" <name>" + testAuthorName + @"</name>", @" <uri>" + testAuthorUri + @"</uri>", @" <email>" + testAuthorEmail + @"</email>", @" </author>", @"</source>"), Extractor = fragmentExtractor(TestAtomConstants.AtomSourceElementName) }, new { // specify default feed metadata as source CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Source = new AtomFeedMetadata()), Xml = string.Join( "$(NL)", @"<source xmlns=""" + TestAtomConstants.AtomNamespace + @""">", @" <id />", @" <title />", @" <updated />", @"</source>"), Extractor = new Func <XElement, XElement>(result => { var source = fragmentExtractor(TestAtomConstants.AtomSourceElementName)(result); // Remove the value of updates as it can't be reliably predicted source.Element(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomUpdatedElementName).Nodes().Remove(); return(source); }) }, new { // specify a summary via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Summary = testSummary), Xml = @"<summary type=""text"" xmlns=""" + TestAtomConstants.AtomNamespace + @""">" + testSummary.Text + @"</summary>", Extractor = fragmentExtractor(TestAtomConstants.AtomSummaryElementName) }, new { // specify a title via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Title = testTitle), Xml = @"<title type=""text"" xmlns=""" + TestAtomConstants.AtomNamespace + @""">" + testTitle.Text + @"</title>", Extractor = fragmentExtractor(TestAtomConstants.AtomTitleElementName) }, new { // specify an updated date via metadata CustomizeMetadata = new Action <AtomEntryMetadata>(metadata => metadata.Updated = DateTimeOffset.Parse(testUpdated)), Xml = @"<updated xmlns=""" + TestAtomConstants.AtomNamespace + @""">" + testUpdated + @"</updated>", Extractor = fragmentExtractor(TestAtomConstants.AtomUpdatedElementName) }, }; // Convert test cases to test descriptions IEnumerable <Func <ODataEntry> > entryCreators = new Func <ODataEntry>[] { () => ObjectModelUtils.CreateDefaultEntry(), () => ObjectModelUtils.CreateDefaultEntryWithAtomMetadata(), }; var testDescriptors = testCases.SelectMany(testCase => entryCreators.Select(entryCreator => { ODataEntry entry = entryCreator(); AtomEntryMetadata metadata = entry.Atom(); this.Assert.IsNotNull(metadata, "Expected default entry metadata on a default entry."); testCase.CustomizeMetadata(metadata); return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, entry, testConfiguration => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.Xml, FragmentExtractor = testCase.Extractor })); })); this.CombinatorialEngineProvider.RunCombinations( testDescriptors.PayloadCases(WriterPayloads.EntryPayloads), this.WriterTestConfigurationProvider.AtomFormatConfigurations, (testDescriptor, testConfiguration) => { testConfiguration = testConfiguration.Clone(); testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); TestWriterUtils.WriteAndVerifyODataPayload(testDescriptor, testConfiguration, this.Assert, this.Logger); }); }
private PayloadWriterTestDescriptor <ODataItem>[] CreateFeedQueryCountDescriptors() { Func <long?, ODataFeed> feedCreator = (c) => { ODataFeed feed = ObjectModelUtils.CreateDefaultFeed(); feed.Count = c; return(feed); }; long?[] counts = new long?[] { 0, 1, 2, 1000, -1 - 10, long.MaxValue, long.MinValue, null }; EdmModel model = new EdmModel(); ODataEntry entry = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata("DefaultEntitySet", "DefaultEntityType", model); var container = model.FindEntityContainer("DefaultContainer"); var entitySet = container.FindEntitySet("DefaultEntitySet") as EdmEntitySet; var entityType = model.FindType("TestModel.DefaultEntityType") as EdmEntityType; var descriptors = counts.Select(count => new PayloadWriterTestDescriptor <ODataItem>(this.Settings, feedCreator(count), (testConfiguration) => { if (testConfiguration.IsRequest && count.HasValue) { return(new WriterTestExpectedResults(this.Settings.ExpectedResultSettings) { ExpectedException2 = ODataExpectedExceptions.ODataException("ODataWriterCore_QueryCountInRequest") }); } if (testConfiguration.Format == ODataFormat.Atom) { if (count.HasValue) { return(new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = @"<m:count xmlns:m =""" + TestAtomConstants.ODataMetadataNamespace + @""">" + count + "</m:count>", FragmentExtractor = (result) => result.Elements(XName.Get("count", TestAtomConstants.ODataMetadataNamespace)).Single() }); } else { return(new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = @"<nocount xmlns=""" + TestAtomConstants.ODataMetadataNamespace + @"""/>", FragmentExtractor = (result) => { var countElement = result.Elements(XName.Get("count", TestAtomConstants.ODataMetadataNamespace)).SingleOrDefault(); if (countElement == null) { countElement = new XElement(TestAtomConstants.ODataMetadataXNamespace + "nocount"); } return countElement; } }); } } else if (testConfiguration.Format == ODataFormat.Json) { if (count.HasValue) { return(new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Json = "$(Indent)$(Indent)\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataCountAnnotationName + "\":\"" + count + "\"", FragmentExtractor = (result) => result.Object().Property(JsonLightConstants.ODataCountAnnotationName) }); } else { return(new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Json = string.Join("$(NL)", "[", string.Empty, "]"), FragmentExtractor = (result) => { return JsonLightWriterUtils.GetTopLevelFeedItemsArray(testConfiguration, result).RemoveAllAnnotations(true); } }); } } else { string formatName = testConfiguration.Format == null ? "null" : testConfiguration.Format.GetType().Name; throw new NotSupportedException("Invalid format detected: " + formatName); } }) { Model = model, PayloadEdmElementContainer = entitySet, PayloadEdmElementType = entityType, }); return(descriptors.ToArray()); }
public void FeedAndEntryUpdatedTimeTests() { ODataFeed defaultFeedWithEmptyMetadata = ObjectModelUtils.CreateDefaultFeed(); defaultFeedWithEmptyMetadata.SetAnnotation <AtomFeedMetadata>(new AtomFeedMetadata()); ODataEntry defaultEntryWithEmptyMetadata = ObjectModelUtils.CreateDefaultEntry(); defaultEntryWithEmptyMetadata.SetAnnotation <AtomEntryMetadata>(new AtomEntryMetadata()); IEnumerable <PayloadWriterTestDescriptor <ODataItem> > testPayloads = new[] { new PayloadWriterTestDescriptor <ODataItem>(this.Settings, ObjectModelUtils.CreateDefaultFeed()), new PayloadWriterTestDescriptor <ODataItem>(this.Settings, defaultFeedWithEmptyMetadata), new PayloadWriterTestDescriptor <ODataItem>(this.Settings, ObjectModelUtils.CreateDefaultFeedWithAtomMetadata()), }.PayloadCases(WriterPayloads.FeedPayloads) .Concat((new[] { new PayloadWriterTestDescriptor <ODataItem>(this.Settings, ObjectModelUtils.CreateDefaultEntry()), new PayloadWriterTestDescriptor <ODataItem>(this.Settings, defaultEntryWithEmptyMetadata), new PayloadWriterTestDescriptor <ODataItem>(this.Settings, ObjectModelUtils.CreateDefaultEntryWithAtomMetadata()), }.PayloadCases(WriterPayloads.EntryPayloads))); this.CombinatorialEngineProvider.RunCombinations( testPayloads, this.WriterTestConfigurationProvider.AtomFormatConfigurationsWithIndent.Where(tc => !tc.IsRequest), (testPayload, testConfiguration) => { testConfiguration = testConfiguration.Clone(); testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); string lastUpdatedTimeStr = null; using (var memoryStream = new MemoryStream()) { using (var testMemoryStream = TestWriterUtils.CreateTestStream(testConfiguration, memoryStream, ignoreDispose: true)) { bool feedWriter = testPayload.PayloadItems[0] is ODataFeed; TestMessage testMessage = null; Exception exception = TestExceptionUtils.RunCatching(() => { using (var messageWriter = TestWriterUtils.CreateMessageWriter(testMemoryStream, testConfiguration, this.Assert, out testMessage, null, testPayload.Model)) { ODataWriter writer = messageWriter.CreateODataWriter(feedWriter); TestWriterUtils.WritePayload(messageWriter, writer, true, testPayload.PayloadItems, testPayload.ThrowUserExceptionAt); } }); this.Assert.IsNull(exception, "Received exception but expected none."); } memoryStream.Position = 0; XElement result = XElement.Load(memoryStream); foreach (XElement updated in result.Descendants(((XNamespace)TestAtomConstants.AtomNamespace) + "updated")) { if (updated.Value != ObjectModelUtils.DefaultFeedUpdated && updated.Value != ObjectModelUtils.DefaultEntryUpdated) { if (lastUpdatedTimeStr == null) { lastUpdatedTimeStr = updated.Value; } else { this.Assert.AreEqual(lastUpdatedTimeStr, updated.Value, "<atom:updated> should contain the same value."); } } } } }); }
public void NamedStreamReadAndEditLinkMetadataWriterTest() { Func <XElement, XElement> fragmentExtractor = (e) => e.Elements(TestAtomConstants.AtomXNamespace + "link").Last(); var allTestCases = linkMetadataTestCases.ConcatSingle(incorrectMediaTypeLinkMetadataTestCases).ConcatSingle(incorrectTitleLinkMetadataTestCases); var readLinkTestDescriptors = allTestCases.Select(testCase => { ODataEntry entry = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata(); ODataStreamReferenceValue streamReferenceValue = new ODataStreamReferenceValue() { ReadLink = new Uri(readLinkHref), ContentType = linkMediaType, }; AtomStreamReferenceMetadata streamReferenceMetadata = new AtomStreamReferenceMetadata() { SelfLink = testCase.LinkMetadata("http://docs.oasis-open.org/odata/ns/mediaresource/Stream", readLinkHref) }; streamReferenceValue.SetAnnotation <AtomStreamReferenceMetadata>(streamReferenceMetadata); entry.Properties = new ODataProperty[] { new ODataProperty { Name = "Id", Value = 1 }, new ODataProperty { Name = "Stream", Value = streamReferenceValue } }; return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, entry, testConfiguration => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.ExpectedXml == null ? null : testCase.ExpectedXml("http://docs.oasis-open.org/odata/ns/mediaresource/Stream", readLinkHref, "Stream", linkMediaType), ExpectedException2 = testCase.ExpectedException == null ? null : testCase.ExpectedException("http://docs.oasis-open.org/odata/ns/mediaresource/Stream", readLinkHref), FragmentExtractor = fragmentExtractor })); }); var editLinkTestDescriptors = allTestCases.Select(testCase => { ODataEntry entry = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata(); ODataStreamReferenceValue streamReferenceValue = new ODataStreamReferenceValue() { ReadLink = new Uri(readLinkHref), EditLink = new Uri(editLinkHref), ContentType = linkMediaType, }; AtomStreamReferenceMetadata streamReferenceMetadata = new AtomStreamReferenceMetadata() { EditLink = testCase.LinkMetadata("http://docs.oasis-open.org/odata/ns/edit-media/Stream", editLinkHref) }; streamReferenceValue.SetAnnotation <AtomStreamReferenceMetadata>(streamReferenceMetadata); entry.Properties = new ODataProperty[] { new ODataProperty { Name = "Id", Value = 1 }, new ODataProperty { Name = "Stream", Value = streamReferenceValue } }; return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, entry, testConfiguration => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.ExpectedXml == null ? null : testCase.ExpectedXml("http://docs.oasis-open.org/odata/ns/edit-media/Stream", editLinkHref, "Stream", linkMediaType), ExpectedException2 = testCase.ExpectedException == null ? null : testCase.ExpectedException("http://docs.oasis-open.org/odata/ns/edit-media/Stream", editLinkHref), FragmentExtractor = fragmentExtractor })); }); var testDescriptors = readLinkTestDescriptors.Concat(editLinkTestDescriptors); this.CombinatorialEngineProvider.RunCombinations( testDescriptors.PayloadCases(WriterPayloads.EntryPayloads), this.WriterTestConfigurationProvider.AtomFormatConfigurations .Where(tc => !tc.IsRequest), (testDescriptor, testConfiguration) => { testConfiguration = testConfiguration.Clone(); testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); TestWriterUtils.WriteAndVerifyODataPayload(testDescriptor, testConfiguration, this.Assert, this.Logger); }); }
public void WriteEntityReferenceLinkAfterFeedJsonLightErrorTest() { EdmModel model = new EdmModel(); var container = new EdmEntityContainer("TestModel", "DefaultContainer"); model.AddElement(container); var customerType = new EdmEntityType("TestModel", "Customer"); var orderType = new EdmEntityType("TestModel", "Order"); var orderNavProp = customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "Orders", Target = orderType, TargetMultiplicity = EdmMultiplicity.Many }); var bestFriendNavProp = customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo { Name = "BestFriend", Target = customerType, TargetMultiplicity = EdmMultiplicity.One }); var customerSet = container.AddEntitySet("Customers", customerType); var orderSet = container.AddEntitySet("Order", orderType); customerSet.AddNavigationTarget(orderNavProp, orderSet); customerSet.AddNavigationTarget(bestFriendNavProp, customerSet); model.AddElement(customerType); model.AddElement(orderType); ODataResource expandedOrderInstance = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata(); expandedOrderInstance.TypeName = "TestModel.Order"; var testCases = new WriterNavigationLinkTests.NavigationLinkTestCase[] { // Entity reference link after empty feed in collection navigation link new WriterNavigationLinkTests.NavigationLinkTestCase { Items = new ODataItem[] { new ODataNestedResourceInfo() { IsCollection = true, Name = "Orders", Url = new Uri("http://odata.org/nav") }, ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(), null, new ODataEntityReferenceLink() { Url = new Uri("http://odata.org/singleton") }, }, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightWriter_EntityReferenceLinkAfterResourceSetInRequest") }, // Entity reference link after non-empty feed in collection navigation link new WriterNavigationLinkTests.NavigationLinkTestCase { Items = new ODataItem[] { new ODataNestedResourceInfo() { IsCollection = true, Name = "Orders", Url = new Uri("http://odata.org/nav") }, ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(), expandedOrderInstance, null, null, new ODataEntityReferenceLink() { Url = new Uri("http://odata.org/singleton") }, }, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightWriter_EntityReferenceLinkAfterResourceSetInRequest") }, // Entity reference link before and after empty feed in collection navigation link new WriterNavigationLinkTests.NavigationLinkTestCase { Items = new ODataItem[] { new ODataNestedResourceInfo() { IsCollection = true, Name = "Orders", Url = new Uri("http://odata.org/nav") }, new ODataEntityReferenceLink() { Url = new Uri("http://odata.org/singleton") }, ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(), null, new ODataEntityReferenceLink() { Url = new Uri("http://odata.org/singleton") }, }, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightWriter_EntityReferenceLinkAfterResourceSetInRequest") }, // Entity reference link after two empty feeds in collection navigation link new WriterNavigationLinkTests.NavigationLinkTestCase { Items = new ODataItem[] { new ODataNestedResourceInfo() { IsCollection = true, Name = "Orders", Url = new Uri("http://odata.org/nav") }, new ODataEntityReferenceLink() { Url = new Uri("http://odata.org/singleton") }, ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(), null, ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(), null, new ODataEntityReferenceLink() { Url = new Uri("http://odata.org/singleton") }, }, ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightWriter_EntityReferenceLinkAfterResourceSetInRequest") }, }; IEnumerable <PayloadWriterTestDescriptor <ODataItem> > testDescriptors = testCases.Select(testCase => testCase.ToEdmTestDescriptor(this.Settings, model, customerSet, customerType)); this.CombinatorialEngineProvider.RunCombinations( testDescriptors, this.WriterTestConfigurationProvider.JsonLightFormatConfigurations.Where(tc => tc.IsRequest), (testDescriptor, testConfiguration) => { TestWriterUtils.WriteAndVerifyODataEdmPayload(testDescriptor, testConfiguration, this.Assert, this.Logger); }); }