public void AddModel_Throws_IfPrefixExisted()
        {
            // Arrange
            ODataOptions options  = new ODataOptions();
            IEdmModel    edmModel = EdmCoreModel.Instance;

            options.AddModel("odata", edmModel);

            // Act
            Action test = () => options.AddModel("odata", edmModel);

            // Assert
            ExceptionAssert.Throws <InvalidOperationException>(test, "The prefix 'odata' was already used for other Edm model.");
        }
        public void OnProvidersExecuted_AddODataRoutingSelector_WhenAttributeRoutingConvention()
        {
            // Arrange
            ODataOptions options = new ODataOptions();

            options.AddModel("odata", _model);

            LoggerFactory            loggerFactory = new LoggerFactory();
            var                      logger        = new Logger <AttributeRoutingConvention>(loggerFactory);
            IODataPathTemplateParser parser        = new DefaultODataPathTemplateParser();

            IODataControllerActionConvention[] conventions = new IODataControllerActionConvention[]
            {
                new AttributeRoutingConvention(logger, parser)
            };

            ODataRoutingApplicationModelProvider provider        = CreateProvider(options, conventions);
            ApplicationModelProviderContext      providerContext = CreateProviderContext(typeof(AttributeRoutingController));

            // Act
            provider.OnProvidersExecuted(providerContext);

            // Assert
            ControllerModel controller = Assert.Single(providerContext.Result.Controllers);

            ActionModel action = Assert.Single(controller.Actions);

            Assert.Equal("AnyMethodNameHere", action.ActionMethod.Name);
            Assert.Single(action.Parameters);
            Assert.Equal(2, action.Selectors.Count);
            Assert.Equal(new[] { "odata/Customers({key})/Name", "odata/Customers/{key}/Name" }, action.Selectors.Select(s => s.AttributeRouteModel.Template));
        }
 static AttributeRoutingConventionTests()
 {
     _edmModel = GetEdmModel();
     _options  = new ODataOptions();
     _options.AddModel(_edmModel);
     _attributeConvention = CreateConvention();
 }
        public void AddModel_Throws_IfModelNull()
        {
            // Arrange
            ODataOptions options = new ODataOptions();

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(() => options.AddModel("odata", null, builder => { }), "model");
        }
        public void GetODataServiceProvider_ReturnsCorrectServiceProvider()
        {
            // Arrange
            ODataOptions options  = new ODataOptions();
            IEdmModel    edmModel = EdmCoreModel.Instance;

            // Act
            options.AddModel("odata", edmModel);

            // & Assert
            IServiceProvider sp = options.GetODataServiceProvider("odata");

            Assert.NotNull(sp);
        }
        public void AddModel_Throws_IfBuilderFactoryReturnsNull()
        {
            // Arrange
            ODataOptions options  = new ODataOptions();
            IEdmModel    edmModel = EdmCoreModel.Instance;

            options.BuilderFactory = () => null;

            // Act
            Action test = () => options.AddModel("odata", edmModel);

            // Assert
            ExceptionAssert.Throws <InvalidOperationException>(test, "The container builder created by the container builder factory must not be null.");
        }
        public void AddModel_WithoutOrWithPrefix_SetModel(string prefix)
        {
            // Arrange
            ODataOptions options  = new ODataOptions();
            IEdmModel    edmModel = EdmCoreModel.Instance;

            // Act
            if (prefix == null)
            {
                options.AddModel(edmModel);
            }
            else
            {
                options.AddModel(prefix, edmModel);
            }

            // Assert
            KeyValuePair <string, (IEdmModel, IServiceProvider)> model = Assert.Single(options.Models);

            Assert.Equal(prefix ?? String.Empty, model.Key);

            Assert.Same(edmModel, model.Value.Item1);
            Assert.NotNull(model.Value.Item2);
        }
Beispiel #8
0
        public void OnProvidersExecuted_AddODataRoutingSelector_WhenEntityRoutingConvention()
        {
            // Arrange
            ODataOptions options = new ODataOptions();

            options.AddModel("odata", _model);

            IODataControllerActionConvention[] conventions = new IODataControllerActionConvention[]
            {
                new EntityRoutingConvention()
            };

            ODataRoutingApplicationModelProvider provider = CreateProvider(options, conventions);

            Type controllerType = typeof(CustomersController);
            ApplicationModelProviderContext providerContext = CreateProviderContext(controllerType);

            // Act
            provider.OnProvidersExecuted(providerContext);

            // Assert
            ControllerModel controller = Assert.Single(providerContext.Result.Controllers);

            Assert.Equal(2, controller.Actions.Count);
            Assert.Collection(controller.Actions,
                              e =>
            {
                // Get()
                Assert.Equal("Get", e.ActionMethod.Name);
                Assert.Empty(e.Parameters);
                var selector = Assert.Single(e.Selectors);
                Assert.Null(selector.AttributeRouteModel);
            },
                              e =>
            {
                // Get(int key)
                Assert.Equal("Get", e.ActionMethod.Name);
                Assert.Single(e.Parameters);
                Assert.Equal(2, e.Selectors.Count);
                Assert.Equal(new[] { "/odata/Customers({key})", "/odata/Customers/{key}" }, e.Selectors.Select(s => s.AttributeRouteModel.Template));
            });
        }
        public void AddModel_WithDependencyInjection_SetModelAndServices()
        {
            // Arrange
            ODataOptions options  = new ODataOptions();
            IEdmModel    edmModel = EdmCoreModel.Instance;

            // Act
            options.AddModel("odata", edmModel, builder => builder.AddService <IODataFeature, ODataFeature>(Microsoft.OData.ServiceLifetime.Singleton));

            // Assert
            KeyValuePair <string, (IEdmModel, IServiceProvider)> model = Assert.Single(options.Models);

            Assert.Equal("odata", model.Key);

            Assert.Same(edmModel, model.Value.Item1);
            Assert.NotNull(model.Value.Item2);
            IODataFeature actual = model.Value.Item2.GetService <IODataFeature>();

            Assert.IsType <ODataFeature>(actual);
        }
        public void AddModel_WithBatchHandler_SetModel()
        {
            // Arrange
            ODataOptions      options  = new ODataOptions();
            IEdmModel         edmModel = EdmCoreModel.Instance;
            ODataBatchHandler handler  = new Mock <ODataBatchHandler>().Object;

            // Act
            options.AddModel(edmModel, handler);

            // Assert
            KeyValuePair <string, (IEdmModel, IServiceProvider)> model = Assert.Single(options.Models);

            Assert.Equal(String.Empty, model.Key);

            Assert.Same(edmModel, model.Value.Item1);
            Assert.NotNull(model.Value.Item2);
            ODataBatchHandler actual = model.Value.Item2.GetService <ODataBatchHandler>();

            Assert.Same(handler, actual);
        }
        public void OnProvidersExecuting_DoesNothing()
        {
            // Arrange
            ODataOptions options = new ODataOptions();

            options.AddModel("odata", _model);

            IODataControllerActionConvention[] conventions = new IODataControllerActionConvention[]
            {
                new EntitySetRoutingConvention()
            };

            ODataRoutingApplicationModelProvider provider = CreateProvider(options, conventions);

            Type controllerType = typeof(CustomersController);
            ApplicationModelProviderContext providerContext = CreateProviderContext(controllerType);

            // Act
            provider.OnProvidersExecuting(providerContext);

            // Assert
            var controller = Assert.Single(providerContext.Result.Controllers);

            Assert.Collection(controller.Actions,
                              e =>
            {
                // Get()
                Assert.Equal("Get", e.ActionMethod.Name);
                Assert.Empty(e.Parameters);
                Assert.Empty(e.Selectors);
            },
                              e =>
            {
                // Get(int key)
                Assert.Equal("Get", e.ActionMethod.Name);
                Assert.Single(e.Parameters);
                Assert.Empty(e.Selectors);
            });
        }
        public void AddModel_CanUse_CustomizedBuilderFactory()
        {
            // Arrange
            IServiceProvider  sp       = new ServiceCollection().BuildServiceProvider();
            IContainerBuilder builer   = new MyInternalContainerBuilder(sp);
            ODataOptions      options  = new ODataOptions();
            IEdmModel         edmModel = EdmCoreModel.Instance;

            options.BuilderFactory = () => builer;

            // Act
            options.AddModel("odata", edmModel);

            // Assert
            KeyValuePair <string, (IEdmModel, IServiceProvider)> model = Assert.Single(options.Models);

            Assert.Equal("odata", model.Key);

            Assert.Same(edmModel, model.Value.Item1);
            Assert.NotNull(model.Value.Item2);
            Assert.Same(sp, model.Value.Item2);
        }