From 08dd58c92ea192b8ad8f70c1f7708dfb54f6820a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 16 Dec 2016 20:30:33 +0300 Subject: [PATCH] Revised MultiTenantManager_Tests. --- src/Volo.Abp.TestBase/AbpIntegratedTest.cs | 14 ++ test/AbpTestBase/project.json | 1 + ...ncLocalAmbientTenantScopeProvider_Tests.cs | 38 ----- .../Abp/MultiTenancy/MultiTenancyTestBase.cs | 9 ++ .../MultiTenancy/MultiTenancyTestModule.cs | 10 ++ ...MultiTenantManager_TenantResolver_Tests.cs | 133 ------------------ .../MultiTenancy/MultiTenantManager_Tests.cs | 99 +++++++++++++ 7 files changed, 133 insertions(+), 171 deletions(-) delete mode 100644 test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/AsyncLocalAmbientTenantScopeProvider_Tests.cs create mode 100644 test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestBase.cs create mode 100644 test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestModule.cs delete mode 100644 test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_TenantResolver_Tests.cs create mode 100644 test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_Tests.cs diff --git a/src/Volo.Abp.TestBase/AbpIntegratedTest.cs b/src/Volo.Abp.TestBase/AbpIntegratedTest.cs index 89d6218167..b1c1e22502 100644 --- a/src/Volo.Abp.TestBase/AbpIntegratedTest.cs +++ b/src/Volo.Abp.TestBase/AbpIntegratedTest.cs @@ -17,8 +17,12 @@ namespace Volo.Abp.TestBase { var services = CreateServiceCollection(); + BeforeAppApplication(services); + Application = services.AddApplication(); + AfterAddApplication(services); + var serviceProvider = CreateServiceProvider(services); Application.Initialize(serviceProvider); @@ -29,6 +33,16 @@ namespace Volo.Abp.TestBase return new ServiceCollection(); } + protected virtual void BeforeAppApplication(IServiceCollection services) + { + + } + + protected virtual void AfterAddApplication(IServiceCollection services) + { + + } + protected virtual IServiceProvider CreateServiceProvider(IServiceCollection services) { MainServiceScope = services.BuildServiceProvider().CreateScope(); diff --git a/test/AbpTestBase/project.json b/test/AbpTestBase/project.json index da032a0397..689d474114 100644 --- a/test/AbpTestBase/project.json +++ b/test/AbpTestBase/project.json @@ -5,6 +5,7 @@ "dotnet-test-xunit": "2.2.0-preview2-build1029", "NSubstitute": "2.0.0-rc", "Shouldly": "2.8.2", + "Volo.Abp.TestBase": "1.0.0-*", "xunit": "2.2.0-beta4-build3444", "xunit.extensibility.execution": "2.2.0-beta4-build3444" }, diff --git a/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/AsyncLocalAmbientTenantScopeProvider_Tests.cs b/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/AsyncLocalAmbientTenantScopeProvider_Tests.cs deleted file mode 100644 index a75a013344..0000000000 --- a/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/AsyncLocalAmbientTenantScopeProvider_Tests.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Shouldly; -using Xunit; - -namespace Volo.Abp.MultiTenancy -{ - public class AsyncLocalAmbientTenantScopeProvider_Tests - { - [Fact] - public void Should_Support_Inner_Scopes() - { - var scopeProvider = new AsyncLocalTenantScopeProvider(); - - Assert.Null(scopeProvider.CurrentScope?.Tenant); - - using (scopeProvider.EnterScope(new TenantInfo("A"))) - { - Assert.Equal("A", scopeProvider.CurrentScope?.Tenant?.Id); - - using (scopeProvider.EnterScope(new TenantInfo("B"))) - { - Assert.Equal("B", scopeProvider.CurrentScope?.Tenant?.Id); - - using (scopeProvider.EnterScope(null)) - { - Assert.NotNull(scopeProvider.CurrentScope); - Assert.Null(scopeProvider.CurrentScope.Tenant); - } - - Assert.Equal("B", scopeProvider.CurrentScope?.Tenant?.Id); - } - - Assert.Equal("A", scopeProvider.CurrentScope?.Tenant?.Id); - } - - scopeProvider.CurrentScope.ShouldBeNull(); - } - } -} diff --git a/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestBase.cs b/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestBase.cs new file mode 100644 index 0000000000..ddb00b47d2 --- /dev/null +++ b/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestBase.cs @@ -0,0 +1,9 @@ +using Volo.Abp.TestBase; + +namespace Volo.Abp.MultiTenancy +{ + public class MultiTenancyTestBase : AbpIntegratedTest + { + + } +} \ No newline at end of file diff --git a/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestModule.cs b/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestModule.cs new file mode 100644 index 0000000000..7c4b0c1574 --- /dev/null +++ b/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenancyTestModule.cs @@ -0,0 +1,10 @@ +using Volo.Abp.Modularity; + +namespace Volo.Abp.MultiTenancy +{ + [DependsOn(typeof(AbpMultiTenancyModule))] + public class MultiTenancyTestModule : AbpModule + { + + } +} \ No newline at end of file diff --git a/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_TenantResolver_Tests.cs b/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_TenantResolver_Tests.cs deleted file mode 100644 index 9d59ece78c..0000000000 --- a/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_TenantResolver_Tests.cs +++ /dev/null @@ -1,133 +0,0 @@ -using System; -using Microsoft.Extensions.Options; -using NSubstitute; -using Shouldly; -using Xunit; - -namespace Volo.Abp.MultiTenancy -{ - public class MultiTenantManager_TenantResolver_Tests - { - [Fact] - public void Should_Get_Current_Tenant_As_Null_If_No_Resolver() - { - //Arrange - - var manager = new MultiTenancyManager( - Substitute.For(), - Substitute.For(), - new OptionsWrapper(new MultiTenancyOptions()) - ); - - //Assert - - manager.CurrentTenant.ShouldBeNull(); - } - - [Fact] - public void Should_Get_Current_Tenant_From_Single_Resolver() - { - //Arrange - - var fakeTenant = new TenantInfo("A"); - - var manager = new MultiTenancyManager( - Substitute.For(), - Substitute.For(), - new OptionsWrapper(new MultiTenancyOptions - { - TenantResolvers = - { - new SimpleTenantResolver(context => - { - context.Tenant = fakeTenant; - context.Handled = true; - }) - } - } - ) - ); - - //Assert - - manager.CurrentTenant.ShouldBe(fakeTenant); - } - - - [Fact] - public void Should_Get_Current_Tenant_From_Multiple_Resolvers() - { - //Arrange - - var selectedTenant = new TenantInfo("A"); - - var manager = new MultiTenancyManager( - Substitute.For(), - Substitute.For(), - new OptionsWrapper(new MultiTenancyOptions - { - TenantResolvers = - { - new SimpleTenantResolver(context => - { - context.Tenant = new TenantInfo("B"); - context.Handled = false; //Causes go to the next resolver - }), - new SimpleTenantResolver(context => - { - context.Tenant = selectedTenant; - }), - new SimpleTenantResolver(context => - { - context.Tenant = new TenantInfo("C"); - }) - } - } - ) - ); - - //Assert - - manager.CurrentTenant.ShouldBe(selectedTenant); - } - - [Fact] - public void Should_Get_Ambient_Tenant_If_Changed() - { - //Arrange - - var oldTenant = new TenantInfo("A"); - - var manager = new MultiTenancyManager( - Substitute.For(), - new AsyncLocalTenantScopeProvider(), - new OptionsWrapper( - new MultiTenancyOptions - { - TenantResolvers = - { - new SimpleTenantResolver(context => - { - context.Tenant = oldTenant; - }) - } - } - ) - ); - - manager.CurrentTenant.ShouldBe(oldTenant); - - //Act - - var overridedTenant = new TenantInfo("B"); - using (manager.ChangeTenant(overridedTenant)) - { - //Assert - manager.CurrentTenant.ShouldBe(overridedTenant); - } - - //Assert - manager.CurrentTenant.ShouldBe(oldTenant); - } - } -} diff --git a/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_Tests.cs b/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_Tests.cs new file mode 100644 index 0000000000..12c223ab04 --- /dev/null +++ b/test/Volo.Abp.MultiTenancy.Tests/Volo/Abp/MultiTenancy/MultiTenantManager_Tests.cs @@ -0,0 +1,99 @@ +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Xunit; + +namespace Volo.Abp.MultiTenancy +{ + public class MultiTenantManager_Tests : MultiTenancyTestBase + { + private readonly IMultiTenancyManager _multiTenancyManager; + + private readonly TenantInfo _tenantA = new TenantInfo("A"); + private readonly TenantInfo _tenantB = new TenantInfo("B"); + private TenantInfo _tenantToBeResolved; + + public MultiTenantManager_Tests() + { + _multiTenancyManager = ServiceProvider.GetRequiredService(); + } + + [Fact] + public void CurrentTenant_Should_Be_Null_As_Default() + { + //Assert + + _multiTenancyManager.CurrentTenant.ShouldBeNull(); + } + + protected override void AfterAddApplication(IServiceCollection services) + { + services.Configure(options => + { + options.TenantResolvers.Add(new SimpleTenantResolver(context => + { + if (_tenantToBeResolved == _tenantA) + { + context.Tenant = _tenantA; + } + })); + + options.TenantResolvers.Add(new SimpleTenantResolver(context => + { + if (_tenantToBeResolved == _tenantB) + { + context.Tenant = _tenantB; + } + })); + }); + } + + [Fact] + public void Should_Get_Current_Tenant_From_Single_Resolver() + { + //Arrange + + _tenantToBeResolved = _tenantA; + + //Assert + + _multiTenancyManager.CurrentTenant.ShouldBe(_tenantA); + } + + + [Fact] + public void Should_Get_Current_Tenant_From_Multiple_Resolvers() + { + //Arrange + + _tenantToBeResolved = _tenantB; + + //Assert + + _multiTenancyManager.CurrentTenant.ShouldBe(_tenantB); + } + + [Fact] + public void Should_Get_Changed_Tenant_If_Wanted() + { + _multiTenancyManager.CurrentTenant.ShouldBe(null); + + _tenantToBeResolved = _tenantB; + + _multiTenancyManager.CurrentTenant.ShouldBe(_tenantB); + + using (_multiTenancyManager.ChangeTenant(_tenantA)) + { + _multiTenancyManager.CurrentTenant.ShouldBe(_tenantA); + + using (_multiTenancyManager.ChangeTenant(_tenantB)) + { + _multiTenancyManager.CurrentTenant.ShouldBe(_tenantB); + } + + _multiTenancyManager.CurrentTenant.ShouldBe(_tenantA); + } + + _multiTenancyManager.CurrentTenant.ShouldBe(_tenantB); + } + } +}