コード例 #1
0
            /// <summary>
            /// Read a batch payload as the message content.
            /// </summary>
            /// <param name="messageReader">The message reader to use for reading.</param>
            /// <param name="expectedBatchPayload">The expected batch payload element; this is used to guide the payload kind detection of the parts.</param>
            /// <returns>An <see cref="ODataBatch"/>, possibly with annotations.</returns>
            public ODataBatch ReadBatch(ODataMessageReaderTestWrapper messageReader, ODataPayloadElement expectedBatchPayload)
            {
                this.assert.IsNotNull(expectedBatchPayload, "Must have a batch payload element.");
                this.assert.IsTrue(
                    expectedBatchPayload is BatchRequestPayload && this.testConfiguration.IsRequest || expectedBatchPayload is BatchResponsePayload && !this.testConfiguration.IsRequest,
                    "Expected a request or a response batch payload depending on what we are reading.");

                ODataBatchReaderTestWrapper batchReader = messageReader.CreateODataBatchReader();
                this.assert.AreEqual(ODataBatchReaderState.Initial, batchReader.State, "Reader states don't match.");

                IMimePart[] expectedParts = this.testConfiguration.IsRequest
                    ? ((BatchRequestPayload)expectedBatchPayload).Parts.ToArray()
                    : ((BatchResponsePayload)expectedBatchPayload).Parts.ToArray();
                int expectedPartIndex = 0;

                List<ODataBatchPart> batchParts = new List<ODataBatchPart>();
                List<ODataBatchOperation> changeSetOperations = null;
                bool errorReadingPartPayload = false;
                try
                {
                    bool isCompleted = false;
                    while (batchReader.Read())
                    {
                        this.assert.IsFalse(isCompleted, "We should not be able to successfully read once we reached the 'Completed' state.");

                        switch (batchReader.State)
                        {
                            case ODataBatchReaderState.Initial:
                                this.assert.Fail("We should never be in state 'Initial' after we started reading.");
                                break;

                            case ODataBatchReaderState.Operation:
                                IMimePart expectedPart = expectedParts[expectedPartIndex];
                                int indexInPart = changeSetOperations == null ? -1 : changeSetOperations.Count;

                                try
                                {
                                    ODataBatchOperation operation = this.testConfiguration.IsRequest
                                        ? this.GetRequestOperation(batchReader, expectedPart, indexInPart)
                                        : this.GetResponseOperation(batchReader, expectedPart, indexInPart);

                                    if (changeSetOperations == null)
                                    {
                                        batchParts.Add(operation);
                                        expectedPartIndex++;
                                    }
                                    else
                                    {
                                        changeSetOperations.Add(operation);
                                    }
                                }
                                catch (Exception)
                                {
                                    // NOTE: The batch reader will not enter exception state when an error is raised while
                                    //       reading a part payload. The batch reader might skip to the next part and continue reading.
                                    errorReadingPartPayload = true;
                                    throw;
                                }

                                break;

                            case ODataBatchReaderState.ChangesetStart:
                                this.assert.IsNull(changeSetOperations, "Must not have pending change set operations when detecting another changeset.");
                                changeSetOperations = new List<ODataBatchOperation>();
                                break;

                            case ODataBatchReaderState.ChangesetEnd:
                                ODataBatchChangeset changeset = new ODataBatchChangeset
                                {
                                    Operations = changeSetOperations,
                                };
                                changeSetOperations = null;
                                batchParts.Add(changeset);
                                expectedPartIndex++;
                                break;

                            case ODataBatchReaderState.Completed:
                                isCompleted = true;
                                break;

                            case ODataBatchReaderState.Exception:
                                this.assert.Fail("We should never be in state 'Exception' without an exception being thrown.");
                                break;

                            default:
                                this.assert.Fail("Unsupported batch reader state '" + batchReader.State + "' detected.");
                                break;
                        }
                    }

                    this.assert.AreEqual(ODataBatchReaderState.Completed, batchReader.State, "Expected state 'Completed' when done reading.");
                }
                catch (Exception e)
                {
                    // NOTE: The batch reader will not enter exception state when an error is raised while
                    //       reading a part payload. The batch reader might skip to the next part and continue reading.
                    if (ExceptionUtilities.IsCatchable(e) && !errorReadingPartPayload)
                    {
                        this.assert.AreEqual(ODataBatchReaderState.Exception, batchReader.State, "Expected the reader to be in 'Exception' state.");
                    }

                    throw;
                }

                return new ODataBatch { Parts = batchParts };
            }
コード例 #2
0
            /// <summary>
            /// Verifies that the result of the test (the message reader) is what the test expected.
            /// </summary>
            /// <param name="messageReader">The message reader which is the result of the test. This method should use it to read the results
            /// of the parsing and verify those.</param>
            /// <param name="payloadKind">The payload kind specified in the test descriptor.</param>
            /// <param name="testConfiguration">The test configuration to use.</param>
            public override void VerifyResult(ODataMessageReaderTestWrapper messageReader, ODataPayloadKind payloadKind, ReaderTestConfiguration testConfiguration)
            {
                // We are not verifying anything here other than reading the result in a non-compliant
                // way by not creating operation messages when we encounter an operation in a batch/changeset.
                ODataBatchReaderTestWrapper batchReader = messageReader.CreateODataBatchReader();
                this.settings.Assert.AreEqual(ODataBatchReaderState.Initial, batchReader.State, "Reader states don't match.");

                while (batchReader.Read())
                {
                    // Just keep reading through the whole batch.
                    // The batch reader should fail if no operation message is created for an operation.
                }
            }