Exemple #1
0
        private void WriteTopLevelEntry(ODataMessageWriterTestWrapper messageWriter, ODataMessageReaderTestWrapper messageReader, ODataEntry entry)
        {
            ODataWriter        entryWriter     = messageWriter.CreateODataEntryWriter();
            Lazy <ODataReader> lazyEntryReader = new Lazy <ODataReader>(() => messageReader.CreateODataEntryReader());

            this.WriteEntry(entryWriter, lazyEntryReader, entry);
        }
Exemple #2
0
        private void WriteParameters(ODataMessageWriterTestWrapper messageWriter, ODataParameters parameters, IEdmOperationImport functionImport)
        {
            ODataParameterWriter parameterWriter = messageWriter.CreateODataParameterWriter(functionImport);

            parameterWriter.WriteStart();
            foreach (var parameter in parameters)
            {
                ODataCollectionStart collectionStart = parameter.Value as ODataCollectionStart;
                ODataResourceSet     feed;
                ODataResource        entry;
                if (collectionStart != null)
                {
                    ODataCollectionWriter collectionWriter = parameterWriter.CreateCollectionWriter(parameter.Key);
                    this.WriteCollection(collectionWriter, collectionStart);
                    collectionWriter.Flush();
                }
                else if ((feed = parameter.Value as ODataResourceSet) != null)
                {
                    this.WriteFeed(parameterWriter.CreateResourceSetWriter(parameter.Key), feed);
                }
                else if ((entry = parameter.Value as ODataResource) != null)
                {
                    this.WriteEntry(parameterWriter.CreateResourceWriter(parameter.Key), entry);
                }
                else
                {
                    parameterWriter.WriteValue(parameter.Key, parameter.Value);
                }
            }

            parameterWriter.WriteEnd();
            parameterWriter.Flush();
        }
Exemple #3
0
        private void WriteTopLevelFeed(ODataMessageWriterTestWrapper messageWriter, ODataMessageReaderTestWrapper messageReader, ODataFeed feed)
        {
            var feedWriter = messageWriter.CreateODataFeedWriter();
            Lazy <ODataReader> lazyReader = new Lazy <ODataReader>(() => messageReader.CreateODataFeedReader());

            this.WriteFeed(feedWriter, lazyReader, feed);
        }
        /// <summary>
        /// Called to write the payload to the specified <paramref name="messageWriter"/>.
        /// </summary>
        /// <param name="messageWriter">The <see cref="ODataMessageWriterTestWrapper"/> to use for writing the payload.</param>
        /// <param name="testConfiguration">The test configuration to generate the payload for.</param>
        protected override void WritePayload(ODataMessageWriterTestWrapper messageWriter, WriterTestConfiguration testConfiguration)
        {
            Debug.Assert(this.messageStream != null, "Streaming test stream must have been created.");
            TestMessage testMessage = this.CreateInputMessageFromStream((TestStream)this.messageStream, testConfiguration, this.PayloadKind, string.Empty, this.UrlResolver);

            testMessage.SetContentType(testConfiguration.Format, this.PayloadKind);

            Exception exception = TestExceptionUtils.RunCatching(() =>
            {
                ODataMessageReaderSettings readerSettings  = this.settings.MessageReaderSettings.Clone();
                readerSettings.EnableMessageStreamDisposal = testConfiguration.MessageWriterSettings.EnableMessageStreamDisposal;

                ReaderTestConfiguration readerConfig = new ReaderTestConfiguration(
                    testConfiguration.Format,
                    readerSettings,
                    testConfiguration.IsRequest,
                    testConfiguration.Synchronous);

                IEdmModel model = this.GetMetadataProvider();
                using (ODataMessageReaderTestWrapper messageReaderWrapper = TestReaderUtils.CreateMessageReader(testMessage, model, readerConfig))
                {
                    ODataPayloadElementToObjectModelConverter payloadElementToOMConverter = new ODataPayloadElementToObjectModelConverter(!testConfiguration.IsRequest);
                    ObjectModelToPayloadElementConverter reverseConverter = new ObjectModelToPayloadElementConverter();
                    ObjectModelWriteReadStreamer streamer = new ObjectModelWriteReadStreamer();

                    this.readObject = reverseConverter.Convert(streamer.WriteMessage(messageWriter, messageReaderWrapper, this.PayloadKind, payloadElementToOMConverter.Convert(this.PayloadElement)), !testConfiguration.IsRequest);
                }
            });
        }
        private void InvokeWriterAction(ODataMessageWriterTestWrapper messageWriter, ODataWriter writer, WriterAction writerAction)
        {
            switch (writerAction)
            {
            case WriterAction.StartResource:
                writer.WriteStart(ObjectModelUtils.CreateDefaultEntry());
                break;

            case WriterAction.StartFeed:
                writer.WriteStart(ObjectModelUtils.CreateDefaultFeed());
                break;

            case WriterAction.StartLink:
                writer.WriteStart(ObjectModelUtils.CreateDefaultCollectionLink());
                break;

            case WriterAction.End:
                writer.WriteEnd();
                break;

            case WriterAction.Error:
                messageWriter.WriteError(new ODataError(), false);
                break;
            }
        }
Exemple #6
0
        private void WriteCollection(ODataMessageWriterTestWrapper messageWriter, ODataCollectionStart collection)
        {
            ODataCollectionWriter collectionWriter = messageWriter.CreateODataCollectionWriter();

            this.WriteCollection(collectionWriter, collection);

            collectionWriter.Flush();
        }
Exemple #7
0
        private void WriteTopLevelEntry(ODataMessageWriterTestWrapper messageWriter, ODataResource entry)
        {
            ExceptionUtilities.CheckArgumentNotNull(messageWriter, "messageWriter");
            ODataWriter entryWriter = messageWriter.CreateODataResourceWriter();

            this.WriteEntry(entryWriter, entry);

            entryWriter.Flush();
        }
Exemple #8
0
        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;
            }
        }
Exemple #9
0
        private void WriteTopLevelFeed(ODataMessageWriterTestWrapper messageWriter, ODataResourceSet feed)
        {
            ExceptionUtilities.CheckArgumentNotNull(messageWriter, "messageWriter");

            var feedWriter = messageWriter.CreateODataResourceSetWriter();

            this.WriteFeed(feedWriter, feed);

            feedWriter.Flush();
        }
Exemple #10
0
        /// <summary>
        /// Creates an ODataWriter for the specified format and the specified version and
        /// writes the property in the descriptor to an in-memory stream. It then parses
        /// the written Xml and compares it to the expected result as specified in the descriptor.
        /// </summary>
        /// <param name="originalPayload">The payload to first clone and then write.</param>
        /// <param name="testConfiguration">Configuration for the test</param>
        /// <param name="model">The model to use.</param>
        protected void WriteAndVerifyODataProperty(ODataPayloadElement originalPayload, WriterTestConfiguration testConfiguration, IEdmModel model)
        {
            this.Logger.LogConfiguration(testConfiguration);
            this.Logger.LogModelPresence(model);

            using (var memoryStream = new TestStream())
            {
                TestMessage testMessage;
                using (ODataMessageWriterTestWrapper writer = TestWriterUtils.CreateMessageWriter(memoryStream, testConfiguration, this.Assert, out testMessage, null, model))
                {
                    Action <ODataPayloadElement> writeElementToStream = payload => this.PropertyPayloadElementWriter.WriteProperty(writer.MessageWriter, payload);
                    this.WriteAndLogODataPayload(originalPayload, writer.Message, testConfiguration.Version, testConfiguration.Format, writeElementToStream);
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// Write payload kind to message.
        /// </summary>
        /// <param name="messageWriter">Message writer to write payload to.</param>
        /// <param name="payloadKind">The kind of payload we are writing.</param>
        /// <param name="payload">The payload to write.</param>
        /// <param name="functionImport">Function import whose parameters are to be written when the payload kind is Parameters.</param>
        /// <returns>The object read after writing.</returns>
        public ODataItem WriteMessage(ODataMessageWriterTestWrapper messageWriter, ODataMessageReaderTestWrapper messageReader, ODataPayloadKind payloadKind, object payload, IEdmOperationImport functionImport = null)
        {
            ExceptionUtilities.CheckArgumentNotNull(messageWriter, "messageWriter");
            ExceptionUtilities.CheckArgumentNotNull(messageReader, "messageReader");

            switch (payloadKind)
            {
            case ODataPayloadKind.Feed:
                this.WriteTopLevelFeed(messageWriter, messageReader, (ODataFeed)payload);
                break;

            case ODataPayloadKind.Entry:
                this.WriteTopLevelEntry(messageWriter, messageReader, (ODataEntry)payload);
                break;

            default:
                ExceptionUtilities.Assert(false, "The payload kind '{0}' is not yet supported by ObjectModelWriteReadStreamer.", payloadKind);
                break;
            }

            return(readItems.SingleOrDefault());
        }
        /// <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;
            }
        }
Exemple #13
0
        private void InvokeCollectionWriterAction(ODataMessageWriterTestWrapper messageWriter, ODataCollectionWriter writer, CollectionWriterAction writerAction)
        {
            switch (writerAction)
            {
            case CollectionWriterAction.Start:
                writer.WriteStart(new ODataCollectionStart {
                    Name = "foo"
                });
                break;

            case CollectionWriterAction.Item:
                writer.WriteItem(42);
                break;

            case CollectionWriterAction.End:
                writer.WriteEnd();
                break;

            case CollectionWriterAction.Error:
                messageWriter.WriteError(new ODataError(), false);
                break;
            }
        }
Exemple #14
0
        /// <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 = null)
        {
            //TODO: Use Logger to verify result, right now this change is only to unblock writer testcase checkin

            if (this.ShouldSkipForTestConfiguration(testConfiguration))
            {
                return;
            }

            // Generate a StreamingTestStream with a NonDisposingStream.
            this.messageStream = new StreamingTestStream(new NonDisposingStream(new MemoryStream()));
            TestMessage message = this.CreateOutputMessage(this.messageStream, testConfiguration);
            IEdmModel   model   = this.GetMetadataProvider();
            StreamingWriterTestExpectedResults expectedResult = (StreamingWriterTestExpectedResults)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(() =>
            {
                using (ODataMessageWriterTestWrapper messageWriterWrapper = TestWriterUtils.CreateMessageWriter(message, model, testConfiguration, this.settings.Assert))
                {
                    this.WritePayload(messageWriterWrapper, testConfiguration);
                    expectedResult.ObservedElement = this.readObject;
                    expectedResult.VerifyResult(message, this.PayloadKind, testConfiguration);
                }
            });

            try
            {
                expectedResult.VerifyException(exception);
            }
            catch (Exception failureException)
            {
                this.TraceFailureInformation(message, this.messageStream, testConfiguration);
                throw failureException;
            }
        }
        public void StreamMessage(ODataMessageReaderTestWrapper reader, ODataMessageWriterTestWrapper writer, ODataPayloadKind payloadKind, WriterTestConfiguration config)
        {
            ExceptionUtilities.CheckArgumentNotNull(reader, "reader is required");
            ExceptionUtilities.CheckArgumentNotNull(writer, "writer is required");
            ExceptionUtilities.CheckArgumentNotNull(payloadKind, "payloadKind is required");
            ExceptionUtilities.CheckArgumentNotNull(config, "config is required");

            this.testConfiguration = config;

            switch (payloadKind)
            {
            case ODataPayloadKind.Entry:
                this.StartRead(reader.CreateODataEntryReader(), writer.CreateODataEntryWriter());
                break;

            case ODataPayloadKind.Feed:
                this.StartRead(reader.CreateODataFeedReader(), writer.CreateODataFeedWriter());
                break;

            default:
                throw new NotSupportedException("ObjectModelReadWriteStreamer currently supports only feed and entry");
            }
            ;
        }
Exemple #16
0
        /// <summary>
        /// Write payload kind to message.
        /// </summary>
        /// <param name="messageWriter">Message writer to write payload to.</param>
        /// <param name="payloadKind">The kind of payload we are writing.</param>
        /// <param name="payload">The payload to write.</param>
        /// <param name="model">The model used for writing the payloads.</param>
        /// <param name="functionImport">Function import whose parameters are to be written when the payload kind is Parameters.</param>
        public virtual void WriteMessage(
            ODataMessageWriterTestWrapper messageWriter,
            ODataPayloadKind payloadKind,
            object payload,
            IEdmModel model = null,
            IEdmOperationImport functionImport = null)
        {
            ExceptionUtilities.CheckArgumentNotNull(messageWriter, "messageReader");

            switch (payloadKind)
            {
            case ODataPayloadKind.Property:
                messageWriter.WriteProperty((ODataProperty)payload);
                break;

            case ODataPayloadKind.ResourceSet:
                this.WriteTopLevelFeed(messageWriter, (ODataResourceSet)payload);
                break;

            case ODataPayloadKind.Resource:
                this.WriteTopLevelEntry(messageWriter, (ODataResource)payload);
                break;

            case ODataPayloadKind.Collection:
                this.WriteCollection(messageWriter, (ODataCollectionStart)payload);
                break;

            case ODataPayloadKind.ServiceDocument:
                this.WriteServiceDocument(messageWriter, (ODataServiceDocument)payload);
                break;

            case ODataPayloadKind.MetadataDocument:
                this.WriteMetadataDocument(messageWriter);
                break;

            case ODataPayloadKind.Error:
                this.WriteError(messageWriter, (ODataError)payload, true);
                break;

            case ODataPayloadKind.EntityReferenceLink:
                this.WriteEntityReferenceLink(messageWriter, (ODataEntityReferenceLink)payload);
                break;

            case ODataPayloadKind.EntityReferenceLinks:
                this.WriteEntityReferenceLinks(messageWriter, (ODataEntityReferenceLinks)payload);
                break;

            case ODataPayloadKind.Value:
                this.WriteValue(messageWriter, payload);
                break;

            case ODataPayloadKind.Batch:
                // TODO: Have to figure out product representation of batch payloads or perhaps keep it separate
                throw new NotSupportedException("Batch not supported in ObjectModelToMessageWriter");

            case ODataPayloadKind.Parameter:
                this.WriteParameters(messageWriter, (ODataParameters)payload, functionImport);
                break;

            default:
                ExceptionUtilities.Assert(false, "The payload kind '{0}' is not yet supported by MessageToObjectModelReader.", payloadKind);
                break;
            }
        }
Exemple #17
0
 private void WriteError(ODataMessageWriterTestWrapper messageWriter, ODataError error, bool debug)
 {
     messageWriter.WriteError(error, debug);
 }
Exemple #18
0
 private void WriteMetadataDocument(ODataMessageWriterTestWrapper messageWriter)
 {
     messageWriter.WriteMetadataDocument();
 }
Exemple #19
0
 private void WriteServiceDocument(ODataMessageWriterTestWrapper messageWriter, ODataServiceDocument serviceDocument)
 {
     messageWriter.WriteServiceDocument(serviceDocument);
 }
Exemple #20
0
        /// <summary>
        /// Writes the collection payload as specified in the <paramref name="testDescriptor"/>.
        /// </summary>
        /// <param name="messageWriter">The message writer.</param>
        /// <param name="writer">The writer to write to.</param>
        /// <param name="flush">True if the stream should be flush before returning; otherwise false.</param>
        /// <param name="testDescriptor">The test descriptor specifying the collection to write.</param>
        internal static void WriteCollectionPayload(ODataMessageWriterTestWrapper messageWriter, ODataCollectionWriter writer, bool flush, CollectionWriterTestDescriptor testDescriptor)
        {
            Debug.Assert(writer != null, "writer != null");
            Debug.Assert(testDescriptor != null, "testDescriptor != null");

            object[] payloadItems     = testDescriptor.PayloadItems;
            int      payloadItemIndex = 0;

            foreach (CollectionWriterTestDescriptor.WriterInvocations invocation in testDescriptor.Invocations)
            {
                switch (invocation)
                {
                case CollectionWriterTestDescriptor.WriterInvocations.StartCollection:
                    ODataCollectionStartSerializationInfo serInfo = null;
                    if (!string.IsNullOrEmpty(testDescriptor.CollectionTypeName))
                    {
                        serInfo = new ODataCollectionStartSerializationInfo();
                        serInfo.CollectionTypeName = testDescriptor.CollectionTypeName;
                    }

                    writer.WriteStart(new ODataCollectionStart {
                        Name = testDescriptor.CollectionName, SerializationInfo = serInfo
                    });
                    break;

                case CollectionWriterTestDescriptor.WriterInvocations.Item:
                    object payloadItem = payloadItems[payloadItemIndex];

                    ODataError error = payloadItem as ODataError;
                    if (error != null)
                    {
                        throw new InvalidOperationException("Expected payload item but found an error.");
                    }

                    writer.WriteItem(payloadItem);
                    payloadItemIndex++;
                    break;

                case CollectionWriterTestDescriptor.WriterInvocations.Error:
                    ODataAnnotatedError error2 = testDescriptor.PayloadItems[payloadItemIndex] as ODataAnnotatedError;
                    if (error2 == null)
                    {
                        throw new InvalidOperationException("Expected an error but found a payload item.");
                    }

                    messageWriter.WriteError(error2.Error, error2.IncludeDebugInformation);
                    payloadItemIndex++;
                    break;

                case CollectionWriterTestDescriptor.WriterInvocations.EndCollection:
                    writer.WriteEnd();
                    break;

                case CollectionWriterTestDescriptor.WriterInvocations.UserException:
                    throw new Exception("User code triggered an exception.");

                default:
                    break;
                }
            }

            if (flush)
            {
                writer.Flush();
            }
        }
Exemple #21
0
        /// <summary>
        /// Called to write the payload to the specified <paramref name="messageWriter"/>.
        /// </summary>
        /// <param name="messageWriter">The <see cref="ODataMessageWriterTestWrapper"/> to use for writing the payload.</param>
        protected override void WritePayload(ODataMessageWriterTestWrapper messageWriter, WriterTestConfiguration config)
        {
            Debug.Assert(messageWriter != null, "messageWriter != null");

            messageWriter.WriteMetadataDocument();
        }
Exemple #22
0
 private void WriteValue(ODataMessageWriterTestWrapper messageWriter, object value)
 {
     messageWriter.WriteValue(value);
 }
Exemple #23
0
 private void WriteEntityReferenceLinks(ODataMessageWriterTestWrapper messageWriter, ODataEntityReferenceLinks referenceLinks)
 {
     messageWriter.WriteEntityReferenceLinks(referenceLinks);
 }
Exemple #24
0
 public void WriteEntityReferenceLink(ODataMessageWriterTestWrapper messageWriter, ODataEntityReferenceLink referenceLink)
 {
     messageWriter.WriteEntityReferenceLink(referenceLink);
 }