/// <summary> /// Verifies the default stream. /// </summary> /// <param name="dataContext">The data context.</param> /// <param name="queryEntityValue">The query entity value.</param> /// <param name="ed">The entity descriptor.</param> /// <param name="continuation">The stream descriptor continuation.</param> private void VerifyDefaultStream(DataServiceContext dataContext, QueryStructuralValue queryEntityValue, EntityDescriptor ed, IAsyncContinuation continuation) { var expectedStreamValue = queryEntityValue.GetDefaultStreamValue(); this.VerifyStreamDescriptorValues(expectedStreamValue, null, null, ed.StreamETag, ed.EditStreamUri, ed.ReadStreamUri); var expectedReadStreamUri = GetExpectedReadStreamUri(expectedStreamValue); Uri readStreamUri = dataContext.GetReadStreamUri(ed.Entity); this.Assert.AreEqual(expectedReadStreamUri, readStreamUri, "Read stream uri did not match for default stream"); dataContext.GetReadStream( continuation, this.isAsynchronous, ed.Entity, null, new DataServiceRequestArgs() { }, response => { UpdateStreamValueFromHeaders(expectedStreamValue, response); this.VerifyStreamDescriptorValues(expectedStreamValue, null, null, ed.StreamETag, ed.EditStreamUri, ed.ReadStreamUri); // skip this verification when using payload driven verification since we don't have the expected content for streams if (!this.DataProviderSettings.UsePayloadDrivenVerification) { this.Assert.IsTrue(this.VerifyStreams(expectedStreamValue, response), "Failed to compare the default stream"); } continuation.Continue(); }); }
/// <summary> /// Verifies the named streams. /// </summary> /// <param name="dataContext">The data context.</param> /// <param name="queryEntityValue">The query entity value.</param> /// <param name="streamProperty">The stream property.</param> /// <param name="ed">The entity descriptor</param> /// <param name="continuation">The stream descriptor continuation.</param> private void VerifyNamedStreams(DataServiceContext dataContext, QueryStructuralValue queryEntityValue, QueryProperty streamProperty, EntityDescriptor ed, IAsyncContinuation continuation) { var expectedStreamValue = queryEntityValue.GetStreamValue(streamProperty.Name); var streamDescriptor = ed.StreamDescriptors.SingleOrDefault(s => s.StreamLink.Name == streamProperty.Name); this.Assert.IsNotNull(streamDescriptor, "Entity missing stream descriptor for stream '{0}'", streamProperty.Name); this.VerifyStreamLink(expectedStreamValue, streamDescriptor.StreamLink); var expectedReadStreamUri = GetExpectedReadStreamUri(expectedStreamValue); Uri readStreamUri = dataContext.GetReadStreamUri(ed.Entity, streamProperty.Name); this.Assert.AreEqual(expectedReadStreamUri, readStreamUri, "Read stream uri did not match for stream '{0}'", streamProperty.Name); dataContext.GetReadStream( continuation, this.isAsynchronous, ed.Entity, streamProperty.Name, new DataServiceRequestArgs() { }, response => { UpdateStreamValueFromHeaders(expectedStreamValue, response); this.VerifyStreamLink(expectedStreamValue, streamDescriptor.StreamLink); // skip this verification when using payload driven verification since we don't have the expected content for streams if (!this.DataProviderSettings.UsePayloadDrivenVerification) { this.Assert.IsTrue(this.VerifyStreams(expectedStreamValue, response), "Failed to compare value of stream '{0}'", streamProperty.Name); } continuation.Continue(); }); }
/// <summary> /// Extension method to perform sync/async version of DataServiceContext.GetReadStream dynamically /// </summary> /// <param name="context">The context to call get read stream on</param> /// <param name="continuation">The asynchronous continuation</param> /// <param name="async">A value indicating whether or not to use async API</param> /// <param name="entity">The entity to get the read stream for</param> /// <param name="streamName">The name of the stream or null to indicate the default stream</param> /// <param name="args">The args to the request</param> /// <param name="onCompletion">A callback for when the call completes</param> public static void GetReadStream(this DataServiceContext context, IAsyncContinuation continuation, bool async, object entity, string streamName, DataServiceRequestArgs args, Action <DataServiceStreamResponse> onCompletion) { ExceptionUtilities.CheckArgumentNotNull(context, "context"); if (streamName == null) { AsyncHelpers.InvokeSyncOrAsyncMethodCall <DataServiceStreamResponse>(continuation, async, () => context.GetReadStream(entity, args), c => context.BeginGetReadStream(entity, args, c, null), r => context.EndGetReadStream(r), onCompletion); } else { AsyncHelpers.InvokeSyncOrAsyncMethodCall <DataServiceStreamResponse>(continuation, async, () => context.GetReadStream(entity, streamName, args), c => context.BeginGetReadStream(entity, streamName, args, c, null), r => context.EndGetReadStream(r), onCompletion); } }
/// <summary> /// Extension method to perform sync/async version of DataServiceContext.GetReadStream dynamically /// </summary> /// <param name="context">The context to call get read stream on</param> /// <param name="continuation">The asynchronous continuation</param> /// <param name="async">A value indicating whether or not to use async API</param> /// <param name="entity">The entity to get the read stream for</param> /// <param name="streamName">The name of the stream or null to indicate the default stream</param> /// <param name="args">The args to the request</param> /// <param name="onCompletion">A callback for when the call completes</param> public static void GetReadStream(this DataServiceContext context, IAsyncContinuation continuation, bool async, object entity, string streamName, DataServiceRequestArgs args, Action <DataServiceStreamResponse> onCompletion) { ExceptionUtilities.CheckArgumentNotNull(context, "context"); if (streamName == null) { AsyncHelpers.InvokeSyncOrAsyncMethodCall <DataServiceStreamResponse>(continuation, async, () => context.GetReadStream(entity, args), c => context.BeginGetReadStream(entity, args, c, null), r => context.EndGetReadStream(r), onCompletion); } else { #if WINDOWS_PHONE throw new TaupoNotSupportedException("Named streams are not supported on Windows Phone yet"); #else AsyncHelpers.InvokeSyncOrAsyncMethodCall <DataServiceStreamResponse>(continuation, async, () => context.GetReadStream(entity, streamName, args), c => context.BeginGetReadStream(entity, streamName, args, c, null), r => context.EndGetReadStream(r), onCompletion); #endif } }
public void VerifyMissingLinkQueryScenario() { // Make sure the query scenarios fail, when self/edit links for named stream is missing TestUtil.RunCombinations( UnitTestsUtil.BooleanValues, UnitTestsUtil.BooleanValues, UnitTestsUtil.BooleanValues, (syncRead, hasSelfLink, hasEditLink) => { using (PlaybackService.OverridingPlayback.Restore()) { DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4); context.EnableAtom = true; string links = null; string contentType = null; string selfLink = null; string editLink = null; if (hasSelfLink) { selfLink = request.ServiceRoot + "/Customers(1)/SelfLink/Thumbnail"; links += GetNamedStreamSelfLink(selfLink); } if (hasEditLink) { editLink = request.ServiceRoot + "/Customers(1)/EditLink/Thumbnail"; contentType = MediaContentType; links += GetNamedStreamEditLink(editLink, contentType); } string payload = AtomParserTests.AnyEntry( id: Id, selfLink: request.ServiceRoot.AbsoluteUri + "/selfLink/Customers(1)", editLink: request.ServiceRoot.AbsoluteUri + "/editLink/Customers(1)", properties: Properties, links: links); PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload); Customer c = context.Execute<Customer>(new Uri("/Customers(1)", UriKind.Relative)).Single(); PlaybackService.OverridingPlayback.Value = null; EntityDescriptor ed = context.Entities.Single(); StreamDescriptor ns = null; bool expectException = false; if (!hasEditLink && !hasSelfLink) { expectException = true; Assert.AreEqual(0, ed.StreamDescriptors.Count, "No named streams should be present"); } else { ns = ed.StreamDescriptors.Single(); Assert.AreEqual(ns.StreamLink.SelfLink, selfLink, "self link must match"); Assert.AreEqual(ns.StreamLink.EditLink, editLink, "edit link must match"); Assert.AreEqual(ns.StreamLink.ContentType, contentType, "content type must match"); Assert.AreEqual(context.GetReadStreamUri(c, ns.StreamLink.Name).AbsoluteUri, ns.StreamLink.SelfLink != null ? ns.StreamLink.SelfLink.AbsoluteUri : ns.StreamLink.EditLink.AbsoluteUri, "Make sure that context.GetReadStreamUri returns the self link if present, otherwise returns edit link"); } try { if (syncRead) { context.GetReadStream(c, "Thumbnail", new DataServiceRequestArgs() { ContentType = "image/jpeg" }); } else { IAsyncResult result = context.BeginGetReadStream(c, "Thumbnail", new DataServiceRequestArgs() { ContentType = "image/jpeg" }, (r) => { context.EndGetReadStream(r); }, null); if (!result.CompletedSynchronously) { Assert.IsTrue(result.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, AstoriaUnitTests.TestConstants.MaxTestTimeout), false), "BeginExecute timeout"); } } Assert.IsTrue(!expectException, "should reach here when no exception is expected"); Assert.IsTrue(PlaybackService.LastPlayback.Contains("GET " + selfLink ?? editLink), "should use self link if present, otherwise editlink"); } catch (ArgumentException ex) { Assert.IsTrue(expectException, "should get exception when expected"); ArgumentException expectedException = new ArgumentException(DataServicesClientResourceUtil.GetString("Context_EntityDoesNotContainNamedStream", "Thumbnail"), "name"); Assert.AreEqual(expectedException.Message, ex.Message, "Error message did not match"); Assert.AreEqual(0, ed.StreamDescriptors.Count, "No named streams should be present"); } } }); }
public void NamedSteams_VerifyGetReadStreamVersion() { // Verify that GetReadStream for a named stream sends version 3 headers // Populate the context with a single customer instance string payload = AtomParserTests.AnyEntry( id: Id, editLink: request.ServiceRoot.AbsoluteUri + "/editLink/Customers(1)", properties: Properties, links: GetNamedStreamSelfLink(request.ServiceRoot + "/Customers(1)/SelfLink/Thumbnail", contentType: MediaContentType)); TestUtil.RunCombinations(UnitTestsUtil.BooleanValues, (syncRead) => { using (PlaybackService.OverridingPlayback.Restore()) { PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload); DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4); context.EnableAtom = true; DataServiceQuery<Customer> q = (DataServiceQuery<Customer>)context.CreateQuery<Customer>("Customers").Where(c1 => c1.ID == 1); Customer c = ((IEnumerable<Customer>)DataServiceContextTestUtil.ExecuteQuery(context, q, QueryMode.AsyncExecute)).Single(); if (syncRead) { context.GetReadStream(c, "Thumbnail", new DataServiceRequestArgs() { ContentType = "img/jpeg" }); } else { IAsyncResult result = context.BeginGetReadStream(c, "Thumbnail", new DataServiceRequestArgs() { ContentType = "image/jpeg" }, (r) => { context.EndGetReadStream(r); }, null); if (!result.CompletedSynchronously) { Assert.IsTrue(result.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, AstoriaUnitTests.TestConstants.MaxTestTimeout), false), "BeginExecute timeout"); } } VerifyRequestWasVersion3(PlaybackService.LastPlayback); } }); }
public void NamedStreams_TestPublicAPIParameters() { // Test public api parameters DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4); #region GetReadStreamUri API DataServiceContextTestUtil.CheckArgumentNull("entity", () => { context.GetReadStreamUri(null, "Thumbnail"); }); DataServiceContextTestUtil.CheckArgumentNull("name", () => { context.GetReadStreamUri(new Customer(), null); }); DataServiceContextTestUtil.CheckArgumentEmpty("name", () => { context.GetReadStreamUri(new Customer(), ""); }); DataServiceContextTestUtil.VerifyInvalidRequest(typeof(InvalidOperationException), "Context_EntityNotContained", () => { context.GetReadStreamUri(new Customer(), "Thumbnail"); }); #endregion GetReadStreamUri API #region BeginGetReadStream DataServiceContextTestUtil.CheckArgumentNull("entity", () => { context.BeginGetReadStream(null, "Thumbnail", new DataServiceRequestArgs(), null, null); }); DataServiceContextTestUtil.CheckArgumentNull("name", () => { context.BeginGetReadStream(new Customer(), null, new DataServiceRequestArgs(), null, null); }); DataServiceContextTestUtil.CheckArgumentEmpty("name", () => { context.BeginGetReadStream(new Customer(), "", new DataServiceRequestArgs(), null, null); }); DataServiceContextTestUtil.CheckArgumentNull("args", () => { context.BeginGetReadStream(new Customer(), "Thumbnail", null, null, null); }); DataServiceContextTestUtil.VerifyInvalidRequest(typeof(InvalidOperationException), "Context_EntityNotContained", () => { context.BeginGetReadStream(new Customer(), "Thumbnail", new DataServiceRequestArgs(), null, null); }); #endregion BeginGetReadStream #region GetReadStream DataServiceContextTestUtil.CheckArgumentNull("entity", () => { context.GetReadStream(null, "Thumbnail", new DataServiceRequestArgs()); }); DataServiceContextTestUtil.CheckArgumentNull("name", () => { context.GetReadStream(new Customer(), null, new DataServiceRequestArgs()); }); DataServiceContextTestUtil.CheckArgumentEmpty("name", () => { context.GetReadStream(new Customer(), "", new DataServiceRequestArgs()); }); DataServiceContextTestUtil.CheckArgumentNull("args", () => { context.GetReadStream(new Customer(), "Thumbnail", null); }); DataServiceContextTestUtil.VerifyInvalidRequest(typeof(InvalidOperationException), "Context_EntityNotContained", () => { context.GetReadStream(new Customer(), "Thumbnail", new DataServiceRequestArgs()); }); #endregion GetReadStream #region SetSaveStream DataServiceContextTestUtil.CheckArgumentNull("entity", () => { context.SetSaveStream(null, "Thumbnail", new MemoryStream(), true, new DataServiceRequestArgs()); }); DataServiceContextTestUtil.CheckArgumentNull("name", () => { context.SetSaveStream(new Customer(), null, new MemoryStream(), true, new DataServiceRequestArgs()); }); DataServiceContextTestUtil.CheckArgumentEmpty("name", () => { context.SetSaveStream(new Customer(), "", new MemoryStream(), true, new DataServiceRequestArgs()); }); DataServiceContextTestUtil.CheckArgumentNull("stream", () => { context.SetSaveStream(new Customer(), "Thumbnail", null, true, new DataServiceRequestArgs()); }); DataServiceContextTestUtil.CheckArgumentNull("args", () => { context.SetSaveStream(new Customer(), "Thumbnail", new MemoryStream(), true, (DataServiceRequestArgs)null); }); DataServiceContextTestUtil.CheckArgumentNull("contentType", () => { context.SetSaveStream(new Customer(), "Thumbnail", new MemoryStream(), true, (string)null); }); DataServiceContextTestUtil.CheckArgumentEmpty("contentType", () => { context.SetSaveStream(new Customer(), "Thumbnail", new MemoryStream(), true, string.Empty); }); DataServiceContextTestUtil.VerifyInvalidRequest(typeof(InvalidOperationException), "Context_EntityNotContained", () => { context.SetSaveStream(new Customer(), "Thumbnail", new MemoryStream(), true, "image/jpeg"); }); DataServiceContextTestUtil.CheckArgumentException("Context_ContentTypeRequiredForNamedStream", () => { context.SetSaveStream(new Customer(), "Thumbnail", new MemoryStream(), true, new DataServiceRequestArgs()); }, "args"); DataServiceContextTestUtil.CheckArgumentException("Context_ContentTypeRequiredForNamedStream", () => { context.SetSaveStream(new Customer(), "Thumbnail", new MemoryStream(), true, new DataServiceRequestArgs() { ContentType = string.Empty }); }, "args"); DataServiceContextTestUtil.VerifyInvalidRequest(typeof(InvalidOperationException), "Context_EntityNotContained", () => { context.SetSaveStream(new Customer(), "Thumbnail", new MemoryStream(), true, new DataServiceRequestArgs() { ContentType = "image/jpg" }); }); #endregion SetSaveStream }
public void Collection_Blobs() { DSPMetadata metadata = CreateMetadataForXFeatureEntity(true); DSPServiceDefinition service = new DSPServiceDefinition() { Metadata = metadata, Writable = true, SupportMediaResource = true, MediaResourceStorage = new DSPMediaResourceStorage() }; byte[] clientBlob = new byte[] { 0xcc, 0x10, 0x00, 0xff }; DSPContext data = new DSPContext(); service.CreateDataSource = (m) => { return data; }; using (TestWebRequest request = service.CreateForInProcessWcf()) using (DSPResourceWithCollectionProperty.CollectionPropertiesResettable.Restore()) { DSPResourceWithCollectionProperty.CollectionPropertiesResettable.Value = true; request.StartService(); XFeatureTestsMLE clientMle = new XFeatureTestsMLE() { ID = 1, Description = "Entity 1", Strings = new List<string>(new string[] { "string 1", "string 2", string.Empty }), Structs = new List<XFeatureTestsComplexType>(new XFeatureTestsComplexType[] { new XFeatureTestsComplexType() { Text = "text 1" }, new XFeatureTestsComplexType() { Text = "text 2" }}) }; DataServiceContext ctx = new DataServiceContext(new Uri(request.BaseUri), ODataProtocolVersion.V4); ctx.EnableAtom = true; ctx.Format.UseAtom(); ctx.AddObject("Entities", clientMle); ctx.SetSaveStream(clientMle, new MemoryStream(clientBlob), true, "application/octet-stream", clientMle.ID.ToString()); ctx.SaveChanges(); VerifyMLEs(service, clientMle, clientBlob); // Read stream and verify stream contents using (Stream serverStream = ctx.GetReadStream(clientMle).Stream) { VerifyStream(clientBlob, serverStream); } // modify MLE and the corresponding stream clientMle.Structs.Add(new XFeatureTestsComplexType() { Text = "text 3" }); clientMle.Strings.RemoveAt(0); clientBlob[0] ^= 0xff; ctx.UpdateObject(clientMle); ctx.SetSaveStream(clientMle, new MemoryStream(clientBlob), true, "application/octet-stream", clientMle.ID.ToString()); ctx.SaveChanges(); VerifyMLEs(service, clientMle, clientBlob); // delete MLE ctx.DeleteObject(clientMle); ctx.SaveChanges(); Assert.IsNull((DSPResource)service.CurrentDataSource.GetResourceSetEntities("Entities"). FirstOrDefault(e => (int)(((DSPResource)e).GetValue("ID")) == (int)clientMle.GetType().GetProperty("ID").GetValue(clientMle, null)), "MLE has not been deleted."); Assert.AreEqual(0, service.MediaResourceStorage.Content.Count(), "The stream on the server has not been deleted."); }; }