From 08f50d0ff21df42ea3218a9bd30a7abebd326953 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Mon, 3 Dec 2018 11:48:19 +0300 Subject: [PATCH] Refactor event bus and fix breaking code. --- .../Distributed/LocalDistributedEventBus.cs | 2 +- .../Volo/Abp/AbpIntegratedTest.cs | 2 + .../Volo/Abp/AbpTestBaseModule.cs | 8 +-- .../LocalDistributedEventBusTestBase.cs | 4 +- .../LocalDistributedEventBus_Test.cs | 8 +-- .../Volo/Abp/Identity/ProfileAppService.cs | 15 ++---- .../AbpIdentitySettingDefinitionProvider.cs | 4 +- .../Shared/PersonalSettingsModal.cshtml | 7 +-- .../Abp/Identity/ProfileAppService_Tests.cs | 2 - .../Identity/DistributedUserUpdateHandler.cs | 29 ++++++++++ .../Distributed_User_Change_Event_Tests.cs | 53 +++++++++++++++++++ ...bpIdentityEntityFrameworkCoreTestModule.cs | 1 - .../Identity/IdentityRoleRepository_Tests.cs | 1 - .../PermissionGrantCacheItemInvalidator.cs | 2 +- 14 files changed, 102 insertions(+), 36 deletions(-) create mode 100644 modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/DistributedUserUpdateHandler.cs create mode 100644 modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/Distributed_User_Change_Event_Tests.cs diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs index c7b0cef7b1..ab23119642 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs @@ -10,7 +10,7 @@ namespace Volo.Abp.EventBus.Distributed { [Dependency(TryRegister = true)] [ExposeServices(typeof(IDistributedEventBus), typeof(LocalDistributedEventBus))] - public class LocalDistributedEventBus : IDistributedEventBus, ITransientDependency + public class LocalDistributedEventBus : IDistributedEventBus, ISingletonDependency { private readonly ILocalEventBus _localEventBus; protected IHybridServiceScopeFactory ServiceScopeFactory { get; } diff --git a/framework/src/Volo.Abp.TestBase/Volo/Abp/AbpIntegratedTest.cs b/framework/src/Volo.Abp.TestBase/Volo/Abp/AbpIntegratedTest.cs index ac969fd06b..587d0d52db 100644 --- a/framework/src/Volo.Abp.TestBase/Volo/Abp/AbpIntegratedTest.cs +++ b/framework/src/Volo.Abp.TestBase/Volo/Abp/AbpIntegratedTest.cs @@ -4,6 +4,8 @@ using Volo.Abp.Modularity; namespace Volo.Abp { + //TODO: Move to "Testing" namespace + public abstract class AbpIntegratedTest : AbpTestBaseWithServiceProvider, IDisposable where TStartupModule : IAbpModule { diff --git a/framework/src/Volo.Abp.TestBase/Volo/Abp/AbpTestBaseModule.cs b/framework/src/Volo.Abp.TestBase/Volo/Abp/AbpTestBaseModule.cs index d956149842..fc262f5447 100644 --- a/framework/src/Volo.Abp.TestBase/Volo/Abp/AbpTestBaseModule.cs +++ b/framework/src/Volo.Abp.TestBase/Volo/Abp/AbpTestBaseModule.cs @@ -1,13 +1,9 @@ -using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Modularity; +using Volo.Abp.Modularity; namespace Volo.Abp { public class AbpTestBaseModule : AbpModule { - public override void ConfigureServices(ServiceConfigurationContext context) - { - - } + } } diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBusTestBase.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBusTestBase.cs index 45671715a3..d4e8df71aa 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBusTestBase.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBusTestBase.cs @@ -2,11 +2,11 @@ namespace Volo.Abp.EventBus.Distributed { public abstract class LocalDistributedEventBusTestBase : AbpIntegratedTest { - protected LocalDistributedEventBus LocalEventBus; + protected IDistributedEventBus DistributedEventBus; protected LocalDistributedEventBusTestBase() { - LocalEventBus = GetRequiredService(); + DistributedEventBus = GetRequiredService(); } protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs index 24dfbf357f..47649ad3c3 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs @@ -8,11 +8,11 @@ namespace Volo.Abp.EventBus.Distributed [Fact] public async Task Should_Call_Handler_AndDispose() { - LocalEventBus.Subscribe(); + DistributedEventBus.Subscribe(); - await LocalEventBus.PublishAsync(new MySimpleEventData(1)); - await LocalEventBus.PublishAsync(new MySimpleEventData(2)); - await LocalEventBus.PublishAsync(new MySimpleEventData(3)); + await DistributedEventBus.PublishAsync(new MySimpleEventData(1)); + await DistributedEventBus.PublishAsync(new MySimpleEventData(2)); + await DistributedEventBus.PublishAsync(new MySimpleEventData(3)); Assert.Equal(3, MySimpleDistributedTransientEventHandler.HandleCount); Assert.Equal(3, MySimpleDistributedTransientEventHandler.DisposeCount); diff --git a/modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/ProfileAppService.cs b/modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/ProfileAppService.cs index 444ccb34b1..5152738794 100644 --- a/modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/ProfileAppService.cs +++ b/modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/ProfileAppService.cs @@ -1,8 +1,8 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Volo.Abp.Identity.Settings; +using Volo.Abp.Settings; using Volo.Abp.Users; namespace Volo.Abp.Identity @@ -28,19 +28,12 @@ namespace Volo.Abp.Identity { var user = await _userManager.GetByIdAsync(CurrentUser.GetId()); - - var isUsernameUpdateEnabled = string.Equals(await (SettingManager.GetOrNullAsync(IdentitySettingNames.User.IsUserNameUpdateEnabled)), - "true", StringComparison.OrdinalIgnoreCase); - - if (isUsernameUpdateEnabled) + if (await SettingManager.IsTrueAsync(IdentitySettingNames.User.IsUserNameUpdateEnabled)) { (await _userManager.SetUserNameAsync(user, input.UserName)).CheckErrors(); } - var isEmailUpdateEnabled = !string.Equals(await (SettingManager.GetOrNullAsync(IdentitySettingNames.User.IsEmailUpdateEnabled)), - "true", StringComparison.OrdinalIgnoreCase); - - if (isEmailUpdateEnabled) + if (await SettingManager.IsTrueAsync(IdentitySettingNames.User.IsEmailUpdateEnabled)) { (await _userManager.SetEmailAsync(user, input.Email)).CheckErrors(); } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/AbpIdentitySettingDefinitionProvider.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/AbpIdentitySettingDefinitionProvider.cs index 472dca8784..c5cbb16a1f 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/AbpIdentitySettingDefinitionProvider.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/AbpIdentitySettingDefinitionProvider.cs @@ -23,8 +23,8 @@ namespace Volo.Abp.Identity new SettingDefinition(IdentitySettingNames.SignIn.RequireConfirmedEmail), new SettingDefinition(IdentitySettingNames.SignIn.RequireConfirmedPhoneNumber), - new SettingDefinition(IdentitySettingNames.User.IsUserNameUpdateEnabled), - new SettingDefinition(IdentitySettingNames.User.IsEmailUpdateEnabled) + new SettingDefinition(IdentitySettingNames.User.IsUserNameUpdateEnabled, "true"), + new SettingDefinition(IdentitySettingNames.User.IsEmailUpdateEnabled, "true") ); } diff --git a/modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Shared/PersonalSettingsModal.cshtml b/modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Shared/PersonalSettingsModal.cshtml index 9da2cfe074..29a0600148 100644 --- a/modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Shared/PersonalSettingsModal.cshtml +++ b/modules/identity/src/Volo.Abp.Identity.Web/Pages/Identity/Shared/PersonalSettingsModal.cshtml @@ -10,11 +10,8 @@ @inject ISettingManager SettingManager @{ Layout = null; - var isUserNameUpdateEnabled = string.Equals(await SettingManager.GetOrNullAsync(IdentitySettingNames.User.IsUserNameUpdateEnabled), "true", - StringComparison.OrdinalIgnoreCase); - - var isEmailUpdateEnabled = string.Equals(await SettingManager.GetOrNullAsync(IdentitySettingNames.User.IsEmailUpdateEnabled), "true", - StringComparison.OrdinalIgnoreCase); + var isUserNameUpdateEnabled = await SettingManager.IsTrueAsync(IdentitySettingNames.User.IsUserNameUpdateEnabled); + var isEmailUpdateEnabled = await SettingManager.IsTrueAsync(IdentitySettingNames.User.IsEmailUpdateEnabled); }
diff --git a/modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/ProfileAppService_Tests.cs b/modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/ProfileAppService_Tests.cs index 70f626ccbc..c1f0518b49 100644 --- a/modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/ProfileAppService_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/ProfileAppService_Tests.cs @@ -53,8 +53,6 @@ namespace Volo.Abp.Identity [Fact] public async Task UpdateAsync() { - //Arrange - //Arrange _currentUser.Id.Returns(_testData.UserJohnId); _currentUser.IsAuthenticated.Returns(true); diff --git a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/DistributedUserUpdateHandler.cs b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/DistributedUserUpdateHandler.cs new file mode 100644 index 0000000000..bf9a0260e9 --- /dev/null +++ b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/DistributedUserUpdateHandler.cs @@ -0,0 +1,29 @@ +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Entities.Events.Distributed; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.Testing.Utils; +using Volo.Abp.Users; + +namespace Volo.Abp.Identity +{ + public class DistributedUserUpdateHandler : IDistributedEventHandler>, ITransientDependency + { + private readonly ITestCounter _testCounter; + + public DistributedUserUpdateHandler(ITestCounter testCounter) + { + _testCounter = testCounter; + } + + public Task HandleEventAsync(EntityUpdatedEto eventData) + { + if (eventData.Entity.UserName == "john.nash") + { + _testCounter.Increment("EntityUpdatedEto"); + } + + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/Distributed_User_Change_Event_Tests.cs b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/Distributed_User_Change_Event_Tests.cs new file mode 100644 index 0000000000..221adca6d5 --- /dev/null +++ b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/Distributed_User_Change_Event_Tests.cs @@ -0,0 +1,53 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Options; +using Shouldly; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.Testing.Utils; +using Volo.Abp.Uow; +using Volo.Abp.Users; +using Xunit; + +namespace Volo.Abp.Identity +{ + public class Distributed_User_Change_Event_Tests : AbpIdentityDomainTestBase + { + private readonly IIdentityUserRepository _userRepository; + private readonly ILookupNormalizer _lookupNormalizer; + private readonly IdentityUserManager _userManager; + private readonly IUnitOfWorkManager _unitOfWorkManager; + private readonly ITestCounter _testCounter; + + public Distributed_User_Change_Event_Tests() + { + _userRepository = GetRequiredService(); + _userManager = GetRequiredService(); + _lookupNormalizer = GetRequiredService(); + _unitOfWorkManager = GetRequiredService(); + _testCounter = GetRequiredService(); + } + + [Fact] + public async Task Should_Register_Handler() + { + var options = GetRequiredService>().Value; + options.EtoMappings.ShouldContain(m => m.Key == typeof(IdentityUser) && m.Value == typeof(UserEto)); + options.Handlers.ShouldContain(h => h == typeof(DistributedUserUpdateHandler)); + } + + [Fact] + public async Task Should_Trigger_Distributed_EntityUpdated_Event() + { + using (var uow = _unitOfWorkManager.Begin()) + { + var user = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize("john.nash")); + await _userManager.SetEmailAsync(user, "john.nash_UPDATED@abp.io"); + + _testCounter.GetValue("EntityUpdatedEto").ShouldBe(0); + await uow.CompleteAsync(); + } + + _testCounter.GetValue("EntityUpdatedEto").ShouldBe(1); + } + } +} diff --git a/modules/identity/test/Volo.Abp.Identity.EntityFrameworkCore.Tests/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreTestModule.cs b/modules/identity/test/Volo.Abp.Identity.EntityFrameworkCore.Tests/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreTestModule.cs index 9eb9fbd6d5..322f07e995 100644 --- a/modules/identity/test/Volo.Abp.Identity.EntityFrameworkCore.Tests/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreTestModule.cs +++ b/modules/identity/test/Volo.Abp.Identity.EntityFrameworkCore.Tests/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreTestModule.cs @@ -2,7 +2,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; -using Microsoft.Extensions.DependencyInjection; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.Modularity; using Volo.Abp.PermissionManagement.EntityFrameworkCore; diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs index 5b06b4ae73..c753c1958b 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs @@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.DependencyInjection; using Shouldly; using Volo.Abp.Modularity; -using Volo.Abp.Uow; using Xunit; namespace Volo.Abp.Identity diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrantCacheItemInvalidator.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrantCacheItemInvalidator.cs index ea12b461fd..9e73e59b79 100644 --- a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrantCacheItemInvalidator.cs +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionGrantCacheItemInvalidator.cs @@ -7,7 +7,7 @@ using Volo.Abp.MultiTenancy; namespace Volo.Abp.PermissionManagement { - public class PermissionGrantCacheItemInvalidator : IEventHandler>, ITransientDependency + public class PermissionGrantCacheItemInvalidator : ILocalEventHandler>, ITransientDependency { protected ICurrentTenant CurrentTenant { get; }