feat: Add newProjectConfigureHttpApiModule for API configuration and localization

feat: Implement SampleAppServiceTests for application service testing

feat: Create newProjectConfigureApplicationTests project for application layer tests

feat: Establish newProjectConfigureDomainTests project for domain layer tests

feat: Introduce Entity Framework Core tests for the newProjectConfigure application

feat: Develop console test application for HTTP API client demonstration

feat: Implement FakeCurrentPrincipalAccessor for testing purposes

chore: Set up test base classes and data seed contributors for consistent testing environment
This commit is contained in:
sayan.kolay committed 2026-08-31 16:18:47 +05:30
1 parent 7909179fca
commit b6f2213ccf
279 files changed
+39466

No files matched your search

@@ -0,0 +1,34 @@
using Shouldly;
using System.Threading.Tasks;
using Volo.Abp.Identity;
using Volo.Abp.Modularity;
using Xunit;
namespace newProjectConfigure.Samples;
/* This is just an example test class.
* Normally, you don't test code of the modules you are using
* (like IIdentityUserAppService here).
* Only test your own application services.
*/
public abstract class SampleAppServiceTests<TStartupModule> : newProjectConfigureApplicationTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
private readonly IIdentityUserAppService _userAppService;
protected SampleAppServiceTests()
{
_userAppService = GetRequiredService<IIdentityUserAppService>();
}
[Fact]
public async Task Initial_Data_Should_Contain_Admin_User()
{
//Act
var result = await _userAppService.GetListAsync(new GetIdentityUsersInput());
//Assert
result.TotalCount.ShouldBeGreaterThan(0);
result.Items.ShouldContain(u => u.UserName == "admin");
}
}
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<RootNamespace>newProjectConfigure</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\newProjectConfigure.Application\newProjectConfigure.Application.csproj" />
<ProjectReference Include="..\newProjectConfigure.Domain.Tests\newProjectConfigure.Domain.Tests.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
</ItemGroup>
</Project>
@@ -0,0 +1,9 @@
using Volo.Abp.Modularity;
namespace newProjectConfigure;
public abstract class newProjectConfigureApplicationTestBase<TStartupModule> : newProjectConfigureTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
}
@@ -0,0 +1,12 @@
using Volo.Abp.Modularity;
namespace newProjectConfigure;
[DependsOn(
typeof(newProjectConfigureApplicationModule),
typeof(newProjectConfigureDomainTestModule)
)]
public class newProjectConfigureApplicationTestModule : AbpModule
{
}
@@ -0,0 +1,46 @@
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Identity;
using Volo.Abp.Modularity;
using Xunit;
namespace newProjectConfigure.Samples;
/* This is just an example test class.
* Normally, you don't test code of the modules you are using
* (like IdentityUserManager here).
* Only test your own domain services.
*/
public abstract class SampleDomainTests<TStartupModule> : newProjectConfigureDomainTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
private readonly IIdentityUserRepository _identityUserRepository;
private readonly IdentityUserManager _identityUserManager;
protected SampleDomainTests()
{
_identityUserRepository = GetRequiredService<IIdentityUserRepository>();
_identityUserManager = GetRequiredService<IdentityUserManager>();
}
[Fact]
public async Task Should_Set_Email_Of_A_User()
{
IdentityUser adminUser;
/* Need to manually start Unit Of Work because
* FirstOrDefaultAsync should be executed while db connection / context is available.
*/
await WithUnitOfWorkAsync(async () =>
{
adminUser = await _identityUserRepository
.FindByNormalizedUserNameAsync("ADMIN");
await _identityUserManager.SetEmailAsync(adminUser, "newemail@abp.io");
await _identityUserRepository.UpdateAsync(adminUser);
});
adminUser = await _identityUserRepository.FindByNormalizedUserNameAsync("ADMIN");
adminUser.Email.ShouldBe("newemail@abp.io");
}
}
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<RootNamespace>newProjectConfigure</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\newProjectConfigure.Domain\newProjectConfigure.Domain.csproj" />
<ProjectReference Include="..\newProjectConfigure.TestBase\newProjectConfigure.TestBase.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,10 @@
using Volo.Abp.Modularity;
namespace newProjectConfigure;
/* Inherit from this class for your domain layer tests. */
public abstract class newProjectConfigureDomainTestBase<TStartupModule> : newProjectConfigureTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
}
@@ -0,0 +1,12 @@
using Volo.Abp.Modularity;
namespace newProjectConfigure;
[DependsOn(
typeof(newProjectConfigureDomainModule),
typeof(newProjectConfigureTestBaseModule)
)]
public class newProjectConfigureDomainTestModule : AbpModule
{
}
@@ -0,0 +1,10 @@
using newProjectConfigure.Samples;
using Xunit;
namespace newProjectConfigure.EntityFrameworkCore.Applications;
[Collection(newProjectConfigureTestConsts.CollectionDefinitionName)]
public class EfCoreSampleAppServiceTests : SampleAppServiceTests<newProjectConfigureEntityFrameworkCoreTestModule>
{
}
@@ -0,0 +1,10 @@
using newProjectConfigure.Samples;
using Xunit;
namespace newProjectConfigure.EntityFrameworkCore.Domains;
[Collection(newProjectConfigureTestConsts.CollectionDefinitionName)]
public class EfCoreSampleDomainTests : SampleDomainTests<newProjectConfigureEntityFrameworkCoreTestModule>
{
}
@@ -0,0 +1,44 @@
using Microsoft.EntityFrameworkCore;
using Shouldly;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Xunit;
namespace newProjectConfigure.EntityFrameworkCore.Samples;
/* This is just an example test class.
* Normally, you don't test ABP framework code
* (like default AppUser repository IRepository<AppUser, Guid> here).
* Only test your custom repository methods.
*/
[Collection(newProjectConfigureTestConsts.CollectionDefinitionName)]
public class SampleRepositoryTests : newProjectConfigureEntityFrameworkCoreTestBase
{
private readonly IRepository<IdentityUser, Guid> _appUserRepository;
public SampleRepositoryTests()
{
_appUserRepository = GetRequiredService<IRepository<IdentityUser, Guid>>();
}
[Fact]
public async Task Should_Query_AppUser()
{
/* Need to manually start Unit Of Work because
* FirstOrDefaultAsync should be executed while db connection / context is available.
*/
await WithUnitOfWorkAsync(async () =>
{
//Act
var adminUser = await (await _appUserRepository.GetQueryableAsync())
.Where(u => u.UserName == "admin")
.FirstOrDefaultAsync();
//Assert
adminUser.ShouldNotBeNull();
});
}
}
@@ -0,0 +1,9 @@
using Xunit;
namespace newProjectConfigure.EntityFrameworkCore;
[CollectionDefinition(newProjectConfigureTestConsts.CollectionDefinitionName)]
public class newProjectConfigureEntityFrameworkCoreCollection : ICollectionFixture<newProjectConfigureEntityFrameworkCoreFixture>
{
}
@@ -0,0 +1,9 @@
using newProjectConfigure.EntityFrameworkCore;
using Xunit;
namespace newProjectConfigure.EntityFrameworkCore;
public class newProjectConfigureEntityFrameworkCoreCollectionFixtureBase : ICollectionFixture<newProjectConfigureEntityFrameworkCoreFixture>
{
}
@@ -0,0 +1,11 @@
using System;
namespace newProjectConfigure.EntityFrameworkCore;
public class newProjectConfigureEntityFrameworkCoreFixture : IDisposable
{
public void Dispose()
{
}
}
@@ -0,0 +1,8 @@
using Volo.Abp;
namespace newProjectConfigure.EntityFrameworkCore;
public abstract class newProjectConfigureEntityFrameworkCoreTestBase : newProjectConfigureTestBase<newProjectConfigureEntityFrameworkCoreTestModule>
{
}
@@ -0,0 +1,71 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Sqlite;
using Volo.Abp.FeatureManagement;
using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement;
using Volo.Abp.SettingManagement;
using Volo.Abp.Uow;
namespace newProjectConfigure.EntityFrameworkCore;
[DependsOn(
typeof(newProjectConfigureApplicationTestModule),
typeof(newProjectConfigureEntityFrameworkCoreModule),
typeof(AbpEntityFrameworkCoreSqliteModule)
)]
public class newProjectConfigureEntityFrameworkCoreTestModule : AbpModule
{
private AbpUnitTestSqliteDatabase _database;
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<FeatureManagementOptions>(options =>
{
options.SaveStaticFeaturesToDatabase = false;
options.IsDynamicFeatureStoreEnabled = false;
});
Configure<PermissionManagementOptions>(options =>
{
options.SaveStaticPermissionsToDatabase = false;
options.IsDynamicPermissionStoreEnabled = false;
});
Configure<SettingManagementOptions>(options =>
{
options.SaveStaticSettingsToDatabase = false;
options.IsDynamicSettingStoreEnabled = false;
});
context.Services.AddAlwaysDisableUnitOfWorkTransaction();
ConfigureInMemorySqlite(context.Services);
}
private void ConfigureInMemorySqlite(IServiceCollection services)
{
_database = new AbpUnitTestSqliteDatabase();
_database.CreateTables(
new newProjectConfigureDbContext(new DbContextOptionsBuilder<newProjectConfigureDbContext>().UseSqlite(_database.ConnectionString).Options));
services.Configure<AbpDbConnectionOptions>(options =>
{
options.ConnectionStrings.Default = _database.ConnectionString;
});
services.Configure<AbpDbContextOptions>(options =>
{
options.Configure(context =>
{
context.UseSqlite();
});
});
}
public override void OnApplicationShutdown(ApplicationShutdownContext context)
{
_database?.Dispose();
}
}
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<RootNamespace>newProjectConfigure</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\newProjectConfigure.EntityFrameworkCore\newProjectConfigure.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\newProjectConfigure.Application.Tests\newProjectConfigure.Application.Tests.csproj" />
<PackageReference Include="Volo.Abp.EntityFrameworkCore.Sqlite" Version="10.6.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
</ItemGroup>
</Project>
@@ -0,0 +1,25 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Account;
using Volo.Abp.DependencyInjection;
namespace newProjectConfigure.HttpApi.Client.ConsoleTestApp;
public class ClientDemoService : ITransientDependency
{
private readonly IProfileAppService _profileAppService;
public ClientDemoService(IProfileAppService profileAppService)
{
_profileAppService = profileAppService;
}
public async Task RunAsync()
{
var output = await _profileAppService.GetAsync();
Console.WriteLine($"UserName : {output.UserName}");
Console.WriteLine($"Email : {output.Email}");
Console.WriteLine($"Name : {output.Name}");
Console.WriteLine($"Surname : {output.Surname}");
}
}
@@ -0,0 +1,40 @@
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
namespace newProjectConfigure.HttpApi.Client.ConsoleTestApp;
public class ConsoleTestAppHostedService : IHostedService
{
private readonly IConfiguration _configuration;
public ConsoleTestAppHostedService(IConfiguration configuration)
{
_configuration = configuration;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
using (var application = await AbpApplicationFactory.CreateAsync<newProjectConfigureConsoleApiClientModule>(options =>
{
options.Services.ReplaceConfiguration(_configuration);
options.UseAutofac();
}))
{
await application.InitializeAsync();
var demo = application.ServiceProvider.GetRequiredService<ClientDemoService>();
await demo.RunAsync();
await application.ShutdownAsync();
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
@@ -0,0 +1,22 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace newProjectConfigure.HttpApi.Client.ConsoleTestApp;
class Program
{
static async Task Main(string[] args)
{
await CreateHostBuilder(args).RunConsoleAsync();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.AddAppSettingsSecretsJson()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<ConsoleTestAppHostedService>();
});
}
@@ -0,0 +1,17 @@
{
"RemoteServices": {
"Default": {
"BaseUrl": "https://localhost:44355"
}
},
"IdentityClients": {
"Default": {
"GrantType": "password",
"ClientId": "newProjectConfigure_App",
"UserName": "admin",
"UserPassword": "1q2w3E*",
"Authority": "https://localhost:44355",
"Scope": "newProjectConfigure"
}
}
}
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
<Content Include="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Remove="appsettings.secrets.json" />
<Content Include="appsettings.secrets.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.Http.Client.IdentityModel" Version="10.6.0" />
<PackageReference Include="Volo.Abp.Autofac" Version="10.6.0" />
<ProjectReference Include="..\..\src\newProjectConfigure.HttpApi.Client\newProjectConfigure.HttpApi.Client.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.9" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="10.0.9" />
</ItemGroup>
</Project>
@@ -0,0 +1,30 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Polly;
using Volo.Abp.Autofac;
using Volo.Abp.Http.Client;
using Volo.Abp.Http.Client.IdentityModel;
using Volo.Abp.Modularity;
namespace newProjectConfigure.HttpApi.Client.ConsoleTestApp;
[DependsOn(
typeof(AbpAutofacModule),
typeof(newProjectConfigureHttpApiClientModule),
typeof(AbpHttpClientIdentityModelModule)
)]
public class newProjectConfigureConsoleApiClientModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
PreConfigure<AbpHttpClientBuilderOptions>(options =>
{
options.ProxyClientBuildActions.Add((remoteServiceName, clientBuilder) =>
{
clientBuilder.AddTransientHttpErrorPolicy(
policyBuilder => policyBuilder.WaitAndRetryAsync(3, i => TimeSpan.FromSeconds(Math.Pow(2, i)))
);
});
});
}
}
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Security.Claims;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Security.Claims;
namespace newProjectConfigure.Security;
[Dependency(ReplaceServices = true)]
public class FakeCurrentPrincipalAccessor : ThreadCurrentPrincipalAccessor
{
protected override ClaimsPrincipal GetClaimsPrincipal()
{
return GetPrincipal();
}
private ClaimsPrincipal GetPrincipal()
{
return new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
{
new Claim(AbpClaimTypes.UserId, "2e701e62-0953-4dd3-910b-dc6cc93ccb0d"),
new Claim(AbpClaimTypes.UserName, "admin"),
new Claim(AbpClaimTypes.Email, "admin@abp.io")
}));
}
}
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<RootNamespace>newProjectConfigure</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.TestBase" Version="10.6.0" />
<PackageReference Include="Volo.Abp.Autofac" Version="10.6.0" />
<PackageReference Include="Volo.Abp.Authorization" Version="10.6.0" />
<PackageReference Include="Volo.Abp.BackgroundJobs.Abstractions" Version="10.6.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.extensibility.execution" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
</Project>
@@ -0,0 +1,60 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Modularity;
using Volo.Abp.Uow;
using Volo.Abp.Testing;
namespace newProjectConfigure;
/* All test classes are derived from this class, directly or indirectly.
*/
public abstract class newProjectConfigureTestBase<TStartupModule> : AbpIntegratedTest<TStartupModule>
where TStartupModule : IAbpModule
{
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options)
{
options.UseAutofac();
}
protected virtual Task WithUnitOfWorkAsync(Func<Task> func)
{
return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(), func);
}
protected virtual async Task WithUnitOfWorkAsync(AbpUnitOfWorkOptions options, Func<Task> action)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
await action();
await uow.CompleteAsync();
}
}
}
protected virtual Task<TResult> WithUnitOfWorkAsync<TResult>(Func<Task<TResult>> func)
{
return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(), func);
}
protected virtual async Task<TResult> WithUnitOfWorkAsync<TResult>(AbpUnitOfWorkOptions options, Func<Task<TResult>> func)
{
using (var scope = ServiceProvider.CreateScope())
{
var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
using (var uow = uowManager.Begin(options))
{
var result = await func();
await uow.CompleteAsync();
return result;
}
}
}
}
@@ -0,0 +1,47 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Authorization;
using Volo.Abp.Autofac;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Data;
using Volo.Abp.Modularity;
using Volo.Abp.Threading;
namespace newProjectConfigure;
[DependsOn(
typeof(AbpAutofacModule),
typeof(AbpTestBaseModule),
typeof(AbpAuthorizationModule),
typeof(AbpBackgroundJobsAbstractionsModule)
)]
public class newProjectConfigureTestBaseModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpBackgroundJobOptions>(options =>
{
options.IsJobExecutionEnabled = false;
});
context.Services.AddAlwaysAllowAuthorization();
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
SeedTestData(context);
}
private static void SeedTestData(ApplicationInitializationContext context)
{
AsyncHelper.RunSync(async () =>
{
using (var scope = context.ServiceProvider.CreateScope())
{
await scope.ServiceProvider
.GetRequiredService<IDataSeeder>()
.SeedAsync();
}
});
}
}
@@ -0,0 +1,6 @@
namespace newProjectConfigure;
public static class newProjectConfigureTestConsts
{
public const string CollectionDefinitionName = "newProjectConfigure collection";
}
@@ -0,0 +1,15 @@
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
namespace newProjectConfigure;
public class newProjectConfigureTestDataSeedContributor : IDataSeedContributor, ITransientDependency
{
public Task SeedAsync(DataSeedContext context)
{
/* Seed additional test data... */
return Task.CompletedTask;
}
}