Skip to content

Commit

Permalink
sonar-findings and unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ntruchsess committed Aug 4, 2023
1 parent cb14634 commit f960e25
Show file tree
Hide file tree
Showing 17 changed files with 595 additions and 585 deletions.
84 changes: 0 additions & 84 deletions src/framework/Framework.Async/AsyncAnyExtensions.cs

This file was deleted.

42 changes: 0 additions & 42 deletions src/framework/Framework.Async/AsyncWhereExtensions.cs

This file was deleted.

41 changes: 0 additions & 41 deletions src/framework/Framework.Async/TasksToAsyncEnumerableExtension.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/framework/Framework.Models/HasNextEnumeratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Framework.Models;

public static class HasNextEnumeratorExtensions
{
private sealed class HasNextEnumeratorWrapper<T> : IHasNextEnumerator<T>, IDisposable
private sealed class HasNextEnumeratorWrapper<T> : IHasNextEnumerator<T>
{
private readonly IEnumerator<T> _enumerator;
private bool _hasNext;
Expand Down Expand Up @@ -54,7 +54,7 @@ public T Current
public static IHasNextEnumerator<T> GetHasNextEnumerator<T>(this IEnumerable<T> source) => new HasNextEnumeratorWrapper<T>(source);
}

public interface IHasNextEnumerator<out T>
public interface IHasNextEnumerator<out T> : IDisposable
{
void Advance();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Task UpdateAuthenticationFlows(string keycloakInstanceName, CancellationT
return handler.UpdateAuthenticationFlows(cancellationToken);
}

private class AuthenticationFlowHandler
private sealed class AuthenticationFlowHandler
{
private readonly string _realm;
private readonly KeycloakClient _keycloak;
Expand All @@ -68,69 +68,71 @@ public async Task UpdateAuthenticationFlows(CancellationToken cancellationToken)
var seedFlows = _seedData.TopLevelCustomAuthenticationFlows;
var topLevelCustomFlows = flows.Where(flow => !(flow.BuiltIn ?? false) && (flow.TopLevel ?? false));

if (topLevelCustomFlows.ExceptBy(seedFlows.Select(x => x.Alias), x => x.Alias).IfAny(
async deleteFlows =>
{
foreach (var delete in deleteFlows)
{
if (delete.Id == null)
throw new ConflictException($"authenticationFlow.id is null {delete.Alias} {delete.Description}");
await _keycloak.DeleteAuthenticationFlowAsync(_realm, delete.Id, cancellationToken).ConfigureAwait(false);
}
},
out var deleteFlowsTask
))
await DeleteRedundantAuthenticationFlows(topLevelCustomFlows, seedFlows, cancellationToken).ConfigureAwait(false);
await AddMissingAuthenticationFlows(topLevelCustomFlows, seedFlows, cancellationToken).ConfigureAwait(false);
await UpdateExistingAuthenticationFlows(topLevelCustomFlows, seedFlows, cancellationToken).ConfigureAwait(false);
}

private async Task DeleteRedundantAuthenticationFlows(IEnumerable<AuthenticationFlow> topLevelCustomFlows, IEnumerable<AuthenticationFlowModel> seedFlows, CancellationToken cancellationToken)
{
foreach (var delete in topLevelCustomFlows.ExceptBy(seedFlows.Select(x => x.Alias), x => x.Alias))
{
await deleteFlowsTask!.ConfigureAwait(false);
if (delete.Id == null)
throw new ConflictException($"authenticationFlow.id is null {delete.Alias} {delete.Description}");
await _keycloak.DeleteAuthenticationFlowAsync(_realm, delete.Id, cancellationToken).ConfigureAwait(false);
}
}

if (seedFlows.ExceptBy(topLevelCustomFlows.Select(x => x.Alias), x => x.Alias).IfAny(
async addFlows =>
{
foreach (var addFlow in addFlows)
{
if (addFlow.Alias == null)
throw new ConflictException($"authenticationFlow.Alias is null {addFlow.Id} {addFlow.Description}");
if (addFlow.BuiltIn ?? false)
throw new ConflictException($"authenticationFlow.buildIn is true. flow cannot be added: {addFlow.Alias}");
await _keycloak.CreateAuthenticationFlowAsync(_realm, CreateUpdateAuthenticationFlow(null, addFlow), cancellationToken).ConfigureAwait(false);
await UpdateAuthenticationFlowExecutions(addFlow.Alias, cancellationToken);
}
},
out var addFlowsTasks
))
private async Task AddMissingAuthenticationFlows(IEnumerable<AuthenticationFlow> topLevelCustomFlows, IEnumerable<AuthenticationFlowModel> seedFlows, CancellationToken cancellationToken)
{
foreach (var addFlow in seedFlows.ExceptBy(topLevelCustomFlows.Select(x => x.Alias), x => x.Alias))
{
await addFlowsTasks!.ConfigureAwait(false);
if (addFlow.Alias == null)
throw new ConflictException($"authenticationFlow.Alias is null {addFlow.Id} {addFlow.Description}");
if (addFlow.BuiltIn ?? false)
throw new ConflictException($"authenticationFlow.buildIn is true. flow cannot be added: {addFlow.Alias}");
await _keycloak.CreateAuthenticationFlowAsync(_realm, CreateUpdateAuthenticationFlow(null, addFlow), cancellationToken).ConfigureAwait(false);
await UpdateAuthenticationFlowExecutions(addFlow.Alias, cancellationToken);
}
}

if (topLevelCustomFlows.Join(
private async Task UpdateExistingAuthenticationFlows(IEnumerable<AuthenticationFlow> topLevelCustomFlows, IEnumerable<AuthenticationFlowModel> seedFlows, CancellationToken cancellationToken)
{
foreach (var (flow, seed) in topLevelCustomFlows
.Join(
seedFlows,
x => x.Alias,
x => x.Alias,
(flow, seed) => (Flow: flow, Seed: seed)
).IfAny(
async updateFlows =>
{
foreach (var (flow, seed) in updateFlows)
{
if (flow.Id == null)
throw new ConflictException($"authenticationFlow.id is null {flow.Alias} {flow.Description}");
if (flow.Alias == null)
throw new ConflictException($"authenticationFlow.Alias is null {flow.Id} {flow.Description}");
if (!Compare(flow, seed))
{
await _keycloak.UpdateAuthenticationFlowAsync(_realm, flow.Id, CreateUpdateAuthenticationFlow(flow.Id, seed), cancellationToken).ConfigureAwait(false);
}
await UpdateAuthenticationFlowExecutions(flow.Alias, cancellationToken);
}
},
out var updateFlowsTask
))
(flow, seed) => (Flow: flow, Seed: seed)))
{
await updateFlowsTask!.ConfigureAwait(false);
if (flow.Id == null)
throw new ConflictException($"authenticationFlow.id is null {flow.Alias} {flow.Description}");
if (flow.Alias == null)
throw new ConflictException($"authenticationFlow.Alias is null {flow.Id} {flow.Description}");
if (!CompareAuthenticationFlow(flow, seed))
{
await _keycloak.UpdateAuthenticationFlowAsync(_realm, flow.Id, CreateUpdateAuthenticationFlow(flow.Id, seed), cancellationToken).ConfigureAwait(false);
}
await UpdateAuthenticationFlowExecutions(flow.Alias, cancellationToken);
}
}

private static AuthenticationFlow CreateUpdateAuthenticationFlow(string? id, AuthenticationFlowModel update) => new AuthenticationFlow
{
Id = id,
Alias = update.Alias,
BuiltIn = update.BuiltIn,
Description = update.Description,
ProviderId = update.ProviderId,
TopLevel = update.TopLevel
};

private static bool CompareAuthenticationFlow(AuthenticationFlow flow, AuthenticationFlowModel update) =>
flow.BuiltIn == update.BuiltIn &&
flow.Description == update.Description &&
flow.ProviderId == update.ProviderId &&
flow.TopLevel == update.TopLevel;

private async Task UpdateAuthenticationFlowExecutions(string alias, CancellationToken cancellationToken)
{
var updateExecutions = _seedData.GetAuthenticationExecutions(alias);
Expand Down Expand Up @@ -345,7 +347,7 @@ private static IDictionary<string, object> CreateDataWithProvider(Authentication
{ "provider", execution.Authenticator ?? throw new ConflictException("authenticationExecution.Authenticator is null")}
};

private class ExecutionNode
private sealed class ExecutionNode
{
private readonly AuthenticationFlowExecution _execution;
private readonly IReadOnlyList<ExecutionNode>? _children;
Expand Down Expand Up @@ -396,20 +398,4 @@ private static IEnumerable<ExecutionNode> ParseInternal(IHasNextEnumerator<Authe
}
}
}

private static AuthenticationFlow CreateUpdateAuthenticationFlow(string? id, AuthenticationFlowModel update) => new AuthenticationFlow
{
Id = id,
Alias = update.Alias,
BuiltIn = update.BuiltIn,
Description = update.Description,
ProviderId = update.ProviderId,
TopLevel = update.TopLevel
};

private static bool Compare(AuthenticationFlow flow, AuthenticationFlowModel update) =>
flow.BuiltIn == update.BuiltIn &&
flow.Description == update.Description &&
flow.ProviderId == update.ProviderId &&
flow.TopLevel == update.TopLevel;
}
Loading

0 comments on commit f960e25

Please sign in to comment.