/// <summary> /// Called to get the expected result of the test. /// </summary> /// <param name="testConfiguration">The test configuration to use.</param> /// <returns>The expected result.</returns> protected override WriterTestExpectedResults GetExpectedResult(WriterTestConfiguration testConfiguration) { WriterTestExpectedResults expectedResult = base.GetExpectedResult(testConfiguration); if (expectedResult == null) { ODataErrorException errorException = null; Exception regularException = null; if (this.ExpectedException != null) { errorException = this.ExpectedException as ODataErrorException; regularException = errorException == null ? this.ExpectedException : null; } return(new MetadataWriterTestExpectedResult(this.settings.ExpectedResultSettings) { EdmVersion = this.EdmVersion, ExpectedException = regularException, ExpectedODataErrorException = errorException, ExpectedODataExceptionMessage = this.ExpectedODataExceptionMessage, }); } else { return(expectedResult); } }
/// <summary> /// Creates an <see cref="ODataCollectionWriter"/> for the specified format and the specified version and /// invokes the specified methods on it. It then parses /// the written Xml/JSON and compares it to the expected result as specified in the descriptor. /// </summary> /// <param name="descriptor">The test descriptor to process.</param> /// <param name="testConfiguration">The configuration of the test.</param> /// <param name="assert">The assertion handler to report errors to.</param> /// <param name="baselineLogger">Logger to log baseline.</param> internal static void WriteAndVerifyCollectionPayload(CollectionWriterTestDescriptor descriptor, WriterTestConfiguration testConfiguration, AssertionHandler assert, BaselineLogger baselineLogger) { baselineLogger.LogConfiguration(testConfiguration); baselineLogger.LogModelPresence(descriptor.Model); // serialize to a memory stream using (var memoryStream = new MemoryStream()) using (var testMemoryStream = new TestStream(memoryStream, ignoreDispose: true)) { TestMessage testMessage = null; Exception exception = TestExceptionUtils.RunCatching(() => { using (var messageWriter = TestWriterUtils.CreateMessageWriter(testMemoryStream, testConfiguration, assert, out testMessage, null, descriptor.Model)) { IEdmTypeReference itemTypeReference = descriptor.ItemTypeParameter; ODataCollectionWriter writer = itemTypeReference == null ? messageWriter.CreateODataCollectionWriter() : messageWriter.CreateODataCollectionWriter(itemTypeReference); WriteCollectionPayload(messageWriter, writer, true, descriptor); } }); exception = TestExceptionUtils.UnwrapAggregateException(exception, assert); WriterTestExpectedResults expectedResults = descriptor.ExpectedResultCallback(testConfiguration); TestWriterUtils.ValidateExceptionOrLogResult(testMessage, testConfiguration, expectedResults, exception, assert, descriptor.TestDescriptorSettings.ExpectedResultSettings.ExceptionVerifier, baselineLogger); TestWriterUtils.ValidateContentType(testMessage, expectedResults, true, assert); } }
public override void RunTest(ReaderTestConfiguration testConfiguration) { //TODO: Use Logger to verify result, right now this change is only to unblock writer testcase checkin BaselineLogger logger = null; if (this.ShouldSkipForTestConfiguration(testConfiguration)) { return; } var originalPayload = this.PayloadElement; this.PayloadElement = this.PayloadElement.DeepCopy(); // Create messages (payload gets serialized in createInputMessage) TestMessage readerMessage = this.CreateInputMessage(testConfiguration); var settings = new ODataMessageWriterSettings() { Version = testConfiguration.Version, BaseUri = testConfiguration.MessageReaderSettings.BaseUri, EnableMessageStreamDisposal = testConfiguration.MessageReaderSettings.EnableMessageStreamDisposal, }; settings.SetContentType(testConfiguration.Format); WriterTestConfiguration writerConfig = new WriterTestConfiguration(testConfiguration.Format, settings, testConfiguration.IsRequest, testConfiguration.Synchronous); TestMessage writerMessage = TestWriterUtils.CreateOutputMessageFromStream(new TestStream(new MemoryStream()), writerConfig, this.PayloadKind, String.Empty, this.UrlResolver); IEdmModel model = this.GetMetadataProvider(testConfiguration); WriterTestExpectedResults expectedResult = this.GetExpectedResult(writerConfig); ExceptionUtilities.Assert(expectedResult != null, "The expected result could not be determined for the test. Did you specify it?"); Exception exception = TestExceptionUtils.RunCatching(() => { using (ODataMessageReaderTestWrapper messageReaderWrapper = TestReaderUtils.CreateMessageReader(readerMessage, model, testConfiguration)) using (ODataMessageWriterTestWrapper messageWriterWrapper = TestWriterUtils.CreateMessageWriter(writerMessage, model, writerConfig, this.settings.Assert)) { var streamer = new ObjectModelReadWriteStreamer(); streamer.StreamMessage(messageReaderWrapper, messageWriterWrapper, this.PayloadKind, writerConfig); expectedResult.VerifyResult(writerMessage, this.PayloadKind, writerConfig, logger); } }); this.PayloadElement = originalPayload; try { expectedResult.VerifyException(exception); } catch (Exception) { this.TraceFailureInformation(testConfiguration); throw; } }
/// <summary> /// Runs the test specified by this test descriptor. /// </summary> /// <param name="testConfiguration">The test configuration to use for running the test.</param> public override void RunTest(WriterTestConfiguration testConfiguration, BaselineLogger logger) { if (this.ShouldSkipForTestConfiguration(testConfiguration)) { return; } // Wrap the memory stream in a non-disposing stream so we can dump the message content // even in the case of a failure where the message stream normally would get disposed. logger.LogConfiguration(testConfiguration); logger.LogModelPresence(this.Model); this.messageStream = new NonDisposingStream(new MemoryStream()); TestMessage message = this.CreateOutputMessage(this.messageStream, testConfiguration, this.PayloadElement); IEdmModel model = this.GetMetadataProvider(); WriterTestExpectedResults expectedResult = this.GetExpectedResult(testConfiguration); ExceptionUtilities.Assert(expectedResult != null, "The expected result could not be determined for the test. Did you specify it?"); Exception exception = TestExceptionUtils.RunCatching(() => { // We create a new test configuration for batch because the payload indicates whether we are dealing with a request or a response and the configuration won't know that in advance var newTestConfig = new WriterTestConfiguration(testConfiguration.Format, testConfiguration.MessageWriterSettings, this.PayloadElement is BatchRequestPayload, testConfiguration.Synchronous); using (ODataMessageWriterTestWrapper messageWriterWrapper = TestWriterUtils.CreateMessageWriter(message, model, newTestConfig, this.settings.Assert, null)) { this.WritePayload(messageWriterWrapper, testConfiguration); expectedResult.VerifyResult(message, this.PayloadKind, testConfiguration, logger); } }); try { expectedResult.VerifyException(exception); } catch (Exception failureException) { this.TraceFailureInformation(message, this.messageStream, testConfiguration); throw failureException; } }
/// <summary> /// Applies the payload case to a test descriptor returning a new test descriptor. /// </summary> /// <param name="testDescriptor">The test descriptor to apply the case to.</param> /// <returns>The new test descriptor.</returns> internal PayloadWriterTestDescriptor <TOut> ApplyToTestDescriptor(PayloadWriterTestDescriptor <TIn> testDescriptor) { IEnumerable <TOut> payloadItems = null; if (this.GetPayloadItems != null) { payloadItems = this.GetPayloadItems(); } else { // for the test descriptor to specify payload items TIn and TOut have to be compatible! Type inputType = typeof(TIn); Type outputType = typeof(TOut); if (inputType != outputType && !outputType.IsAssignableFrom(inputType)) { throw new NotSupportedException( "The test descriptor can only specify payload items when they are compatible with the expected output type. " + inputType.FullName + " is not compatible with " + outputType.FullName + "."); } payloadItems = testDescriptor.PayloadItems.Cast <TOut>(); } EdmModel model = (EdmModel)testDescriptor.Model; IEdmElement payloadElementModelContainer = testDescriptor.PayloadEdmElementContainer; IEdmElement payloadElementType = testDescriptor.PayloadEdmElementType; if (model != null && this.ModelBuilder != null) { model = this.ModelBuilder(model); } return(new PayloadWriterTestDescriptor <TOut>( testDescriptor.TestDescriptorSettings, payloadItems, (testConfiguration) => { if (this.ShouldSkip != null && this.ShouldSkip(testConfiguration)) { return null; } WriterTestExpectedResults expectedResults = testDescriptor.ExpectedResultCallback(testConfiguration); AtomWriterTestExpectedResults atomResults = expectedResults as AtomWriterTestExpectedResults; if (atomResults != null) { return new AtomWriterTestExpectedResults(atomResults) { FragmentExtractor = this.AtomFragmentExtractor == null ? atomResults.FragmentExtractor : (result) => atomResults.FragmentExtractor(this.AtomFragmentExtractor(testConfiguration, result)), }; } JsonWriterTestExpectedResults jsonResults = expectedResults as JsonWriterTestExpectedResults; if (jsonResults != null) { return new JsonWriterTestExpectedResults(jsonResults) { FragmentExtractor = this.JsonLightFragmentExtractor == null ? jsonResults.FragmentExtractor : (result) => jsonResults.FragmentExtractor(this.JsonLightFragmentExtractor(testConfiguration, result)), }; } return expectedResults; }) { SkipTestConfiguration = testDescriptor.SkipTestConfiguration, IsGeneratedPayload = !this.NotGenerated, Model = model, PayloadEdmElementContainer = payloadElementModelContainer, PayloadEdmElementType = payloadElementType, }); }