Example #1
0
        public void ResolverUriTest()
        {
            Uri inputUri          = new Uri("inputUri", UriKind.Relative);
            Uri resultRelativeUri = new Uri("resultRelativeUri", UriKind.Relative);
            Uri resultAbsoluteUri = new Uri("http://odata.org/absoluteresolve");

            var resolvers = new[]
            {
                // Resolver which always returns relative URL
                new
                {
                    Resolver = new Func <Uri, Uri, Uri>((baseUri, payloadUri) => {
                        if (payloadUri.OriginalString == inputUri.OriginalString)
                        {
                            return(resultRelativeUri);
                        }
                        else
                        {
                            return(null);
                        }
                    }),
                    ResultUri = resultRelativeUri
                },
                // Resolver which always returns absolute URL
                new
                {
                    Resolver = new Func <Uri, Uri, Uri>((baseUri, payloadUri) => {
                        if (payloadUri.OriginalString == inputUri.OriginalString)
                        {
                            return(resultAbsoluteUri);
                        }
                        else
                        {
                            return(null);
                        }
                    }),
                    ResultUri = resultAbsoluteUri
                }
            };

            var testDescriptors = uriTestCases.SelectMany(testCase => resolvers.Select(resolver =>
            {
                return(new
                {
                    TestCase = testCase,
                    Descriptor = new PayloadWriterTestDescriptor <ODataItem>(
                        this.Settings,
                        testCase.ItemFunc(inputUri),
                        CreateUriTestCaseExpectedResultCallback(/*baseUri*/ null, resolver.ResultUri, testCase))
                    {
                        UrlResolver = new TestUrlResolver()
                        {
                            ResolutionCallback = resolver.Resolver
                        }
                    }
                });
            }));

            // ToDo: Fix places where we've lost JsonVerbose coverage to add JsonLight
            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                new bool[] { false, true },
                this.WriterTestConfigurationProvider.ExplicitFormatConfigurations.Where(c => false),
                (testDescriptor, runInBatch, testConfiguration) =>
            {
                testConfiguration = testConfiguration.Clone();
                testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri);

                if ((!testConfiguration.IsRequest || !testDescriptor.TestCase.ResponseOnly) &&
                    (testConfiguration.Format == ODataFormat.Json && testDescriptor.TestCase.JsonExtractor != null))
                {
                    var td = testDescriptor.Descriptor.DeferredLinksToEntityReferenceLinksInRequest(testConfiguration);
                    if (!runInBatch)
                    {
                        TestWriterUtils.WriteAndVerifyODataPayload(td, testConfiguration, this.Assert, this.Logger);
                    }
                    else
                    {
                        testConfiguration = testConfiguration.Clone();
                        testConfiguration.MessageWriterSettings.EnableMessageStreamDisposal = true;
                        var batchDescriptor = new List <BatchWriterTestDescriptor.InvocationAndOperationDescriptor>();
                        if (testConfiguration.IsRequest)
                        {
                            batchDescriptor.Add(BatchWriterUtils.StartBatch());
                            batchDescriptor.Add(BatchWriterUtils.StartChangeSet());
                            batchDescriptor.Add(BatchWriterUtils.ChangeSetRequest(
                                                    "PUT",
                                                    new Uri("http://odata.org"),
                                                    null,
                                                    null,
                                                    new BatchWriterUtils.ODataPayload()
                            {
                                Items = td.PayloadItems.ToArray(),
                                WriterTestExpectedResults = td.ExpectedResultCallback(testConfiguration),
                                TestConfiguration         = testConfiguration
                            }));
                            batchDescriptor.Add(BatchWriterUtils.EndChangeSet());
                            batchDescriptor.Add(BatchWriterUtils.EndBatch());
                        }
                        else
                        {
                            batchDescriptor.Add(BatchWriterUtils.StartBatch());
                            batchDescriptor.Add(BatchWriterUtils.QueryOperationResponse(
                                                    200,
                                                    new BatchWriterUtils.ODataPayload()
                            {
                                Items = td.PayloadItems.ToArray(),
                                WriterTestExpectedResults = td.ExpectedResultCallback(testConfiguration),
                                TestConfiguration         = testConfiguration
                            }));
                            batchDescriptor.Add(BatchWriterUtils.EndBatch());
                        }

                        var batchTd = new BatchWriterTestDescriptor(
                            this.BatchSettings,
                            batchDescriptor.ToArray(),
                            (Dictionary <string, string>)null,
                            new Uri("http://odata.org/service"),
                            td.UrlResolver);

                        ODataMessageWriterSettings batchWriterSettings = testConfiguration.MessageWriterSettings.Clone();
                        batchWriterSettings.SetContentType(null);
                        WriterTestConfiguration batchTestConfiguration = new WriterTestConfiguration(
                            null,
                            batchWriterSettings,
                            testConfiguration.IsRequest,
                            testConfiguration.Synchronous);
                        BatchWriterUtils.WriteAndVerifyBatchPayload(batchTd, batchTestConfiguration, testConfiguration, this.Assert);
                    }
                }
            });
        }
        private static Func<string, string> GetComparerForWriterTestExpectedResults(BatchWriterUtils.ODataPayload odataPayload)
        {
            if (odataPayload.TestConfiguration.Format == ODataFormat.Atom)
            {
                return (observed) =>
                {
                    string error;
                    if (!TestWriterUtils.CompareAtomResults(
                        ((AtomWriterTestExpectedResults)odataPayload.WriterTestExpectedResults),
                        observed,
                        odataPayload.TestConfiguration,
                        out error))
                    {
                        return error;
                    }

                    return null;
                };
            }

            if (odataPayload.TestConfiguration.Format == ODataFormat.Json)
            {
                return (observed) =>
                {
                    string error;
                    if (!TestWriterUtils.CompareJsonResults(
                        ((JsonWriterTestExpectedResults)odataPayload.WriterTestExpectedResults),
                        observed,
                        odataPayload.TestConfiguration,
                        out error))
                    {
                        return error;
                    }

                    return null;
                };
            }

            throw new NotSupportedException("Only ATOM and JSON formats are supported for batch payloads.");
        }