Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
# Build Files
JwtAuthenticationApi/bin/*
JwtAuthenticationApi/obj/*
JwtAuthenticationApi.UnitTests/bin/*
JwtAuthenticationApi.UnitTests/obj/*
JwtAuthenticationApi.Services/bin/*
JwtAuthenticationApi.Services/obj/*
JwtAuthenticationApi.Common/bin/*
JwtAuthenticationApi.Common/obj/*
JwtAuthenticationApi.Controllers/bin/*
JwtAuthenticationApi.Controllers/obj/*
JwtAuthenticationApi.Security/bin/*
JwtAuthenticationApi.Security/obj/*
JwtAuthenticationApi.Infrastructure/bin/*
JwtAuthenticationApi.Infrastructure/obj/*

# Build Files - Tests Projects
JwtAuthenticationApi.Services.Tests/bin/*
JwtAuthenticationApi.Services.Tests/obj/*
JwtAuthenticationApi.Common.Tests/bin/*
JwtAuthenticationApi.Common.Tests/obj/*
JwtAuthenticationApi.Controllers.Tests/bin/*
JwtAuthenticationApi.Controllers.Tests/obj/*
JwtAuthenticationApi.Security.Tests/bin/*
JwtAuthenticationApi.Security.Tests/obj/*
JwtAuthenticationApi.Infrastructure.Tests/bin/*
JwtAuthenticationApi.Infrastructure.Tests/obj/*

# Migrations
JwtAuthenticationApi/Migrations/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace JwtAuthenticationApi.UnitTests.Factories.Polly
namespace JwtAuthenticationApi.Common.Tests.Factories.Polly
{
using JwtAuthenticationApi.Factories.Polly;

using Common.Factories.Polly;
using FluentAssertions;
using NUnit.Framework;

[TestFixture, Parallelizable]
public class PollySleepingIntervalsFactoryTests
Expand All @@ -16,7 +17,7 @@ public void SetUp()

[TestCase(1u,6u)]
[TestCase(6u,9u)]
public void ShouldCreateConstantSleepingInterval(uint sleepTime, uint totalRetries)
public void CreateConstantInterval_CreatesConstantSleepingInterval(uint sleepTime, uint totalRetries)
{
// Act
var actual = _uut.CreateConstantInterval(sleepTime, totalRetries).ToList();
Expand All @@ -32,7 +33,7 @@ public void ShouldCreateConstantSleepingInterval(uint sleepTime, uint totalRetri

[TestCase(2u,1u, 6u)]
[TestCase(3u,4u, 9u)]
public void ShouldCreateLinearSleepingInterval(uint baseSleepTime, uint sleepIncreaseTime, uint totalRetries)
public void CreateLinearInterval_CreateLinearSleepingInterval(uint baseSleepTime, uint sleepIncreaseTime, uint totalRetries)
{
// Act
var actual = _uut.CreateLinearInterval(baseSleepTime,sleepIncreaseTime, totalRetries).ToList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace JwtAuthenticationApi.UnitTests.Factories.Wrappers
namespace JwtAuthenticationApi.Common.Tests.Factories.Wrappers
{
using JwtAuthenticationApi.Factories.Wrappers;
using JwtAuthenticationApi.Wrappers.Threading;
using Abstraction.Wrappers.Threading;
using Common.Factories.Wrappers;
using Common.Wrappers.Threading;
using FluentAssertions;
using NUnit.Framework;

[TestFixture, Parallelizable]
[TestFixture, Parallelizable]
public class MutexWrapperFactoryTests
{
private MutexWrapperFactory _uut;
Expand All @@ -16,7 +19,7 @@ public void SetUp()

[TestCase(true, "Name")]
[TestCase(false, "")]
public void ShouldCreateMutexWrapper(bool initiallyOwned, string name)
public void Create_CreatesMutexWrapper(bool initiallyOwned, string name)
{
// Act
using IMutexWrapper actual = _uut.Create(initiallyOwned, name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using JwtAuthenticationApi.Factories.Wrappers;
using JwtAuthenticationApi.Wrappers.Threading;

namespace JwtAuthenticationApi.UnitTests.Factories.Wrappers
namespace JwtAuthenticationApi.Common.Tests.Factories.Wrappers
{
using Abstraction.Wrappers.Threading;
using Common.Factories.Wrappers;
using Common.Wrappers.Threading;
using FluentAssertions;
using NUnit.Framework;

[TestFixture, Parallelizable]
public class SemaphoreWrapperFactoryTests
{
Expand All @@ -17,14 +20,15 @@ public void SetUp()
[TestCase(0,2,"yyy")]
[TestCase(1, 9, "zzz")]

public void ShouldCreateSemaphoreWrapper(int initial, int max, string name)
public void Create_CreatesSemaphoreWrapper(int initial, int max, string name)
{
// Act
var actual = _uut.Create(initial, max, name);

// Assert
actual.Should().BeAssignableTo<ISemaphoreWrapper>();
actual.Should().BeOfType<SemaphoreWrapper>();
actual.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace JwtAuthenticationApi.UnitTests.Handlers
namespace JwtAuthenticationApi.Common.Tests.Handlers
{
using JwtAuthenticationApi.Abstraction.Commands;
using JwtAuthenticationApi.Commands.Models;
using JwtAuthenticationApi.Handlers;
using Abstraction.Commands;
using Common.Handlers;
using FluentAssertions;
using Models;
using NSubstitute;
using NUnit.Framework;
using TddXt.AnyRoot.Strings;
using static TddXt.AnyRoot.Root;

Expand All @@ -18,7 +21,7 @@ public void SetUp()
}

[Test]
public async Task ShouldHandleCommandExecutionAndReturnResult()
public async Task HandleAsync_HandlesCommandExecutionAndReturnResult()
{
// Arrange
string stringResult = Any.String();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Any" Version="9.3.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MockQueryable.NSubstitute" Version="7.0.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\JwtAuthenticationApi.Common\JwtAuthenticationApi.Common.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using JwtAuthenticationApi.Commands.Models;

namespace JwtAuthenticationApi.Abstraction.Commands
namespace JwtAuthenticationApi.Common.Abstraction.Commands
{
using Models;

/// <summary>
/// Defines base method that should be implemented command classes.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions JwtAuthenticationApi.Common/Abstraction/Factories/IGuidFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace JwtAuthenticationApi.Common.Abstraction.Factories
{
/// <summary>
/// Defines method for <see cref="Guid"/> wrapper.
/// </summary>
public interface IGuidFactory
{
/// <inheritdoc cref="Guid.NewGuid"/>
Guid CreateGuid();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace JwtAuthenticationApi.Common.Abstraction.Factories.Polly
{

/// <summary>
/// Defines method for polly sleeping intervals factory class.
/// </summary>
public interface IPollySleepingIntervalsFactory
{
/// <summary>
/// Creates constant time polly sleep interval and returns it as collection of <see cref="TimeSpan"/>.
/// </summary>
/// <param name="sleepTime">Constant value of sleep time per retry.</param>
/// <param name="retryCount">Total amount of retries.</param>
/// <returns><see cref="IEnumerable{T}"/> that contains sleep intervals as a <see cref="TimeSpan"/></returns>
IEnumerable<TimeSpan> CreateConstantInterval(uint sleepTime, uint retryCount);

/// <summary>
/// Creates time polly sleep interval that increases linear and returns it as collection of <see cref="TimeSpan"/>.
/// </summary>
/// <param name="baseSleepTime">Starting sleep time.</param>
/// <param name="sleepTimeIncrease">Defines increase of time interval between retries.</param>
/// <param name="retryCount">Total amount of retires.</param>
/// <returns><see cref="IEnumerable{T}"/> that contains sleep intervals as a <see cref="TimeSpan"/></returns>
IEnumerable<TimeSpan> CreateLinearInterval(uint baseSleepTime, uint sleepTimeIncrease, uint retryCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace JwtAuthenticationApi.Common.Abstraction.Factories.Wrappers
{
using JwtAuthenticationApi.Common.Abstraction.Wrappers.Threading;

/// <summary>
/// Defines method for <see cref="IMutexWrapper"/> factory class.
/// </summary>
public interface IMutexWrapperFactory
{
/// <summary>
/// Creates <see cref="IMutexWrapper"/>.
/// </summary>
/// <param name="initiallyOwned">Defines if calling thread is mutex owner.</param>
/// <param name="name">Mutex name.</param>
/// <returns>New instance of <see cref="IMutexWrapper"/> with provided name.</returns>
IMutexWrapper Create(bool initiallyOwned, string name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace JwtAuthenticationApi.Common.Abstraction.Factories.Wrappers
{
using JwtAuthenticationApi.Common.Abstraction.Wrappers.Threading;
using JwtAuthenticationApi.Common.Wrappers.Threading;

/// <summary>
/// Defines method for <see cref="ISemaphoreWrapper"/> factory class.
/// </summary>
public interface ISemaphoreWrapperFactory
{
/// <summary>
/// Creates and returns <see cref="ISemaphoreWrapper"/>.
/// </summary>
/// <param name="initialCount">The initial number of requests for the semaphore that can be granted concurrently</param>
/// <param name="maximalCount">The maximum number of requests for the semaphore that can be granted concurrently.</param>
/// <param name="name">
/// The name, if the synchronization object is to be shared with other processes; otherwise, <see langword="null"/> or an empty string. The name is case-sensitive.
/// </param>
/// <returns>New instance of <see cref="SemaphoreWrapper"/>.</returns>
ISemaphoreWrapper Create(int initialCount, int maximalCount, string name);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace JwtAuthenticationApi.Common.Abstraction.Handlers
{
using Models;
using Commands;
/// <summary>
/// Define method for handling <see cref="ICommand{TResult}"/> requests in asynchronous way.
/// </summary>
public interface ICommandHandler
{
/// <summary>
/// Handles <see cref="ICommand{TResult}"/> command and returns its result.
/// </summary>
/// <typeparam name="TResult">Defines type that will be returned after handling command.</typeparam>
/// <param name="command">Command that implements <see cref="ICommand{TResult}"/> interface.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <exception cref="T:JwtAuthenticationApi.Exceptions.CommandExecutionException"/>
/// <returns>A task that represents the asynchronous password mix operation. The task result of handling provided command.</returns>
Task<Result<TResult>> HandleAsync<TResult>(ICommand<TResult> command, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace JwtAuthenticationApi.Common.Abstraction.Wrappers.Threading
{
/// <summary>
/// Defines method for wrapping <see cref="Mutex"/> class.
/// </summary>
public interface IMutexWrapper : IDisposable
{
/// <inheritdoc cref="Mutex.WaitOne"/>
void WaitOne();
/// <inheritdoc cref="Mutex.ReleaseMutex"/>
void ReleaseMutex();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace JwtAuthenticationApi.Common.Abstraction.Wrappers.Threading;

/// <summary>
/// Defines methods for <see cref="Semaphore"/> wrapper.
/// </summary>
public interface ISemaphoreWrapper : IDisposable
{
/// <summary>
/// Blocks current thread until the current <see cref="WaitHandle"/> receives a signal.
/// </summary>
/// <returns>
/// <see langword="true"/> if the current instance receives a signal. If the current instance is never signaled, WaitOne() never returns.
/// </returns>
bool WaitOne();
/// <inheritdoc cref="Semaphore.Release"/>
int Release();
}
27 changes: 27 additions & 0 deletions JwtAuthenticationApi.Common/CommonInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace JwtAuthenticationApi.Common
{
using Abstraction.Factories.Polly;
using Abstraction.Factories.Wrappers;
using Abstraction.Handlers;
using Abstraction.Wrappers;
using Factories.Polly;
using Factories.Wrappers;
using Handlers;
using JwtAuthenticationApi.Common.Abstraction.Factories;
using JwtAuthenticationApi.Common.Factories;
using Microsoft.Extensions.DependencyInjection;
using Wrappers;

public static class CommonInstaller
{
public static void InstallCommon(this IServiceCollection serviceCollection)
{
serviceCollection.AddTransient<IPollySleepingIntervalsFactory, PollySleepingIntervalsFactory>();
serviceCollection.AddTransient<IMutexWrapperFactory, MutexWrapperFactory>();
serviceCollection.AddTransient<ISemaphoreWrapperFactory, SemaphoreWrapperFactory>();
serviceCollection.AddTransient<ICommandHandler, CommandHandler>();
serviceCollection.AddTransient<IGuidFactory, GuidFactory>();
}

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace JwtAuthenticationApi.Constants
namespace JwtAuthenticationApi.Common.Constants
{
using System.Diagnostics.CodeAnalysis;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace JwtAuthenticationApi.Exceptions
namespace JwtAuthenticationApi.Common.Exceptions
{
using System.Diagnostics.CodeAnalysis;

Expand Down
16 changes: 16 additions & 0 deletions JwtAuthenticationApi.Common/Factories/GuidFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace JwtAuthenticationApi.Common.Factories
{
using JwtAuthenticationApi.Common.Abstraction.Factories;

/// <summary>
/// <see cref="Guid"/> wrapper.
/// </summary>
internal class GuidFactory : IGuidFactory
{
/// <inheritdoc cref="Guid.NewGuid"/>
public Guid CreateGuid()
{
return Guid.NewGuid();
}
}
}
Loading