public void GetEntityKeyValue_ThrowsForNullKeys_WithMultipleKeys()
        {
            // Arrange
            IStructuralTypeConfiguration structuralType = new Mock <IStructuralTypeConfiguration>().Object;
            var entityInstance = new { Key1 = "abc", Key2 = "def", Key3 = (string)null };

            PrimitivePropertyConfiguration[] keys =
            {
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key1"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key2"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key3"), structuralType),
            };

            Mock <IEntityTypeConfiguration> entityType = new Mock <IEntityTypeConfiguration>();

            entityType.Setup(e => e.Keys).Returns(keys);
            entityType.Setup(e => e.FullName).Returns("EntityType");

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext {
                EntityInstance = entityInstance
            }, entityType.Object),
                "Key property 'Key3' of type 'EntityType' is null. Key properties cannot have null values.");
        }
        public void GetEntityKeyValue_MultipleKeys_DerivedType()
        {
            // Arrange
            Mock <IEntityTypeConfiguration> baseEntityType = new Mock <IEntityTypeConfiguration>();

            var entityInstance = new { Key1 = "key1", Key2 = 2, Key3 = true };

            PrimitivePropertyConfiguration[] keys =
            {
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key1"), baseEntityType.Object),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key2"), baseEntityType.Object),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key3"), baseEntityType.Object),
            };

            baseEntityType.Setup(e => e.Keys).Returns(keys);

            Mock <IEntityTypeConfiguration> derivedEntityType = new Mock <IEntityTypeConfiguration>();

            derivedEntityType.Setup(e => e.BaseType).Returns(baseEntityType.Object);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext {
                EntityInstance = entityInstance
            }, derivedEntityType.Object);

            // Assert
            Assert.Equal("Key1='key1',Key2=2,Key3=true", keyValue);
        }
        internal static Uri GenerateNavigationPropertyLink(EntityInstanceContext entityContext, IEdmNavigationProperty navigationProperty, IEntitySetConfiguration configuration, bool includeCast)
        {
            string routeName;

            Dictionary <string, object> routeValues = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            routeValues.Add(LinkGenerationConstants.Controller, configuration.Name);
            routeValues.Add(LinkGenerationConstants.ParentId, ConventionsHelpers.GetEntityKeyValue(entityContext, configuration.EntityType));
            routeValues.Add(LinkGenerationConstants.NavigationProperty, navigationProperty.Name);

            if (includeCast)
            {
                routeName = ODataRouteNames.PropertyNavigationWithCast;
                routeValues.Add(LinkGenerationConstants.Entitytype, entityContext.EntityType.FullName());
            }
            else
            {
                routeName = ODataRouteNames.PropertyNavigation;
            }

            string link = entityContext.UrlHelper.Link(routeName, routeValues);

            if (link == null)
            {
                throw Error.InvalidOperation(SRResources.NavigationPropertyRouteMissingOrIncorrect, navigationProperty.Name, ODataRouteNames.PropertyNavigation);
            }

            return(new Uri(link));
        }
        public void Apply(IEntitySetConfiguration configuration, ODataModelBuilder model)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            foreach (NavigationPropertyConfiguration property in configuration.EntityType.NavigationProperties)
            {
                if (configuration.GetNavigationPropertyLink(property.Name) == null)
                {
                    configuration.HasNavigationPropertyLink(
                        property,
                        (entityContext, navigationProperty) =>
                    {
                        string route = PropertyNavigationRouteName ?? ODataRouteNames.PropertyNavigation;
                        string link  = entityContext.UrlHelper.Link(
                            route,
                            new
                        {
                            Controller         = configuration.Name,
                            ParentId           = ConventionsHelpers.GetEntityKeyValue(entityContext, configuration.EntityType),
                            NavigationProperty = navigationProperty.Name
                        });

                        if (link == null)
                        {
                            throw Error.InvalidOperation(SRResources.NavigationPropertyRouteMissingOrIncorrect, navigationProperty.Name, ODataRouteNames.PropertyNavigation);
                        }
                        return(new Uri(link));
                    });
                }
            }
        }
        public void GetEntityKeyValue_MultipleKeys()
        {
            // Arrange
            StructuralTypeConfiguration structuralType = new Mock <StructuralTypeConfiguration>().Object;
            var entityInstance = new { Key1 = "key1", Key2 = 2, Key3 = true };

            PrimitivePropertyConfiguration[] keys =
            {
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key1"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key2"), structuralType),
                new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key3"), structuralType),
            };

            Mock <EntityTypeConfiguration> entityType = new Mock <EntityTypeConfiguration>();

            entityType
            .Setup(e => e.Keys)
            .Returns(keys);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext {
                EntityInstance = entityInstance
            }, entityType.Object);

            // Assert
            Assert.Equal("Key1='key1',Key2=2,Key3=true", keyValue);
        }
        public void Apply(IEntitySetConfiguration configuration, ODataModelBuilder model)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            // We only need to configure the EditLink by convention, ReadLink and IdLink both delegate to EditLink
            if (configuration.GetEditLink() == null)
            {
                configuration.HasEditLink(entityContext =>
                {
                    string routeName = SelfRouteName ?? ODataRouteNames.GetById;
                    string editlink  = entityContext.UrlHelper.Link(
                        routeName,
                        new
                    {
                        controller = configuration.Name,
                        id         = ConventionsHelpers.GetEntityKeyValue(entityContext, configuration.EntityType)
                    });
                    if (editlink == null)
                    {
                        throw Error.InvalidOperation(SRResources.GetByIdRouteMissingOrIncorrect, routeName);
                    }
                    return(new Uri(editlink));
                });
            }
        }
Exemplo n.º 7
0
        internal static Uri GenerateActionLink(EntityInstanceContext entityContext, ActionConfiguration action)
        {
            // the entity type the action is bound to.
            EntityTypeConfiguration actionEntityType = action.BindingParameter.TypeConfiguration as EntityTypeConfiguration;

            Contract.Assert(actionEntityType != null, "we have already verified that binding paramter type is entity");

            List <ODataPathSegment> actionPathSegments = new List <ODataPathSegment>();

            actionPathSegments.Add(new EntitySetPathSegment(entityContext.EntitySet));
            actionPathSegments.Add(new KeyValuePathSegment(ConventionsHelpers.GetEntityKeyValue(entityContext, actionEntityType)));

            // generate link with cast if the entityset type doesn't match the entity type the action is bound to.
            if (!entityContext.EntitySet.ElementType.IsOrInheritsFrom(entityContext.EdmModel.FindDeclaredType(actionEntityType.FullName)))
            {
                actionPathSegments.Add(new CastPathSegment(entityContext.EntityType));
            }

            actionPathSegments.Add(new ActionPathSegment(action.Name));

            string actionLink = entityContext.Url.ODataLink(actionPathSegments);

            if (actionLink == null)
            {
                return(null);
            }

            return(new Uri(actionLink));
        }
Exemplo n.º 8
0
        public void GetEntityKeyValue_ThrowsForNullKeys()
        {
            // Arrange
            var           entityInstance = new { Key = (string)null };
            EdmEntityType entityType     = new EdmEntityType("NS", "Name");

            entityType.AddKeys(entityType.AddStructuralProperty("Key", EdmPrimitiveTypeKind.String));

            EntityInstanceContext entityInstanceContext = new EntityInstanceContext(_writeContext, entityType.AsReference(), entityInstance);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => ConventionsHelpers.GetEntityKeyValue(entityInstanceContext),
                "Key property 'Key' of type 'NS.Name' is null. Key properties cannot have null values.");
        }
Exemplo n.º 9
0
        public void GetEntityKeyValue_SingleKey_DifferentDataTypes(object value, object expectedValue)
        {
            // Arrange
            EdmEntityType entityType = new EdmEntityType("NS", "Name");

            entityType.AddKeys(entityType.AddStructuralProperty("Property", EdmPrimitiveTypeKind.String));
            var entityInstance = new { Property = value };

            EntityInstanceContext entityInstanceContext = new EntityInstanceContext(_writeContext, entityType.AsReference(), entityInstance);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(entityInstanceContext);

            // Assert
            Assert.Equal(expectedValue, keyValue);
        }
Exemplo n.º 10
0
        public void GetEntityKeyValue_DerivedType()
        {
            // Arrange
            var           entityInstance = new { Key = "key" };
            EdmEntityType baseEntityType = new EdmEntityType("NS", "Name");

            baseEntityType.AddKeys(baseEntityType.AddStructuralProperty("Key", EdmPrimitiveTypeKind.String));
            EdmEntityType derivedEntityType = new EdmEntityType("NS", "Derived", baseEntityType);

            EntityInstanceContext entityInstanceContext = new EntityInstanceContext(_writeContext, derivedEntityType.AsReference(), entityInstance);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(entityInstanceContext);

            // Assert
            Assert.Equal("'key'", keyValue);
        }
Exemplo n.º 11
0
        public void GetEntityKeyValue_MultipleKeys()
        {
            // Arrange
            var           entityInstance = new { Key1 = "key1", Key2 = 2, Key3 = true };
            EdmEntityType entityType     = new EdmEntityType("NS", "Name");

            entityType.AddKeys(entityType.AddStructuralProperty("Key1", EdmPrimitiveTypeKind.String));
            entityType.AddKeys(entityType.AddStructuralProperty("Key2", EdmPrimitiveTypeKind.Int32));
            entityType.AddKeys(entityType.AddStructuralProperty("Key3", EdmPrimitiveTypeKind.Boolean));

            EntityInstanceContext entityInstanceContext = new EntityInstanceContext(_writeContext, entityType.AsReference(), entityInstance);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(entityInstanceContext);

            // Assert
            Assert.Equal("Key1='key1',Key2=2,Key3=true", keyValue);
        }
        public void GetEntityKeyValue_SingleKey()
        {
            // Arrange
            var entityInstance = new { Key = "key" };

            PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key")) };

            Mock <IEntityTypeConfiguration> entityType = new Mock <IEntityTypeConfiguration>();

            entityType
            .Setup(e => e.Keys)
            .Returns(keys);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext {
                EntityInstance = entityInstance
            }, entityType.Object);

            // Assert
            Assert.Equal("'key'", keyValue);
        }
        internal static Uri GenerateSelfLink(EntitySetConfiguration configuration, EntityInstanceContext entityContext, bool includeCast)
        {
            List <ODataPathSegment> editLinkPathSegments = new List <ODataPathSegment>();

            editLinkPathSegments.Add(new EntitySetPathSegment(entityContext.EntitySet));
            editLinkPathSegments.Add(new KeyValuePathSegment(ConventionsHelpers.GetEntityKeyValue(entityContext, configuration.EntityType)));

            if (includeCast)
            {
                editLinkPathSegments.Add(new CastPathSegment(entityContext.EntityType));
            }

            string editLink = entityContext.UrlHelper.ODataLink(entityContext.PathHandler, editLinkPathSegments);

            if (editLink == null)
            {
                return(null);
            }

            return(new Uri(editLink));
        }
        internal static string GenerateSelfLink(EntitySetConfiguration configuration, EntityInstanceContext entityContext, bool includeCast)
        {
            List <ODataPathSegment> idLinkPathSegments = new List <ODataPathSegment>();

            idLinkPathSegments.Add(new EntitySetPathSegment(entityContext.EntitySet));
            idLinkPathSegments.Add(new KeyValuePathSegment(ConventionsHelpers.GetEntityKeyValue(entityContext, configuration.EntityType)));

            if (includeCast)
            {
                idLinkPathSegments.Add(new CastPathSegment(entityContext.EntityType));
            }

            string idLink = entityContext.Url.ODataLink(idLinkPathSegments);

            if (idLink == null)
            {
                return(null);
            }

            return(idLink);
        }
        public void GetEntityKeyValue_DerivedType()
        {
            // Arrange
            var entityInstance = new { Key = "key" };

            Mock <EntityTypeConfiguration> baseEntityType = new Mock <EntityTypeConfiguration>();

            PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), baseEntityType.Object) };
            baseEntityType.Setup(e => e.Keys).Returns(keys);

            Mock <EntityTypeConfiguration> derivedEntityType = new Mock <EntityTypeConfiguration>();

            derivedEntityType.Setup(e => e.BaseType).Returns(baseEntityType.Object);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext {
                EntityInstance = entityInstance
            }, derivedEntityType.Object);

            // Assert
            Assert.Equal("'key'", keyValue);
        }
        public void GetEntityKeyValue_SingleKey_DifferentDataTypes(object value, object expectedValue)
        {
            // Arrange
            IStructuralTypeConfiguration structuralType = new Mock <IStructuralTypeConfiguration>().Object;
            var entityInstance = new { Key = value };

            PrimitivePropertyConfiguration[] keys = { new PrimitivePropertyConfiguration(entityInstance.GetType().GetProperty("Key"), structuralType) };

            Mock <IEntityTypeConfiguration> entityType = new Mock <IEntityTypeConfiguration>();

            entityType
            .Setup(e => e.Keys)
            .Returns(keys);

            // Act
            var keyValue = ConventionsHelpers.GetEntityKeyValue(new EntityInstanceContext {
                EntityInstance = entityInstance
            }, entityType.Object);

            // Assert
            Assert.Equal(expectedValue, keyValue);
        }
Exemplo n.º 17
0
        internal static Uri GenerateNavigationPropertyLink(EntityInstanceContext entityContext, IEdmNavigationProperty navigationProperty, EntitySetConfiguration configuration, bool includeCast)
        {
            List <ODataPathSegment> navigationPathSegments = new List <ODataPathSegment>();

            navigationPathSegments.Add(new EntitySetPathSegment(entityContext.EntitySet));
            navigationPathSegments.Add(new KeyValuePathSegment(ConventionsHelpers.GetEntityKeyValue(entityContext, configuration.EntityType)));

            if (includeCast)
            {
                navigationPathSegments.Add(new CastPathSegment(entityContext.EntityType));
            }

            navigationPathSegments.Add(new NavigationPathSegment(navigationProperty));

            string link = entityContext.Url.ODataLink(navigationPathSegments);

            if (link == null)
            {
                return(null);
            }

            return(new Uri(link));
        }
        internal static Uri GenerateActionLink(EntityInstanceContext entityContext, ActionConfiguration action)
        {
            // the entity type the action is bound to.
            IEntityTypeConfiguration actionEntityType = action.BindingParameter.TypeConfiguration as IEntityTypeConfiguration;

            Contract.Assert(actionEntityType != null, "we have already verified that binding paramter type is entity");

            Dictionary <string, object> routeValues = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            routeValues.Add(LinkGenerationConstants.Controller, entityContext.EntitySet.Name);
            routeValues.Add(LinkGenerationConstants.BoundId, ConventionsHelpers.GetEntityKeyValue(entityContext, actionEntityType));
            routeValues.Add(LinkGenerationConstants.ODataAction, action.Name);

            string routeName;

            // generate link without cast if the entityset type matches the entity type the action is bound to.
            if (entityContext.EntitySet.ElementType.IsOrInheritsFrom(entityContext.EdmModel.FindDeclaredType(actionEntityType.FullName)))
            {
                routeName = ODataRouteNames.InvokeBoundAction;
            }
            else
            {
                routeName = ODataRouteNames.InvokeBoundActionWithCast;
                routeValues.Add(LinkGenerationConstants.Entitytype, entityContext.EntityType.FullName());
            }

            string actionLink = entityContext.UrlHelper.Link(routeName, routeValues);

            if (actionLink == null)
            {
                return(null);
            }
            else
            {
                return(new Uri(actionLink));
            }
        }