Adds the scheduling module tests and centralizes domain event test fixtures

This commit is contained in:
BinaryAlley committed 2026-09-04 00:16:41 -07:00
1 parent 237be9e2c8
commit 99a6159f7f
135 files changed
+10203 -165

No files matched your search

@@ -0,0 +1,159 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Domain.Common.Errors;
using Lumina.Application.Core.Scheduling.Commands.AddScheduledJob;
using Lumina.Contracts.Fixtures.Core.Requests.Scheduling;
using Lumina.Contracts.Fixtures.Core.Responses.Scheduling;
using Lumina.Contracts.Requests.Scheduling;
using Lumina.Contracts.Responses.Scheduling;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
using Lumina.Presentation.Api.Core.Endpoints.Scheduling.AddScheduledJob;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Scheduling.AddScheduledJob;
/// <summary>
/// Contains unit tests for the <see cref="AddScheduledJobEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class AddScheduledJobEndpointTests
{
private readonly Application.Common.CQRS.ICommandHandler<AddScheduledJobCommand, Result<ScheduledJobResponse>> _mockHandler;
private readonly AddScheduledJobEndpoint _sut;
private readonly AddScheduledJobRequestFixture _addScheduledJobRequestFixture = new();
private readonly ScheduledJobResponseFixture _scheduledJobResponseFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="AddScheduledJobEndpointTests"/> class.
/// </summary>
public AddScheduledJobEndpointTests()
{
_mockHandler = Substitute.For<Application.Common.CQRS.ICommandHandler<AddScheduledJobCommand, Result<ScheduledJobResponse>>>();
_sut = Factory.Create<AddScheduledJobEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithScheduledJobResponse()
{
// Arrange
AddScheduledJobRequest request = _addScheduledJobRequestFixture.Create(name: "Scan job", scheduleType: ScheduleType.WithIntervalInMinutes, intervalMinutes: 60);
CancellationToken cancellationToken = CancellationToken.None;
ScheduledJobResponse expectedResponse = _scheduledJobResponseFixture.Create(
name: request.Name,
taskType: request.TaskType,
scheduleType: request.ScheduleType,
intervalMinutes: request.IntervalMinutes,
hour: request.Hour,
minute: request.Minute,
status: ScheduledJobStatus.Added);
_mockHandler.HandleAsync(Arg.Any<AddScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(expectedResponse));
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
Ok<ScheduledJobResponse> okResult = Assert.IsType<Ok<ScheduledJobResponse>>(result);
Assert.Equal(expectedResponse, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
AddScheduledJobRequest request = _addScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = Lumina.Domain.Common.Errors.Errors.Scheduling.ScheduledJobNameCannotBeEmpty;
_mockHandler.HandleAsync(Arg.Any<AddScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemResult.StatusCode);
Assert.Equal("application/problem+json", problemResult.ContentType);
Assert.NotNull(problemResult.ProblemDetails.Extensions["traceId"]);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendMappedCommandToHandler()
{
// Arrange
AddScheduledJobRequest request = _addScheduledJobRequestFixture.Create(
name: "Mapped job",
taskType: ScheduledTaskType.CleanTemporaryFiles,
scheduleType: ScheduleType.DailyAtHourAndMinute,
hour: 2,
minute: 15);
CancellationToken cancellationToken = CancellationToken.None;
ScheduledJobResponse response = _scheduledJobResponseFixture.Create(
name: request.Name,
taskType: request.TaskType,
scheduleType: request.ScheduleType,
hour: request.Hour,
minute: request.Minute,
status: ScheduledJobStatus.Added);
_mockHandler.HandleAsync(Arg.Any<AddScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(response));
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<AddScheduledJobCommand>(command =>
command.Name == request.Name &&
command.TaskType == request.TaskType &&
command.ScheduleType == request.ScheduleType &&
command.IntervalMinutes == request.IntervalMinutes &&
command.Hour == request.Hour &&
command.Minute == request.Minute),
Arg.Is(cancellationToken));
}
[Fact]
public async Task ExecuteAsync_WhenCancellationRequested_ShouldCancelOperation()
{
// Arrange
AddScheduledJobRequest request = _addScheduledJobRequestFixture.Create();
CancellationTokenSource cts = new();
TaskCompletionSource<bool> operationStarted = new();
TaskCompletionSource<bool> cancellationRequested = new();
ScheduledJobResponse response = _scheduledJobResponseFixture.Create(
name: request.Name,
taskType: request.TaskType,
scheduleType: request.ScheduleType,
intervalMinutes: request.IntervalMinutes,
hour: request.Hour,
minute: request.Minute,
status: ScheduledJobStatus.Added);
_mockHandler.HandleAsync(Arg.Any<AddScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(callInfo => Task.Run(async () =>
{
operationStarted.SetResult(true);
await cancellationRequested.Task;
callInfo.Arg<CancellationToken>().ThrowIfCancellationRequested();
return Result.From(response);
}, callInfo.Arg<CancellationToken>()));
// Act
Task<IResult> operationTask = _sut.ExecuteAsync(request, cts.Token);
await operationStarted.Task;
cts.Cancel();
cancellationRequested.SetResult(true);
// Assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => operationTask);
}
}
@@ -0,0 +1,164 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Domain.Common.Errors;
using Lumina.Application.Core.Scheduling.Commands.FireScheduledJob;
using Lumina.Contracts.Fixtures.Core.Requests.Scheduling;
using Lumina.Contracts.Fixtures.Core.Responses.Scheduling;
using Lumina.Contracts.Requests.Scheduling;
using Lumina.Contracts.Responses.Scheduling;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
using Lumina.Presentation.Api.Core.Endpoints.Scheduling.FireScheduledJob;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Scheduling.FireScheduledJob;
/// <summary>
/// Contains unit tests for the <see cref="FireScheduledJobEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class FireScheduledJobEndpointTests
{
private readonly Application.Common.CQRS.ICommandHandler<FireScheduledJobCommand, Result<ScheduledJobResponse>> _mockHandler;
private readonly FireScheduledJobEndpoint _sut;
private readonly FireScheduledJobRequestFixture _fireScheduledJobRequestFixture = new();
private readonly ScheduledJobResponseFixture _scheduledJobResponseFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="FireScheduledJobEndpointTests"/> class.
/// </summary>
public FireScheduledJobEndpointTests()
{
_mockHandler = Substitute.For<Application.Common.CQRS.ICommandHandler<FireScheduledJobCommand, Result<ScheduledJobResponse>>>();
_sut = Factory.Create<FireScheduledJobEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithScheduledJobResponse()
{
// Arrange
FireScheduledJobRequest request = _fireScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
ScheduledJobResponse expectedResponse = _scheduledJobResponseFixture.Create(
id: request.ScheduledJobId,
name: "Scan job",
taskType: ScheduledTaskType.ScanMediaLibraries,
scheduleType: ScheduleType.WithIntervalInMinutes,
intervalMinutes: 60,
status: ScheduledJobStatus.Active);
_mockHandler.HandleAsync(Arg.Any<FireScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(expectedResponse));
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
Ok<ScheduledJobResponse> okResult = Assert.IsType<Ok<ScheduledJobResponse>>(result);
Assert.Equal(expectedResponse, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
FireScheduledJobRequest request = _fireScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = Errors.Scheduling.ScheduledJobNotFound;
_mockHandler.HandleAsync(Arg.Any<FireScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status404NotFound, problemResult.StatusCode);
Assert.Equal("application/problem+json", problemResult.ContentType);
Assert.NotNull(problemResult.ProblemDetails.Extensions["traceId"]);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsNotAuthorized_ShouldReturnForbiddenProblemResult()
{
// Arrange
FireScheduledJobRequest request = _fireScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<FireScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(ApplicationErrors.Authorization.NotAuthorized);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendMappedCommandToHandler()
{
// Arrange
FireScheduledJobRequest request = _fireScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
ScheduledJobResponse response = _scheduledJobResponseFixture.Create(
id: request.ScheduledJobId,
name: "Scan job",
taskType: ScheduledTaskType.ScanMediaLibraries,
scheduleType: ScheduleType.WithIntervalInMinutes,
intervalMinutes: 60,
status: ScheduledJobStatus.Active);
_mockHandler.HandleAsync(Arg.Any<FireScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(response));
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<FireScheduledJobCommand>(command => command.ScheduledJobId == request.ScheduledJobId),
Arg.Is(cancellationToken));
}
[Fact]
public async Task ExecuteAsync_WhenCancellationRequested_ShouldCancelOperation()
{
// Arrange
FireScheduledJobRequest request = _fireScheduledJobRequestFixture.Create();
CancellationTokenSource cts = new();
TaskCompletionSource<bool> operationStarted = new();
TaskCompletionSource<bool> cancellationRequested = new();
ScheduledJobResponse response = _scheduledJobResponseFixture.Create(
id: request.ScheduledJobId,
name: "Scan job",
taskType: ScheduledTaskType.ScanMediaLibraries,
scheduleType: ScheduleType.WithIntervalInMinutes,
intervalMinutes: 60,
status: ScheduledJobStatus.Active);
_mockHandler.HandleAsync(Arg.Any<FireScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(callInfo => Task.Run(async () =>
{
operationStarted.SetResult(true);
await cancellationRequested.Task;
callInfo.Arg<CancellationToken>().ThrowIfCancellationRequested();
return Result.From(response);
}, callInfo.Arg<CancellationToken>()));
// Act
Task<IResult> operationTask = _sut.ExecuteAsync(request, cts.Token);
await operationStarted.Task;
cts.Cancel();
cancellationRequested.SetResult(true);
// Assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => operationTask);
}
}
@@ -0,0 +1,97 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.Scheduling.Queries.GetScheduledJobHistory;
using Lumina.Contracts.Fixtures.Core.Requests.Scheduling;
using Lumina.Contracts.Fixtures.Core.Responses.Scheduling;
using Lumina.Contracts.Requests.Scheduling;
using Lumina.Contracts.Responses.Scheduling;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Core.Endpoints.Scheduling.GetScheduledJobHistory;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Scheduling.GetScheduledJobHistory;
/// <summary>
/// Contains unit tests for the <see cref="GetScheduledJobHistoryEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetScheduledJobHistoryEndpointTests
{
private readonly IQueryHandler<GetScheduledJobHistoryQuery, Result<IEnumerable<ScheduledJobExecutionResponse>>> _mockHandler;
private readonly GetScheduledJobHistoryEndpoint _sut;
private readonly GetScheduledJobHistoryRequestFixture _getScheduledJobHistoryRequestFixture = new();
private readonly ScheduledJobExecutionResponseFixture _scheduledJobExecutionResponseFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="GetScheduledJobHistoryEndpointTests"/> class.
/// </summary>
public GetScheduledJobHistoryEndpointTests()
{
_mockHandler = Substitute.For<IQueryHandler<GetScheduledJobHistoryQuery, Result<IEnumerable<ScheduledJobExecutionResponse>>>>();
_sut = Factory.Create<GetScheduledJobHistoryEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithExecutionResponses()
{
// Arrange
GetScheduledJobHistoryRequest request = _getScheduledJobHistoryRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
List<ScheduledJobExecutionResponse> expectedResponses = _scheduledJobExecutionResponseFixture.CreateMany(2);
_mockHandler.HandleAsync(Arg.Any<GetScheduledJobHistoryQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ScheduledJobExecutionResponse>>(expectedResponses));
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
Ok<IEnumerable<ScheduledJobExecutionResponse>> okResult = Assert.IsType<Ok<IEnumerable<ScheduledJobExecutionResponse>>>(result);
Assert.Equal(2, okResult.Value.Count());
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
GetScheduledJobHistoryRequest request = _getScheduledJobHistoryRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<GetScheduledJobHistoryQuery>(), Arg.Any<CancellationToken>())
.Returns(ApplicationErrors.Authorization.NotAuthorized);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendMappedQueryToHandler()
{
// Arrange
GetScheduledJobHistoryRequest request = _getScheduledJobHistoryRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<GetScheduledJobHistoryQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ScheduledJobExecutionResponse>>([]));
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<GetScheduledJobHistoryQuery>(query => query.From == request.From && query.To == request.To),
Arg.Is(cancellationToken));
}
}
@@ -0,0 +1,90 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.Scheduling.Queries.GetScheduledJobs;
using Lumina.Contracts.Fixtures.Core.Responses.Scheduling;
using Lumina.Contracts.Responses.Scheduling;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Core.Endpoints.Scheduling.GetScheduledJobs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Scheduling.GetScheduledJobs;
/// <summary>
/// Contains unit tests for the <see cref="GetScheduledJobsEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetScheduledJobsEndpointTests
{
private readonly IQueryHandler<GetScheduledJobsQuery, Result<IEnumerable<ScheduledJobResponse>>> _mockHandler;
private readonly GetScheduledJobsEndpoint _sut;
private readonly ScheduledJobResponseFixture _scheduledJobResponseFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="GetScheduledJobsEndpointTests"/> class.
/// </summary>
public GetScheduledJobsEndpointTests()
{
_mockHandler = Substitute.For<IQueryHandler<GetScheduledJobsQuery, Result<IEnumerable<ScheduledJobResponse>>>>();
_sut = Factory.Create<GetScheduledJobsEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithScheduledJobResponses()
{
// Arrange
CancellationToken cancellationToken = CancellationToken.None;
List<ScheduledJobResponse> expectedResponses = _scheduledJobResponseFixture.CreateMany(2);
_mockHandler.HandleAsync(Arg.Any<GetScheduledJobsQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ScheduledJobResponse>>(expectedResponses));
// Act
IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, cancellationToken);
// Assert
Ok<IEnumerable<ScheduledJobResponse>> okResult = Assert.IsType<Ok<IEnumerable<ScheduledJobResponse>>>(result);
Assert.Equal(2, okResult.Value.Count());
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<GetScheduledJobsQuery>(), Arg.Any<CancellationToken>())
.Returns(ApplicationErrors.Authorization.NotAuthorized);
// Act
IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendQueryToHandler()
{
// Arrange
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<GetScheduledJobsQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ScheduledJobResponse>>([]));
// Act
await _sut.ExecuteAsync(EmptyRequest.Instance, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Any<GetScheduledJobsQuery>(),
Arg.Is(cancellationToken));
}
}
@@ -0,0 +1,88 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.Scheduling.Queries.GetSchedulerDisplayPreferences;
using Lumina.Contracts.Fixtures.Core.Responses.Scheduling;
using Lumina.Contracts.Responses.Scheduling;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Core.Endpoints.Scheduling.GetSchedulerDisplayPreferences;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Scheduling.GetSchedulerDisplayPreferences;
/// <summary>
/// Contains unit tests for the <see cref="GetSchedulerDisplayPreferencesEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetSchedulerDisplayPreferencesEndpointTests
{
private readonly IQueryHandler<GetSchedulerDisplayPreferencesQuery, Result<SchedulerDisplayPreferencesResponse>> _mockHandler;
private readonly GetSchedulerDisplayPreferencesEndpoint _sut;
private readonly SchedulerDisplayPreferencesResponseFixture _schedulerDisplayPreferencesResponseFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="GetSchedulerDisplayPreferencesEndpointTests"/> class.
/// </summary>
public GetSchedulerDisplayPreferencesEndpointTests()
{
_mockHandler = Substitute.For<IQueryHandler<GetSchedulerDisplayPreferencesQuery, Result<SchedulerDisplayPreferencesResponse>>>();
_sut = Factory.Create<GetSchedulerDisplayPreferencesEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithDisplayPreferencesResponse()
{
// Arrange
CancellationToken cancellationToken = CancellationToken.None;
SchedulerDisplayPreferencesResponse expectedResponse = _schedulerDisplayPreferencesResponseFixture.Create();
_mockHandler.HandleAsync(Arg.Any<GetSchedulerDisplayPreferencesQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From(expectedResponse));
// Act
IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, cancellationToken);
// Assert
Ok<SchedulerDisplayPreferencesResponse> okResult = Assert.IsType<Ok<SchedulerDisplayPreferencesResponse>>(result);
Assert.Equal(expectedResponse, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<GetSchedulerDisplayPreferencesQuery>(), Arg.Any<CancellationToken>())
.Returns(ApplicationErrors.Authorization.NotAuthorized);
// Act
IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendQueryToHandler()
{
// Arrange
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<GetSchedulerDisplayPreferencesQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From(_schedulerDisplayPreferencesResponseFixture.Create()));
// Act
await _sut.ExecuteAsync(EmptyRequest.Instance, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Any<GetSchedulerDisplayPreferencesQuery>(),
Arg.Is(cancellationToken));
}
}
@@ -0,0 +1,109 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.Scheduling.Commands.RemoveScheduledJob;
using Lumina.Contracts.Fixtures.Core.Requests.Scheduling;
using Lumina.Contracts.Requests.Scheduling;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Core.Endpoints.Scheduling.RemoveScheduledJob;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Scheduling.RemoveScheduledJob;
/// <summary>
/// Contains unit tests for the <see cref="RemoveScheduledJobEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class RemoveScheduledJobEndpointTests
{
private readonly Application.Common.CQRS.ICommandHandler<RemoveScheduledJobCommand, Result<Success>> _mockHandler;
private readonly RemoveScheduledJobEndpoint _sut;
private readonly RemoveScheduledJobRequestFixture _removeScheduledJobRequestFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="RemoveScheduledJobEndpointTests"/> class.
/// </summary>
public RemoveScheduledJobEndpointTests()
{
_mockHandler = Substitute.For<Application.Common.CQRS.ICommandHandler<RemoveScheduledJobCommand, Result<Success>>>();
_sut = Factory.Create<RemoveScheduledJobEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResult()
{
// Arrange
RemoveScheduledJobRequest request = _removeScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<RemoveScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.Success);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
IStatusCodeHttpResult statusCodeResult = Assert.IsAssignableFrom<IStatusCodeHttpResult>(result);
Assert.Equal(StatusCodes.Status200OK, statusCodeResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
RemoveScheduledJobRequest request = _removeScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = Domain.Common.Errors.Errors.Scheduling.ScheduledJobNotFound;
_mockHandler.HandleAsync(Arg.Any<RemoveScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status404NotFound, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsNotAuthorized_ShouldReturnForbiddenProblemResult()
{
// Arrange
RemoveScheduledJobRequest request = _removeScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<RemoveScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(ApplicationErrors.Authorization.NotAuthorized);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendMappedCommandToHandler()
{
// Arrange
RemoveScheduledJobRequest request = _removeScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<RemoveScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.Success);
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<RemoveScheduledJobCommand>(command => command.ScheduledJobId == request.ScheduledJobId),
Arg.Is(cancellationToken));
}
}
@@ -0,0 +1,115 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Application.Core.Scheduling.Commands.StartScheduledJob;
using Lumina.Contracts.Fixtures.Core.Requests.Scheduling;
using Lumina.Contracts.Fixtures.Core.Responses.Scheduling;
using Lumina.Contracts.Requests.Scheduling;
using Lumina.Contracts.Responses.Scheduling;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
using Lumina.Presentation.Api.Core.Endpoints.Scheduling.StartScheduledJob;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Scheduling.StartScheduledJob;
/// <summary>
/// Contains unit tests for the <see cref="StartScheduledJobEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class StartScheduledJobEndpointTests
{
private readonly Application.Common.CQRS.ICommandHandler<StartScheduledJobCommand, Result<ScheduledJobResponse>> _mockHandler;
private readonly StartScheduledJobEndpoint _sut;
private readonly StartScheduledJobRequestFixture _startScheduledJobRequestFixture = new();
private readonly ScheduledJobResponseFixture _scheduledJobResponseFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="StartScheduledJobEndpointTests"/> class.
/// </summary>
public StartScheduledJobEndpointTests()
{
_mockHandler = Substitute.For<Application.Common.CQRS.ICommandHandler<StartScheduledJobCommand, Result<ScheduledJobResponse>>>();
_sut = Factory.Create<StartScheduledJobEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithScheduledJobResponse()
{
// Arrange
StartScheduledJobRequest request = _startScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
ScheduledJobResponse expectedResponse = _scheduledJobResponseFixture.Create(
id: request.ScheduledJobId,
status: ScheduledJobStatus.Active);
_mockHandler.HandleAsync(Arg.Any<StartScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(expectedResponse));
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
Ok<ScheduledJobResponse> okResult = Assert.IsType<Ok<ScheduledJobResponse>>(result);
Assert.Equal(expectedResponse, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
StartScheduledJobRequest request = _startScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = Errors.Scheduling.ScheduledJobNotFound;
_mockHandler.HandleAsync(Arg.Any<StartScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status404NotFound, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsNotAuthorized_ShouldReturnForbiddenProblemResult()
{
// Arrange
StartScheduledJobRequest request = _startScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<StartScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(ApplicationErrors.Authorization.NotAuthorized);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendMappedCommandToHandler()
{
// Arrange
StartScheduledJobRequest request = _startScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<StartScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(_scheduledJobResponseFixture.Create(id: request.ScheduledJobId)));
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<StartScheduledJobCommand>(command => command.ScheduledJobId == request.ScheduledJobId),
Arg.Is(cancellationToken));
}
}
@@ -0,0 +1,115 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Application.Core.Scheduling.Commands.StopScheduledJob;
using Lumina.Contracts.Fixtures.Core.Requests.Scheduling;
using Lumina.Contracts.Fixtures.Core.Responses.Scheduling;
using Lumina.Contracts.Requests.Scheduling;
using Lumina.Contracts.Responses.Scheduling;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
using Lumina.Presentation.Api.Core.Endpoints.Scheduling.StopScheduledJob;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Scheduling.StopScheduledJob;
/// <summary>
/// Contains unit tests for the <see cref="StopScheduledJobEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class StopScheduledJobEndpointTests
{
private readonly Application.Common.CQRS.ICommandHandler<StopScheduledJobCommand, Result<ScheduledJobResponse>> _mockHandler;
private readonly StopScheduledJobEndpoint _sut;
private readonly StopScheduledJobRequestFixture _stopScheduledJobRequestFixture = new();
private readonly ScheduledJobResponseFixture _scheduledJobResponseFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="StopScheduledJobEndpointTests"/> class.
/// </summary>
public StopScheduledJobEndpointTests()
{
_mockHandler = Substitute.For<Application.Common.CQRS.ICommandHandler<StopScheduledJobCommand, Result<ScheduledJobResponse>>>();
_sut = Factory.Create<StopScheduledJobEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithScheduledJobResponse()
{
// Arrange
StopScheduledJobRequest request = _stopScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
ScheduledJobResponse expectedResponse = _scheduledJobResponseFixture.Create(
id: request.ScheduledJobId,
status: ScheduledJobStatus.Added);
_mockHandler.HandleAsync(Arg.Any<StopScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(expectedResponse));
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
Ok<ScheduledJobResponse> okResult = Assert.IsType<Ok<ScheduledJobResponse>>(result);
Assert.Equal(expectedResponse, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
StopScheduledJobRequest request = _stopScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = Errors.Scheduling.ScheduledJobNotStarted;
_mockHandler.HandleAsync(Arg.Any<StopScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsNotAuthorized_ShouldReturnForbiddenProblemResult()
{
// Arrange
StopScheduledJobRequest request = _stopScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<StopScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(ApplicationErrors.Authorization.NotAuthorized);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendMappedCommandToHandler()
{
// Arrange
StopScheduledJobRequest request = _stopScheduledJobRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<StopScheduledJobCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(_scheduledJobResponseFixture.Create(id: request.ScheduledJobId)));
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<StopScheduledJobCommand>(command => command.ScheduledJobId == request.ScheduledJobId),
Arg.Is(cancellationToken));
}
}
@@ -0,0 +1,112 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.Scheduling.Commands.UpdateSchedulerDisplayPreferences;
using Lumina.Contracts.Fixtures.Core.Requests.Scheduling;
using Lumina.Contracts.Requests.Scheduling;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Core.Endpoints.Scheduling.UpdateSchedulerDisplayPreferences;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
using DomainErrors = Lumina.Domain.Common.Errors.Errors;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Scheduling.UpdateSchedulerDisplayPreferences;
/// <summary>
/// Contains unit tests for the <see cref="UpdateSchedulerDisplayPreferencesEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class UpdateSchedulerDisplayPreferencesEndpointTests
{
private readonly Application.Common.CQRS.ICommandHandler<UpdateSchedulerDisplayPreferencesCommand, Result<Updated>> _mockHandler;
private readonly UpdateSchedulerDisplayPreferencesEndpoint _sut;
private readonly UpdateSchedulerDisplayPreferencesRequestFixture _updateSchedulerDisplayPreferencesRequestFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="UpdateSchedulerDisplayPreferencesEndpointTests"/> class.
/// </summary>
public UpdateSchedulerDisplayPreferencesEndpointTests()
{
_mockHandler = Substitute.For<Application.Common.CQRS.ICommandHandler<UpdateSchedulerDisplayPreferencesCommand, Result<Updated>>>();
_sut = Factory.Create<UpdateSchedulerDisplayPreferencesEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithUpdated()
{
// Arrange
UpdateSchedulerDisplayPreferencesRequest request = _updateSchedulerDisplayPreferencesRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<UpdateSchedulerDisplayPreferencesCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.Updated);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
Ok<Updated> okResult = Assert.IsType<Ok<Updated>>(result);
Assert.Equal(Result.Updated, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
UpdateSchedulerDisplayPreferencesRequest request = _updateSchedulerDisplayPreferencesRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = DomainErrors.Scheduling.SchedulerDisplayTimeSpanMustBePositive;
_mockHandler.HandleAsync(Arg.Any<UpdateSchedulerDisplayPreferencesCommand>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsNotAuthorized_ShouldReturnForbiddenProblemResult()
{
// Arrange
UpdateSchedulerDisplayPreferencesRequest request = _updateSchedulerDisplayPreferencesRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<UpdateSchedulerDisplayPreferencesCommand>(), Arg.Any<CancellationToken>())
.Returns(ApplicationErrors.Authorization.NotAuthorized);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemResult.StatusCode);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendMappedCommandToHandler()
{
// Arrange
UpdateSchedulerDisplayPreferencesRequest request = _updateSchedulerDisplayPreferencesRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<UpdateSchedulerDisplayPreferencesCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.Updated);
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<UpdateSchedulerDisplayPreferencesCommand>(command =>
command.JobTypeFilter == request.JobTypeFilter &&
command.DisplayTimeSpan == request.DisplayTimeSpan &&
command.DisplayTimeUnit == request.DisplayTimeUnit),
Arg.Is(cancellationToken));
}
}