diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Conventions/AbpServiceConvention_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Conventions/AbpServiceConvention_Tests.cs new file mode 100644 index 0000000000..f16e8a1df9 --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Conventions/AbpServiceConvention_Tests.cs @@ -0,0 +1,91 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ApplicationModels; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Shouldly; +using System; +using System.Reflection; +using Volo.Abp.DependencyInjection; +using Xunit; + +namespace Volo.Abp.AspNetCore.Mvc.Conventions +{ + public class AbpServiceConvention_Tests : AspNetCoreMvcTestBase + { + private readonly IConventionalRouteBuilder _conventionalRouteBuilder; + private readonly IOptions _options; + + public AbpServiceConvention_Tests() + { + _conventionalRouteBuilder = GetRequiredService(); + _options = GetRequiredService>(); + } + + [Fact] + public void Should_Not_Remove_Derived_Controller_If_Not_Expose_Service() + { + // Arrange + var applicationModel = new ApplicationModel(); + var baseControllerModel = new ControllerModel(typeof(BaseController).GetTypeInfo(), Array.Empty()) + { + Application = applicationModel + }; + applicationModel.Controllers.Add(baseControllerModel); + + var derivedControllerModel = new ControllerModel(typeof(DerivedController).GetTypeInfo(), Array.Empty()) + { + Application = applicationModel + }; + applicationModel.Controllers.Add(derivedControllerModel); + + var abpServiceConvention = new AbpServiceConvention(_options, _conventionalRouteBuilder); + + // Act + abpServiceConvention.Apply(applicationModel); + + // Assert + applicationModel.Controllers.ShouldContain(baseControllerModel); + applicationModel.Controllers.ShouldContain(derivedControllerModel); + } + + [Fact] + public void Should_Remove_Derived_Controller_If_Expose_Service() + { + // Arrange + var applicationModel = new ApplicationModel(); + var baseControllerModel = new ControllerModel(typeof(BaseController).GetTypeInfo(), Array.Empty()) + { + Application = applicationModel + }; + applicationModel.Controllers.Add(baseControllerModel); + + var derivedControllerModel = new ControllerModel(typeof(ExposeServiceDerivedController).GetTypeInfo(), Array.Empty()) + { + Application = applicationModel + }; + applicationModel.Controllers.Add(derivedControllerModel); + + var abpServiceConvention = new AbpServiceConvention(_options, _conventionalRouteBuilder); + + // Act + abpServiceConvention.Apply(applicationModel); + + // Assert + applicationModel.Controllers.ShouldContain(baseControllerModel); + applicationModel.Controllers.ShouldNotContain(derivedControllerModel); + } + } + + public class BaseController : Controller + { + } + + public class DerivedController : BaseController + { + } + + [ExposeServices(typeof(BaseController))] + public class ExposeServiceDerivedController : BaseController + { + } +}