コード例 #1
0
        public void AttributeMappingsIsInitialized_WithRightActionAndTemplate(Type controllerType,
                                                                              string expectedPathTemplate, string expectedActionName)
        {
            // Arrange
            CustomersModelWithInheritance model      = new CustomersModelWithInheritance();
            HttpControllerDescriptor      controller = new HttpControllerDescriptor(new HttpConfiguration(), "TestController",
                                                                                    controllerType);

            ODataPathTemplate pathTemplate = new ODataPathTemplate();
            Mock <IODataPathTemplateHandler> pathTemplateHandler = new Mock <IODataPathTemplateHandler>();

            pathTemplateHandler
            .Setup(p => p.ParseTemplate(model.Model, expectedPathTemplate))
            .Returns(pathTemplate)
            .Verifiable();

            AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model,
                                                                                   new[] { controller }, pathTemplateHandler.Object);

            // Act
            convention.SelectController(new ODataPath(), new HttpRequestMessage());

            // Assert
            pathTemplateHandler.VerifyAll();
            Assert.NotNull(convention.AttributeMappings);
            Assert.Equal(expectedActionName, convention.AttributeMappings[pathTemplate].ActionName);
        }
コード例 #2
0
        public void AttributeMappingsIsInitialized_WithRightActionAndTemplate(Type controllerType,
                                                                              string expectedPathTemplate, string expectedActionName)
        {
            // Arrange
            HttpControllerDescriptor controller = new HttpControllerDescriptor(DependencyInjectionHelper.CreateConfigurationWithRootContainer(), "TestController",
                                                                               controllerType);

            ODataPathTemplate pathTemplate = new ODataPathTemplate();
            Mock <IODataPathTemplateHandler> pathTemplateHandler = new Mock <IODataPathTemplateHandler>();

            pathTemplateHandler
            .Setup(p => p.ParseTemplate(expectedPathTemplate, controller.Configuration.GetODataRootContainer(RouteName)))
            .Returns(pathTemplate)
            .Verifiable();

            AttributeRoutingConvention convention = new AttributeRoutingConvention(RouteName, new[] { controller }, pathTemplateHandler.Object);

            // Act
            convention.SelectController(new ODataPath(), new HttpRequestMessage());

            // Assert
            pathTemplateHandler.VerifyAll();
            Assert.NotNull(convention.AttributeMappings);
            Assert.Equal(expectedActionName, convention.AttributeMappings[pathTemplate].ActionName);
        }
コード例 #3
0
        public void AttributeRoutingConvention_ConfigEnsureInitialized_DoesNotThrowForValidPathTemplate()
        {
            // Arrange
            IEdmModel                  model = new CustomersModelWithInheritance().Model;
            HttpConfiguration          configuration = new[] { typeof(TestODataController) }.GetHttpConfiguration();
            AttributeRoutingConvention convention = new AttributeRoutingConvention(model, configuration);

            // Act & Assert
            Assert.DoesNotThrow(() => configuration.EnsureInitialized());
        }
コード例 #4
0
        public void AttributeMappingsInitialization_ThrowsInvalidOperation_IfNoConfigEnsureInitialized()
        {
            // Arrange
            HttpConfiguration          configuration = DependencyInjectionHelper.CreateConfigurationWithRootContainer();
            AttributeRoutingConvention convention    = new AttributeRoutingConvention(RouteName, configuration);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => convention.AttributeMappings,
                "The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called " +
                "in the application's startup code after all other initialization code.");
        }
コード例 #5
0
        public void AttributeMappingsInitialization_ThrowsInvalidOperation_IfNoConfigEnsureInitialized()
        {
            // Arrange
            HttpConfiguration          configuration = new HttpConfiguration();
            IEdmModel                  model         = Mock.Of <IEdmModel>();
            AttributeRoutingConvention convention    = new AttributeRoutingConvention(model, configuration);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => convention.AttributeMappings,
                "The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called " +
                "in the application's startup code after all other initialization code.");
        }
コード例 #6
0
        public void AttributeRoutingConvention_ConfigEnsureInitialized_ThrowsForInvalidPathTemplate()
        {
            // Arrange
            IEdmModel                  model = new EdmModel();
            HttpConfiguration          configuration = new[] { typeof(TestODataController) }.GetHttpConfiguration();
            AttributeRoutingConvention convention = new AttributeRoutingConvention(model, configuration);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => configuration.EnsureInitialized(),
                "The path template 'Customers' on the action 'GetCustomers' in controller 'TestOData' is not a valid OData path template. " +
                "The operation import overloads matching 'Customers' are invalid. This is most likely an error in the IEdmModel.");
        }
コード例 #7
0
        public void AttributeMappingsInitialization_ThrowsInvalidOperation_IfFailsToParsePathTemplate()
        {
            // Arrange
            CustomersModelWithInheritance model      = new CustomersModelWithInheritance();
            HttpControllerDescriptor      controller = new HttpControllerDescriptor(new HttpConfiguration(), "TestController",
                                                                                    typeof(InvalidPathTemplateController));

            AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model, new[] { controller });

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => convention.SelectController(new ODataPath(), new HttpRequestMessage()),
                "The path template 'Customers/Order' on the action 'GetCustomers' in controller 'TestController' is not " +
                "a valid OData path template. Invalid action detected. 'Order' is not an action that can bind to 'Collec" +
                "tion([NS.Customer Nullable=False])'.");
        }
コード例 #8
0
        public void AttributeMappingsInitialization_ThrowsInvalidOperation_IfFailsToParsePathTemplate()
        {
            // Arrange
            CustomersModelWithInheritance model      = new CustomersModelWithInheritance();
            HttpControllerDescriptor      controller = new HttpControllerDescriptor(new HttpConfiguration(), "TestController",
                                                                                    typeof(InvalidPathTemplateController));

            AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model, new[] { controller });

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => convention.SelectController(new ODataPath(), new HttpRequestMessage()),
                "The path template 'Customers/Order' on the action 'GetCustomers' in controller 'TestController' is not " +
                "a valid OData path template. The request URI is not valid. Since the segment 'Customers' refers to a " +
                "collection, this must be the last segment in the request URI or it must be followed by an function or " +
                "action that can be bound to it otherwise all intermediate segments must refer to a single resource.");
        }
コード例 #9
0
        /// <summary>
        /// Creates a mutable list of the default OData routing conventions with attribute routing enabled.
        /// </summary>
        /// <param name="configuration">The server configuration.</param>
        /// <param name="model">The EDM model to use for parsing OData paths.</param>
        /// <returns>A mutable list of the default OData routing conventions.</returns>
        public static IList<IODataRoutingConvention> CreateDefaultWithAttributeRouting(
            HttpConfiguration configuration,
            IEdmModel model)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }
            if (model == null)
            {
                throw Error.ArgumentNull("model");
            }

            IList<IODataRoutingConvention> routingConventions = CreateDefault();
            AttributeRoutingConvention routingConvention = new AttributeRoutingConvention(model, configuration);
            routingConventions.Insert(0, routingConvention);
            return routingConventions;
        }
コード例 #10
0
        /// <summary>
        /// Creates a mutable list of the default OData routing conventions with attribute routing enabled.
        /// </summary>
        /// <param name="configuration">The server configuration.</param>
        /// <param name="model">The EDM model to use for parsing OData paths.</param>
        /// <returns>A mutable list of the default OData routing conventions.</returns>
        public static IList <IODataRoutingConvention> CreateDefaultWithAttributeRouting(
            HttpConfiguration configuration,
            IEdmModel model)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }
            if (model == null)
            {
                throw Error.ArgumentNull("model");
            }

            IList <IODataRoutingConvention> routingConventions = CreateDefault();
            AttributeRoutingConvention      routingConvention  = new AttributeRoutingConvention(model, configuration);

            routingConventions.Insert(0, routingConvention);
            return(routingConventions);
        }
コード例 #11
0
        /// <summary>
        /// Creates a mutable list of the default OData routing conventions with attribute routing enabled.
        /// </summary>
        /// <param name="routeName">The name of the route.</param>
        /// <param name="configuration">The server configuration.</param>
        /// <returns>A mutable list of the default OData routing conventions.</returns>
        public static IList <IODataRoutingConvention> CreateDefaultWithAttributeRouting(
            string routeName,
            HttpConfiguration configuration)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

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

            IList <IODataRoutingConvention> routingConventions = CreateDefault();
            AttributeRoutingConvention      routingConvention  = new AttributeRoutingConvention(routeName, configuration);

            routingConventions.Insert(0, routingConvention);
            return(routingConventions);
        }
コード例 #12
0
        public void CtorTakingHttpConfiguration_InitializesAttributeMappings_OnFirstSelectControllerCall()
        {
            // Arrange
            HttpConfiguration             config = new HttpConfiguration();
            CustomersModelWithInheritance model  = new CustomersModelWithInheritance();

            ODataPathTemplate pathTemplate = new ODataPathTemplate();
            Mock <IODataPathTemplateHandler> pathTemplateHandler = new Mock <IODataPathTemplateHandler>();

            pathTemplateHandler.Setup(p => p.ParseTemplate(model.Model, "Customers")).Returns(pathTemplate).Verifiable();

            AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model, config, pathTemplateHandler.Object);

            config.EnsureInitialized();

            // Act
            convention.SelectController(new ODataPath(), new HttpRequestMessage());

            // Assert
            pathTemplateHandler.VerifyAll();
            Assert.NotNull(convention.AttributeMappings);
            Assert.Equal("GetCustomers", convention.AttributeMappings[pathTemplate].ActionName);
        }
コード例 #13
0
        public void CtorTakingHttpConfiguration_InitializesAttributeMappings_OnFirstSelectControllerCall()
        {
            // Arrange
            HttpConfiguration config = DependencyInjectionHelper.CreateConfigurationWithRootContainer();

            ODataPathTemplate pathTemplate = new ODataPathTemplate();
            Mock <IODataPathTemplateHandler> pathTemplateHandler = new Mock <IODataPathTemplateHandler>();

            pathTemplateHandler.Setup(p => p.ParseTemplate("Customers", config.GetODataRootContainer(RouteName)))
            .Returns(pathTemplate).Verifiable();

            AttributeRoutingConvention convention = new AttributeRoutingConvention(RouteName, config, pathTemplateHandler.Object);

            config.EnsureInitialized();

            // Act
            convention.SelectController(new ODataPath(), new HttpRequestMessage());

            // Assert
            pathTemplateHandler.VerifyAll();
            Assert.NotNull(convention.AttributeMappings);
            Assert.Equal("GetCustomers", convention.AttributeMappings[pathTemplate].ActionName);
        }
コード例 #14
0
        [InlineData(typeof(SingletonTestControllerWithPrefix), "VipCustomer/Orders", "GetVipCustomerOrdersWithPrefix")] // Singleton/Navigation
        public void AttributeMappingsIsInitialized_WithRightActionAndTemplate(Type controllerType,
            string expectedPathTemplate, string expectedActionName)
        {
            // Arrange
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            HttpControllerDescriptor controller = new HttpControllerDescriptor(new HttpConfiguration(), "TestController",
                controllerType);

            ODataPathTemplate pathTemplate = new ODataPathTemplate();
            Mock<IODataPathTemplateHandler> pathTemplateHandler = new Mock<IODataPathTemplateHandler>();
            pathTemplateHandler
                .Setup(p => p.ParseTemplate(model.Model, expectedPathTemplate))
                .Returns(pathTemplate)
                .Verifiable();

            AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model,
                new[] { controller }, pathTemplateHandler.Object);

            // Act
            convention.SelectController(new ODataPath(), new HttpRequestMessage());

            // Assert
            pathTemplateHandler.VerifyAll();
            Assert.NotNull(convention.AttributeMappings);
            Assert.Equal(expectedActionName, convention.AttributeMappings[pathTemplate].ActionName);
        }
コード例 #15
0
        public void AttributeMappingsInitialization_ThrowsInvalidOperation_IfFailsToParsePathTemplate()
        {
            // Arrange
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            HttpControllerDescriptor controller = new HttpControllerDescriptor(new HttpConfiguration(), "TestController",
                typeof(InvalidPathTemplateController));

            AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model, new[] { controller });

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => convention.SelectController(new ODataPath(), new HttpRequestMessage()),
                "The path template 'Customers/Order' on the action 'GetCustomers' in controller 'TestController' is not " +
                "a valid OData path template. The request URI is not valid. Since the segment 'Customers' refers to a " +
                "collection, this must be the last segment in the request URI or it must be followed by an function or " +
                "action that can be bound to it otherwise all intermediate segments must refer to a single resource.");
        }
コード例 #16
0
        public void AttributeMappingsInitialization_ThrowsInvalidOperation_IfNoConfigEnsureInitialized()
        {
            // Arrange
            HttpConfiguration configuration = new HttpConfiguration();
            IEdmModel model = Mock.Of<IEdmModel>();
            AttributeRoutingConvention convention = new AttributeRoutingConvention(model, configuration);

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => convention.AttributeMappings,
                "The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called " +
                "in the application's startup code after all other initialization code.");
        }
コード例 #17
0
        public void AttributeRoutingConvention_ConfigEnsureInitialized_ThrowsForInvalidPathTemplate()
        {
            // Arrange
            IEdmModel model = new EdmModel();
            HttpConfiguration configuration = new[] { typeof(TestODataController) }.GetHttpConfiguration();
            AttributeRoutingConvention convention = new AttributeRoutingConvention(model, configuration);

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => configuration.EnsureInitialized(),
                "The path template 'Customers' on the action 'GetCustomers' in controller 'TestOData' is not a valid OData path template. " +
                "The operation import overloads matching 'Customers' are invalid. This is most likely an error in the IEdmModel.");
        }
コード例 #18
0
        public void AttributeMappingsInitialization_ThrowsInvalidOperation_IfFailsToParsePathTemplate()
        {
            // Arrange
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();
            HttpControllerDescriptor controller = new HttpControllerDescriptor(new HttpConfiguration(), "TestController",
                typeof(InvalidPathTemplateController));

            AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model, new[] { controller });

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => convention.SelectController(new ODataPath(), new HttpRequestMessage()),
                "The path template 'Customers/Order' on the action 'GetCustomers' in controller 'TestController' is not " +
                "a valid OData path template. Invalid action detected. 'Order' is not an action that can bind to 'Collec" +
                "tion([NS.Customer Nullable=False])'.");
        }
コード例 #19
0
ファイル: ODataRoute.cs プロジェクト: tlycken/aspnetwebstack
 /// <summary>
 /// Maps the OData route attributes.
 /// </summary>
 /// <param name="configuration">The http configuration used to search for all the OData controllers to map.</param>
 public void MapODataRouteAttributes(HttpConfiguration configuration)
 {
     AttributeRoutingConvention routingConvention = new AttributeRoutingConvention(PathRouteConstraint.EdmModel, configuration);
     PathRouteConstraint.RoutingConventions.Insert(0, routingConvention);
 }
コード例 #20
0
        public void CtorTakingHttpConfiguration_InitializesAttributeMappings_OnFirstSelectControllerCall()
        {
            // Arrange
            HttpConfiguration config = new HttpConfiguration();
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            ODataPathTemplate pathTemplate = new ODataPathTemplate();
            Mock<IODataPathTemplateHandler> pathTemplateHandler = new Mock<IODataPathTemplateHandler>();
            pathTemplateHandler.Setup(p => p.ParseTemplate(model.Model, "Customers")).Returns(pathTemplate).Verifiable();

            AttributeRoutingConvention convention = new AttributeRoutingConvention(model.Model, config, pathTemplateHandler.Object);
            config.EnsureInitialized();

            // Act
            convention.SelectController(new ODataPath(), new HttpRequestMessage());

            // Assert
            pathTemplateHandler.VerifyAll();
            Assert.NotNull(convention.AttributeMappings);
            Assert.Equal("GetCustomers", convention.AttributeMappings[pathTemplate].ActionName);
        }
コード例 #21
0
        public void AttributeRoutingConvention_ConfigEnsureInitialized_DoesNotThrowForValidPathTemplate()
        {
            // Arrange
            IEdmModel model = new CustomersModelWithInheritance().Model;
            HttpConfiguration configuration = new[] { typeof(TestODataController) }.GetHttpConfiguration();
            AttributeRoutingConvention convention = new AttributeRoutingConvention(model, configuration);

            // Act & Assert
            Assert.DoesNotThrow(() => configuration.EnsureInitialized());
        }