Skip to content

Commit

Permalink
use nullable (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
iatsuta authored Sep 20, 2024
1 parent 61fab6f commit b84b893
Show file tree
Hide file tree
Showing 68 changed files with 242 additions and 633 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<PackageId>Luxoft.Framework.AutomationCore.ServiceEnvironment.LegacyContext</PackageId>
<RootNamespace>Automation.ServiceEnvironmen</RootNamespace>
<nullable>enable</nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IRootServiceProviderContainer<out TBLLContext> : IRootServicePr
{
async Task<TResult> IServiceEvaluator<TBLLContext>.EvaluateAsync<TResult>(
DBSessionMode sessionMode,
string customPrincipalName,
string? customPrincipalName,
Func<TBLLContext, Task<TResult>> getResult)
{
return await this.RootServiceProvider.GetRequiredService<IServiceEvaluator<TBLLContext>>().EvaluateAsync(sessionMode, customPrincipalName, getResult);
Expand All @@ -21,7 +21,7 @@ public interface IRootServiceProviderContainer<TBLLContext, TMappingService> : I
{
async Task<TResult> IContextEvaluator<TBLLContext, TMappingService>.EvaluateAsync<TResult>(
DBSessionMode sessionMode,
string customPrincipalName,
string? customPrincipalName,
Func<EvaluatedData<TBLLContext, TMappingService>, Task<TResult>> getResult)
{
return await this.RootServiceProvider.GetRequiredService<IContextEvaluator<TBLLContext, TMappingService>>().EvaluateAsync(sessionMode, customPrincipalName, getResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<PackageId>Luxoft.Framework.AutomationCore.ServiceEnvironment</PackageId>
<RootNamespace>Automation.ServiceEnvironment</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public string GetCurrentUserLogin()
return this.ManagerEvaluator.Evaluate(DBSessionMode.Read, manager => manager.GetCurrentUserLogin());
}

public void LoginAs(string principalName = null)
public void LoginAs(string? principalName = null)
{
this.UserAuthenticationService.SetUserName(principalName);
}
Expand All @@ -40,28 +40,23 @@ public async Task<Guid> SavePrincipalAsync(string name, CancellationToken cancel
return await this.ManagerEvaluator.EvaluateAsync(DBSessionMode.Write, async manger => await manger.SavePrincipalAsync(name, cancellationToken));
}

public void AddUserRole(string principalName, params TestPermission[] permissions)
public void AddUserRole(string? principalName, params TestPermission[] permissions)
{
this.AddUserRoleAsync(principalName, permissions).GetAwaiter().GetResult();
}

public async Task AddUserRoleAsync(string principalName, TestPermission[] permissions, CancellationToken cancellationToken = default)
public async Task AddUserRoleAsync(string? principalName, TestPermission[] permissions, CancellationToken cancellationToken = default)
{
await this.ManagerEvaluator.EvaluateAsync(DBSessionMode.Write, async manger => await manger.AddUserRoleAsync(principalName, permissions, cancellationToken));
}

public virtual void AddUserToAdmin(string principalName)
public virtual void AddUserToAdmin(string? principalName)
{
this.SetUserRole(principalName, SecurityRole.Administrator, SecurityRole.SystemIntegration);
}

public void SetUserRole(string principalName, params TestPermission[] permissions)
public void SetUserRole(string? principalName, params TestPermission[] permissions)
{
if (permissions == null)
{
throw new ArgumentNullException(nameof(permissions));
}

this.RemovePermissions(principalName);

this.AddUserRole(principalName, permissions);
Expand All @@ -78,12 +73,12 @@ public void SetCurrentUserRole(params TestPermission[] permissions)
this.SetUserRole(default, permissions);
}

public void RemovePermissions(string principalName)
public void RemovePermissions(string? principalName)
{
this.RemovePermissionsAsync(principalName).GetAwaiter().GetResult();
}

public async Task RemovePermissionsAsync(string principalName, CancellationToken cancellationToken = default)
public async Task RemovePermissionsAsync(string? principalName, CancellationToken cancellationToken = default)
{
await this.ManagerEvaluator.EvaluateAsync(DBSessionMode.Write, async manager => await manager.RemovePermissionsAsync(principalName, cancellationToken));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<Guid> SavePrincipalAsync(string name, CancellationToken cancel
return principal.Id;
}

public async Task AddUserRoleAsync(string principalName, TestPermission[] testPermissions, CancellationToken cancellationToken = default)
public async Task AddUserRoleAsync(string? principalName, TestPermission[] testPermissions, CancellationToken cancellationToken = default)
{
var principal = await principalDomainService.GetOrCreateAsync(principalName ?? this.GetCurrentUserLogin(), cancellationToken);

Expand Down Expand Up @@ -62,7 +62,7 @@ public async Task AddUserRoleAsync(string principalName, TestPermission[] testPe
await principalDomainService.SaveAsync(principal, cancellationToken);
}

public async Task RemovePermissionsAsync(string principalName, CancellationToken cancellationToken = default)
public async Task RemovePermissionsAsync(string? principalName, CancellationToken cancellationToken = default)
{
var currentUserName = principalName ?? this.GetCurrentUserLogin();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@

namespace Automation.ServiceEnvironment;

public abstract class RootServiceProviderContainer : IRootServiceProviderContainer
public abstract class RootServiceProviderContainer(IServiceProvider rootServiceProvider) : IRootServiceProviderContainer
{
public RootServiceProviderContainer(IServiceProvider rootServiceProvider)
{
this.RootServiceProvider = rootServiceProvider;
}

public virtual IServiceProvider RootServiceProvider { get; }
public virtual IServiceProvider RootServiceProvider { get; } = rootServiceProvider;

public AutomationFrameworkSettings AutomationFrameworkSettings => this.GetAutomationFrameworkSettings();

Expand All @@ -25,7 +20,7 @@ public RootServiceProviderContainer(IServiceProvider rootServiceProvider)

public IDatabaseContext DatabaseContext => this.GetDatabaseContext();

public virtual ControllerEvaluator<TController> GetControllerEvaluator<TController>(string principalName = null)
public virtual ControllerEvaluator<TController> GetControllerEvaluator<TController>(string? principalName = null)
where TController : ControllerBase
{
return this.RootServiceProvider.GetDefaultControllerEvaluator<TController>(principalName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,12 @@

namespace Automation.ServiceEnvironment;

public class ControllerEvaluator<TController>
where TController : ControllerBase
public class ControllerEvaluator<TController>(IServiceProvider rootServiceProvider, string? customPrincipalName = null)
where TController : ControllerBase
{
private readonly IServiceProvider rootServiceProvider;

private readonly string customPrincipalName;

public ControllerEvaluator(IServiceProvider rootServiceProvider)
: this(rootServiceProvider, null)
{
}

private ControllerEvaluator(IServiceProvider rootServiceProvider, string customPrincipalName)
{
this.rootServiceProvider = rootServiceProvider ?? throw new ArgumentNullException(nameof(rootServiceProvider));
this.customPrincipalName = customPrincipalName;
}

public void Evaluate(Expression<Action<TController>> actionExpr)
{
this.InternalEvaluateAsync<object>(actionExpr, async c =>
this.InternalEvaluateAsync<object?>(actionExpr, async c =>
{
actionExpr.Eval(c);
return default;
Expand All @@ -48,7 +33,7 @@ public T Evaluate<T>(Expression<Func<TController, T>> funcExpr)

public async Task EvaluateAsync(Expression<Func<TController, Task>> actionExpr)
{
await this.InternalEvaluateAsync<object>(actionExpr, async c =>
await this.InternalEvaluateAsync<object?>(actionExpr, async c =>
{
await actionExpr.Eval(c);
return default;
Expand All @@ -62,18 +47,18 @@ public async Task<T> EvaluateAsync<T>(Expression<Func<TController, Task<T>>> fun

private async Task<T> InternalEvaluateAsync<T>(LambdaExpression invokeExpr, Func<TController, Task<T>> func)
{
await using var scope = this.rootServiceProvider.CreateAsyncScope();
await using var scope = rootServiceProvider.CreateAsyncScope();

var c = new DefaultHttpContext { RequestServices = scope.ServiceProvider };

await new WebApiInvoker(c, context => InvokeController(context, func))
.WithMiddleware(next => new ImpersonateMiddleware(next), (middleware, httpContext) => middleware.Invoke(httpContext, this.customPrincipalName))
.WithMiddleware(next => new ImpersonateMiddleware(next), (middleware, httpContext) => middleware.Invoke(httpContext, customPrincipalName))
.WithMiddleware(next => new TryProcessDbSessionMiddleware(next), (middleware, httpContext) => middleware.Invoke(httpContext, httpContext.RequestServices.GetRequiredService<IDBSessionManager>(), httpContext.RequestServices.GetRequiredService<IWebApiDBSessionModeResolver>()))
.WithMiddleware(next => new InitCurrentMethodMiddleware(next), (middleware, httpContext) => middleware.Invoke(httpContext, invokeExpr))
.WithMiddleware(next => new WebApiExceptionExpanderMiddleware(next), (middleware, httpContext) => middleware.Invoke(httpContext, httpContext.RequestServices.GetRequiredService<IWebApiExceptionExpander>()))
.Invoke();

return (T)c.Items["Result"];
return (T)c.Items["Result"]!;
}

private static async Task InvokeController<T>(HttpContext context, Func<TController, Task<T>> func)
Expand All @@ -89,44 +74,30 @@ private static async Task InvokeController<T>(HttpContext context, Func<TControl

public ControllerEvaluator<TController> WithImpersonate(string newCustomPrincipalName)
{
return new ControllerEvaluator<TController>(this.rootServiceProvider, newCustomPrincipalName);
return new ControllerEvaluator<TController>(rootServiceProvider, newCustomPrincipalName);
}

private class ImpersonateMiddleware
private class ImpersonateMiddleware(RequestDelegate next)
{
private readonly RequestDelegate next;

public ImpersonateMiddleware(RequestDelegate next)
{
this.next = next;
}

public async Task Invoke(HttpContext context, string customPrincipalName)
public async Task Invoke(HttpContext context, string? customPrincipalName)
{
if (customPrincipalName == null)
{
await this.next(context);
await next(context);
}
else
{
await context.RequestServices.GetRequiredService<IIntegrationTestUserAuthenticationService>().WithImpersonateAsync(customPrincipalName, async () =>
{
await this.next(context);
await next(context);
return default(object);
});
}
}
}

private class InitCurrentMethodMiddleware
private class InitCurrentMethodMiddleware(RequestDelegate next)
{
private readonly RequestDelegate next;

public InitCurrentMethodMiddleware(RequestDelegate next)
{
this.next = next;
}

public async Task Invoke(HttpContext context, LambdaExpression invokeExpr)
{
var currentMethod = invokeExpr.UpdateBodyBase(ExpandConstVisitor.Value)
Expand All @@ -135,30 +106,20 @@ public async Task Invoke(HttpContext context, LambdaExpression invokeExpr)

context.RequestServices.GetRequiredService<TestWebApiCurrentMethodResolver>().SetCurrentMethod(currentMethod);

await this.next(context);
await next(context);
}
}

private class WebApiInvoker
private class WebApiInvoker(HttpContext context, RequestDelegate next)
{
private readonly HttpContext context;

private readonly RequestDelegate next;

public WebApiInvoker(HttpContext context, RequestDelegate next)
{
this.context = context;
this.next = next;
}

public WebApiInvoker WithMiddleware<TMiddleware>(Func<RequestDelegate, TMiddleware> createFunc, Func<TMiddleware, HttpContext, Task> invokeDelegate)
{
return new WebApiInvoker(this.context, c => invokeDelegate(createFunc(this.next), c));
return new WebApiInvoker(context, c => invokeDelegate(createFunc(next), c));
}

public async Task Invoke()
{
await this.next(this.context);
await next(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static async Task RunJob<TJob>(this IServiceProvider rootServiceProvider,

public static ControllerEvaluator<TController> GetDefaultControllerEvaluator<TController>(
this IServiceProvider rootServiceProvider,
string principalName = null)
string? principalName = null)
where TController : ControllerBase
{
var controllerEvaluator = rootServiceProvider.GetRequiredService<ControllerEvaluator<TController>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Automation.ServiceEnvironment.Services;

public interface IIntegrationTestUserAuthenticationService : IDefaultUserAuthenticationService, IAuditRevisionUserAuthenticationService, IUserAuthenticationService
{
void SetUserName(string customUserName);
void SetUserName(string? customUserName);

void Reset();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class IntegrationTestUserAuthenticationService(IOptions<AutomationFramewo
{
private string IntegrationTestUserName => settings.Value.IntegrationTestUserName;

public string CustomUserName { get; internal set; }
public string? CustomUserName { get; internal set; }

public void SetUserName(string customUserName) => this.CustomUserName = customUserName ?? this.IntegrationTestUserName;
public void SetUserName(string? customUserName) => this.CustomUserName = customUserName ?? this.IntegrationTestUserName;

public void Reset() => this.CustomUserName = this.IntegrationTestUserName;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
namespace Automation.ServiceEnvironment;

public abstract class IntegrationWebApiBase : WebApiBase
public abstract class IntegrationWebApiBase(IServiceProvider rootServiceProvider) : WebApiBase(rootServiceProvider)
{
protected IntegrationWebApiBase(IServiceProvider rootServiceProvider)
: base(rootServiceProvider)
{
}

protected abstract string IntegrationUserName { get; }

public override ControllerEvaluator<TController> GetControllerEvaluator<TController>(string principalName = null) =>
public override ControllerEvaluator<TController> GetControllerEvaluator<TController>(string? principalName = null) =>
base.GetControllerEvaluator<TController>(principalName ?? this.IntegrationUserName);
}
20 changes: 12 additions & 8 deletions src/Framework.AutomationCore/Environment/TestEnvironmentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ namespace Automation;

public class TestEnvironmentBuilder
{
private IConfiguration withConfiguration;
private Func<IConfiguration, IServiceCollection, IServiceCollection> withServiceProviderBuildFunc;
private Action<IServiceProvider> withServiceProvAfterBuildAction;
private Action<AutomationFrameworkSettings> withAutomationFrameworkSettings;
private Type withDatabaseGenerator;
private IConfiguration? withConfiguration;

private Func<IConfiguration, IServiceCollection, IServiceCollection>? withServiceProviderBuildFunc;

private Action<IServiceProvider> withServiceProvAfterBuildAction = _ => { };

private Action<AutomationFrameworkSettings> withAutomationFrameworkSettings = _ => { };

private Type? withDatabaseGenerator;

public TestEnvironmentBuilder WithConfiguration(IConfiguration rootConfiguration)
{
Expand Down Expand Up @@ -87,7 +91,7 @@ public TestEnvironment Build()
}

var serviceProviderPool = this.GetServiceProviderPool(
this.withConfiguration,
this.withConfiguration!,
this.withServiceProviderBuildFunc,
this.withServiceProvAfterBuildAction,
this.withDatabaseGenerator,
Expand Down Expand Up @@ -151,7 +155,7 @@ private IServiceProvider ServiceProviderGenerationFunc(

var environmentServiceProvider = this.BuildServiceProvider(environmentServices);

serviceProviderAfterBuildAction?.Invoke(environmentServiceProvider);
serviceProviderAfterBuildAction(environmentServiceProvider);

return environmentServiceProvider;
}
Expand All @@ -160,7 +164,7 @@ private static IOptions<AutomationFrameworkSettings> GetSettings(IConfiguration
{
var settings = new AutomationFrameworkSettings();
configuration.GetSection(nameof(AutomationFrameworkSettings)).Bind(settings);
action?.Invoke(settings);
action(settings);

return Options.Create(settings);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Framework.AutomationCore/Framework.AutomationCore.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageId>Luxoft.Framework.AutomationCore</PackageId>
<RootNamespace>Automation</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\__SolutionItems\CommonAssemblyInfo.cs" Link="Properties\CommonAssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AutomationFrameworkSettings

public string DatabaseCollation { get; set; } = "";

public string TestRunServerRootFolder { get; set; }
public string TestRunServerRootFolder { get; set; } = default!;

public string DbDataDirectory => Path.Combine(this.TestRunServerRootFolder, "data");

Expand Down
Loading

0 comments on commit b84b893

Please sign in to comment.