Esempio n. 1
0
        private PayloadWriterTestDescriptor <ODataItem> CreateDefaultFeedWithAtomMetadataDescriptor(bool withModel)
        {
            EdmModel model = null;

            if (withModel)
            {
                model = new EdmModel();
            }

            ODataResourceSet resourceCollection = ObjectModelUtils.CreateDefaultFeed("CustomersSet", "CustomerType", model);

            EdmEntitySet  customerSet  = null;
            EdmEntityType customerType = null;

            if (model != null)
            {
                var container = model.FindEntityContainer("DefaultContainer");
                customerSet  = container.FindEntitySet("CustomersSet") as EdmEntitySet;
                customerType = model.FindType("TestModel.CustomerType") as EdmEntityType;
            }

            return(new PayloadWriterTestDescriptor <ODataItem>(
                       this.Settings,
                       ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(),
                       (testConfiguration) =>
            {
                if (testConfiguration.Format == ODataFormat.Json)
                {
                    return new JsonWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
                    {
                        Json = string.Join("$(NL)",
                                           "{",
                                           "\"" + JsonLightConstants.ODataPropertyAnnotationSeparator + JsonLightConstants.ODataContextAnnotationName + "\":\"http://odata.org/test/$metadata#CustomersSet\",\"value\":[",
                                           string.Empty,
                                           "]",
                                           "}"),
                        FragmentExtractor = (result) => result.RemoveAllAnnotations(true)
                    };
                }
                else
                {
                    string formatName = testConfiguration.Format == null ? "null" : testConfiguration.Format.GetType().Name;
                    throw new NotSupportedException("Invalid format detected: " + formatName);
                }
            })
            {
                // JSON Light does not support writing without model
                SkipTestConfiguration = tc => model == null && tc.Format == ODataFormat.Json,
                Model = model,
                PayloadEdmElementContainer = customerSet,
                PayloadEdmElementType = customerType,
            });
        }
Esempio n. 2
0
        public void GeneratorMetadataWriterTest()
        {
            string testName    = "Test generator";
            string testUri     = "http://odata.org/generator";
            string testVersion = "3.0";

            var testCases = new[]
            {
                new
                {
                    Generator = new AtomGeneratorMetadata()
                    {
                        Name    = testName,
                        Uri     = new Uri(testUri),
                        Version = testVersion
                    },
                    Xml = @"<generator uri=""" + testUri + @""" version=""" + testVersion + @""" xmlns=""" + TestAtomConstants.AtomNamespace + @""">" + testName + @"</generator>",
                    ExpectedException = (string)null
                },
                new
                {
                    Generator = new AtomGeneratorMetadata()
                    {
                        Name    = testName,
                        Uri     = null,
                        Version = null
                    },
                    Xml = @"<generator xmlns=""" + TestAtomConstants.AtomNamespace + @""">" + testName + @"</generator>",
                    ExpectedException = (string)null
                },
                new
                {
                    Generator = new AtomGeneratorMetadata()
                    {
                        Name    = string.Empty,
                        Uri     = null,
                        Version = null
                    },
                    Xml = @"<generator xmlns=""" + TestAtomConstants.AtomNamespace + @"""></generator>",
                    ExpectedException = (string)null
                },
                new
                {
                    Generator = new AtomGeneratorMetadata()
                    {
                        Name    = null,
                        Uri     = null,
                        Version = null
                    },
                    Xml = @"<generator xmlns=""" + TestAtomConstants.AtomNamespace + @""" />",
                    ExpectedException = (string)null
                },
                new
                {
                    Generator = new AtomGeneratorMetadata()
                    {
                        Name    = "\n\t ",
                        Uri     = null,
                        Version = null
                    },
                    Xml = @"<generator xml:space=""preserve"" xmlns=""" + TestAtomConstants.AtomNamespace + "\">\n\t </generator>",
                    ExpectedException = (string)null
                },
            };

            Func <XElement, XElement> fragmentExtractor = (e) => e.Element(TestAtomConstants.AtomXNamespace + TestAtomConstants.AtomGeneratorElementName);

            // Convert test cases to test descriptions
            var testDescriptors = testCases.Select(testCase =>
            {
                ODataFeed feed            = ObjectModelUtils.CreateDefaultFeedWithAtomMetadata();
                AtomFeedMetadata metadata = feed.Atom();
                this.Assert.IsNotNull(metadata, "Expected default feed metadata on a default feed.");
                metadata.Generator = testCase.Generator;
                return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, feed, testConfiguration =>
                                                                   new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
                {
                    Xml = testCase.Xml, ExpectedODataExceptionMessage = testCase.ExpectedException, FragmentExtractor = fragmentExtractor, PreserveWhitespace = true
                }));
            });

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors.PayloadCases(WriterPayloads.FeedPayloads),
                this.WriterTestConfigurationProvider.AtomFormatConfigurations,
                (testDescriptor, testConfiguration) =>
            {
                testConfiguration = testConfiguration.Clone();
                testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri);

                TestWriterUtils.WriteAndVerifyODataPayload(testDescriptor, testConfiguration, this.Assert, this.Logger);
            });
        }
        private void CreateTestDescriptorsAndRunTests(FeedMetadataTestCase[] testCases, Func <PayloadWriterTestDescriptor <ODataItem>, IEnumerable <PayloadWriterTestDescriptor <ODataItem> > > payloads)
        {
            // Convert test cases to test descriptions
            IEnumerable <Func <ODataFeed> > feedCreators = new Func <ODataFeed>[] { () => ObjectModelUtils.CreateDefaultFeed(), () => ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(), };
            var testDescriptors = testCases.SelectMany(testCase => feedCreators.Select(feedCreator =>
            {
                ODataFeed feed            = feedCreator();
                AtomFeedMetadata metadata = feed.Atom();
                this.Assert.IsNotNull(metadata, "Expected default feed metadata on a default feed.");
                testCase.CustomizeMetadata(metadata);
                ODataItem[] payloadItems = testCase.CustomizePayload == null ? new ODataItem[] { feed } : testCase.CustomizePayload(feed);
                return(new PayloadWriterTestDescriptor <ODataItem>(this.Settings, payloadItems, testConfiguration =>
                {
                    if (testCase.SkipTestConfiguration != null && testCase.SkipTestConfiguration(testConfiguration))
                    {
                        return null;
                    }
                    return new AtomWriterTestExpectedResults(this.Settings.ExpectedResultSettings)
                    {
                        Xml = testCase.Xml, FragmentExtractor = testCase.Extractor, ExpectedException2 = testCase.ExpectedException
                    };
                }));
            }));

            this.CombinatorialEngineProvider.RunCombinations(testDescriptors.PayloadCases(WriterPayloads.TopLevelValuePayload), this.WriterTestConfigurationProvider.AtomFormatConfigurations, (testDescriptor, testConfiguration) =>
            {
                testConfiguration = testConfiguration.Clone();
                testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri);

                TestWriterUtils.WriteAndVerifyODataPayload(testDescriptor, testConfiguration, this.Assert, this.Logger);
            });
        }
Esempio n. 4
0
        public void FeedAndEntryUpdatedTimeTests()
        {
            ODataFeed defaultFeedWithEmptyMetadata = ObjectModelUtils.CreateDefaultFeed();

            defaultFeedWithEmptyMetadata.SetAnnotation <AtomFeedMetadata>(new AtomFeedMetadata());
            ODataEntry defaultEntryWithEmptyMetadata = ObjectModelUtils.CreateDefaultEntry();

            defaultEntryWithEmptyMetadata.SetAnnotation <AtomEntryMetadata>(new AtomEntryMetadata());

            IEnumerable <PayloadWriterTestDescriptor <ODataItem> > testPayloads =
                new[]
            {
                new PayloadWriterTestDescriptor <ODataItem>(this.Settings, ObjectModelUtils.CreateDefaultFeed()),
                new PayloadWriterTestDescriptor <ODataItem>(this.Settings, defaultFeedWithEmptyMetadata),
                new PayloadWriterTestDescriptor <ODataItem>(this.Settings, ObjectModelUtils.CreateDefaultFeedWithAtomMetadata()),
            }.PayloadCases(WriterPayloads.FeedPayloads)
            .Concat((new[]
            {
                new PayloadWriterTestDescriptor <ODataItem>(this.Settings, ObjectModelUtils.CreateDefaultEntry()),
                new PayloadWriterTestDescriptor <ODataItem>(this.Settings, defaultEntryWithEmptyMetadata),
                new PayloadWriterTestDescriptor <ODataItem>(this.Settings, ObjectModelUtils.CreateDefaultEntryWithAtomMetadata()),
            }.PayloadCases(WriterPayloads.EntryPayloads)));

            this.CombinatorialEngineProvider.RunCombinations(
                testPayloads,
                this.WriterTestConfigurationProvider.AtomFormatConfigurationsWithIndent.Where(tc => !tc.IsRequest),
                (testPayload, testConfiguration) =>
            {
                testConfiguration = testConfiguration.Clone();
                testConfiguration.MessageWriterSettings.SetServiceDocumentUri(ServiceDocumentUri);

                string lastUpdatedTimeStr = null;
                using (var memoryStream = new MemoryStream())
                {
                    using (var testMemoryStream = TestWriterUtils.CreateTestStream(testConfiguration, memoryStream, ignoreDispose: true))
                    {
                        bool feedWriter         = testPayload.PayloadItems[0] is ODataFeed;
                        TestMessage testMessage = null;
                        Exception exception     = TestExceptionUtils.RunCatching(() =>
                        {
                            using (var messageWriter = TestWriterUtils.CreateMessageWriter(testMemoryStream, testConfiguration, this.Assert, out testMessage, null, testPayload.Model))
                            {
                                ODataWriter writer = messageWriter.CreateODataWriter(feedWriter);
                                TestWriterUtils.WritePayload(messageWriter, writer, true, testPayload.PayloadItems, testPayload.ThrowUserExceptionAt);
                            }
                        });
                        this.Assert.IsNull(exception, "Received exception but expected none.");
                    }

                    memoryStream.Position = 0;
                    XElement result       = XElement.Load(memoryStream);
                    foreach (XElement updated in result.Descendants(((XNamespace)TestAtomConstants.AtomNamespace) + "updated"))
                    {
                        if (updated.Value != ObjectModelUtils.DefaultFeedUpdated && updated.Value != ObjectModelUtils.DefaultEntryUpdated)
                        {
                            if (lastUpdatedTimeStr == null)
                            {
                                lastUpdatedTimeStr = updated.Value;
                            }
                            else
                            {
                                this.Assert.AreEqual(lastUpdatedTimeStr, updated.Value, "<atom:updated> should contain the same value.");
                            }
                        }
                    }
                }
            });
        }
Esempio n. 5
0
        public void WriteEntityReferenceLinkAfterFeedJsonLightErrorTest()
        {
            EdmModel model = new EdmModel();

            var container = new EdmEntityContainer("TestModel", "DefaultContainer");

            model.AddElement(container);

            var customerType = new EdmEntityType("TestModel", "Customer");
            var orderType    = new EdmEntityType("TestModel", "Order");
            var orderNavProp = customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "Orders", Target = orderType, TargetMultiplicity = EdmMultiplicity.Many
            });
            var bestFriendNavProp = customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "BestFriend", Target = customerType, TargetMultiplicity = EdmMultiplicity.One
            });
            var customerSet = container.AddEntitySet("Customers", customerType);
            var orderSet    = container.AddEntitySet("Order", orderType);

            customerSet.AddNavigationTarget(orderNavProp, orderSet);
            customerSet.AddNavigationTarget(bestFriendNavProp, customerSet);
            model.AddElement(customerType);
            model.AddElement(orderType);

            ODataResource expandedOrderInstance = ObjectModelUtils.CreateDefaultEntryWithAtomMetadata();

            expandedOrderInstance.TypeName = "TestModel.Order";

            var testCases = new WriterNavigationLinkTests.NavigationLinkTestCase[]
            {
                // Entity reference link after empty feed in collection navigation link
                new WriterNavigationLinkTests.NavigationLinkTestCase
                {
                    Items = new ODataItem[] {
                        new ODataNestedResourceInfo()
                        {
                            IsCollection = true, Name = "Orders", Url = new Uri("http://odata.org/nav")
                        },
                        ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(),
                        null,
                        new ODataEntityReferenceLink()
                        {
                            Url = new Uri("http://odata.org/singleton")
                        },
                    },
                    ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightWriter_EntityReferenceLinkAfterResourceSetInRequest")
                },
                // Entity reference link after non-empty feed in collection navigation link
                new WriterNavigationLinkTests.NavigationLinkTestCase
                {
                    Items = new ODataItem[] {
                        new ODataNestedResourceInfo()
                        {
                            IsCollection = true, Name = "Orders", Url = new Uri("http://odata.org/nav")
                        },
                        ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(),
                        expandedOrderInstance,
                        null,
                        null,
                        new ODataEntityReferenceLink()
                        {
                            Url = new Uri("http://odata.org/singleton")
                        },
                    },
                    ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightWriter_EntityReferenceLinkAfterResourceSetInRequest")
                },
                // Entity reference link before and after empty feed in collection navigation link
                new WriterNavigationLinkTests.NavigationLinkTestCase
                {
                    Items = new ODataItem[] {
                        new ODataNestedResourceInfo()
                        {
                            IsCollection = true, Name = "Orders", Url = new Uri("http://odata.org/nav")
                        },
                        new ODataEntityReferenceLink()
                        {
                            Url = new Uri("http://odata.org/singleton")
                        },
                        ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(),
                        null,
                        new ODataEntityReferenceLink()
                        {
                            Url = new Uri("http://odata.org/singleton")
                        },
                    },
                    ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightWriter_EntityReferenceLinkAfterResourceSetInRequest")
                },
                // Entity reference link after two empty feeds in collection navigation link
                new WriterNavigationLinkTests.NavigationLinkTestCase
                {
                    Items = new ODataItem[] {
                        new ODataNestedResourceInfo()
                        {
                            IsCollection = true, Name = "Orders", Url = new Uri("http://odata.org/nav")
                        },
                        new ODataEntityReferenceLink()
                        {
                            Url = new Uri("http://odata.org/singleton")
                        },
                        ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(),
                        null,
                        ObjectModelUtils.CreateDefaultFeedWithAtomMetadata(),
                        null,
                        new ODataEntityReferenceLink()
                        {
                            Url = new Uri("http://odata.org/singleton")
                        },
                    },
                    ExpectedException = ODataExpectedExceptions.ODataException("ODataJsonLightWriter_EntityReferenceLinkAfterResourceSetInRequest")
                },
            };

            IEnumerable <PayloadWriterTestDescriptor <ODataItem> > testDescriptors =
                testCases.Select(testCase => testCase.ToEdmTestDescriptor(this.Settings, model, customerSet, customerType));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                this.WriterTestConfigurationProvider.JsonLightFormatConfigurations.Where(tc => tc.IsRequest),
                (testDescriptor, testConfiguration) =>
            {
                TestWriterUtils.WriteAndVerifyODataEdmPayload(testDescriptor, testConfiguration, this.Assert, this.Logger);
            });
        }