Exemple #1
0
        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="other">The <see cref="WriterTestExpectedResults"/> instance to copy.</param>
        public WriterTestExpectedResults(WriterTestExpectedResults other)
        {
            ExceptionUtilities.CheckArgumentNotNull(other, "other");

            this.settings                      = other.settings;
            this.ExpectedContentType           = other.ExpectedContentType;
            this.ExpectedException             = other.ExpectedException;
            this.ExpectedException2            = other.ExpectedException2;
            this.ExpectedODataErrorException   = other.ExpectedODataErrorException;
            this.ExpectedODataExceptionMessage = other.ExpectedODataExceptionMessage;
        }
        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="other">The <see cref="WriterTestExpectedResults"/> instance to copy.</param>
        public WriterTestExpectedResults(WriterTestExpectedResults other)
        {
            ExceptionUtilities.CheckArgumentNotNull(other, "other");

            this.settings = other.settings;
            this.ExpectedContentType = other.ExpectedContentType;
            this.ExpectedException = other.ExpectedException;
            this.ExpectedException2 = other.ExpectedException2;
            this.ExpectedODataErrorException = other.ExpectedODataErrorException;
            this.ExpectedODataExceptionMessage = other.ExpectedODataExceptionMessage;
        }
        /// <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)
            {
                throw new NotImplementedException();
            }
            else
            {
                return(expectedResult);
            }
        }
        /// <summary>
        /// Creates the expected result callback by computing the expected Xml and JSON responses
        /// based on the expected results for the items in a collection.
        /// </summary>
        /// <param name="collectionName">The name of the collection.</param>
        /// <param name="itemDescriptions">The items descriptions to created the expected result for.</param>
        /// <param name="errorOnly">A flag indicating whether we are writing only error items or not.</param>
        /// <param name="assert">The assertion handler to use.</param>
        /// <returns>
        /// A <see cref="PayloadWriterTestDescriptor.WriterTestExpectedResultCallback"/> that will be used 
        /// during test execution to validate the results.
        /// </returns>
        internal static PayloadWriterTestDescriptor.WriterTestExpectedResultCallback CreateExpectedResultCallback(
            string collectionName,
            CollectionWriterTestDescriptor.ItemDescription[] itemDescriptions,
            bool errorOnly,
            WriterTestExpectedResults.Settings settings)
        {
            CollectionWriterTestDescriptor.ItemDescription[] items = itemDescriptions;
            bool writeCollection = items != null;
            items = items ?? new CollectionWriterTestDescriptor.ItemDescription[0];

            string xmlResultTemplate, jsonLightResultTemplate;
            CreateResultTemplates(writeCollection, items, out xmlResultTemplate, out jsonLightResultTemplate);

            return (testConfiguration) =>
                {
                    if (testConfiguration.Format == ODataFormat.Atom)
                    {
                        return new AtomWriterTestExpectedResults(settings) 
                        {
                            Xml = PopulateXmlResultTemplate(collectionName, xmlResultTemplate),
                            FragmentExtractor = (result) => NormalizeNamespacePrefixes(result),
                        };
                    }
                    else if (testConfiguration.Format == ODataFormat.Json)
                    {
                        return new JsonWriterTestExpectedResults(settings)
                        {
                            Json = jsonLightResultTemplate,
                            FragmentExtractor = (result) => errorOnly
                                ? result.Object().PropertyValue(JsonLightConstants.ODataErrorPropertyName)
                                : result.Object().PropertyValue("value"),
                        };
                    }
                    else
                    {
                        throw new NotSupportedException("Unsupported format " + testConfiguration.Format.ToString() + " found.");
                    }
                };
        }
        /// <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;
            }
        }
        private static PayloadWriterTestDescriptor.WriterTestExpectedResultCallback CreateErrorResultCallback(
            ExpectedException expectedException,
            ODataFormat onlyForFormat,
            WriterTestExpectedResults.Settings expectedResultSettings)
        {
            return testConfiguration =>
            {
                if (onlyForFormat != null && testConfiguration.Format != onlyForFormat)
                {
                    return null;
                }

                return new WriterTestExpectedResults(expectedResultSettings)
                {
                    // Replace the expected exception for requests where an ODataException is expected
                    ExpectedException2 = 
                        testConfiguration.IsRequest && (expectedException != null && expectedException.ExpectedExceptionType.Equals(typeof(ODataException)))
                        ? ODataExpectedExceptions.ODataException("ODataMessageWriter_ServiceDocumentInRequest")
                        : expectedException,
                };
            };
        }
 /// <summary>
 /// Creates the expected result callback by setting the expected exception message(s).
 /// </summary>
 /// <param name="collectionName">The name of the collection.</param>
 /// <param name="expectedExceptionFunc">A func to compute the expected exception from the writer test configuration.</param>
 /// <param name="settings">The settings to use.</param>
 /// <returns>
 /// A <see cref="PayloadWriterTestDescriptor.WriterTestExpectedResultCallback"/> that will be used 
 /// during test execution to validate the results.
 /// </returns>
 internal static PayloadWriterTestDescriptor.WriterTestExpectedResultCallback CreateExpectedErrorResultCallback(
     string collectionName,
     Func<WriterTestConfiguration, ExpectedException> expectedExceptionFunc,
     WriterTestExpectedResults.Settings settings)
 {
     return (testConfiguration) =>
     {
         if (testConfiguration.Format == ODataFormat.Atom)
         {
             return new AtomWriterTestExpectedResults(settings)
             {
                 FragmentExtractor = (result) => NormalizeNamespacePrefixes(result),
                 ExpectedException2 = expectedExceptionFunc(testConfiguration)
             };
         }
         else if (testConfiguration.Format == ODataFormat.Json)
         {
             return new JsonWriterTestExpectedResults(settings)
             {
                 FragmentExtractor = (result) => JsonUtils.UnwrapTopLevelValue(testConfiguration, result),
                 ExpectedException2 = expectedExceptionFunc(testConfiguration)
             };
         }
         else
         {
             throw new NotSupportedException("Unsupported format " + testConfiguration.Format.ToString() + " found.");
         }
     };
 }
 private static PayloadWriterTestDescriptor.WriterTestExpectedResultCallback CreateErrorResultCallback(
     string expectedErrorMessage, 
     object value,
     WriterTestExpectedResults.Settings expectedResultSettings)
 {
     return testConfiguration =>
         {
             return new WriterTestExpectedResults(expectedResultSettings)
             {
                 ExpectedODataExceptionMessage = testConfiguration.Format != null
                     ? (value == null ? expectedErrorMessage : "A default MIME type could not be found for the requested payload in format 'Atom'.")
                     : expectedErrorMessage
             };
         };
 }
        private static PayloadWriterTestDescriptor.WriterTestExpectedResultCallback CreateExpectedResultCallback(
            InvocationAndOperationDescriptor[] invocationsAndOperationDescriptors, 
            Dictionary<string, string> expectedHeaders,
            WriterTestExpectedResults.Settings settings)
        {
            return (testConfiguration) =>
            {
                if (testConfiguration.Format != null && testConfiguration.Format != ODataFormat.Batch)
                {
                    throw new NotSupportedException("Unsupported format " + testConfiguration.Format.ToString() + " for writing batch messages found.");
                }

                return new BatchWriterTestExpectedResults(settings)
                {
                    ExpectedContentType = "multipart/mixed",
                    InvocationsAndOperationDescriptors = invocationsAndOperationDescriptors,
                    ExpectedHeaders = expectedHeaders,
                };
            };
        }
        private static PayloadWriterTestDescriptor.WriterTestExpectedResultCallback CreateExpectedErrorResultCallback(
            string expectedExceptionMessage,
            Exception exception,
            ExpectedException expectedException,
            WriterTestExpectedResults.Settings settings)
        {
            return (testConfiguration) =>
            {
                string errorMessage = expectedExceptionMessage;
                Exception error = exception;

                return new BatchWriterTestExpectedResults(settings)
                {
                    ExpectedODataExceptionMessage = errorMessage,
                    ExpectedException = error,
                    ExpectedException2 = expectedException,
                };
            };
        }