Example #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);
        }
        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);
        }
        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])'.");
        }
        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.");
        }
Example #5
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);
        }
        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);
        }
        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])'.");
        }
        [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);
        }
        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);
        }
        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.");
        }