public void EntityReferenceLinkTest()
        {
            IEdmModel model    = Test.OData.Utils.Metadata.TestModels.BuildTestModel();
            var       cityType = model.FindType("TestModel.CityType");

            Debug.Assert(cityType != null, "cityType != null");

            // TODO: add test cases that use relative URIs

            // Few hand-crafted payloads
            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = new PayloadReaderTestDescriptor[]
            {
                // Single entity reference link for a singleton
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadElement    = PayloadBuilder.Entity("TestModel.CityType").WithTypeAnnotation(cityType)
                                        .Property(PayloadBuilder.NavigationProperty("PoliceStation", "http://odata.org/PoliceStation").IsCollection(false)),
                    PayloadEdmModel = model
                },
                // Single entity reference link for a collection
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadElement    = PayloadBuilder.Entity("TestModel.CityType").WithTypeAnnotation(cityType)
                                        .Property(PayloadBuilder.NavigationProperty("CityHall", "http://odata.org/CityHall").IsCollection(true)),
                    PayloadEdmModel = model
                },

                // Multiple entity reference links
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadElement    = PayloadBuilder.Entity("TestModel.CityType").WithTypeAnnotation(cityType)
                                        .Property(PayloadBuilder.NavigationProperty("CityHall", "http://odata.org/CityHall").IsCollection(true))
                                        .Property(PayloadBuilder.NavigationProperty("PoliceStation", "http://odata.org/PoliceStation").IsCollection(false)),
                    PayloadEdmModel = model
                },

                // Multiple entity reference links with primitive properties in between
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadDescriptor = new PayloadTestDescriptor(),
                    PayloadElement    = PayloadBuilder.Entity("TestModel.CityType").WithTypeAnnotation(cityType)
                                        .Property("Id", PayloadBuilder.PrimitiveValue(1))
                                        .Property(PayloadBuilder.NavigationProperty("CityHall", "http://odata.org/CityHall").IsCollection(true))
                                        .Property("Name", PayloadBuilder.PrimitiveValue("Vienna"))
                                        .Property(PayloadBuilder.NavigationProperty("DOL", "http://odata.org/DOL").IsCollection(true)),
                    PayloadEdmModel = model
                },
            };

            // Generate interesting payloads around the entry
            testDescriptors = testDescriptors.SelectMany(td => this.PayloadGenerator.GenerateReaderPayloads(td));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                // Entity reference links are request only.
                this.ReaderTestConfigurationProvider.ExplicitFormatConfigurations.Where(tc => tc.IsRequest),
                (testDescriptor, testConfiguration) =>
            {
                testDescriptor.RunTest(testConfiguration);
            });
        }
Beispiel #2
0
        public void EntryReadingNullUriTest()
        {
            EdmModel model = (EdmModel)Microsoft.Test.OData.Utils.Metadata.TestModels.BuildTestModel();

            model.Fixup();

            NullUriValueTestCase <EntityInstance>[] testCases = new NullUriValueTestCase <EntityInstance>[]
            {
                // Setting the read link of an entry
                // NOTE: in JSON, the 'uri' property will be always omitted from __metadata when the read link is null (and no edit link exists);
                //       in ATOM, the self link will be omitted from the payload
                new NullUriValueTestCase <EntityInstance>
                {
                    SetNullUriAction = (instance, uri, testConfig) => instance.RemoveAnnotations(typeof(SelfLinkAnnotation)),
                },

                // Setting the edit link of an entry
                // NOTE: in JSON, the 'uri' property will be always omitted from __metadata when the edit link is null (and no read link exists);
                //       in ATOM, the link with the 'edit' rel will be omitted from the payload
                new NullUriValueTestCase <EntityInstance>
                {
                    SetNullUriAction = (instance, uri, testConfig) => instance.WithEditLink(null),
                },

                // Setting the navigation link of an entry
                // NOTE: in JSON, the 'uri' property in the __deferred object will have a 'null' value (and we expect an error)
                //       in ATOM, the 'related' link for the navigation property will not have the 'href' property.
                new NullUriValueTestCase <EntityInstance>
                {
                    SetNullUriAction =
                        (instance, uri, testConfig) => ((NavigationPropertyInstance)instance.GetProperty("CityHall")).Value = new DeferredLink(),
                },

                // Setting the association link of an entry
                // NOTE: in JSON, the associationuri property will have a 'null' value (and we expect an error)
                //       in ATOM, the 'relatedlinks' link will not have the 'href' property
                new NullUriValueTestCase <EntityInstance>
                {
                    SetNullUriAction =
                        (instance, uri, testConfig) => ((NavigationPropertyInstance)instance.GetProperty("CityHall")).AssociationLink = null,
                },

                // Setting the read link of a stream property
                // NOTE: in JSON, the 'media_src' property will be omitted from the __mediaresource object.
                //       in ATOM, the 'mediaresource' link of the stream property will be omitted from the payload.
                new NullUriValueTestCase <EntityInstance>
                {
                    SetNullUriAction =
                        (instance, uri, testConfig) => ((NamedStreamInstance)instance.GetProperty("Skyline")).SourceLink = null,
                },

                // Setting the edit link of a stream property
                // NOTE: in JSON, the 'edit_media' property will be omitted from the __mediaresource object.
                //       in ATOM, the 'edit-media' link of the stream property will be omitted from the payload.
                new NullUriValueTestCase <EntityInstance>
                {
                    SetNullUriAction =
                        (instance, uri, testConfig) => ((NamedStreamInstance)instance.GetProperty("Skyline")).EditLink = null,
                },

                // Setting the read link of the default stream
                // NOTE: in JSON, the 'media_src' property will be omitted from the __metadata object.
                //       in ATOM, the <content> element will be omitted from the payload.
                new NullUriValueTestCase <EntityInstance>
                {
                    SetNullUriAction = (instance, uri, testConfig) => instance.StreamSourceLink = null,
                },

                // Setting the edit link of the default stream
                // NOTE: in JSON, the 'edit_media' property will be omitted from the __metadata object.
                //       in ATOM, the edit-media link will be omitted from the payload.
                new NullUriValueTestCase <EntityInstance>
                {
                    SetNullUriAction = (instance, uri, testConfig) => instance.StreamEditLink = null,
                },
            };

            EntityInstance entry = PayloadBuilder.Entity("TestModel.CityWithMapType")
                                   .PrimitiveProperty("Id", 1)
                                   .Property(PayloadBuilder.NavigationProperty("CityHall", /*link*/ "http://odata.org/nav-prop").IsCollection(true))
                                   .StreamProperty("Skyline", /*readLink*/ "http://odata.org/stream-read", /*editLink*/ "http://odata.org/stream-edit")
                                   .AsMediaLinkEntry()
                                   .StreamSourceLink(/*link*/ "http://odata.org/media-read");

            this.CombinatorialEngineProvider.RunCombinations(
                baseUriValues,
                testCases,
                this.ReaderTestConfigurationProvider.AtomFormatConfigurations,
                (baseUriValue, testCase, testConfiguration) =>
            {
                this.RunNullUriReadingTest(entry, testCase, model, baseUriValue, testConfiguration);
            });
        }
Beispiel #3
0
        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")
                                     .PrimitiveProperty("Id", 1)
                                     .Property(
                        PayloadBuilder.NavigationProperty("Nonexistant", null, "http://odata.org/CityHallLink").IsCollection(true)),
                    PayloadEdmModel        = model,
                    ExpectedResultCallback =
                        (tc) => new PayloadReaderTestExpectedResult(this.Settings.ExpectedResultSettings)
                    {
                        ExpectedException = null
                    },
                },
                // 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("ODataJsonLightResourceDeserializer_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));
            });
        }
Beispiel #4
0
        public void EntryReadingBaseUriTest()
        {
            EdmModel model = (EdmModel)Microsoft.Test.OData.Utils.Metadata.TestModels.BuildTestModel();

            model.Fixup();

            Action <EntityInstance, Uri, ReaderTestConfiguration>[] setUriActions = new Action <EntityInstance, Uri, ReaderTestConfiguration>[]
            {
                // Setting the read link of an entry
                (instance, uri, testConfig) =>
                {
                    instance.SetAnnotation(new SelfLinkAnnotation(UriToString(uri)));
                },
                // Setting the edit link of an entry
                (instance, uri, testConfig) =>
                {
                    instance.WithEditLink(UriToString(uri));
                },

                // Setting the navigation link of an entry
                (instance, uri, testConfig) =>
                {
                    NavigationPropertyInstance navProperty = instance.GetProperty("CityHall") as NavigationPropertyInstance;
                    this.Assert.IsNotNull(navProperty, "Did not find expected navigation property 'CityHall'.");
                    DeferredLink deferredLink = navProperty.Value as DeferredLink;
                    this.Assert.IsNotNull(deferredLink, "Did not find expected deferred link.");
                    deferredLink.UriString = UriToString(uri);
                },

                // Setting the association link of an entry
                (instance, uri, testConfig) =>
                {
                    NavigationPropertyInstance navProperty = instance.GetProperty("CityHall") as NavigationPropertyInstance;
                    this.Assert.IsNotNull(navProperty, "Did not find expected navigation property 'CityHall'.");
                    DeferredLink deferredLink = navProperty.AssociationLink as DeferredLink;
                    this.Assert.IsNotNull(deferredLink, "Did not find expected assocation link.");
                    deferredLink.UriString = UriToString(uri);
                },

                // Setting the read link of a stream property
                (instance, uri, testConfig) =>
                {
                    NamedStreamInstance namedStream = instance.GetProperty("Skyline") as NamedStreamInstance;
                    this.Assert.IsNotNull(namedStream, "Did not find expected stream property 'Skyline'.");
                    namedStream.SourceLink = UriToString(uri);
                },

                // Setting the edit link of a stream property
                (instance, uri, testConfig) =>
                {
                    NamedStreamInstance namedStream = instance.GetProperty("Skyline") as NamedStreamInstance;
                    this.Assert.IsNotNull(namedStream, "Did not find expected stream property 'Skyline'.");
                    namedStream.EditLink = UriToString(uri);
                },

                // Setting the read link of the default stream
                (instance, uri, testConfig) =>
                {
                    instance.StreamSourceLink = UriToString(uri);
                },

                // Setting the edit link of the default stream
                (instance, uri, testConfig) =>
                {
                    instance.StreamEditLink = UriToString(uri);
                },

                // TODO: add tests for operation links (not yet supported in the test infrastructure).
            };

            EntityInstance entry = PayloadBuilder.Entity("TestModel.CityWithMapType")
                                   .PrimitiveProperty("Id", 1)
                                   .Property(PayloadBuilder.NavigationProperty("CityHall", /*url*/ "http://odata.org/dummy", /*associationUrl*/ "http://odata.org/dummy").IsCollection(true))
                                   .StreamProperty("Skyline", "http://odata.org./dummy")
                                   .AsMediaLinkEntry()
                                   .StreamSourceLink("http://odata.org/dummy");

            this.CombinatorialEngineProvider.RunCombinations(
                payloadUris,
                baseUriValues,
                resolvers,
                setUriActions,
                this.ReaderTestConfigurationProvider.AtomFormatConfigurations,
                (payloadUri, baseUriValue, resolver, setUriAction, testConfiguration) =>
            {
                this.CombinatorialEngineProvider.RunCombinations(
                    new bool[] { false, true },
                    runInBatch =>
                {
                    this.RunBaseUriReadingTest(entry, setUriAction, model, payloadUri, baseUriValue, resolver, testConfiguration, runInBatch);
                });
            });
        }