Ejemplo n.º 1
0
        /// <summary>
        /// Helper method to run a single iteration of the URI reading tests in a specified configuration.
        /// </summary>
        /// <typeparam name="T">The type of the payload to read.</typeparam>
        /// <param name="payloadElement">The payload to read.</param>
        /// <param name="setExpectedUriAction">An action to set the URI in question on the payload.</param>
        /// <param name="model">The metadata model.</param>
        /// <param name="baseUriValue">The base URI value for the current iteration.</param>
        /// <param name="testConfiguration">The reader test configuration.</param>
        private void RunNullUriReadingTest <T>(
            T payloadElement,
            NullUriValueTestCase <T> testCase,
            IEdmModel model,
            BaseUriValue baseUriValue,
            ReaderTestConfiguration testConfiguration) where T : ODataPayloadElement
        {
            this.Assert.IsNotNull(testCase.SetNullUriAction, "SetNullUriAction must not be null.");
            this.Assert.IsNull(testConfiguration.MessageReaderSettings.BaseUri, "No base URI expected on reader settings.");

            ExpectedException expectedException = null;
            Uri settingsBaseUri = baseUriValue.ReaderSettingBaseUri;

            // Set the base URI on the message reader settings if specified
            if (settingsBaseUri != null)
            {
                testConfiguration = new ReaderTestConfiguration(testConfiguration);
                testConfiguration.MessageReaderSettings.BaseUri = settingsBaseUri;
            }

            // Create the payload element
            T clonedPayloadElement = payloadElement.DeepCopy();

            // modify the payload to represent the 'null' URI
            testCase.SetNullUriAction(clonedPayloadElement, /*uri*/ null, testConfiguration);

            var setExpectedUriAction = testCase.SetExpectedUriAction ?? testCase.SetNullUriAction;

            if (testConfiguration.Format == ODataFormat.Atom)
            {
                XElement xmlRepresentation = this.PayloadElementToXmlConverter.ConvertToXml(clonedPayloadElement);

                // add an xml:base attribute if specified
                Uri xmlBaseUri = baseUriValue.XmlBaseUri;
                if (xmlBaseUri != null)
                {
                    xmlRepresentation.Add(new XAttribute(XNamespace.Xml.GetName("base"), xmlBaseUri.OriginalString));
                }

                clonedPayloadElement.XmlRepresentation(xmlRepresentation);

                // First check all error conditions that are independent of a potential base URI
                if (payloadElement.ElementType == ODataPayloadElementType.ServiceDocumentInstance)
                {
                    // Resource collections must have a URL
                    expectedException = ODataExpectedExceptions.ODataException("ValidationUtils_ServiceDocumentElementUrlMustNotBeNull");
                }
                else
                {
                    if (xmlBaseUri != null)
                    {
                        setExpectedUriAction(clonedPayloadElement, xmlBaseUri, testConfiguration);
                    }
                    else if (settingsBaseUri != null)
                    {
                        setExpectedUriAction(clonedPayloadElement, settingsBaseUri, testConfiguration);
                    }
                    else
                    {
                        // fail for relative URIs without base URI
                        if (payloadElement.ElementType == ODataPayloadElementType.DeferredLink ||
                            payloadElement.ElementType == ODataPayloadElementType.LinkCollection)
                        {
                            expectedException = ODataExpectedExceptions.ODataException("ODataAtomDeserializer_RelativeUriUsedWithoutBaseUriSpecified", string.Empty);
                        }
                    }

                    // NOTE: ATOM properly detects MLEs even without read or edit link
                }
            }
            else
            {
                throw new NotSupportedException("Unsupported configuration format: " + testConfiguration.Format.ToString());
            }

            PayloadReaderTestDescriptor testDescriptor = new PayloadReaderTestDescriptor(this.Settings)
            {
                PayloadElement        = clonedPayloadElement,
                PayloadEdmModel       = model,
                ExpectedException     = expectedException,
                SkipTestConfiguration = tc => ODataPayloadElementConfigurationValidator.GetSkipTestConfiguration(payloadElement, ODataPayloadElementConfigurationValidator.AllValidators)(tc),
            };

            testDescriptor.RunTest(testConfiguration);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Helper method to run a single iteration of the URI reading tests in a specified configuration.
        /// </summary>
        /// <typeparam name="T">The type of the payload to read.</typeparam>
        /// <param name="payloadElement">The payload to read.</param>
        /// <param name="setExpectedUriAction">An action to set the URI in question on the payload.</param>
        /// <param name="model">The metadata model.</param>
        /// <param name="payloadUri">The payload URI for the current iteration.</param>
        /// <param name="baseUriValue">The base URI value for the current iteration.</param>
        /// <param name="resolver">The resolver to use.</param>
        /// <param name="testConfiguration">The reader test configuration.</param>
        private void RunBaseUriReadingTest <T>(
            T payloadElement,
            Action <T, Uri, ReaderTestConfiguration> setExpectedUriAction,
            IEdmModel model,
            Uri payloadUri,
            BaseUriValue baseUriValue,
            KeyValuePair <Func <Uri, Uri, Uri, Uri>, Uri> resolver,
            ReaderTestConfiguration testConfiguration,
            bool runInBatch = false) where T : ODataPayloadElement
        {
            this.Assert.IsNull(testConfiguration.MessageReaderSettings.BaseUri, "No base URI expected on reader settings.");
            ExpectedException expectedException = null;

            Uri settingsBaseUri = baseUriValue.ReaderSettingBaseUri;

            // Set the base URI on the message reader settings if specified
            if (settingsBaseUri != null)
            {
                testConfiguration = new ReaderTestConfiguration(testConfiguration);
                testConfiguration.MessageReaderSettings.BaseUri = settingsBaseUri;
            }

            // Create the payload element
            T clonedPayloadElement = payloadElement.DeepCopy();

            setExpectedUriAction(clonedPayloadElement, payloadUri, testConfiguration);

            if (testConfiguration.Format == ODataFormat.Atom)
            {
                XElement xmlRepresentation = this.PayloadElementToXmlConverter.ConvertToXml(clonedPayloadElement);

                // add an xml:base attribute if specified
                Uri xmlBaseUri = baseUriValue.XmlBaseUri;
                if (xmlBaseUri != null)
                {
                    xmlRepresentation.Add(new XAttribute(XNamespace.Xml.GetName("base"), xmlBaseUri.OriginalString));
                }

                clonedPayloadElement.XmlRepresentation(xmlRepresentation);

                if (resolver.Value != null)
                {
                    setExpectedUriAction(clonedPayloadElement, resolver.Value, testConfiguration);
                }
                else
                {
                    // compute the expected URI value for ATOM
                    if (!payloadUri.IsAbsoluteUri)
                    {
                        if (xmlBaseUri != null)
                        {
                            setExpectedUriAction(clonedPayloadElement, new Uri(xmlBaseUri, payloadUri), testConfiguration);
                        }
                        else if (settingsBaseUri != null)
                        {
                            setExpectedUriAction(clonedPayloadElement, new Uri(settingsBaseUri, payloadUri), testConfiguration);
                        }
                        else
                        {
                            // fail for relative URIs without base URI
                            expectedException = ODataExpectedExceptions.ODataException("ODataAtomDeserializer_RelativeUriUsedWithoutBaseUriSpecified", payloadUri.OriginalString);
                        }
                    }
                }
            }
            else
            {
                throw new NotSupportedException("Unsupported configuration format: " + testConfiguration.Format.ToString());
            }

            PayloadReaderTestDescriptor testDescriptor = new PayloadReaderTestDescriptor(resolver.Key == null ? this.Settings : this.NoValidatorSettings)
            {
                PayloadElement    = clonedPayloadElement,
                PayloadEdmModel   = model,
                ExpectedException = expectedException,
                UrlResolver       = resolver.Key == null ? null : new TestUrlResolver {
                    ResolutionCallback = (baseUri, realPayloadUri) => resolver.Key(payloadUri, baseUri, realPayloadUri)
                },
                SkipTestConfiguration = tc => ODataPayloadElementConfigurationValidator.GetSkipTestConfiguration(payloadElement, ODataPayloadElementConfigurationValidator.AllValidators)(tc),
            };

            if (runInBatch)
            {
                // TODO: Batch reader does not enter Exception state upon cross reference error in payload.
                // Once fixed allow the batch tests to run even for error cases.
                if (expectedException != null)
                {
                    return;
                }

                if (testConfiguration.IsRequest)
                {
                    testDescriptor = new PayloadReaderTestDescriptor(testDescriptor)
                    {
                        PayloadElement =
                            PayloadBuilder.BatchRequestPayload(
                                BatchUtils.GetRequestChangeset(
                                    new IMimePart[] { testDescriptor.PayloadDescriptor.InRequestOperation(
                                                          HttpVerb.Put,
                                                          new ODataUri(ODataUriBuilder.Root(new Uri("http://odata.org/service"))),
                                                          this.RequestManager,
                                                          TestMediaTypeUtils.GetDefaultContentType(testDescriptor.PayloadDescriptor.PayloadKind, testConfiguration.Format)) },
                                    this.RequestManager))
                            .AddAnnotation(new BatchBoundaryAnnotation("bb_request"))
                    };
                }
                else
                {
                    testDescriptor = new PayloadReaderTestDescriptor(testDescriptor)
                    {
                        PayloadElement =
                            PayloadBuilder.BatchResponsePayload(
                                testDescriptor.PayloadDescriptor.InResponseOperation(
                                    200,
                                    this.RequestManager,
                                    TestMediaTypeUtils.GetDefaultContentType(testDescriptor.PayloadDescriptor.PayloadKind, testConfiguration.Format)))
                            .AddAnnotation(new BatchBoundaryAnnotation("bb_response"))
                    };
                }

                testConfiguration = new ReaderTestConfiguration(null, testConfiguration.MessageReaderSettings, testConfiguration.IsRequest, testConfiguration.Synchronous, testConfiguration.Version);
            }

            testDescriptor.RunTest(testConfiguration);
        }