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);
        }
 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'.");
 }
        public void CanConfigureAllLinksViaIdLink()
        {
            // Arrange
            ODataModelBuilder builder = GetCommonModel();
            var expectedEditLink = "http://server/service/Products(15)";

            var products = builder.EntitySet<EntitySetLinkConfigurationTest_Product>("Products");
            products.HasIdLink(c =>
                string.Format(
                    "http://server/service/Products({0})",
                    c.GetPropertyValue("ID")
                ),
                followsConventions: false);

            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 serializerContext = new ODataSerializerContext { Model = model, EntitySet = productsSet };
            var entityContext = new EntityInstanceContext(serializerContext, productType.AsReference(), productInstance);
            var entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(actor);

            // Act
            var selfLinks = entitySetLinkBuilderAnnotation.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.Default);

            // Assert
            Assert.NotNull(selfLinks.EditLink);
            Assert.Equal(expectedEditLink, selfLinks.EditLink.ToString());
            Assert.NotNull(selfLinks.ReadLink);
            Assert.Equal(expectedEditLink, selfLinks.ReadLink.ToString());
            Assert.NotNull(selfLinks.IdLink);
            Assert.Equal(expectedEditLink, selfLinks.IdLink);
        }
Example #4
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'.");
        }
        public static void SetEntitySetLinkBuilder(this IEdmModel model, IEdmEntitySet entitySet, EntitySetLinkBuilderAnnotation entitySetLinkBuilder)
        {
            if (model == null)
            {
                throw Error.ArgumentNull("model");
            }

            model.SetAnnotationValue(entitySet, entitySetLinkBuilder);
        }
Example #6
0
        internal static EntitySetLinkBuilderAnnotation GetEntitySetLinkBuilder(this IEdmModel model, IEdmEntitySet entitySet)
        {
            EntitySetLinkBuilderAnnotation annotation = model.GetAnnotationValue <EntitySetLinkBuilderAnnotation>(entitySet);

            if (annotation == null)
            {
                throw Error.NotSupported(SRResources.EntitySetHasNoBuildLinkAnnotation, entitySet.Name);
            }

            return(annotation);
        }
Example #7
0
        public void CanConfigureIdLinkToNotFollowConventions()
        {
            string idLink = "http://id_link";

            _entitySet.HasIdLink(new SelfLinkBuilder <string>((ctxt) => idLink, followsConventions: false));

            var selfLinks = new EntitySetLinkBuilderAnnotation(_entitySet).BuildEntitySelfLinks(new EntityInstanceContext(), ODataMetadataLevel.MinimalMetadata);

            Assert.Equal(idLink, selfLinks.IdLink);
            Assert.Null(selfLinks.EditLink);
            Assert.Null(selfLinks.ReadLink);
        }
Example #8
0
        public void Ctor_FollowingConventions_GeneratesFeedSelfLinkFollowingConventions()
        {
            // Arrange
            CustomersModelWithInheritance model   = new CustomersModelWithInheritance();
            HttpRequestMessage            request = GetODataRequest(model.Model);

            // Act
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(model.Customers, model.Model);
            Uri result = linkBuilder.BuildFeedSelfLink(new FeedContext {
                EntitySet = model.Customers, Url = request.GetUrlHelper()
            });

            // Assert
            Assert.Equal("http://localhost/Customers", result.AbsoluteUri);
        }
Example #9
0
        public void BuildReadLink_WhenReadLinkIsNotSameAsEditLink(bool followsConventions, object metadataLevel, bool linkEmitted)
        {
            _entitySet.HasReadLink(new SelfLinkBuilder <Uri>((context) => new Uri("http://readlink/"), followsConventions));
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(_entitySet);

            Uri generatedReadLink = linkBuilder.BuildReadLink(new EntityInstanceContext(), (ODataMetadataLevel)metadataLevel, new Uri("http://editLink"));

            if (linkEmitted)
            {
                Assert.Equal("http://readlink/", generatedReadLink.AbsoluteUri);
            }
            else
            {
                Assert.Null(generatedReadLink);
            }
        }
        public void GetEntitySetLinkBuilder_After_SetEntitySetLinkBuilder()
        {
            // Arrange
            IEdmModel model = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            EdmEntityType entityType = new EdmEntityType("NS", "Entity");
            IEdmEntitySet entitySet = new EdmEntitySet(container, "EntitySet", entityType);
            EntitySetLinkBuilderAnnotation entitySetLinkBuilder = new EntitySetLinkBuilderAnnotation();

            // Act
            model.SetEntitySetLinkBuilder(entitySet, entitySetLinkBuilder);
            var result = model.GetEntitySetLinkBuilder(entitySet);

            // Assert
            Assert.Same(entitySetLinkBuilder, result);
        }
Example #11
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);
            }
        }
        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);
            }
        }
Example #13
0
        public void BuildEditLink_WhenEditLinkIsSameAsIdLink_And_IsNotSet(bool followsConventions, object metadataLevel, bool linkEmitted)
        {
            _entitySet.HasIdLink(new SelfLinkBuilder <string>((context) => "http://selflink/", followsConventions));
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(_entitySet);

            Uri generatedEditLink = linkBuilder.BuildEditLink(new EntityInstanceContext(), (ODataMetadataLevel)metadataLevel, "http://selflink");

            if (linkEmitted)
            {
                Assert.Equal("http://selflink/", generatedEditLink.AbsoluteUri);
            }
            else
            {
                Assert.Null(generatedEditLink);
            }
        }
        public static EntitySetLinkBuilderAnnotation GetEntitySetLinkBuilder(this IEdmModel model, IEdmEntitySet entitySet)
        {
            if (model == null)
            {
                throw Error.ArgumentNull("model");
            }

            EntitySetLinkBuilderAnnotation annotation = model.GetAnnotationValue<EntitySetLinkBuilderAnnotation>(entitySet);
            if (annotation == null)
            {
                // construct and set an entity set link builder that follows OData URL conventions.
                annotation = new EntitySetLinkBuilderAnnotation(entitySet, model);
                model.SetEntitySetLinkBuilder(entitySet, annotation);
            }

            return annotation;
        }
Example #15
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);
        }
Example #16
0
        public void Ctor_FollowingConventions_GeneratesNavigationLinkWithCast_ForDerivedNavigationProperty()
        {
            // 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.SpecialCustomer.AsReference(), new { ID = 42 });
            IEdmNavigationProperty ordersProperty  = model.SpecialCustomer.NavigationProperties().First(p => p.Name == "SpecialOrders");

            // Act
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(model.Customers, model.Model);
            Uri result = linkBuilder.BuildNavigationLink(instanceContext, ordersProperty, ODataMetadataLevel.Default);

            // Assert
            Assert.Equal("http://localhost/Customers(42)/NS.SpecialCustomer/SpecialOrders", result.AbsoluteUri);
        }
Example #17
0
        public void BuildNavigationLink(bool followsConventions, object metadataLevel, bool linkEmitted)
        {
            var       navigationProperty = _entitySet.EntityType.AddNavigationProperty(typeof(Customer).GetProperty("Orders"), EdmMultiplicity.Many);
            IEdmModel model = _modelBuilder.GetEdmModel();
            IEdmNavigationProperty edmNavProperty = model.GetEdmTypeReference(typeof(Customer)).AsEntity().DeclaredNavigationProperties().Single(p => p.Name == "Orders");

            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(_entitySet);

            linkBuilder.AddNavigationPropertyLinkBuilder(edmNavProperty, new NavigationLinkBuilder((context, property) => new Uri("http://navigationlink"), followsConventions));

            Uri generatedNavigationLink = linkBuilder.BuildNavigationLink(new EntityInstanceContext(), edmNavProperty, (ODataMetadataLevel)metadataLevel);

            if (linkEmitted)
            {
                Assert.Equal("http://navigationlink/", generatedNavigationLink.AbsoluteUri);
            }
            else
            {
                Assert.Null(generatedNavigationLink);
            }
        }
        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);
        }
Example #19
0
        public void CanConfigureAllLinksViaIdLink()
        {
            // Arrange
            ODataModelBuilder builder = GetCommonModel();
            var expectedEditLink      = "http://server/service/Products(15)";

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

            products.HasIdLink(c =>
                               string.Format(
                                   "http://server/service/Products({0})",
                                   c.GetPropertyValue("ID")
                                   ),
                               followsConventions: false);

            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 serializerContext = new ODataSerializerContext {
                Model = model, EntitySet = productsSet
            };
            var entityContext = new EntityInstanceContext(serializerContext, productType.AsReference(), productInstance);
            var entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(actor);

            // Act
            var selfLinks = entitySetLinkBuilderAnnotation.BuildEntitySelfLinks(entityContext, ODataMetadataLevel.Default);

            // Assert
            Assert.NotNull(selfLinks.EditLink);
            Assert.Equal(expectedEditLink, selfLinks.EditLink.ToString());
            Assert.NotNull(selfLinks.ReadLink);
            Assert.Equal(expectedEditLink, selfLinks.ReadLink.ToString());
            Assert.NotNull(selfLinks.IdLink);
            Assert.Equal(expectedEditLink, selfLinks.IdLink);
        }
        private static void AddNavigationBindings(EntitySetConfiguration configuration, EdmEntitySet entitySet, EntitySetLinkBuilderAnnotation linkBuilder, ODataModelBuilder builder,
            Dictionary<Type, IEdmStructuredType> edmTypeMap, Dictionary<string, EdmEntitySet> edmEntitySetMap)
        {
            foreach (EntityTypeConfiguration entity in builder.ThisAndBaseAndDerivedTypes(configuration.EntityType))
            {
                foreach (NavigationPropertyConfiguration navigation in entity.NavigationProperties)
                {
                    NavigationPropertyBinding binding = configuration.FindBinding(navigation);
                    if (binding != null)
                    {
                        EdmEntityType edmEntityType = edmTypeMap[entity.ClrType] as EdmEntityType;
                        IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name);

                        entitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]);

                        Func<EntityInstanceContext, IEdmNavigationProperty, Uri> linkBuilderFunc = configuration.GetNavigationPropertyLink(navigation);
                        if (linkBuilderFunc != null)
                        {
                            linkBuilder.AddNavigationPropertyLinkBuilder(edmNavigationProperty, linkBuilderFunc);
                        }
                    }
                }
            }
        }
        public void BuildEditLink_WhenEditLinkIsSameAsIdLink_And_IsNotSet(bool followsConventions, object metadataLevel, bool linkEmitted)
        {
            _entitySet.HasIdLink(new SelfLinkBuilder<string>((context) => "http://selflink/", followsConventions));
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(_entitySet);

            Uri generatedEditLink = linkBuilder.BuildEditLink(new EntityInstanceContext(), (ODataMetadataLevel)metadataLevel, "http://selflink");

            if (linkEmitted)
            {
                Assert.Equal("http://selflink/", generatedEditLink.AbsoluteUri);
            }
            else
            {
                Assert.Null(generatedEditLink);
            }
        }
Example #22
0
 internal static void SetEntitySetLinkBuilderAnnotation(this IEdmModel model, IEdmEntitySet entitySet, EntitySetLinkBuilderAnnotation entitySetLinkBuilder)
 {
     model.SetAnnotationValue(entitySet, entitySetLinkBuilder);
 }
        public static IEdmModel BuildEdmModel(string containerNamespace, string containerName, IEnumerable <IStructuralTypeConfiguration> entityTypeConfigurations, IEnumerable <IEntitySetConfiguration> entitySetConfigurations)
        {
            if (entityTypeConfigurations == null)
            {
                throw Error.ArgumentNull("entityTypeConfigurations");
            }

            if (entitySetConfigurations == null)
            {
                throw Error.ArgumentNull("entitySetConfigurations");
            }

            EdmModel model = new EdmModel();

            Dictionary <string, IEdmStructuredType> types = EdmTypeBuilder.GetTypes(entityTypeConfigurations)
                                                            .OfType <IEdmStructuredType>()
                                                            .ToDictionary(t => t.ToString());

            foreach (IEdmStructuredType type in types.Values)
            {
                if (type.TypeKind == EdmTypeKind.Complex)
                {
                    model.AddElement(type as IEdmComplexType);
                }
                else if (type.TypeKind == EdmTypeKind.Entity)
                {
                    model.AddElement(type as IEdmEntityType);
                }
                else
                {
                    throw Error.InvalidOperation(SRResources.UnsupportedEntityTypeInModel);
                }
            }

            if (entitySetConfigurations.Any())
            {
                EdmEntityContainer container = new EdmEntityContainer(containerNamespace, containerName);
                Dictionary <string, EdmEntitySet> edmEntitySetMap = new Dictionary <string, EdmEntitySet>();
                foreach (IEntitySetConfiguration entitySet in entitySetConfigurations)
                {
                    EdmEntitySet edmEntitySet = container.AddEntitySet(entitySet.Name, (IEdmEntityType)types[entitySet.EntityType.FullName]);
                    EntitySetLinkBuilderAnnotation entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(entitySet);

                    model.SetEntitySetLinkBuilderAnnotation(edmEntitySet, entitySetLinkBuilderAnnotation);
                    model.SetAnnotationValue <EntitySetUrlAnnotation>(edmEntitySet, new EntitySetUrlAnnotation {
                        Url = entitySet.GetUrl()
                    });
                    edmEntitySetMap.Add(edmEntitySet.Name, edmEntitySet);
                }

                foreach (IEntitySetConfiguration entitySet in entitySetConfigurations)
                {
                    EdmEntitySet edmEntitySet = edmEntitySetMap[entitySet.Name];
                    EntitySetLinkBuilderAnnotation entitySetLinkBuilderAnnotation = model.GetEntitySetLinkBuilder(edmEntitySet) as EntitySetLinkBuilderAnnotation;
                    foreach (NavigationPropertyConfiguration navigation in entitySet.EntityType.NavigationProperties)
                    {
                        NavigationPropertyBinding binding = entitySet.FindBinding(navigation);
                        if (binding != null)
                        {
                            EdmEntityType          edmEntityType         = types[entitySet.EntityType.FullName] as EdmEntityType;
                            IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name);

                            edmEntitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]);
                            entitySetLinkBuilderAnnotation.AddNavigationPropertyLinkBuilder(edmNavigationProperty, entitySet.GetNavigationPropertyLink(edmNavigationProperty.Name));
                        }
                    }
                }

                model.AddElement(container);
            }

            return(model);
        }
        public void Ctor_FollowingConventions_GeneratesFeedSelfLinkFollowingConventions()
        {
            // Arrange
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            HttpRequestMessage request = GetODataRequest(model.Model);

            // Act
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(model.Customers, model.Model);
            Uri result = linkBuilder.BuildFeedSelfLink(new FeedContext { EntitySet = model.Customers, Url = request.GetUrlHelper() });

            // Assert
            Assert.Equal("http://localhost/Customers", result.AbsoluteUri);
        }
        public void Ctor_FollowingConventions_GeneratesNavigationLinkWithCast_ForDerivedNavigationProperty()
        {
            // 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.SpecialCustomer.AsReference(), new { ID = 42 });
            IEdmNavigationProperty ordersProperty = model.SpecialCustomer.NavigationProperties().First(p => p.Name == "SpecialOrders");

            // Act
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(model.Customers, model.Model);
            Uri result = linkBuilder.BuildNavigationLink(instanceContext, ordersProperty, ODataMetadataLevel.Default);

            // Assert
            Assert.Equal("http://localhost/Customers(42)/NS.SpecialCustomer/SpecialOrders", result.AbsoluteUri);
        }
        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);
        }
        public void CanConfigureReadLinkToNotFollowConventions()
        {
            string idLink = "http://id_link";
            Uri readLink = new Uri("http://read_link");

            _entitySet.HasIdLink(new SelfLinkBuilder<string>((ctxt) => idLink, followsConventions: true));
            _entitySet.HasReadLink(new SelfLinkBuilder<Uri>((ctxt) => readLink, followsConventions: false));

            var selfLinks = new EntitySetLinkBuilderAnnotation(_entitySet).BuildEntitySelfLinks(new EntityInstanceContext(), ODataMetadataLevel.MinimalMetadata);

            Assert.Null(selfLinks.IdLink);
            Assert.Null(selfLinks.EditLink);
            Assert.Equal(readLink, selfLinks.ReadLink);
        }
        public void BuildNavigationLink(bool followsConventions, object metadataLevel, bool linkEmitted)
        {
            var navigationProperty = _entitySet.EntityType.AddNavigationProperty(typeof(Customer).GetProperty("Orders"), EdmMultiplicity.Many);
            IEdmModel model = _modelBuilder.GetEdmModel();
            IEdmNavigationProperty edmNavProperty = model.GetEdmTypeReference(typeof(Customer)).AsEntity().DeclaredNavigationProperties().Single(p => p.Name == "Orders");

            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(_entitySet);
            linkBuilder.AddNavigationPropertyLinkBuilder(edmNavProperty, new NavigationLinkBuilder((context, property) => new Uri("http://navigationlink"), followsConventions));

            Uri generatedNavigationLink = linkBuilder.BuildNavigationLink(new EntityInstanceContext(), edmNavProperty, (ODataMetadataLevel)metadataLevel);

            if (linkEmitted)
            {
                Assert.Equal("http://navigationlink/", generatedNavigationLink.AbsoluteUri);
            }
            else
            {
                Assert.Null(generatedNavigationLink);
            }
        }
        public void BuildReadLink_WhenReadLinkIsNotSameAsEditLink(bool followsConventions, object metadataLevel, bool linkEmitted)
        {
            _entitySet.HasReadLink(new SelfLinkBuilder<Uri>((context) => new Uri("http://readlink/"), followsConventions));
            EntitySetLinkBuilderAnnotation linkBuilder = new EntitySetLinkBuilderAnnotation(_entitySet);

            Uri generatedReadLink = linkBuilder.BuildReadLink(new EntityInstanceContext(), (ODataMetadataLevel)metadataLevel, new Uri("http://editLink"));

            if (linkEmitted)
            {
                Assert.Equal("http://readlink/", generatedReadLink.AbsoluteUri);
            }
            else
            {
                Assert.Null(generatedReadLink);
            }
        }
        public static IEdmModel BuildEdmModel(string containerNamespace, string containerName, IEnumerable<IStructuralTypeConfiguration> entityTypeConfigurations, IEnumerable<IEntitySetConfiguration> entitySetConfigurations)
        {
            if (entityTypeConfigurations == null)
            {
                throw Error.ArgumentNull("entityTypeConfigurations");
            }

            if (entitySetConfigurations == null)
            {
                throw Error.ArgumentNull("entitySetConfigurations");
            }

            EdmModel model = new EdmModel();

            Dictionary<string, IEdmStructuredType> types = EdmTypeBuilder.GetTypes(entityTypeConfigurations)
                .OfType<IEdmStructuredType>()
                .ToDictionary(t => t.ToString());

            foreach (IEdmStructuredType type in types.Values)
            {
                if (type.TypeKind == EdmTypeKind.Complex)
                {
                    model.AddElement(type as IEdmComplexType);
                }
                else if (type.TypeKind == EdmTypeKind.Entity)
                {
                    model.AddElement(type as IEdmEntityType);
                }
                else
                {
                    throw Error.InvalidOperation(SRResources.UnsupportedEntityTypeInModel);
                }
            }

            if (entitySetConfigurations.Any())
            {
                EdmEntityContainer container = new EdmEntityContainer(containerNamespace, containerName);
                Dictionary<string, EdmEntitySet> edmEntitySetMap = new Dictionary<string, EdmEntitySet>();
                foreach (IEntitySetConfiguration entitySet in entitySetConfigurations)
                {
                    EdmEntitySet edmEntitySet = container.AddEntitySet(entitySet.Name, (IEdmEntityType)types[entitySet.EntityType.FullName]);
                    EntitySetLinkBuilderAnnotation entitySetLinkBuilderAnnotation = new EntitySetLinkBuilderAnnotation(entitySet);

                    model.SetEntitySetLinkBuilderAnnotation(edmEntitySet, entitySetLinkBuilderAnnotation);
                    model.SetAnnotationValue<EntitySetUrlAnnotation>(edmEntitySet, new EntitySetUrlAnnotation { Url = entitySet.GetUrl() });
                    edmEntitySetMap.Add(edmEntitySet.Name, edmEntitySet);
                }

                foreach (IEntitySetConfiguration entitySet in entitySetConfigurations)
                {
                    EdmEntitySet edmEntitySet = edmEntitySetMap[entitySet.Name];
                    EntitySetLinkBuilderAnnotation entitySetLinkBuilderAnnotation = model.GetEntitySetLinkBuilder(edmEntitySet) as EntitySetLinkBuilderAnnotation;
                    foreach (NavigationPropertyConfiguration navigation in entitySet.EntityType.NavigationProperties)
                    {
                        NavigationPropertyBinding binding = entitySet.FindBinding(navigation);
                        if (binding != null)
                        {
                            EdmEntityType edmEntityType = types[entitySet.EntityType.FullName] as EdmEntityType;
                            IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name);

                            edmEntitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]);
                            entitySetLinkBuilderAnnotation.AddNavigationPropertyLinkBuilder(edmNavigationProperty, entitySet.GetNavigationPropertyLink(edmNavigationProperty.Name));
                        }
                    }
                }

                model.AddElement(container);
            }

            return model;
        }
 internal static void SetEntitySetLinkBuilderAnnotation(this IEdmModel model, IEdmEntitySet entitySet, EntitySetLinkBuilderAnnotation entitySetLinkBuilder)
 {
     model.SetAnnotationValue(entitySet, entitySetLinkBuilder);
 }
        private static void AddNavigationBindings(EntitySetConfiguration configuration, EdmEntitySet entitySet, EntitySetLinkBuilderAnnotation linkBuilder, ODataModelBuilder builder,
                                                  Dictionary <Type, IEdmStructuredType> edmTypeMap, Dictionary <string, EdmEntitySet> edmEntitySetMap)
        {
            foreach (EntityTypeConfiguration entity in builder.ThisAndBaseAndDerivedTypes(configuration.EntityType))
            {
                foreach (NavigationPropertyConfiguration navigation in entity.NavigationProperties)
                {
                    NavigationPropertyBindingConfiguration binding = configuration.FindBinding(navigation);
                    if (binding != null)
                    {
                        EdmEntityType          edmEntityType         = edmTypeMap[entity.ClrType] as EdmEntityType;
                        IEdmNavigationProperty edmNavigationProperty = edmEntityType.NavigationProperties().Single(np => np.Name == navigation.Name);

                        entitySet.AddNavigationTarget(edmNavigationProperty, edmEntitySetMap[binding.EntitySet.Name]);

                        NavigationLinkBuilder linkBuilderFunc = configuration.GetNavigationPropertyLink(navigation);
                        if (linkBuilderFunc != null)
                        {
                            linkBuilder.AddNavigationPropertyLinkBuilder(edmNavigationProperty, linkBuilderFunc);
                        }
                    }
                }
            }
        }