Пример #1
0
        public void TopLevelAtomErrorTest()
        {
            #region Extra attributes on the error elements (and sub-elements)
            var extraAttributePayloads = new[]
            {
                new { PayloadElement = PayloadBuilder.Error(), Template = "<m:error {0} />", },
                new { PayloadElement = PayloadBuilder.Error().Code("ErrorCode"), Template = "<m:error><m:code {0}>ErrorCode</m:code></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().Code("ErrorCode").Message("Message"), Template = "<m:error><m:code>ErrorCode</m:code><m:message {0}>Message</m:message></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError()), Template = "<m:error><m:innererror {0}></m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().Message("InnerMessage")), Template = "<m:error><m:innererror><m:message {0}>InnerMessage</m:message></m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().TypeName("TypeName")), Template = "<m:error><m:innererror><m:type {0}>TypeName</m:type></m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().StackTrace("StackTrace")), Template = "<m:error><m:innererror><m:stacktrace {0}>StackTrace</m:stacktrace></m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().InnerError(new ODataInternalExceptionPayload())), Template = "<m:error><m:innererror><m:internalexception {0}></m:internalexception></m:innererror></m:error>" },
            };

            string[] attributes = new string[]
            {
                "foo='bar'",
                "m:foo='bar'",
                "foo=''",
                "lang='invalid'",
            };

            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = extraAttributePayloads.SelectMany(extraAttributePayload =>
                                                                                                          attributes.Select(attribute =>
            {
                var payloadElement = extraAttributePayload.PayloadElement.DeepCopy();
                string xmlRep      = string.Format(CultureInfo.InvariantCulture, extraAttributePayload.Template, attribute);
                payloadElement     = payloadElement.XmlRepresentation(xmlRep);

                return(new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = payloadElement
                });
            }));
            #endregion Extra attributes on the error elements (and sub-elements)

            #region Extra padding in the error elements' content
            var extraPaddingPayloads = new[]
            {
                new { PayloadElement = PayloadBuilder.Error().Code("ErrorCode"), Template = "<m:error>{0}<m:code>ErrorCode</m:code></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().Code("ErrorCode"), Template = "<m:error><m:code>ErrorCode</m:code>{0}</m:error>" },
                new { PayloadElement = PayloadBuilder.Error().Code("ErrorCode").Message("Message"), Template = "<m:error><m:code>ErrorCode</m:code>{0}<m:message>Message</m:message></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().Code("ErrorCode").Message("Message").InnerError(PayloadBuilder.InnerError()), Template = "<m:error><m:code>ErrorCode</m:code><m:message>Message</m:message>{0}<m:innererror></m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().Code("ErrorCode").Message("Message").InnerError(PayloadBuilder.InnerError()), Template = "<m:error><m:code>ErrorCode</m:code><m:message>Message</m:message><m:innererror>{0}</m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().Message("InnerMessage")), Template = "<m:error><m:innererror>{0}<m:message>InnerMessage</m:message></m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().Message("InnerMessage")), Template = "<m:error><m:innererror><m:message>InnerMessage</m:message>{0}</m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().TypeName("TypeName")), Template = "<m:error><m:innererror>{0}<m:type>TypeName</m:type></m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().TypeName("TypeName")), Template = "<m:error><m:innererror><m:type>TypeName</m:type>{0}</m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().TypeName("TypeName").StackTrace("StackTrace")), Template = "<m:error><m:innererror><m:type>TypeName</m:type>{0}<m:stacktrace>StackTrace</m:stacktrace></m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().TypeName("TypeName").StackTrace("StackTrace")), Template = "<m:error><m:innererror><m:type>TypeName</m:type><m:stacktrace>StackTrace</m:stacktrace>{0}</m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().InnerError(new ODataInternalExceptionPayload())), Template = "<m:error><m:innererror>{0}<m:internalexception></m:internalexception></m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().InnerError(new ODataInternalExceptionPayload())), Template = "<m:error><m:innererror><m:internalexception>{0}</m:internalexception></m:innererror></m:error>" },
                new { PayloadElement = PayloadBuilder.Error().InnerError(PayloadBuilder.InnerError().InnerError(new ODataInternalExceptionPayload())), Template = "<m:error><m:innererror><m:internalexception></m:internalexception>{0}</m:innererror></m:error>" },
            };

            string[] xmlPaddingToIgnore = new string[]
            {
                string.Empty,                                           // Nothing
                "  \r\n\t",                                             // Whitespace only
                "<!--s--> <?value?>",                                   // Insignificant nodes
                "some text <![CDATA[cdata]]>",                          // Significant nodes to be ignored
                "<foo xmlns=''/>",                                      // Element in no namespace
                "<c:foo xmlns:c='uri' attr='1'><c:child/>text</c:foo>", // Element in custom namespace
                "<m:properties/>",                                      // Element in metadata namespace (should be ignored as well)
                "<entry/>",                                             // Element in atom namespace (should also be ignored)
            };

            IEnumerable <PayloadReaderTestDescriptor> extraPaddingTestDescriptors = extraPaddingPayloads.SelectMany(extraPaddingPayload =>
                                                                                                                    xmlPaddingToIgnore.Select(xmlPadding =>
            {
                var payloadElement = extraPaddingPayload.PayloadElement.DeepCopy();
                string xmlRep      = string.Format(CultureInfo.InvariantCulture, extraPaddingPayload.Template, xmlPadding);
                payloadElement     = payloadElement.XmlRepresentation(xmlRep);

                return(new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = payloadElement
                });
            }));

            testDescriptors = testDescriptors.Concat(extraPaddingTestDescriptors);
            #endregion Extra padding in the error elements' content

            #region Extra elements in various interesting places of an error payload
            ODataErrorPayload errorPayload = PayloadBuilder.Error()
                                             .Code("ErrorCode")
                                             .Message("Message")
                                             .InnerError(
                PayloadBuilder.InnerError()
                .Message("InnerMessage")
                .TypeName("TypeName")
                .StackTrace("StackTrace")
                .InnerError(
                    PayloadBuilder.InnerError()
                    .Message("InnerInnerMessage")));

            XElement xmlRepresentation = new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataErrorElementName,
                                                      new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataErrorCodeElementName, "ErrorCode"),
                                                      new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataErrorMessageElementName, "Message"),
                                                      new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorElementName,
                                                                   new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorMessageElementName, "InnerMessage"),
                                                                   new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorTypeElementName, "TypeName"),
                                                                   new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorStackTraceElementName, "StackTrace"),
                                                                   new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorInnerErrorElementName,
                                                                                new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorMessageElementName, "InnerInnerMessage"))));

            XElement[] extraElements = new XElement[]
            {
                new XElement(XName.Get("foo"), "bar"),
                new XElement(XName.Get("foo"), new XElement(XName.Get("bar"))),
                new XElement(XName.Get("foo"), new XAttribute(XName.Get("bar"), "attribute-value")),
                new XElement(TestAtomConstants.ODataMetadataXNamespace + "foo", "bar"),
                new XElement(TestAtomConstants.ODataMetadataXNamespace + "foo", new XElement(TestAtomConstants.ODataMetadataXNamespace + "bar")),
                new XElement(XName.Get("foo"), new XAttribute(TestAtomConstants.ODataMetadataXNamespace + "bar", "attribute-value")),
            };

            IEnumerable <PayloadReaderTestDescriptor> extraElementTestDescriptors = extraElements.SelectMany(extraElement =>
            {
                return(InjectElement(extraElement, xmlRepresentation).Select(errorWithInjectedElement =>
                                                                             new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = errorPayload.DeepCopy().XmlRepresentation(errorWithInjectedElement)
                }
                                                                             ));
            });
            testDescriptors = testDescriptors.Concat(extraElementTestDescriptors);
            #endregion Extra elements in various interesting places of an error payload

            #region Various payload orders for an error element
            errorPayload = PayloadBuilder.Error()
                           .Code("ErrorCode")
                           .Message("Message")
                           .InnerError(
                PayloadBuilder.InnerError()
                .Message("InnerMessage")
                .TypeName("TypeName")
                .StackTrace("StackTrace")
                .InnerError(
                    PayloadBuilder.InnerError()
                    .Message("InnerInnerMessage")));

            xmlRepresentation = new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataErrorElementName,
                                             new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataErrorCodeElementName, "ErrorCode"),
                                             new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataErrorMessageElementName, "Message"),
                                             new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorElementName,
                                                          new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorMessageElementName, "InnerMessage"),
                                                          new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorTypeElementName, "TypeName"),
                                                          new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorStackTraceElementName, "StackTrace"),
                                                          new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorInnerErrorElementName,
                                                                       new XElement(TestAtomConstants.ODataMetadataXNamespace + TestAtomConstants.ODataInnerErrorMessageElementName, "InnerInnerMessage"))));

            IEnumerable <PayloadReaderTestDescriptor> payloadOrderTestDescriptors = GeneratePayloadOrders(xmlRepresentation).Select(xmlRep =>
                                                                                                                                    new PayloadReaderTestDescriptor(this.Settings)
            {
                PayloadElement = errorPayload.DeepCopy().XmlRepresentation(xmlRep)
            }
                                                                                                                                    );
            testDescriptors = testDescriptors.Concat(payloadOrderTestDescriptors);
            #endregion Various payload orders for an error element

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.ReaderTestConfigurationProvider.AtomFormatConfigurations.Where(tc => !tc.IsRequest),
                (testDescriptor, testConfiguration) =>
            {
                testDescriptor.RunTest(testConfiguration);
            });
        }