コード例 #1
0
        public void CanConfigureAllLinksViaEditLink()
        {
            // Arrange
            ODataModelBuilder builder = GetCommonModel();
            var expectedEditLink = "http://server/service/Products(15)";

            var products = builder.EntitySet<EntitySetLinkConfigurationTest_Product>("Products");
            products.HasEditLink(c => new Uri(
                string.Format(
                    "http://server/service/Products({0})",
                    c.EntityInstance.ID
                )
            ));

            var actor = builder.EntitySets.Single();
            var model = builder.GetEdmModel();
            var productType = model.SchemaElements.OfType<IEdmEntityType>().Single();
            var productsSet = model.SchemaElements.OfType<IEdmEntityContainer>().Single().EntitySets().Single();
            var productInstance = new EntitySetLinkConfigurationTest_Product { ID = 15 };
            var entityContext = new EntityInstanceContext { EdmModel = model, EntitySet = productsSet, EntityType = productType, EntityInstance = productInstance, UrlHelper = new UrlHelper(new HttpRequestMessage()) };
            var entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(actor);

            // Act
            var editLinkUri = entitySetLinkBuilderAnnotation.BuildEditLink(entityContext);
            var readLinkUri = entitySetLinkBuilderAnnotation.BuildReadLink(entityContext);
            var idLink = entitySetLinkBuilderAnnotation.BuildIdLink(entityContext);

            // Assert
            Assert.NotNull(editLinkUri);
            Assert.Equal(expectedEditLink, editLinkUri.ToString());
            Assert.NotNull(readLinkUri);
            Assert.Equal(expectedEditLink, readLinkUri.ToString());
            Assert.NotNull(idLink);
            Assert.Equal(expectedEditLink, idLink);
        }
コード例 #2
0
 public void BuildIdLink_Throws_IfIdLinkBuilderIsNull(object metadataLevel)
 {
     EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(_entitySet);
     Assert.Throws<InvalidOperationException>(
         () => linkBuilder.BuildIdLink(new EntityInstanceContext(), (ODataMetadataLevel)metadataLevel),
         "No IdLink factory was found. Try calling HasIdLink on the EntitySetConfiguration for 'Customers'.");
 }
コード例 #3
0
        public void BuildIdLink_Throws_IfIdLinkBuilderIsNull(object metadataLevel)
        {
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(_entitySet);

            Assert.Throws <InvalidOperationException>(
                () => linkBuilder.BuildIdLink(new EntityInstanceContext(), (ODataMetadataLevel)metadataLevel),
                "No IdLink factory was found. Try calling HasIdLink on the EntitySetConfiguration for 'Customers'.");
        }
コード例 #4
0
        public void BuildIdLink(bool followsConventions, object metadataLevel, bool linkEmitted)
        {
            _entitySet.HasIdLink(new SelfLinkBuilder<string>((context) => "http://selflink", followsConventions));
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(_entitySet);

            string generatedIdLink = linkBuilder.BuildIdLink(new EntityInstanceContext(), (ODataMetadataLevel)metadataLevel);

            if (linkEmitted)
            {
                Assert.Equal("http://selflink", generatedIdLink);
            }
            else
            {
                Assert.Null(generatedIdLink);
            }
        }
コード例 #5
0
        public void BuildIdLink(bool followsConventions, object metadataLevel, bool linkEmitted)
        {
            _entitySet.HasIdLink(new SelfLinkBuilder <string>((context) => "http://selflink", followsConventions));
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(_entitySet);

            string generatedIdLink = linkBuilder.BuildIdLink(new EntityInstanceContext(), (ODataMetadataLevel)metadataLevel);

            if (linkEmitted)
            {
                Assert.Equal("http://selflink", generatedIdLink);
            }
            else
            {
                Assert.Null(generatedIdLink);
            }
        }
コード例 #6
0
        public void Ctor_FollowingConventions_GeneratesSelfLinkWithCast_IfDerivedTypeHasNavigationProperty()
        {
            // Arrange
            CustomersModelWithInheritance model             = new CustomersModelWithInheritance();
            HttpRequestMessage            request           = GetODataRequest(model.Model);
            ODataSerializerContext        serializerContext = new ODataSerializerContext {
                Model = model.Model, EntitySet = model.Customers, Url = request.GetUrlHelper()
            };
            EntityInstanceContext instanceContext = new EntityInstanceContext(serializerContext, model.Customer.AsReference(), new { ID = 42 });

            // Act
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(model.Customers, model.Model);
            string result = linkBuilder.BuildIdLink(instanceContext, ODataMetadataLevel.Default);

            // Assert
            Assert.Equal("http://localhost/Customers(42)/NS.Customer", result);
        }
コード例 #7
0
        public void CanConfigureAllLinksViaEditLink()
        {
            // Arrange
            ODataModelBuilder builder = GetCommonModel();
            var expectedEditLink      = "http://server/service/Products(15)";

            var products = builder.EntitySet <EntitySetLinkConfigurationTest_Product>("Products");

            products.HasEditLink(c => new Uri(
                                     string.Format(
                                         "http://server/service/Products({0})",
                                         c.EntityInstance.ID
                                         )
                                     ));

            var actor           = builder.EntitySets.Single();
            var model           = builder.GetEdmModel();
            var productType     = model.SchemaElements.OfType <IEdmEntityType>().Single();
            var productsSet     = model.SchemaElements.OfType <IEdmEntityContainer>().Single().EntitySets().Single();
            var productInstance = new EntitySetLinkConfigurationTest_Product {
                ID = 15
            };
            var entityContext = new EntityInstanceContext {
                EdmModel = model, EntitySet = productsSet, EntityType = productType, EntityInstance = productInstance, UrlHelper = new UrlHelper(new HttpRequestMessage())
            };
            var entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(actor);

            // Act
            var editLinkUri = entitySetLinkBuilderAnnotation.BuildEditLink(entityContext);
            var readLinkUri = entitySetLinkBuilderAnnotation.BuildReadLink(entityContext);
            var idLink      = entitySetLinkBuilderAnnotation.BuildIdLink(entityContext);

            // Assert
            Assert.NotNull(editLinkUri);
            Assert.Equal(expectedEditLink, editLinkUri.ToString());
            Assert.NotNull(readLinkUri);
            Assert.Equal(expectedEditLink, readLinkUri.ToString());
            Assert.NotNull(idLink);
            Assert.Equal(expectedEditLink, idLink);
        }
コード例 #8
0
        public void Ctor_FollowingConventions_GeneratesSelfLinkWithoutCast_IfDerivedTypesHaveNoNavigationProperty()
        {
            // Arrange
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            IEdmEntitySet specialCustomers = new EdmEntitySet(model.Container, "SpecialCustomers", model.SpecialCustomer);
            HttpRequestMessage request = GetODataRequest(model.Model);
            ODataSerializerContext serializerContext = new ODataSerializerContext { Model = model.Model, EntitySet = specialCustomers, Url = request.GetUrlHelper() };
            EntityInstanceContext instanceContext = new EntityInstanceContext(serializerContext, model.Customer.AsReference(), new { ID = 42 });

            // Act
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(specialCustomers, model.Model);
            string result = linkBuilder.BuildIdLink(instanceContext, ODataMetadataLevel.Default);

            // Assert
            Assert.Equal("http://localhost/SpecialCustomers(42)", result);
        }