private Action <WriterTestConfiguration> WriteServiceDocument(Func <XElement, XElement> fragmentExtractor, string expectedXml) { var sampleWorkspace = ObjectModelUtils.CreateDefaultWorkspace(); return((testConfiguration) => { if (testConfiguration.IsRequest) { return; } ODataMessageWriterSettings actualSettings = testConfiguration.MessageWriterSettings; if (actualSettings.PayloadBaseUri == null) { actualSettings = actualSettings.Clone(); actualSettings.PayloadBaseUri = new Uri("http://odata.org"); } TestWriterUtils.WriteAndVerifyTopLevelContent( new PayloadWriterTestDescriptor <ODataServiceDocument>(this.Settings, sampleWorkspace, expectedXml, null, fragmentExtractor, null, /*disableXmlNamespaceNormalization*/ true), testConfiguration, (messageWriter) => messageWriter.WriteServiceDocument(sampleWorkspace), this.Assert, actualSettings, 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 (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 }; 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 }; singletons.Add(singleton); } serviceDocument.Singletons = singletons; } return(serviceDocument); }
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); }
public void ServiceDocumentErrorTests() { IEdmModel model = this.CreateMetadata(new[] { new CollectionInfo() { Url = "EntitySet1" }, new CollectionInfo() { Url = "EntitySet2" } }); var testCases = new[] { new { // relative uri without any base Uri Workspace = new Func <ODataServiceDocument>(() => { var defaultWorkspace = ObjectModelUtils.CreateDefaultWorkspace(); defaultWorkspace.EntitySets = new ODataEntitySetInfo[] { new ODataEntitySetInfo() { Url = new Uri("SomeUri", UriKind.Relative) } }; return(defaultWorkspace); })(), MessageWriterSettings = (ODataMessageWriterSettings)null, ExpectedException = ODataExpectedExceptions.ODataException("ODataWriter_RelativeUriUsedWithoutBaseUriSpecified", "SomeUri"), Model = (IEdmModel)null, OnlyForFormat = ODataFormat.Atom }, new { // empty relative uri without any base Uri Workspace = new Func <ODataServiceDocument>(() => { var defaultWorkspace = ObjectModelUtils.CreateDefaultWorkspace(); defaultWorkspace.EntitySets = new ODataEntitySetInfo[] { new ODataEntitySetInfo() { Url = new Uri(string.Empty, UriKind.Relative) } }; return(defaultWorkspace); })(), MessageWriterSettings = (ODataMessageWriterSettings)null, ExpectedException = ODataExpectedExceptions.ODataException("ODataWriter_RelativeUriUsedWithoutBaseUriSpecified", string.Empty), Model = (IEdmModel)null, OnlyForFormat = ODataFormat.Atom }, new { // empty collection name Workspace = new Func <ODataServiceDocument>(() => { var defaultWorkspace = ObjectModelUtils.CreateDefaultWorkspace(); defaultWorkspace.EntitySets = new ODataEntitySetInfo[] { new ODataEntitySetInfo() { Url = null } }; return(defaultWorkspace); })(), MessageWriterSettings = settingsWithBaseUri, ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_ResourceMustSpecifyUrl"), Model = (IEdmModel)null, OnlyForFormat = (ODataFormat)null }, new { // null collection Workspace = new Func <ODataServiceDocument>(() => { var defaultWorkspace = ObjectModelUtils.CreateDefaultWorkspace(); defaultWorkspace.EntitySets = new ODataEntitySetInfo[] { null }; return(defaultWorkspace); })(), MessageWriterSettings = settingsWithBaseUri, ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_WorkspaceResourceMustNotContainNullItem"), Model = (IEdmModel)null, OnlyForFormat = (ODataFormat)null }, new { // no serviceDocument Workspace = (ODataServiceDocument)null, MessageWriterSettings = settingsWithBaseUri, ExpectedException = new ExpectedException(typeof(ArgumentNullException)), Model = (IEdmModel)null, OnlyForFormat = (ODataFormat)null }, }; var testDescriptors = testCases.Select(tc => new PayloadWriterTestDescriptor <WorkspaceWithSettings>( this.Settings, new WorkspaceWithSettings() { ServiceDocument = tc.Workspace, WriterSettings = tc.MessageWriterSettings }, CreateErrorResultCallback(tc.ExpectedException, tc.OnlyForFormat, this.Settings.ExpectedResultSettings)) { Model = tc.Model, }); this.CombinatorialEngineProvider.RunCombinations( testDescriptors, this.WriterTestConfigurationProvider.ExplicitFormatConfigurationsWithIndent, (testDescriptor, testConfig) => { testConfig = testConfig.Clone(); testConfig.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri); WorkspaceWithSettings docWithSettings = testDescriptor.PayloadItems.Single(); if (docWithSettings.WriterSettings != null) { docWithSettings.WriterSettings.DisableMessageStreamDisposal = testConfig.MessageWriterSettings.DisableMessageStreamDisposal; docWithSettings.WriterSettings.SetServiceDocumentUri(ServiceDocumentUri); } TestWriterUtils.WriteAndVerifyTopLevelContent( testDescriptor, testConfig, (messageWriter) => messageWriter.WriteServiceDocument(docWithSettings.ServiceDocument), this.Assert, docWithSettings.WriterSettings, baselineLogger: this.Logger); }); }