public void WriteServiceDocument() { var msgWriter = new ODataMessageWriter(_response, _writerSettings, _map.Model); var collections = new List <ODataResourceCollectionInfo>(); foreach ( var entityContainer in _map.Model.EntityContainers().Where(ec => _map.Model.IsDefaultEntityContainer(ec))) { foreach (var es in entityContainer.EntitySets()) { var collectionInfo = new ODataResourceCollectionInfo { Url = new Uri(es.Name, UriKind.Relative) }; var metadata = new AtomResourceCollectionMetadata { Title = es.Name }; collectionInfo.SetAnnotation(metadata); collections.Add(collectionInfo); } } var workspace = new ODataWorkspace { Collections = collections }; msgWriter.WriteServiceDocument(workspace); }
/// <summary> /// Writes the ATOM metadata for a single (resource) collection element. /// </summary> /// <param name="writer">The <see cref="XmlWriter"/> to write to.</param> /// <param name="collection">The collection element to get the metadata for and write it.</param> internal static void WriteCollectionMetadata(XmlWriter writer, ODataResourceCollectionInfo collection) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(writer != null, "writer != null"); Debug.Assert(collection != null, "collection != null"); Debug.Assert(collection.Name != null, "collection.Name should have been validated at this point"); // TODO: implement writing the rest of the NYI collection metadata [see AtomPub spec]. AtomResourceCollectionMetadata metadata = collection.Atom(); string title = null; if (metadata != null) { title = metadata.Title; } if (title == null) { title = collection.Name; } // <atom:title>title</atom:title> ODataAtomWriterUtils.WriteElementWithTextContent(writer, AtomConstants.NonEmptyAtomNamespacePrefix, AtomConstants.AtomTitleElementName, AtomConstants.AtomNamespace, title); }
public void DefaultValuesTest() { AtomResourceCollectionMetadata resourceCollection = new AtomResourceCollectionMetadata(); this.Assert.IsNull(resourceCollection.Title, "Expected null default value for property 'Title'."); this.Assert.IsNull(resourceCollection.Accept, "Expected null default value for property 'Accept'."); this.Assert.IsNull(resourceCollection.Categories, "Expected null default value for property 'Categories'."); }
public void CategoryMetadataOnWorkspaceCollectionCategoriesWriterTest() { var testCases = this.CreateAtomCategoryTestCases(); // Convert test cases to test descriptions var testDescriptors = testCases.Select(testCase => { AtomResourceCollectionMetadata metadata = new AtomResourceCollectionMetadata { Categories = new AtomCategoriesMetadata { Categories = new[] { testCase.Category } } }; ODataEntitySetInfo collection = new ODataEntitySetInfo { Url = new Uri("http://odata.org/collection") }; collection.SetAnnotation(metadata); ODataServiceDocument serviceDocument = new ODataServiceDocument { EntitySets = new[] { collection } }; return(new PayloadWriterTestDescriptor <ODataServiceDocument>( this.Settings, serviceDocument, testConfiguration => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.Xml, ExpectedException2 = testCase.ExpectedException, FragmentExtractor = result => result .Element(TestAtomConstants.AtomPublishingXNamespace + TestAtomConstants.AtomPublishingWorkspaceElementName) .Element(TestAtomConstants.AtomPublishingXNamespace + TestAtomConstants.AtomPublishingCollectionElementName) .Element(TestAtomConstants.AtomPublishingXNamespace + TestAtomConstants.AtomPublishingCategoriesElementName) .Element(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomCategoryElementName) })); }); this.CombinatorialEngineProvider.RunCombinations( testDescriptors, this.WriterTestConfigurationProvider.AtomFormatConfigurations.Where(tc => !tc.IsRequest), (testDescriptor, testConfiguration) => { testConfiguration = testConfiguration.Clone(); testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); TestWriterUtils.WriteAndVerifyTopLevelContent( testDescriptor, testConfiguration, (messageWriter) => messageWriter.WriteServiceDocument(testDescriptor.PayloadItems.Single()), this.Assert, baselineLogger: this.Logger); }); }
public void PropertySettersNullTest() { AtomResourceCollectionMetadata resourceCollection = new AtomResourceCollectionMetadata() { Title = null, Accept = null, Categories = null, }; this.Assert.IsNull(resourceCollection.Title, "Expected null value for property 'Title'."); this.Assert.IsNull(resourceCollection.Accept, "Expected null value for property 'Accept'."); this.Assert.IsNull(resourceCollection.Categories, "Expected null value for property 'Categories'."); }
public void PropertyGettersAndSettersTest() { AtomTextConstruct title = new AtomTextConstruct(); string accept = "mime/type"; AtomCategoriesMetadata categories = new AtomCategoriesMetadata(); AtomResourceCollectionMetadata resourceCollection = new AtomResourceCollectionMetadata() { Title = title, Accept = accept, Categories = categories }; this.Assert.AreSame(title, resourceCollection.Title, "Expected reference equal values for property 'Title'."); this.Assert.AreSame(accept, resourceCollection.Accept, "Expected reference equal values for property 'Accept'."); this.Assert.AreSame(categories, resourceCollection.Categories, "Expected reference equal values for property 'Categories'."); }
internal void WriteServiceDocument(DataServiceProviderWrapper provider) { ODataWorkspace defaultWorkspace = new ODataWorkspace { Collections = provider.GetResourceSets().Select <ResourceSetWrapper, ODataResourceCollectionInfo>(delegate(ResourceSetWrapper rs) { ODataResourceCollectionInfo info = new ODataResourceCollectionInfo { Url = new Uri(rs.Name, UriKind.RelativeOrAbsolute) }; AtomResourceCollectionMetadata annotation = new AtomResourceCollectionMetadata(); AtomTextConstruct construct = new AtomTextConstruct { Text = rs.Name }; annotation.Title = construct; info.SetAnnotation <AtomResourceCollectionMetadata>(annotation); return(info); }) }; this.writer.WriteServiceDocument(defaultWorkspace); }
/// <summary> /// Visits a resource collection. /// </summary> /// <param name="entitySetInfo">The resource collection to visit.</param> protected override ODataPayloadElement VisitResourceCollection(ODataEntitySetInfo entitySetInfo) { ExceptionUtilities.CheckArgumentNotNull(entitySetInfo, "entitySetInfo"); ResourceCollectionInstance collectionInstance = (ResourceCollectionInstance)base.VisitResourceCollection(entitySetInfo); AtomResourceCollectionMetadata atomMetadata = entitySetInfo.GetAnnotation <AtomResourceCollectionMetadata>(); if (atomMetadata != null) { if (atomMetadata.Title != null) { collectionInstance.Title = atomMetadata.Title.Text; collectionInstance.AtomTitle(atomMetadata.Title.Text, ToString(atomMetadata.Title.Kind)); } if (atomMetadata.Accept != null) { collectionInstance.AppAccept(atomMetadata.Accept); } if (atomMetadata.Categories != null) { if (atomMetadata.Categories.Href != null) { collectionInstance.AppOutOfLineCategories(atomMetadata.Categories.Href.OriginalString); } else { collectionInstance.AppInlineCategories( atomMetadata.Categories.Fixed == null ? null : (atomMetadata.Categories.Fixed.Value ? TestAtomConstants.AtomPublishingFixedYesValue : TestAtomConstants.AtomPublishingFixedNoValue), atomMetadata.Categories.Scheme, atomMetadata.Categories.Categories == null ? new XmlTreeAnnotation[0] : atomMetadata.Categories.Categories.Select(category => AtomMetadataBuilder.AtomCategory(category.Term, category.Scheme, category.Label)).ToArray()); } } } return(collectionInstance); }
/// <summary> /// Visits an ATOM resource collection metadata. /// </summary> /// <param name="atomResourceCollectionMetadata">The resource collection metadata to visit.</param> protected virtual void VisitAtomResourceCollectionMetadata(AtomResourceCollectionMetadata atomResourceCollectionMetadata) { this.VisitAtomMetadata(atomResourceCollectionMetadata.Title); this.VisitAtomMetadata(atomResourceCollectionMetadata.Categories); }
/// <summary> /// Visits an ATOM metadata object. /// </summary> /// <param name="atomMetadata"></param> protected virtual void VisitAtomMetadata(object atomMetadata) { if (atomMetadata == null) { return; } AtomCategoryMetadata atomCategoryMetadata = atomMetadata as AtomCategoryMetadata; if (atomCategoryMetadata != null) { this.VisitAtomCategoryMetadata(atomCategoryMetadata); return; } AtomEntryMetadata atomEntryMetadata = atomMetadata as AtomEntryMetadata; if (atomEntryMetadata != null) { this.VisitAtomEntryMetadata(atomEntryMetadata); return; } AtomFeedMetadata atomFeedMetadata = atomMetadata as AtomFeedMetadata; if (atomFeedMetadata != null) { this.VisitAtomFeedMetadata(atomFeedMetadata); return; } AtomGeneratorMetadata atomGeneratorMetadata = atomMetadata as AtomGeneratorMetadata; if (atomGeneratorMetadata != null) { this.VisitAtomGeneratorMetadata(atomGeneratorMetadata); return; } AtomLinkMetadata atomLinkMetadata = atomMetadata as AtomLinkMetadata; if (atomLinkMetadata != null) { this.VisitAtomLinkMetadata(atomLinkMetadata); return; } AtomPersonMetadata atomPersonMetadata = atomMetadata as AtomPersonMetadata; if (atomPersonMetadata != null) { this.VisitAtomPersonMetadata(atomPersonMetadata); return; } AtomResourceCollectionMetadata atomResourceCollectionMetadata = atomMetadata as AtomResourceCollectionMetadata; if (atomResourceCollectionMetadata != null) { this.VisitAtomResourceCollectionMetadata(atomResourceCollectionMetadata); return; } AtomStreamReferenceMetadata atomStreamReferenceMetadata = atomMetadata as AtomStreamReferenceMetadata; if (atomStreamReferenceMetadata != null) { this.VisitAtomStreamReferenceMetadata(atomStreamReferenceMetadata); return; } AtomTextConstruct atomTextConstruct = atomMetadata as AtomTextConstruct; if (atomTextConstruct != null) { this.VisitAtomTextConstruct(atomTextConstruct); return; } AtomWorkspaceMetadata atomWorkspaceMetadata = atomMetadata as AtomWorkspaceMetadata; if (atomWorkspaceMetadata != null) { this.VisitAtomWorkspaceMetadata(atomWorkspaceMetadata); return; } AtomCategoriesMetadata atomCategoriesMetadata = atomMetadata as AtomCategoriesMetadata; if (atomCategoriesMetadata != null) { this.VisitAtomCategoriesMetadata(atomCategoriesMetadata); return; } ExceptionUtilities.Assert(false, "Unrecognized ATOM metadata object {0} of type {1}.", atomMetadata.ToString(), atomMetadata.GetType().ToString()); }
public void AtomResourceCollectionMetadataTest() { var testCases = new[] { // No title specified - an empty one is used as it's required by the spec. new { CustomizeMetadata = (Action <ODataEntitySetInfo>)(collection => { AtomResourceCollectionMetadata metadata = collection.Atom(); }), Xml = "<Collection><title xmlns='http://www.w3.org/2005/Atom'/></Collection>" }, // Simple title (other titles are tested in AtomTextConstructMetadataTests) new { CustomizeMetadata = (Action <ODataEntitySetInfo>)(collection => { AtomResourceCollectionMetadata metadata = collection.Atom(); metadata.Title = new AtomTextConstruct { Text = "collection title" }; }), Xml = "<Collection><title type='text' xmlns='http://www.w3.org/2005/Atom'>collection title</title></Collection>" }, // With accept new { CustomizeMetadata = (Action <ODataEntitySetInfo>)(collection => { AtomResourceCollectionMetadata metadata = collection.Atom(); metadata.Title = "collection title"; metadata.Accept = "mime/type"; }), Xml = "<Collection><title type='text' xmlns='http://www.w3.org/2005/Atom'>collection title</title><accept xmlns='http://www.w3.org/2007/app'>mime/type</accept></Collection>" }, // With empty accept new { CustomizeMetadata = (Action <ODataEntitySetInfo>)(collection => { AtomResourceCollectionMetadata metadata = collection.Atom(); metadata.Title = "collection title"; metadata.Accept = string.Empty; }), Xml = "<Collection><title type='text' xmlns='http://www.w3.org/2005/Atom'>collection title</title><accept xmlns='http://www.w3.org/2007/app'></accept></Collection>" }, // With categories new { CustomizeMetadata = (Action <ODataEntitySetInfo>)(collection => { AtomResourceCollectionMetadata metadata = collection.Atom(); metadata.Title = "collection title"; metadata.Categories = new AtomCategoriesMetadata(); }), Xml = "<Collection><title type='text' xmlns='http://www.w3.org/2005/Atom'>collection title</title><categories xmlns='http://www.w3.org/2007/app' /></Collection>" }, // With accept and categories new { CustomizeMetadata = (Action <ODataEntitySetInfo>)(collection => { AtomResourceCollectionMetadata metadata = collection.Atom(); metadata.Title = "collection title"; metadata.Accept = "mime/type"; metadata.Categories = new AtomCategoriesMetadata(); }), Xml = "<Collection><title type='text' xmlns='http://www.w3.org/2005/Atom'>collection title</title><accept xmlns='http://www.w3.org/2007/app'>mime/type</accept><categories xmlns='http://www.w3.org/2007/app' /></Collection>" }, }; Func <ODataEntitySetInfo>[] collectionCreators = new Func <ODataEntitySetInfo>[] { () => new ODataEntitySetInfo { Url = new Uri("http://odata.org/url") }, () => { var collection = new ODataEntitySetInfo { Url = new Uri("http://odata.org/url") }; collection.SetAnnotation(new AtomResourceCollectionMetadata()); return(collection); } }; var testDescriptors = testCases.SelectMany(testCase => collectionCreators.Select(collectionCreator => { ODataEntitySetInfo collection = collectionCreator(); testCase.CustomizeMetadata(collection); ODataServiceDocument serviceDocument = new ODataServiceDocument { EntitySets = new[] { collection } }; return(new PayloadWriterTestDescriptor <ODataServiceDocument>( this.Settings, new[] { serviceDocument }, tc => new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.Xml, FragmentExtractor = result => new XElement("Collection", result .Element(TestAtomConstants.AtomPublishingXNamespace + TestAtomConstants.AtomPublishingWorkspaceElementName) .Element(TestAtomConstants.AtomPublishingXNamespace + TestAtomConstants.AtomPublishingCollectionElementName) .Elements()) })); })); this.CombinatorialEngineProvider.RunCombinations( testDescriptors, this.WriterTestConfigurationProvider.AtomFormatConfigurations.Where(tc => !tc.IsRequest), (testDescriptor, testConfiguration) => { testConfiguration = testConfiguration.Clone(); testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); TestWriterUtils.WriteAndVerifyTopLevelContent( testDescriptor, testConfiguration, (messageWriter) => messageWriter.WriteServiceDocument(testDescriptor.PayloadItems.Single()), this.Assert, baselineLogger: this.Logger); }); }
public void AtomCategoriesMetadataTest() { var testCases = new [] { // Empty categories new { CategoriesMetadata = new AtomCategoriesMetadata(), Xml = "<categories xmlns='http://www.w3.org/2007/app'/>", ExpectedException = (ExpectedException)null }, // Categories with href new { CategoriesMetadata = new AtomCategoriesMetadata() { Href = new Uri("http://odata.org/href") }, Xml = "<categories href='http://odata.org/href' xmlns='http://www.w3.org/2007/app'/>", ExpectedException = (ExpectedException)null }, // Categories with href and non-null empty categories (null and empty collection should be treated the same) new { CategoriesMetadata = new AtomCategoriesMetadata() { Href = new Uri("http://odata.org/href"), Categories = new AtomCategoryMetadata[0] }, Xml = "<categories href='http://odata.org/href' xmlns='http://www.w3.org/2007/app'/>", ExpectedException = (ExpectedException)null }, // Categories with fixed yes new { CategoriesMetadata = new AtomCategoriesMetadata() { Fixed = true }, Xml = "<categories fixed='yes' xmlns='http://www.w3.org/2007/app'/>", ExpectedException = (ExpectedException)null }, // Categories with fixed no new { CategoriesMetadata = new AtomCategoriesMetadata() { Fixed = false }, Xml = "<categories fixed='no' xmlns='http://www.w3.org/2007/app'/>", ExpectedException = (ExpectedException)null }, // Categories with scheme new { CategoriesMetadata = new AtomCategoriesMetadata() { Scheme = "http://odata.org/scheme" }, Xml = "<categories scheme='http://odata.org/scheme' xmlns='http://www.w3.org/2007/app'/>", ExpectedException = (ExpectedException)null }, // Categories with scheme and fixed new { CategoriesMetadata = new AtomCategoriesMetadata() { Fixed = true, Scheme = "http://odata.org/scheme" }, Xml = "<categories fixed='yes' scheme='http://odata.org/scheme' xmlns='http://www.w3.org/2007/app'/>", ExpectedException = (ExpectedException)null }, // Categories with single category new { CategoriesMetadata = new AtomCategoriesMetadata() { Categories = new [] { new AtomCategoryMetadata { Term = "myterm", Scheme = "http://odata.org/scheme" } } }, Xml = "<categories xmlns='http://www.w3.org/2007/app'><atom:category term='myterm' scheme='http://odata.org/scheme' xmlns:atom='http://www.w3.org/2005/Atom'/></categories>", ExpectedException = (ExpectedException)null }, // Categories with two categories new { CategoriesMetadata = new AtomCategoriesMetadata() { Categories = new [] { new AtomCategoryMetadata { Term = "myterm", Scheme = "http://odata.org/scheme" }, new AtomCategoryMetadata { Term = "second", Scheme = "http://odata.org/scheme2" } } }, Xml = "<categories xmlns='http://www.w3.org/2007/app'>" + "<atom:category term='myterm' scheme='http://odata.org/scheme' xmlns:atom='http://www.w3.org/2005/Atom'/>" + "<atom:category term='second' scheme='http://odata.org/scheme2' xmlns:atom='http://www.w3.org/2005/Atom'/>" + "</categories>", ExpectedException = (ExpectedException)null }, // Categories with href and fixed (error) new { CategoriesMetadata = new AtomCategoriesMetadata() { Href = new Uri("http://odata.org/href"), Fixed = true }, Xml = string.Empty, ExpectedException = ODataExpectedExceptions.ODataException("ODataAtomWriterMetadataUtils_CategoriesHrefWithOtherValues") }, // Categories with href and scheme (error) new { CategoriesMetadata = new AtomCategoriesMetadata() { Href = new Uri("http://odata.org/href"), Scheme = "http://odata.org/scheme" }, Xml = string.Empty, ExpectedException = ODataExpectedExceptions.ODataException("ODataAtomWriterMetadataUtils_CategoriesHrefWithOtherValues") }, // Categories with href and non-empty categories (error) new { CategoriesMetadata = new AtomCategoriesMetadata() { Href = new Uri("http://odata.org/href"), Categories = new [] { new AtomCategoryMetadata() } }, Xml = string.Empty, ExpectedException = ODataExpectedExceptions.ODataException("ODataAtomWriterMetadataUtils_CategoriesHrefWithOtherValues") }, }; var testDescriptors = testCases.Select(testCase => { ODataEntitySetInfo collection = new ODataEntitySetInfo { Url = new Uri("http://odata.org/url") }; AtomResourceCollectionMetadata metadata = new AtomResourceCollectionMetadata() { Categories = testCase.CategoriesMetadata }; collection.SetAnnotation(metadata); ODataServiceDocument serviceDocument = new ODataServiceDocument { EntitySets = new [] { collection } }; return(new PayloadWriterTestDescriptor <ODataServiceDocument>( this.Settings, new [] { serviceDocument }, tc => { if (testCase.ExpectedException != null) { return new WriterTestExpectedResults(this.Settings.ExpectedResultSettings) { ExpectedException2 = testCase.ExpectedException }; } else { return new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings) { Xml = testCase.Xml, FragmentExtractor = result => result .Element(TestAtomConstants.AtomPublishingXNamespace + TestAtomConstants.AtomPublishingWorkspaceElementName) .Element(TestAtomConstants.AtomPublishingXNamespace + TestAtomConstants.AtomPublishingCollectionElementName) .Element(TestAtomConstants.AtomPublishingXNamespace + TestAtomConstants.AtomPublishingCategoriesElementName) }; } })); }); this.CombinatorialEngineProvider.RunCombinations( testDescriptors, this.WriterTestConfigurationProvider.AtomFormatConfigurations.Where(tc => !tc.IsRequest), (testDescriptor, testConfiguration) => { testConfiguration = testConfiguration.Clone(); testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); TestWriterUtils.WriteAndVerifyTopLevelContent( testDescriptor, testConfiguration, (messageWriter) => messageWriter.WriteServiceDocument(testDescriptor.PayloadItems.Single()), this.Assert, baselineLogger: this.Logger); }); }
private static ODataServiceDocument CreateWorkspace(bool createMetadataFirst, string workspaceName = null, IEnumerable <CollectionInfo> incomingCollections = null, IEnumerable <SingletonInfo> incomingSingletons = null) { ODataServiceDocument serviceDocument = ObjectModelUtils.CreateDefaultWorkspace(); if (createMetadataFirst) { serviceDocument.SetAnnotation(new AtomWorkspaceMetadata()); } if (workspaceName != null) { AtomWorkspaceMetadata metadata = serviceDocument.Atom(); metadata.Title = new AtomTextConstruct { Text = workspaceName }; } if (incomingCollections != null) { var collections = new List <ODataEntitySetInfo>(); foreach (var collectionInfo in incomingCollections) { var collection = new ODataEntitySetInfo() { Url = new Uri(collectionInfo.Url, UriKind.RelativeOrAbsolute), Name = collectionInfo.Name, Title = collectionInfo.TitleAnnotation }; if (createMetadataFirst) { collection.SetAnnotation(new AtomResourceCollectionMetadata()); } if (collectionInfo.TitleAnnotation != null) { AtomResourceCollectionMetadata metadata = collection.Atom(); metadata.Title = new AtomTextConstruct { Text = collectionInfo.TitleAnnotation }; } collections.Add(collection); } serviceDocument.EntitySets = collections; } if (incomingSingletons != null) { var singletons = new List <ODataSingletonInfo>(); foreach (var singletonInfo in incomingSingletons) { var singleton = new ODataSingletonInfo() { Url = new Uri(singletonInfo.Url, UriKind.RelativeOrAbsolute), Name = singletonInfo.Name, Title = singletonInfo.TitleAnnotation }; if (createMetadataFirst) { singleton.SetAnnotation(new AtomResourceCollectionMetadata()); } singletons.Add(singleton); } serviceDocument.Singletons = singletons; } return(serviceDocument); }