/// <summary> /// Creates a batch payload with the specified number of changesets and the specified number of operations in each changeset. /// </summary> /// <param name="requestManager">Used for building the requests/responses.</param> /// <param name="changeSetCount">The number of changesets to create in the batch payload.</param> /// <param name="changeSetSizes">The size of each changeset.</param> /// <param name="forRequest">true if creating a batch request payload; otherwise false.</param> /// <param name="batchBoundary">The batch boundary to use; or null to use an auto-generated one.</param> /// <returns>A <see cref="PayloadTestDescriptor"/> for the batch payload.</returns> public static PayloadTestDescriptor CreateDefaultQueryBatch( IODataRequestManager requestManager, int queryCount, bool forRequest, string batchBoundary = null) { Debug.Assert(queryCount >= 0, "batchSize >= 0"); var emptyPayload = new PayloadTestDescriptor() { PayloadEdmModel = CreateEmptyEdmModel() }; var root = ODataUriBuilder.Root(new Uri("http://www.odata.org/service.svc")); IMimePart[] parts = new IMimePart[queryCount]; if (forRequest) { var queryOperation = emptyPayload.InRequestOperation(HttpVerb.Get, new ODataUri(new ODataUriSegment[] { root }), requestManager); for (int i = 0; i < queryCount; ++i) { parts[i] = queryOperation; } ; string requestBoundary = batchBoundary ?? "bb_multiple_request_queries_" + queryCount; return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload(parts) .AddAnnotation(new BatchBoundaryAnnotation(requestBoundary)), }); } // Response operation with no payload and a status code of 200 var emptyPayloadResponse = emptyPayload.InResponseOperation(200, requestManager); for (int i = 0; i < queryCount; ++i) { parts[i] = emptyPayloadResponse; } ; string responseBoundary = batchBoundary ?? "bb_multiple_response_queries_" + queryCount; return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchResponsePayload(parts) .AddAnnotation(new BatchBoundaryAnnotation(responseBoundary)), }); }
/// <summary> /// Creates several PayloadTestDescriptors containing Batch Requests /// </summary> /// <param name="requestManager">Used for building the requests</param> /// <param name="model">The model to use for adding additional types.</param> /// <param name="withTypeNames">Whether or not to use full type names.</param> /// <returns>PayloadTestDescriptors</returns> public static IEnumerable <PayloadTestDescriptor> CreateBatchRequestTestDescriptors( IODataRequestManager requestManager, EdmModel model, bool withTypeNames = false) { ExceptionUtilities.CheckArgumentNotNull(requestManager, "requestManager"); EdmEntityType personType = null; EdmComplexType carType = null; EdmEntitySet personsEntitySet = null; EdmEntityContainer container = model.EntityContainer as EdmEntityContainer; if (model != null) { //TODO: Clone EdmModel //model = model.Clone(); if (container == null) { container = new EdmEntityContainer("TestModel", "DefaultContainer"); model.AddElement(container); } personType = model.FindDeclaredType("TestModel.TFPerson") as EdmEntityType; carType = model.FindDeclaredType("TestModel.TFCar") as EdmComplexType; // Create the metadata types for the entity instance used in the entity set if (carType == null) { carType = new EdmComplexType("TestModel", "TFCar"); model.AddElement(carType); carType.AddStructuralProperty("Make", EdmPrimitiveTypeKind.String, true); carType.AddStructuralProperty("Color", EdmPrimitiveTypeKind.String, true); } if (personType == null) { personType = new EdmEntityType("TestModel", "TFPerson"); model.AddElement(personType); personType.AddKeys(personType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32)); personType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String, true); personType.AddStructuralProperty("Car", carType.ToTypeReference()); container.AddEntitySet("Customers", personType); } personsEntitySet = container.AddEntitySet("People", personType); } ComplexInstance carInstance = PayloadBuilder.ComplexValue(withTypeNames ? "TestModel.TFCar" : null) .Property("Make", PayloadBuilder.PrimitiveValue("Ford")) .Property("Color", PayloadBuilder.PrimitiveValue("Blue")); ComplexProperty carProperty = (ComplexProperty)PayloadBuilder.Property("Car", carInstance) .WithTypeAnnotation(personType); EntityInstance personInstance = PayloadBuilder.Entity(withTypeNames ? "TestModel.TFPerson" : null) .Property("Id", PayloadBuilder.PrimitiveValue(1)) .Property("Name", PayloadBuilder.PrimitiveValue("John Doe")) .Property("Car", carInstance) .WithTypeAnnotation(personType); var carPropertyPayload = new PayloadTestDescriptor() { PayloadElement = carProperty, PayloadEdmModel = model }; var emptyPayload = new PayloadTestDescriptor() { PayloadEdmModel = CreateEmptyEdmModel() }; var personPayload = new PayloadTestDescriptor() { PayloadElement = personInstance, PayloadEdmModel = model }; var root = ODataUriBuilder.Root(new Uri("http://www.odata.org/service.svc")); var entityset = ODataUriBuilder.EntitySet(personsEntitySet); // Get operations var queryOperation1 = emptyPayload.InRequestOperation(HttpVerb.Get, new ODataUri(new ODataUriSegment[] { root }), requestManager); var queryOperation2 = emptyPayload.InRequestOperation(HttpVerb.Get, new ODataUri(new ODataUriSegment[] { root }), requestManager); // Post operation containing a complex property var postOperation = carPropertyPayload.InRequestOperation(HttpVerb.Post, new ODataUri(new ODataUriSegment[] { root, entityset }), requestManager, MimeTypes.ApplicationJsonLight); // Delete operation with no payload var deleteOperation = emptyPayload.InRequestOperation(HttpVerb.Delete, new ODataUri(new ODataUriSegment[] { root, entityset }), requestManager); // Put operation where the payload is an EntityInstance var putOperation = personPayload.InRequestOperation(HttpVerb.Put, new ODataUri(new ODataUriSegment[] { root, entityset }), requestManager); // A changeset containing a delete with no payload and a put var twoOperationsChangeset = BatchUtils.GetRequestChangeset(new IMimePart[] { postOperation, deleteOperation }, requestManager); // A changeset containing a delete with no payload var oneOperationChangeset = BatchUtils.GetRequestChangeset(new IMimePart[] { deleteOperation }, requestManager); // A changeset containing a put, post and delete var threeOperationsChangeset = BatchUtils.GetRequestChangeset(new IMimePart[] { putOperation, postOperation, deleteOperation }, requestManager); // A changeset containing no operations var emptyChangeset = BatchUtils.GetRequestChangeset(new IMimePart[] { }, requestManager); // Empty Batch yield return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload() .AddAnnotation(new BatchBoundaryAnnotation("bb_emptybatch")), PayloadEdmModel = emptyPayload.PayloadEdmModel, SkipTestConfiguration = (tc) => !tc.IsRequest, }); // Single Operation yield return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload(queryOperation1) .AddAnnotation(new BatchBoundaryAnnotation("bb_singleoperation")), PayloadEdmModel = model, SkipTestConfiguration = (tc) => !tc.IsRequest, }); // Multiple Operations yield return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload(queryOperation1, queryOperation2) .AddAnnotation(new BatchBoundaryAnnotation("bb_multipleoperations")), PayloadEdmModel = model, SkipTestConfiguration = (tc) => !tc.IsRequest, }); // Single Changeset yield return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload(twoOperationsChangeset) .AddAnnotation(new BatchBoundaryAnnotation("bb_singlechangeset")), PayloadEdmModel = model, SkipTestConfiguration = (tc) => !tc.IsRequest, }); // Multiple Changesets (different content types) yield return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload(twoOperationsChangeset, oneOperationChangeset, emptyChangeset) .AddAnnotation(new BatchBoundaryAnnotation("bb_multiplechangesets")), PayloadEdmModel = model, SkipTestConfiguration = (tc) => !tc.IsRequest, }); // Operations and changesets yield return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload(twoOperationsChangeset, queryOperation1, oneOperationChangeset) .AddAnnotation(new BatchBoundaryAnnotation("bb_operationsandchangesets_1")), PayloadEdmModel = model, SkipTestConfiguration = (tc) => !tc.IsRequest, }); yield return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload(queryOperation1, oneOperationChangeset, queryOperation2) .AddAnnotation(new BatchBoundaryAnnotation("bb_operationsandchangesets_2")), PayloadEdmModel = model, SkipTestConfiguration = (tc) => !tc.IsRequest, }); yield return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload(queryOperation1, queryOperation2, twoOperationsChangeset, oneOperationChangeset) .AddAnnotation(new BatchBoundaryAnnotation("bb_operationsandchangesets_3")), PayloadEdmModel = model, SkipTestConfiguration = (tc) => !tc.IsRequest, }); yield return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload(queryOperation1, threeOperationsChangeset, queryOperation2, twoOperationsChangeset, queryOperation1, oneOperationChangeset) .AddAnnotation(new BatchBoundaryAnnotation("bb_operationsandchangesets_4")), PayloadEdmModel = model, SkipTestConfiguration = (tc) => !tc.IsRequest, }); yield return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload(queryOperation1, emptyChangeset, queryOperation1, threeOperationsChangeset, queryOperation2, oneOperationChangeset) .AddAnnotation(new BatchBoundaryAnnotation("bb_operationsandchangesets_5")), PayloadEdmModel = model, SkipTestConfiguration = (tc) => !tc.IsRequest, }); }
/// <summary> /// Creates a batch payload with the specified number of changesets and the specified number of operations in each changeset. /// </summary> /// <param name="requestManager">Used for building the requests/responses.</param> /// <param name="changeSetCount">The number of changesets to create in the batch payload.</param> /// <param name="changeSetSizes">The size of each changeset.</param> /// <param name="forRequest">true if creating a batch request payload; otherwise false.</param> /// <param name="changeSetBoundary">The changeset boundary to use; or null to use an auto-generated one.</param> /// <returns>A <see cref="PayloadTestDescriptor"/> for the batch payload.</returns> public static PayloadTestDescriptor CreateDefaultChangeSetBatch( IODataRequestManager requestManager, int changeSetCount, int[] changeSetSizes, bool forRequest, string changeSetBoundary = null) { Debug.Assert(changeSetCount >= 0, "batchSize >= 0"); Debug.Assert(changeSetSizes != null, "changeSetSizes != null"); Debug.Assert(changeSetSizes.Length == changeSetCount, "Size of the batch must match the length of the change set sizes array!"); var emptyPayload = new PayloadTestDescriptor() { PayloadEdmModel = CreateEmptyEdmModel() }; var root = ODataUriBuilder.Root(new Uri("http://www.odata.org/service.svc")); IMimePart[] parts = new IMimePart[changeSetCount]; if (forRequest) { // Delete operation with no payload var deleteOperation = emptyPayload.InRequestOperation(HttpVerb.Delete, new ODataUri(new ODataUriSegment[] { root }), requestManager); for (int i = 0; i < changeSetCount; ++i) { int changeSetSize = changeSetSizes[i]; var deleteOperations = new IMimePart[changeSetSize]; for (int j = 0; j < changeSetSize; ++j) { deleteOperations[j] = deleteOperation; } var changeset = BatchUtils.GetRequestChangeset(deleteOperations, requestManager); parts[i] = changeset; } ; string requestBoundary = changeSetBoundary ?? "bb_multiple_request_changesets_" + changeSetCount; return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchRequestPayload(parts) .AddAnnotation(new BatchBoundaryAnnotation(requestBoundary)), }); } // Response operation with no payload and a status code of 200 var emptyPayloadOperation = emptyPayload.InResponseOperation(200, requestManager); for (int i = 0; i < changeSetCount; ++i) { int changeSetSize = changeSetSizes[i]; var operationResponses = new IMimePart[changeSetSize]; for (int j = 0; j < changeSetSize; ++j) { operationResponses[j] = emptyPayloadOperation; } var changeset = BatchUtils.GetResponseChangeset(operationResponses, requestManager); parts[i] = changeset; } ; string responseBoundary = changeSetBoundary ?? "bb_multiple_response_changesets_" + changeSetCount; return(new PayloadTestDescriptor() { PayloadElement = PayloadBuilder.BatchResponsePayload(parts) .AddAnnotation(new BatchBoundaryAnnotation(responseBoundary)), }); }
/// <summary> /// Creates several test descriptors for testing that not creating operation messages for operations really fails. /// </summary> /// <returns>The created test descriptors.</returns> private IEnumerable<BatchMessageForEachOperationTestDescriptor> CreateMessageForEachOperationTestDescriptors() { var edmModel = new EdmModel().Fixup(); var emptyPayload = new PayloadTestDescriptor() { PayloadEdmModel = edmModel }; var root = ODataUriBuilder.Root(new Uri("http://www.odata.org/service.svc")); var rootUri = new ODataUri(new ODataUriSegment[] { root }); ODataResponse responseOperation = emptyPayload.InResponseOperation(200, this.RequestManager); ODataRequest requestOperation = emptyPayload.InRequestOperation(HttpVerb.Get, rootUri, this.RequestManager); BatchRequestChangeset emptyRequestChangeset = BatchUtils.GetRequestChangeset(new IMimePart[0], this.RequestManager); BatchResponseChangeset emptyResponseChangeset = BatchUtils.GetResponseChangeset(new IMimePart[0], this.RequestManager); BatchRequestChangeset requestChangeset = BatchUtils.GetRequestChangeset(new IMimePart[] { requestOperation }, this.RequestManager); BatchResponseChangeset responseChangeset = BatchUtils.GetResponseChangeset(new IMimePart[] { responseOperation }, this.RequestManager); return new BatchMessageForEachOperationTestDescriptor[] { // Empty batch payloads new BatchMessageForEachOperationTestDescriptor(this.PayloadReaderSettings) { DebugDescription = "Empty payload without operations in request; should not fail.", PayloadElement = PayloadBuilder.BatchRequestPayload() .AddAnnotation(new BatchBoundaryAnnotation("request_emptybatch")), SkipTestConfiguration = testConfig => !testConfig.IsRequest }, new BatchMessageForEachOperationTestDescriptor(this.PayloadReaderSettings) { DebugDescription = "Empty payload without operations in response; should not fail.", PayloadElement = PayloadBuilder.BatchResponsePayload() .AddAnnotation(new BatchBoundaryAnnotation("response_emptybatch")), SkipTestConfiguration = testConfig => testConfig.IsRequest }, // Single query operation payloads new BatchMessageForEachOperationTestDescriptor(this.PayloadReaderSettings) { DebugDescription = "Single query operation in request; should fail.", PayloadElement = PayloadBuilder.BatchRequestPayload(requestOperation) .AddAnnotation(new BatchBoundaryAnnotation("request_singleoperation")), ExpectedException = ODataExpectedExceptions.ODataException("ODataBatchReader_NoMessageWasCreatedForOperation"), SkipTestConfiguration = testConfig => !testConfig.IsRequest }, new BatchMessageForEachOperationTestDescriptor(this.PayloadReaderSettings) { DebugDescription = "Empty payload without operations in response; should fail.", PayloadElement = PayloadBuilder.BatchResponsePayload(responseOperation) .AddAnnotation(new BatchBoundaryAnnotation("response_singleoperation")), ExpectedException = ODataExpectedExceptions.ODataException("ODataBatchReader_NoMessageWasCreatedForOperation"), SkipTestConfiguration = testConfig => testConfig.IsRequest }, // Empty changeset payloads new BatchMessageForEachOperationTestDescriptor(this.PayloadReaderSettings) { DebugDescription = "Empty changeset in request; should not fail.", PayloadElement = PayloadBuilder.BatchRequestPayload(emptyRequestChangeset) .AddAnnotation(new BatchBoundaryAnnotation("request_emptychangeset")), SkipTestConfiguration = testConfig => !testConfig.IsRequest }, new BatchMessageForEachOperationTestDescriptor(this.PayloadReaderSettings) { DebugDescription = "Empty changeset in response; should not fail.", PayloadElement = PayloadBuilder.BatchResponsePayload(responseChangeset) .AddAnnotation(new BatchBoundaryAnnotation("response_changeset")), ExpectedException = ODataExpectedExceptions.ODataException("ODataBatchReader_NoMessageWasCreatedForOperation"), SkipTestConfiguration = testConfig => testConfig.IsRequest }, // Changeset with query operation payloads new BatchMessageForEachOperationTestDescriptor(this.PayloadReaderSettings) { DebugDescription = "Empty changeset in request; should not fail.", PayloadElement = PayloadBuilder.BatchRequestPayload(requestChangeset) .AddAnnotation(new BatchBoundaryAnnotation("request_changeset")), ExpectedException = ODataExpectedExceptions.ODataException("ODataBatchReader_NoMessageWasCreatedForOperation"), SkipTestConfiguration = testConfig => !testConfig.IsRequest }, new BatchMessageForEachOperationTestDescriptor(this.PayloadReaderSettings) { DebugDescription = "Empty changeset in response; should not fail.", PayloadElement = PayloadBuilder.BatchResponsePayload(emptyResponseChangeset) .AddAnnotation(new BatchBoundaryAnnotation("response_emptychangeset")), SkipTestConfiguration = testConfig => testConfig.IsRequest }, }; }