Exemple #1
0
        internal protected IEnumerable <ControllerActionDescriptor> GetDescriptors()
        {
            var applicationModel = BuildModel();

            ApplicationModelConventions.ApplyConventions(applicationModel, _conventions);
            return(ControllerActionDescriptorBuilder.Build(applicationModel));
        }
Exemple #2
0
        public void ApplicationModelConventions_CopiesParameterModelCollectionOnApply()
        {
            // Arrange
            var controllerType  = typeof(HelloController).GetTypeInfo();
            var app             = new ApplicationModel();
            var controllerModel = new ControllerModel(controllerType, new List <object>())
            {
                Application = app
            };

            app.Controllers.Add(controllerModel);
            var actionModel = new ActionModel(controllerType.GetMethod(nameof(HelloController.GetInfo)), new List <object>())
            {
                Controller = controllerModel
            };

            controllerModel.Actions.Add(actionModel);
            var parameterModel = new ParameterModel(
                controllerType.GetMethod(nameof(HelloController.GetInfo)).GetParameters()[0],
                new List <object>())
            {
                Action = actionModel
            };

            actionModel.Parameters.Add(parameterModel);

            var parameterModelConvention = new ParameterModelCollectionModifyingConvention();
            var conventions = new List <IApplicationModelConvention>();

            conventions.Add(parameterModelConvention);

            // Act & Assert
            ApplicationModelConventions.ApplyConventions(app, conventions);
        }
    public void ApplicationModelConventions_CopiesPropertyModelCollectionOnApply()
    {
        // Arrange
        var controllerType   = typeof(HelloController).GetTypeInfo();
        var applicationModel = new ApplicationModel();
        var controllerModel  = new ControllerModel(controllerType, Array.Empty <object>())
        {
            Application = applicationModel
        };

        controllerModel.ControllerProperties.Add(
            new PropertyModel(controllerType.GetProperty(nameof(HelloController.Property1)), Array.Empty <object>())
        {
            Controller = controllerModel
        });
        applicationModel.Controllers.Add(controllerModel);

        var propertyModelConvention = new ParameterModelBaseConvention();
        var conventions             = new List <IApplicationModelConvention>();

        conventions.Add(propertyModelConvention);

        // Act & Assert
        ApplicationModelConventions.ApplyConventions(applicationModel, conventions);
    }
    public void ApplicationModelConventions_CopiesActionModelCollectionOnApply()
    {
        // Arrange
        var controllerType   = typeof(HelloController).GetTypeInfo();
        var applicationModel = new ApplicationModel();
        var controllerModel  = new ControllerModel(controllerType, Array.Empty <object>())
        {
            Application = applicationModel
        };

        controllerModel.Actions.Add(
            new ActionModel(controllerType.GetMethod(nameof(HelloController.GetHello)), Array.Empty <object>())
        {
            Controller = controllerModel
        });
        applicationModel.Controllers.Add(controllerModel);

        var actionModelConvention = new ActionModelCollectionModifyingConvention();
        var conventions           = new List <IApplicationModelConvention>();

        conventions.Add(actionModelConvention);

        // Act & Assert
        ApplicationModelConventions.ApplyConventions(applicationModel, conventions);
    }
Exemple #5
0
        public IEnumerable <ControllerActionDescriptor> GetDescriptors()
        {
            var applicationModel = BuildModel();

            ApplicationModelConventions.ApplyConventions(applicationModel, _modelConventions);
            if (_logger.IsEnabled(LogLevel.Verbose))
            {
                foreach (var controller in applicationModel.Controllers)
                {
                    _logger.WriteVerbose(new ControllerModelValues(controller));
                }
            }
            return(ControllerActionDescriptorBuilder.Build(applicationModel));
        }
    public void ApplicationModelConventions_CopiesControllerModelCollectionOnApply_WhenRegisteredAsAnAttribute()
    {
        // Arrange
        var controllerModelConvention = new ControllerModelCollectionModifyingConvention();
        var applicationModel          = new ApplicationModel();

        applicationModel.Controllers.Add(
            new ControllerModel(typeof(HelloController).GetTypeInfo(), new[] { controllerModelConvention })
        {
            Application = applicationModel
        });

        var conventions = new List <IApplicationModelConvention>();

        // Act & Assert
        ApplicationModelConventions.ApplyConventions(applicationModel, conventions);
    }
Exemple #7
0
        public void ApplicationModelConventions_CopiesControllerModelCollectionOnApply()
        {
            // Arrange
            var applicationModel = new ApplicationModel();

            applicationModel.Controllers.Add(
                new ControllerModel(typeof(HelloController).GetTypeInfo(), new List <object>())
            {
                Application = applicationModel
            });

            var controllerModelConvention = new ControllerModelCollectionModifyingConvention();
            var conventions = new List <IApplicationModelConvention>();

            conventions.Add(controllerModelConvention);

            // Act & Assert
            ApplicationModelConventions.ApplyConventions(applicationModel, conventions);
        }
    public ApplicationModel CreateApplicationModel(IEnumerable <TypeInfo> controllerTypes)
    {
        if (controllerTypes == null)
        {
            throw new ArgumentNullException(nameof(controllerTypes));
        }

        var context = new ApplicationModelProviderContext(controllerTypes);

        for (var i = 0; i < _applicationModelProviders.Length; i++)
        {
            _applicationModelProviders[i].OnProvidersExecuting(context);
        }

        for (var i = _applicationModelProviders.Length - 1; i >= 0; i--)
        {
            _applicationModelProviders[i].OnProvidersExecuted(context);
        }

        ApplicationModelConventions.ApplyConventions(context.Result, _conventions);

        return(context.Result);
    }
    public void AddedParameterConvention_AppliesToAllPropertiesAndParameters()
    {
        // Arrange
        var app             = new ApplicationModel();
        var controllerType1 = typeof(HelloController).GetTypeInfo();
        var parameterModel1 = new ParameterModel(
            controllerType1.GetMethod(nameof(HelloController.GetInfo)).GetParameters()[0],
            Array.Empty <object>());
        var actionMethod1    = controllerType1.GetMethod(nameof(HelloController.GetInfo));
        var property1        = controllerType1.GetProperty(nameof(HelloController.Property1));
        var controllerModel1 = new ControllerModel(controllerType1, Array.Empty <object>())
        {
            ControllerProperties =
            {
                new PropertyModel(property1, Array.Empty <object>()),
            },
            Actions =
            {
                new ActionModel(actionMethod1, Array.Empty <object>())
                {
                    Parameters =
                    {
                        parameterModel1,
                    }
                }
            }
        };

        var controllerType2  = typeof(WorldController).GetTypeInfo();
        var property2        = controllerType2.GetProperty(nameof(WorldController.Property2));
        var controllerModel2 = new ControllerModel(controllerType2, Array.Empty <object>())
        {
            ControllerProperties =
            {
                new PropertyModel(property2, Array.Empty <object>()),
            },
        };

        app.Controllers.Add(controllerModel1);
        app.Controllers.Add(controllerModel2);

        var options    = new MvcOptions();
        var convention = new SimplePropertyConvention();

        options.Conventions.Add(convention);

        // Act
        ApplicationModelConventions.ApplyConventions(app, options.Conventions);

        // Assert
        var kvp = Assert.Single(controllerModel1.ControllerProperties[0].Properties);

        Assert.Equal("TestProperty", kvp.Key);
        Assert.Equal("TestValue", kvp.Value);

        kvp = Assert.Single(controllerModel2.ControllerProperties[0].Properties);
        Assert.Equal("TestProperty", kvp.Key);
        Assert.Equal("TestValue", kvp.Value);

        kvp = Assert.Single(controllerModel1.Actions[0].Parameters[0].Properties);
        Assert.Equal("TestProperty", kvp.Key);
        Assert.Equal("TestValue", kvp.Value);
    }