/// <summary> /// Visits the children of the given payload element and replaces it with a copy if any child changes /// </summary> /// <param name="payloadElement">The payload element to potentially replace</param> /// <returns>The original element or a copy to replace it with</returns> public virtual ODataPayloadElement Visit(BatchResponsePayload payloadElement) { ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement"); return(this.VisitBatch <BatchResponsePayload, BatchResponseChangeset, HttpResponseData>( payloadElement, this.VisitResponseOperation)); }
/// <summary> /// Puts payload in a batch response. /// </summary> /// <typeparam name="T">T must be a PayloadTestDescriptor.</typeparam> /// <param name="originalPayload">Payload to be inserted into batch.</param> /// <param name="statusCode">Status code associated with payload</param> /// <param name="random">Use of random makes this method non deterministic</param> /// <param name="requestManager">Used to construct the response payload.</param> /// <param name="inChangeset">Specifies whether this is in a changeset or an operation.</param> /// <param name="operationsBefore">Number of operations/changesets to go before payload.</param> /// <param name="operationsAfter">Number of operations/changesets to go after payload.</param> /// <param name="version">Maximum version for extra generated payloads</param> /// <returns>Test descriptor for the new BatchResponsePayload.</returns> public static T InBatchResponse <T>( this T originalPayload, int statusCode, IRandomNumberGenerator random, IODataRequestManager requestManager, bool inChangeset = false, int operationsBefore = 0, int operationsAfter = 0, ODataVersion version = ODataVersion.V4 ) where T : PayloadTestDescriptor { ExceptionUtilities.CheckArgumentNotNull(originalPayload, "originalPayload"); ExceptionUtilities.CheckArgumentNotNull(statusCode, "statusCode"); ExceptionUtilities.CheckArgumentNotNull(random, "random"); ExceptionUtilities.CheckArgumentNotNull(requestManager, "requestManager"); var payload = (T)originalPayload.Clone(); var headers = new Dictionary <string, string> { { "ResponseHeader", "ResponseValue" } }; var baseUri = new ODataUri(ODataUriBuilder.Root(new Uri("http://www.odata.org/service.svc"))); IMimePart[] extraoperations = BatchUtils.ExtraResponseChangesets(random, (EdmModel)payload.PayloadEdmModel, version); // add extraChangesets methods extraoperations.Concat(BatchUtils.ExtraResponseOperations(random, (EdmModel)payload.PayloadEdmModel, version)); //Work out the operations and changesets to go before the payload var parts = Enumerable.Range(0, operationsBefore).Select(i => random.ChooseFrom(extraoperations)); if (!inChangeset) { extraoperations.ConcatSingle(payload.InResponseOperation(statusCode, requestManager)); } else { extraoperations.ConcatSingle(payload.InResponseChangeset(statusCode, random, requestManager, random.NextFromRange(0, 2), random.NextFromRange(0, 2), version)); } parts.Concat(Enumerable.Range(0, operationsAfter).Select(i => random.ChooseFrom(extraoperations))); var batchResponse = new BatchResponsePayload(); foreach (var part in parts) { HttpResponseData operation = part as HttpResponseData; if (operation != null) { batchResponse.Add(operation.AsBatchFragment()); } BatchResponseChangeset changeset = part as BatchResponseChangeset; if (changeset != null) { batchResponse.Add(changeset); } } //payload.PayloadEdmModel.Fixup(); payload.PayloadElement = batchResponse; return(payload); }
/// <summary> /// Serializes a batch response payload /// </summary> /// <param name="payload">The batch payload</param> /// <param name="boundary">The batch payload boundary</param> /// <param name="encodingName">Optional name of an encoding to use if it is relevant to the current format. May be null if no character-set information was known.</param> /// <returns>The serialized batch payload</returns> public byte[] SerializeBatchPayload(BatchResponsePayload payload, string boundary, string encodingName) { ExceptionUtilities.CheckArgumentNotNull(payload, "payload"); var encoding = HttpUtilities.GetEncodingOrDefault(encodingName); return(WriteBatchParts <HttpResponseData, BatchResponseChangeset>(payload, boundary, encoding, this.WriteResponse)); }
/// <summary> /// Visits the entity set and removes the title and entity set annotations /// </summary> /// <param name="payloadElement">The payload element to visit</param> public override void Visit(BatchResponsePayload payloadElement) { ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement"); var annotation = payloadElement.Annotations.Where(a => a is BatchBoundaryAnnotation).SingleOrDefault(); payloadElement.Annotations.Remove(annotation); base.Visit(payloadElement); }
/// <summary> /// Visits the payload element. /// </summary> /// <param name="payloadElement">The payload element to visit.</param> public override void Visit(BatchResponsePayload payloadElement) { var observed = this.GetNextObservedElement <BatchResponsePayload>(); this.WrapAccept( payloadElement, observed, (e) => this.VisitBatch <HttpResponseData, BatchResponseChangeset, BatchResponsePayload>((BatchResponsePayload)e, this.VisitResponseOperation)); }
/// <summary> /// Visits the entity set and removes the title and entity set annotations /// </summary> /// <param name="payloadElement">The payload element to visit</param> public override void Visit(BatchResponsePayload payloadElement) { ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement"); var annotation = payloadElement.Annotations.Where(a => a is BatchBoundaryAnnotation).SingleOrDefault(); payloadElement.Annotations.Remove(annotation); base.Visit(payloadElement); }
/// <summary> /// Visits a payload element whose root is a BatchResponsePayload. /// </summary> /// <param name="expected">The root node of payload element being visited.</param> public void Visit(BatchResponsePayload expected) { ExceptionUtilities.CheckArgumentNotNull(expected, "expected"); var observed = this.GetNextObservedElement <BatchResponsePayload>(); using (this.Assert.WithMessage("Batch response did not match expectation")) { this.batchComparer.CompareBatchPayload(expected, observed); } }
/// <summary> /// Deserializes the given response's binary payload into a batch payload /// </summary> /// <param name="requestPayload">The batch request payload that corresponds to the request</param> /// <param name="response">The response to deserialize</param> /// <returns>The deserialized batch response payload</returns> public BatchResponsePayload DeserializeBatchResponse(BatchRequestPayload requestPayload, HttpResponseData response) { var encoding = HttpUtilities.DefaultEncoding; string charset; if (response.TryGetMimeCharset(out charset)) { encoding = Encoding.GetEncoding(charset); } MultipartMimeData <MimePartData <byte[]> > split; ExceptionUtilities.Assert(TrySplitMimePart(response, encoding, out split), "Failed to split batch response body"); var batchResponse = new BatchResponsePayload(); var requestPartQueue = new Queue <IMimePart>(requestPayload.Parts); foreach (var subPart in split.ToList()) { MultipartMimeData <MimePartData <byte[]> > splitChangeset; if (TrySplitMimePart(subPart, encoding, out splitChangeset)) { ExceptionUtilities.Assert(requestPartQueue.Count > 0, "Response changeset did not line up anything in request"); var requestChangeset = requestPartQueue.Dequeue() as BatchRequestChangeset; ExceptionUtilities.CheckObjectNotNull(requestChangeset, "Response changeset did not line up with a request changeset"); var requestsQueue = new Queue <IHttpRequest>(requestChangeset.Operations); var changeset = new BatchResponseChangeset(); changeset.Headers.AddRange(splitChangeset.Headers); foreach (var changesetPart in splitChangeset) { ExceptionUtilities.Assert(requestsQueue.Count > 0, "Response did not line up anything in request changeset"); changeset.Add(this.BuildResponseFromPart(changesetPart, requestsQueue.Dequeue(), encoding)); } batchResponse.Add(changeset); } else { ExceptionUtilities.Assert(requestPartQueue.Count > 0, "Response did not line up anything in request"); var requestOperation = requestPartQueue.Dequeue() as MimePartData <IHttpRequest>; ExceptionUtilities.CheckObjectNotNull(requestOperation, "Response operation did not line up with request operation"); batchResponse.Add(this.BuildResponseFromPart(subPart, requestOperation.Body, encoding)); } } return(batchResponse); }
/// <summary> /// Generates a dummy request based on the supplied response and model /// </summary> /// <param name="batchResponse">Response to build the request for.</param> /// <param name="model">Model to get the types from.</param> /// <returns></returns> public BatchRequestPayload GenerateRequestPayload(BatchResponsePayload batchResponse, EntityModelSchema model) { ExceptionUtilities.CheckArgumentNotNull(batchResponse, "batchResponse"); ExceptionUtilities.CheckArgumentNotNull(model, "model"); var batchRequest = new BatchRequestPayload(); foreach (var part in batchResponse.Parts) { var operation = part as MimePartData<HttpResponseData>; if (operation != null) { batchRequest.Add(this.GenerateRequestOperation(operation.Body as ODataResponse, model).AsBatchFragment()); } BatchResponseChangeset changeset = part as BatchResponseChangeset; if (changeset != null) { batchRequest.Add(this.GenerateRequestChangeset(changeset, model)); } } return batchRequest; }
/// <summary> /// Generates a dummy request based on the supplied response and model /// </summary> /// <param name="batchResponse">Response to build the request for.</param> /// <param name="model">Model to get the types from.</param> /// <returns></returns> public BatchRequestPayload GenerateRequestPayload(BatchResponsePayload batchResponse, EntityModelSchema model) { ExceptionUtilities.CheckArgumentNotNull(batchResponse, "batchResponse"); ExceptionUtilities.CheckArgumentNotNull(model, "model"); var batchRequest = new BatchRequestPayload(); foreach (var part in batchResponse.Parts) { var operation = part as MimePartData <HttpResponseData>; if (operation != null) { batchRequest.Add(this.GenerateRequestOperation(operation.Body as ODataResponse, model).AsBatchFragment()); } BatchResponseChangeset changeset = part as BatchResponseChangeset; if (changeset != null) { batchRequest.Add(this.GenerateRequestChangeset(changeset, model)); } } return(batchRequest); }
/// <summary> /// Visits a payload element whose root is a BatchResponsePayload. /// </summary> /// <param name="payloadElement">The root node of payload element being visited.</param> public void Visit(BatchResponsePayload payloadElement) { throw new TaupoNotSupportedException("Batch payloads are not supported"); }
/// <summary> /// Visits a BatchResponsePayload /// </summary> /// <param name="payloadElement">payload Element</param> public override void Visit(BatchResponsePayload payloadElement) { throw new TaupoNotSupportedException("Batch payloads are not supported"); }
/// <summary> /// Deserializes the given response's binary payload into a batch payload /// </summary> /// <param name="requestPayload">The batch request payload that corresponds to the request</param> /// <param name="response">The response to deserialize</param> /// <returns>The deserialized batch response payload</returns> public BatchResponsePayload DeserializeBatchResponse(BatchRequestPayload requestPayload, HttpResponseData response) { var encoding = HttpUtilities.DefaultEncoding; string charset; if (response.TryGetMimeCharset(out charset)) { encoding = Encoding.GetEncoding(charset); } MultipartMimeData<MimePartData<byte[]>> split; ExceptionUtilities.Assert(TrySplitMimePart(response, encoding, out split), "Failed to split batch response body"); var batchResponse = new BatchResponsePayload(); var requestPartQueue = new Queue<IMimePart>(requestPayload.Parts); foreach (var subPart in split.ToList()) { MultipartMimeData<MimePartData<byte[]>> splitChangeset; if (TrySplitMimePart(subPart, encoding, out splitChangeset)) { ExceptionUtilities.Assert(requestPartQueue.Count > 0, "Response changeset did not line up anything in request"); var requestChangeset = requestPartQueue.Dequeue() as BatchRequestChangeset; ExceptionUtilities.CheckObjectNotNull(requestChangeset, "Response changeset did not line up with a request changeset"); var requestsQueue = new Queue<IHttpRequest>(requestChangeset.Operations); var changeset = new BatchResponseChangeset(); changeset.Headers.AddRange(splitChangeset.Headers); foreach (var changesetPart in splitChangeset) { ExceptionUtilities.Assert(requestsQueue.Count > 0, "Response did not line up anything in request changeset"); changeset.Add(this.BuildResponseFromPart(changesetPart, requestsQueue.Dequeue(), encoding)); } batchResponse.Add(changeset); } else { ExceptionUtilities.Assert(requestPartQueue.Count > 0, "Response did not line up anything in request"); var requestOperation = requestPartQueue.Dequeue() as MimePartData<IHttpRequest>; ExceptionUtilities.CheckObjectNotNull(requestOperation, "Response operation did not line up with request operation"); batchResponse.Add(this.BuildResponseFromPart(subPart, requestOperation.Body, encoding)); } } return batchResponse; }
/// <summary> /// Visits the payload element /// </summary> /// <param name="payloadElement">The payload element to visit</param> public virtual void Visit(BatchResponsePayload payloadElement) { this.VisitBatch <HttpResponseData, BatchResponseChangeset, BatchResponsePayload>(payloadElement, this.VisitResponseOperation); }