/// <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)
        {
            ODataPayloadElement payload = this.PayloadElement.DeepCopy();

            if (testConfiguration.Format == ODataFormat.Json)
            {
                payload.Accept(new ODataPayloadJsonNormalizer());
                //Fixup added as odatalib requires ids on feeds even though it can't be represented in json
                payload.Accept(new AddFeedIDFixup());
            }
            else if (testConfiguration.Format == ODataFormat.Atom)
            {
                // TODO: Remove visitor
                payload.Accept(new ODataPayloadElementNullIDVisitor());
            }

            ODataPayloadElementToObjectModelConverter converter = new ODataPayloadElementToObjectModelConverter(!testConfiguration.IsRequest);

            if (this.PayloadKind != ODataPayloadKind.Batch)
            {
                this.settings.ObjectModelToMessageWriter.WriteMessage(messageWriter, this.PayloadKind, converter.Convert(payload));
            }
            else
            {
                TestWriterUtils.WriteBatchPayload(messageWriter,
                                                  payload,
                                                  converter,
                                                  this.settings.ObjectModelToMessageWriter,
                                                  this.Model,
                                                  this.settings.Assert,
                                                  testConfiguration,
                                                  true);
            }
        }
        /// <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();
        }
        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");
            };
        }
예제 #4
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 virtual 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);
            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(() =>
            {
                using (ODataMessageWriterTestWrapper messageWriterWrapper = TestWriterUtils.CreateMessageWriter(message, model, testConfiguration, this.settings.Assert))
                {
                    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;
            }
        }
예제 #5
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();
            }
        }
 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;
     }
 }
예제 #7
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 abstract void WritePayload(ODataMessageWriterTestWrapper messageWriter, WriterTestConfiguration config);
 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);
 }
예제 #9
0
 private void InvokeWriterAction(ODataMessageWriterTestWrapper messageWriter, ODataWriter writer, WriterAction writerAction)
 {
     switch (writerAction)
     {
         case WriterAction.StartEntry:
             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;
     }
 }
        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;
                ODataFeed feed;
                ODataEntry entry;
                if (collectionStart != null)
                {
                    ODataCollectionWriter collectionWriter = parameterWriter.CreateCollectionWriter(parameter.Key);
                    this.WriteCollection(collectionWriter, collectionStart);
                    collectionWriter.Flush();
                }
                else if ((feed = parameter.Value as ODataFeed) != null)
                {
                    this.WriteFeed(parameterWriter.CreateFeedWriter(parameter.Key), feed);
                }
                else if ((entry = parameter.Value as ODataEntry) != null)
                {
                    this.WriteEntry(parameterWriter.CreateEntryWriter(parameter.Key), entry);
                }
                else
                {
                    parameterWriter.WriteValue(parameter.Key, parameter.Value);
                }
            }

            parameterWriter.WriteEnd();
            parameterWriter.Flush();
        }
 private void WriteCollection(ODataMessageWriterTestWrapper messageWriter, ODataCollectionStart collection)
 {
     ODataCollectionWriter collectionWriter = messageWriter.CreateODataCollectionWriter();
     this.WriteCollection(collectionWriter, collection);
     
     collectionWriter.Flush();
 }
 private void WriteValue(ODataMessageWriterTestWrapper messageWriter, object value)
 {
     messageWriter.WriteValue(value);
 }
 private void WriteEntityReferenceLinks(ODataMessageWriterTestWrapper messageWriter, ODataEntityReferenceLinks referenceLinks)
 {
     messageWriter.WriteEntityReferenceLinks(referenceLinks);
 }
 public void WriteEntityReferenceLink(ODataMessageWriterTestWrapper messageWriter, ODataEntityReferenceLink referenceLink)
 {
     messageWriter.WriteEntityReferenceLink(referenceLink);
 }
        private void WriteTopLevelEntry(ODataMessageWriterTestWrapper messageWriter, ODataEntry entry)
        {
            ExceptionUtilities.CheckArgumentNotNull(messageWriter, "messageWriter");
            ODataWriter entryWriter = messageWriter.CreateODataEntryWriter();

            this.WriteEntry(entryWriter, entry);
            
            entryWriter.Flush();
            
        }
예제 #16
0
        public void NullStreamMessageTest()
        {
            IEdmModel model = Microsoft.Test.OData.Utils.Metadata.TestModels.BuildTestModel();

            this.CombinatorialEngineProvider.RunCombinations(
                writerActionsForPayloadKinds,
                this.WriterTestConfigurationProvider.ExplicitFormatConfigurations,
                (writerInvocation, testConfiguration) =>
                {
                    if (!writerInvocation.PayloadKind.IsSupported(testConfiguration))
                    {
                        return;
                    }

                    ODataMessageWriter messageWriter;
                    if (testConfiguration.IsRequest)
                    {
                        messageWriter = new ODataMessageWriter(new NullStreamRequestMessage(), testConfiguration.MessageWriterSettings, model);
                    }
                    else
                    {
                        messageWriter = new ODataMessageWriter(new NullStreamResponseMessage(), testConfiguration.MessageWriterSettings, model);
                    }

                    using (ODataMessageWriterTestWrapper messageWriterWrapper = new ODataMessageWriterTestWrapper(messageWriter, testConfiguration))
                    {
                        this.Assert.ExpectedException(
                            () => TestExceptionUtils.UnwrapAggregateException(() => writerInvocation.WriterAction(messageWriterWrapper), this.Assert),
                            testConfiguration.IsRequest
                                ? ODataExpectedExceptions.ODataException("ODataRequestMessage_MessageStreamIsNull")
                                : ODataExpectedExceptions.ODataException("ODataResponseMessage_MessageStreamIsNull"),
                            this.ExceptionVerifier);
                    }
                });
        }
예제 #17
0
        public void NullStreamTaskMessageTest()
        {
            this.CombinatorialEngineProvider.RunCombinations(
                writerActionsForPayloadKinds,
                this.WriterTestConfigurationProvider.ExplicitFormatConfigurations.Where(tc => !tc.Synchronous),
                (writerInvocation, testConfiguration) =>
                {
                    if (!writerInvocation.PayloadKind.IsSupported(testConfiguration))
                    {
                        return;
                    }

                    ODataMessageWriter messageWriter;
                    if (testConfiguration.IsRequest)
                    {
                        messageWriter = new ODataMessageWriter(new NullStreamTaskRequestMessage(), testConfiguration.MessageWriterSettings);
                    }
                    else
                    {
                        messageWriter = new ODataMessageWriter(new NullStreamTaskResponseMessage(), testConfiguration.MessageWriterSettings);
                    }

                    using (ODataMessageWriterTestWrapper messageWriterWrapper = new ODataMessageWriterTestWrapper(messageWriter, testConfiguration))
                    {
                        this.Assert.ExpectedException(
                            () => TestExceptionUtils.UnwrapAggregateException(() => writerInvocation.WriterAction(messageWriterWrapper), this.Assert),
                            testConfiguration.IsRequest
                                ? ODataExpectedExceptions.ODataException("ODataRequestMessage_StreamTaskIsNull")
                                : ODataExpectedExceptions.ODataException("ODataResponseMessage_StreamTaskIsNull"),
                            this.ExceptionVerifier);
                    }
                });
        }
 private void WriteServiceDocument(ODataMessageWriterTestWrapper messageWriter, ODataServiceDocument serviceDocument)
 {
     messageWriter.WriteServiceDocument(serviceDocument);
 }
        /// <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)
        {
            ODataPayloadElement payload = this.PayloadElement.DeepCopy();

            if (testConfiguration.Format == ODataFormat.Json)
            {
                payload.Accept(new ODataPayloadJsonNormalizer());
                //Fixup added as odatalib requires ids on feeds even though it can't be represented in json
                payload.Accept(new AddFeedIDFixup());
            }
            else if (testConfiguration.Format == ODataFormat.Atom)
            {
                // TODO: Remove visitor
                payload.Accept(new ODataPayloadElementNullIDVisitor());
            }

            ODataPayloadElementToObjectModelConverter converter = new ODataPayloadElementToObjectModelConverter(!testConfiguration.IsRequest);
            if (this.PayloadKind != ODataPayloadKind.Batch)
            {    
                this.settings.ObjectModelToMessageWriter.WriteMessage(messageWriter, this.PayloadKind, converter.Convert(payload));
            }
            else
            {
                TestWriterUtils.WriteBatchPayload(messageWriter, 
                    payload, 
                    converter, 
                    this.settings.ObjectModelToMessageWriter, 
                    this.Model,
                    this.settings.Assert, 
                    testConfiguration, 
                    true);
            }
        }
 private void WriteMetadataDocument(ODataMessageWriterTestWrapper messageWriter)
 {
     messageWriter.WriteMetadataDocument();
 }
 private void WriteError(ODataMessageWriterTestWrapper messageWriter, ODataError error, bool debug)
 {
     messageWriter.WriteError(error, debug);
 }
        /// <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.Feed:
                    this.WriteTopLevelFeed(messageWriter, (ODataFeed)payload);
                    break;

                case ODataPayloadKind.Entry:
                    this.WriteTopLevelEntry(messageWriter, (ODataEntry)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;
            }
        }
 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);
 }
        private void WriteTopLevelFeed(ODataMessageWriterTestWrapper messageWriter, ODataFeed feed)
        {
            ExceptionUtilities.CheckArgumentNotNull(messageWriter, "messageWriter");

            var feedWriter = messageWriter.CreateODataFeedWriter();
            this.WriteFeed(feedWriter, feed);
            
            feedWriter.Flush();
            
        }
예제 #25
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 abstract void WritePayload(ODataMessageWriterTestWrapper messageWriter, WriterTestConfiguration config);
        /// <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();
        }