public void ReadAssociationLinkTest()
        {
            IEdmModel model = TestModels.BuildTestModel();

            // TODO: add a payload with a relative association Uri

            IEnumerable<PayloadReaderTestDescriptor> testDescriptors = new PayloadReaderTestDescriptor[]
            {
                // Association link with nav. link
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder
                        .NavigationProperty("NavPropWithAssociationUri", "http://odata.org/NavProp", "http://odata.org/NavPropWithAssociationUri")
                        .IsCollection(true),
                    PayloadEdmModel = model
                },

                // No need to add expanded nav links since those will be generated for us by the payload generator below.
            }.SelectMany(td => this.PayloadGenerator.GenerateReaderPayloads(td));

            // Association links with nav. link
            IEnumerable<PayloadReaderTestDescriptor> associationLinkTestDescriptors = new PayloadReaderTestDescriptor[]
            {
                // Association links without a nav. link
                // Association link for a singleton nav. property.
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity("TestModel.CityType").PrimitiveProperty("Id", 1),
                    PayloadEdmModel = model
                },
                // Association link for a collection nav. property.
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity("TestModel.CityType").PrimitiveProperty("Id", 1),
                    PayloadEdmModel = model
                },
                // Association link which is not declared
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity("TestModel.CityType").PrimitiveProperty("Id", 1).Property(
                        PayloadBuilder.NavigationProperty("Nonexistant", null, "http://odata.org/CityHallLink").IsCollection(true)),
                    PayloadEdmModel = model,
                    ExpectedException = ODataExpectedExceptions.ODataException("ValidationUtils_PropertyDoesNotExistOnType", "Nonexistant", "TestModel.CityType")
                },
                // Association link which is not declared on an open type
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity("TestModel.CityOpenType").Property(
                        PayloadBuilder.NavigationProperty("Nonexistant", null, "http://odata.org/CityHallLink").IsCollection(true)),
                    PayloadEdmModel = model,
                    ExpectedResultCallback = 
                        (tc) => new PayloadReaderTestExpectedResult(this.Settings.ExpectedResultSettings)
                                {
                                    ExpectedException = 
                                        (tc.Format == ODataFormat.Json)
                                        ? ODataExpectedExceptions.ODataException("ODataJsonLightEntryAndFeedDeserializer_OpenPropertyWithoutValue", "Nonexistant")
                                        : ODataExpectedExceptions.ODataException("ValidationUtils_OpenNavigationProperty", "Nonexistant", "TestModel.CityOpenType"),
                                },
                },
                // Association link which is declared but of wrong kind
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity("TestModel.CityType").PrimitiveProperty("Id", 1).Property(
                        PayloadBuilder.NavigationProperty("Name", null, "http://odata.org/CityHallLink").IsCollection(true)),
                    PayloadEdmModel = model,
                    ExpectedResultCallback = 
                        (tc) => new PayloadReaderTestExpectedResult(this.Settings.ExpectedResultSettings)
                                {
                                    ExpectedException = 
                                        (tc.Format == ODataFormat.Json)
                                        ? ODataExpectedExceptions.ODataException("ODataJsonLightEntryAndFeedDeserializer_PropertyWithoutValueWithWrongType", "Name", "Edm.String")
                                        : ODataExpectedExceptions.ODataException("ValidationUtils_NavigationPropertyExpected", "Name", "TestModel.CityType", "Structural"),
                                },
                },
            };

            // Generate interesting payloads around the navigation property - this will skip failure cases like request payloads or wrong versions.
            testDescriptors = testDescriptors.Concat(associationLinkTestDescriptors.SelectMany(td => this.PayloadGenerator.GenerateReaderPayloads(td)));

            // Add the same cases again, but without skipping interesting configurations.
            testDescriptors = testDescriptors.Concat(associationLinkTestDescriptors.Select(td =>
                {
                    PayloadReaderTestDescriptor result = new PayloadReaderTestDescriptor(td);
                    var originalResultCallback = result.ExpectedResultCallback;
                    result.ExpectedResultCallback = tc =>
                        new PayloadReaderTestExpectedResult(this.Settings.ExpectedResultSettings)
                        {
                            ExpectedException = tc.IsRequest
                                ? null
                                : originalResultCallback == null ? result.ExpectedException : originalResultCallback(tc).ExpectedException,
                            ExpectedPayloadElement = tc.IsRequest
                                ? RemoveAssociationLinkPayloadElementNormalizer.Normalize(result.PayloadElement.DeepCopy())
                                : result.PayloadElement
                        };

                    // Setting the ExpectedResultCallback prevents normalizers from being run.
                    result.SkipTestConfiguration = tc => tc.Format == ODataFormat.Json;

                    return result;
                }));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                ODataVersionUtils.AllSupportedVersions,
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations,
                (testDescriptor, maxProtocolVersion, testConfiguration) =>
                {
                    if (maxProtocolVersion < testConfiguration.Version)
                    {
                        return;
                    }

                    testDescriptor.RunTest(testConfiguration.CloneAndApplyMaxProtocolVersion(maxProtocolVersion));
                });
        }
Пример #2
0
        public void TestInvalidUriInAtomMetadata()
        {
            // A string which breaks URI syntax, as defined by RFC 2396
            const string invalidUri = "http://::";

            // Other unimportant values to use for creating payloads to test.
            const string personName = "Name";
            const string personEmail = "*****@*****.**";
            const string collectionTitle = "Collection";
            const string validUri = "http://odata.org";
            const string generatorName = "Test Generator";
            const string generatorVersion = "1.0";
            const string nonOdataRelValue = "someRel";
            const string linkType = "text/html";

            // Create a test descriptor for each possible place the spec says a Uri can appear and is exposed as ATOM metadata.
            // Note: This excludes the two spots where the spec says the value is a Uri, but we expose it as a string: 
            //       scheme attributes of category and categories elements, and the id of the source feed of an entry.
            //       This also excludes any Uri values that are always parsed, whether or not ATOM metadata reading is on,
            //       such as the href attribute in a collection element.
            IEnumerable<PayloadReaderTestDescriptor> testDescriptors = new PayloadReaderTestDescriptor[]
            {
                #region uri attribute in a generator
                // atom:feed/atom:generator/@uri
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.EntitySet().AtomGenerator(generatorName, invalidUri, generatorVersion),
                },
                // atom:entry/atom:source/atom:generator/@uri
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomGenerator(generatorName, invalidUri, generatorVersion)),
                },
                #endregion uri attribute in a generator

                #region uri element in person constructs
                // atom:feed/atom:author/atom:uri
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.EntitySet().AtomAuthor(personName, invalidUri, personEmail),
                },
                // atom:feed/atom:contributor/atom:uri
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.EntitySet().AtomContributor(personName, invalidUri, personEmail),
                },
                // atom:entry/atom:author/atom:uri
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity().AtomAuthor(personName, invalidUri, personEmail),
                },
                // atom:entry/atom:contributor/atom:uri
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity().AtomContributor(personName, invalidUri, personEmail),
                },
                // atom:entry/atom:source/atom:author/atom:uri
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomAuthor(personName, invalidUri, personEmail)),
                },
                // atom:entry/atom:source/atom:contributor/atom:uri
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomContributor(personName, invalidUri, personEmail)),
                },
                #endregion uri element in person constructs

                #region content of icon element
                // atom:feed/atom:icon
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.EntitySet().AtomIcon(invalidUri),
                },
                // atom:entry/atom:source/atom:icon
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomIcon(invalidUri)),
                },
                #endregion content of icon element
                
                #region content of logo element
                // atom:feed/atom:logo
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.EntitySet().AtomLogo(invalidUri),
                },
                // atom:entry/atom:source/atom:logo
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomLogo(invalidUri)),
                },
                #endregion content of logo element

                #region href attribute in a link
                // atom:feed/atom:link/@href
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.EntitySet().AtomLink(invalidUri, nonOdataRelValue, linkType),
                },
                // atom:entry/atom:link/@href
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity().AtomLink(invalidUri, nonOdataRelValue, linkType),
                },
                // atom:entry/atom:source/atom:link/@href
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.Entity().AtomSource(PayloadBuilder.EntitySet().AtomLink(invalidUri, nonOdataRelValue, linkType)),
                },
                #endregion href attribute in a link

                #region href attribute in an out-of-line categories element
                // app:service/app:workspace/app:collection/app:categories/@href
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = PayloadBuilder.ServiceDocument()
                        .Workspace(PayloadBuilder.Workspace()
                            .ResourceCollection(PayloadBuilder.ResourceCollection(collectionTitle, validUri)
                                .AppOutOfLineCategories(invalidUri))),
                    SkipTestConfiguration = tc => tc.IsRequest,
                },
                #endregion href attribute in an out-of-line categories element
            };

            // Add the expected exception to all test descriptors.
            testDescriptors = testDescriptors.Select(td =>
                new PayloadReaderTestDescriptor(td)
                {
                    ExpectedException = new ExpectedException(typeof(System.UriFormatException)),
                });

            // This method is set up so that expected exceptions are only expected when ATOM metadata reading is on.
            this.RunAtomMetadataReaderTests(testDescriptors);
        }