Hardens test assertions and adds missing test coverage.

This commit is contained in:
BinaryAlley committed 2026-08-18 08:28:16 -07:00
1 parent 6b86fd5805
commit 1189bbaf36
489 files changed
+39982 -512

No files matched your search

@@ -0,0 +1,134 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.Plugins.Queries.GetLibraryMetadataProviders;
using Lumina.Contracts.Fixtures.Core.Requests.Plugins;
using Lumina.Contracts.Requests.Plugins;
using Lumina.Contracts.Responses.Plugins;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Core.Endpoints.Plugins.GetLibraryMetadataProviders;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Plugins.GetLibraryMetadataProviders;
/// <summary>
/// Contains unit tests for the <see cref="GetLibraryMetadataProvidersEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetLibraryMetadataProvidersEndpointTests
{
private readonly IQueryHandler<GetLibraryMetadataProvidersQuery, Result<IReadOnlyList<LibraryMetadataProviderResponse>>> _mockHandler;
private readonly GetLibraryMetadataProvidersEndpoint _sut;
private readonly GetLibraryMetadataProvidersRequestFixture _getLibraryMetadataProvidersRequestFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="GetLibraryMetadataProvidersEndpointTests"/> class.
/// </summary>
public GetLibraryMetadataProvidersEndpointTests()
{
_mockHandler = Substitute.For<IQueryHandler<GetLibraryMetadataProvidersQuery, Result<IReadOnlyList<LibraryMetadataProviderResponse>>>>();
_sut = Factory.Create<GetLibraryMetadataProvidersEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithLibraryMetadataProviderResponses()
{
// Arrange
GetLibraryMetadataProvidersRequest request = _getLibraryMetadataProvidersRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
IReadOnlyList<LibraryMetadataProviderResponse> expectedResponses =
[
new LibraryMetadataProviderResponse(Guid.NewGuid(), "Provider A", true, 1),
new LibraryMetadataProviderResponse(Guid.NewGuid(), "Provider B", false, 2)
];
_mockHandler.HandleAsync(Arg.Any<GetLibraryMetadataProvidersQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From(expectedResponses));
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
Ok<IReadOnlyList<LibraryMetadataProviderResponse>> okResult = Assert.IsType<Ok<IReadOnlyList<LibraryMetadataProviderResponse>>>(result);
Assert.Equal(expectedResponses, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
GetLibraryMetadataProvidersRequest request = _getLibraryMetadataProvidersRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = Error.NotFound("Library.NotFound", "The requested library was not found.");
_mockHandler.HandleAsync(Arg.Any<GetLibraryMetadataProvidersQuery>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemDetails = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status404NotFound, problemDetails.StatusCode);
Assert.Equal("application/problem+json", problemDetails.ContentType);
Assert.IsType<Microsoft.AspNetCore.Mvc.ProblemDetails>(problemDetails.ProblemDetails);
Assert.Equal("Library.NotFound", problemDetails.ProblemDetails.Title);
Assert.Equal("The requested library was not found.", problemDetails.ProblemDetails.Detail);
Assert.Equal(StatusCodes.Status404NotFound, problemDetails.ProblemDetails.Status);
Assert.Equal("https://tools.ietf.org/html/rfc9110#section-15.5.5", problemDetails.ProblemDetails.Type);
Assert.NotNull(problemDetails.ProblemDetails.Extensions["traceId"]);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendGetLibraryMetadataProvidersQueryToSender()
{
// Arrange
GetLibraryMetadataProvidersRequest request = _getLibraryMetadataProvidersRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<GetLibraryMetadataProvidersQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<LibraryMetadataProviderResponse>>([]));
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<GetLibraryMetadataProvidersQuery>(query => query.LibraryId == request.LibraryId),
Arg.Is(cancellationToken));
}
[Fact]
public async Task ExecuteAsync_WhenCancellationRequested_ShouldCancelOperation()
{
// Arrange
GetLibraryMetadataProvidersRequest request = _getLibraryMetadataProvidersRequestFixture.Create();
CancellationTokenSource cts = new();
TaskCompletionSource<bool> operationStarted = new();
TaskCompletionSource<bool> cancellationRequested = new();
_mockHandler.HandleAsync(Arg.Any<GetLibraryMetadataProvidersQuery>(), Arg.Any<CancellationToken>())
.Returns(callInfo => Task.Run(async () =>
{
operationStarted.SetResult(true);
await cancellationRequested.Task;
callInfo.Arg<CancellationToken>().ThrowIfCancellationRequested();
return Result.From<IReadOnlyList<LibraryMetadataProviderResponse>>([]);
}, 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,133 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.Plugins.Queries.GetPluginSettings;
using Lumina.Contracts.Fixtures.Core.Requests.Plugins;
using Lumina.Contracts.Requests.Plugins;
using Lumina.Contracts.Responses.Plugins;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Core.Endpoints.Plugins.GetPluginSettings;
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.Plugins.GetPluginSettings;
/// <summary>
/// Contains unit tests for the <see cref="GetPluginSettingsEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPluginSettingsEndpointTests
{
private readonly IQueryHandler<GetPluginSettingsQuery, Result<PluginSettingsResponse>> _mockHandler;
private readonly GetPluginSettingsEndpoint _sut;
private readonly GetPluginSettingsRequestFixture _getPluginSettingsRequestFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="GetPluginSettingsEndpointTests"/> class.
/// </summary>
public GetPluginSettingsEndpointTests()
{
_mockHandler = Substitute.For<IQueryHandler<GetPluginSettingsQuery, Result<PluginSettingsResponse>>>();
_sut = Factory.Create<GetPluginSettingsEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithPluginSettingsResponse()
{
// Arrange
GetPluginSettingsRequest request = _getPluginSettingsRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
PluginSettingsResponse expectedResponse = new(
PluginId: request.PluginId,
Schema: [],
Settings: null
);
_mockHandler.HandleAsync(Arg.Any<GetPluginSettingsQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From(expectedResponse));
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
Ok<PluginSettingsResponse> okResult = Assert.IsType<Ok<PluginSettingsResponse>>(result);
Assert.Equal(expectedResponse, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
GetPluginSettingsRequest request = _getPluginSettingsRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = Error.NotFound("Plugin.NotFound", "The requested plugin was not found.");
_mockHandler.HandleAsync(Arg.Any<GetPluginSettingsQuery>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemDetails = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status404NotFound, problemDetails.StatusCode);
Assert.Equal("application/problem+json", problemDetails.ContentType);
Assert.IsType<Microsoft.AspNetCore.Mvc.ProblemDetails>(problemDetails.ProblemDetails);
Assert.Equal("Plugin.NotFound", problemDetails.ProblemDetails.Title);
Assert.Equal("The requested plugin was not found.", problemDetails.ProblemDetails.Detail);
Assert.Equal(StatusCodes.Status404NotFound, problemDetails.ProblemDetails.Status);
Assert.Equal("https://tools.ietf.org/html/rfc9110#section-15.5.5", problemDetails.ProblemDetails.Type);
Assert.NotNull(problemDetails.ProblemDetails.Extensions["traceId"]);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendGetPluginSettingsQueryToSender()
{
// Arrange
GetPluginSettingsRequest request = _getPluginSettingsRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<GetPluginSettingsQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From(new PluginSettingsResponse(request.PluginId, [], null)));
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<GetPluginSettingsQuery>(query => query.PluginId == request.PluginId),
Arg.Is(cancellationToken));
}
[Fact]
public async Task ExecuteAsync_WhenCancellationRequested_ShouldCancelOperation()
{
// Arrange
GetPluginSettingsRequest request = _getPluginSettingsRequestFixture.Create();
CancellationTokenSource cts = new();
TaskCompletionSource<bool> operationStarted = new();
TaskCompletionSource<bool> cancellationRequested = new();
_mockHandler.HandleAsync(Arg.Any<GetPluginSettingsQuery>(), Arg.Any<CancellationToken>())
.Returns(callInfo => Task.Run(async () =>
{
operationStarted.SetResult(true);
await cancellationRequested.Task;
callInfo.Arg<CancellationToken>().ThrowIfCancellationRequested();
return Result.From(new PluginSettingsResponse(request.PluginId, [], null));
}, 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,124 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.Plugins.Queries.GetPlugins;
using Lumina.Contracts.Fixtures.Core.Responses.Plugins;
using Lumina.Contracts.Responses.Plugins;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.Plugins;
using Lumina.Presentation.Api.Core.Endpoints.Plugins.GetPlugins;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Plugins.GetPlugins;
/// <summary>
/// Contains unit tests for the <see cref="GetPluginsEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPluginsEndpointTests
{
private readonly IQueryHandler<GetPluginsQuery, Result<IReadOnlyList<PluginResponse>>> _mockHandler;
private readonly GetPluginsEndpoint _sut;
private readonly PluginResponseFixture _pluginResponseFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="GetPluginsEndpointTests"/> class.
/// </summary>
public GetPluginsEndpointTests()
{
_mockHandler = Substitute.For<IQueryHandler<GetPluginsQuery, Result<IReadOnlyList<PluginResponse>>>>();
_sut = Factory.Create<GetPluginsEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithPluginResponses()
{
// Arrange
CancellationToken cancellationToken = CancellationToken.None;
IReadOnlyList<PluginResponse> expectedResponses = [_pluginResponseFixture.Create(), _pluginResponseFixture.Create()];
_mockHandler.HandleAsync(Arg.Any<GetPluginsQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From(expectedResponses));
// Act
IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, cancellationToken);
// Assert
Ok<IReadOnlyList<PluginResponse>> okResult = Assert.IsType<Ok<IReadOnlyList<PluginResponse>>>(result);
Assert.Equal(expectedResponses, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = Error.Failure("Plugin.GetPlugins.Failed", "Failed to get the plugins.");
_mockHandler.HandleAsync(Arg.Any<GetPluginsQuery>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(EmptyRequest.Instance, cancellationToken);
// Assert
ProblemHttpResult problemDetails = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemDetails.StatusCode);
Assert.Equal("application/problem+json", problemDetails.ContentType);
Assert.IsType<Microsoft.AspNetCore.Mvc.ProblemDetails>(problemDetails.ProblemDetails);
Assert.Equal("Plugin.GetPlugins.Failed", problemDetails.ProblemDetails.Title);
Assert.Equal("Failed to get the plugins.", problemDetails.ProblemDetails.Detail);
Assert.Equal(StatusCodes.Status403Forbidden, problemDetails.ProblemDetails.Status);
Assert.Equal("https://tools.ietf.org/html/rfc9110#section-15.5.4", problemDetails.ProblemDetails.Type);
Assert.NotNull(problemDetails.ProblemDetails.Extensions["traceId"]);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendGetPluginsQueryToHandler()
{
// Arrange
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<GetPluginsQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<PluginResponse>>([]));
// Act
await _sut.ExecuteAsync(EmptyRequest.Instance, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(Arg.Any<GetPluginsQuery>(), Arg.Is(cancellationToken));
}
[Fact]
public async Task ExecuteAsync_WhenCancellationRequested_ShouldCancelOperation()
{
// Arrange
CancellationTokenSource cts = new();
TaskCompletionSource<bool> operationStarted = new();
TaskCompletionSource<bool> cancellationRequested = new();
_mockHandler.HandleAsync(Arg.Any<GetPluginsQuery>(), Arg.Any<CancellationToken>())
.Returns(callInfo => Task.Run(async () =>
{
operationStarted.SetResult(true);
await cancellationRequested.Task;
callInfo.Arg<CancellationToken>().ThrowIfCancellationRequested();
return Result.From<IReadOnlyList<PluginResponse>>([_pluginResponseFixture.Create()]);
}, callInfo.Arg<CancellationToken>()));
// Act
Task<IResult> operationTask = _sut.ExecuteAsync(EmptyRequest.Instance, cts.Token);
await operationStarted.Task;
cts.Cancel();
cancellationRequested.SetResult(true);
// Assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => operationTask);
}
}
@@ -0,0 +1,129 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.Plugins.Commands.ReorderLibraryMetadataProviders;
using Lumina.Contracts.Fixtures.Core.Requests.Plugins;
using Lumina.Contracts.Requests.Plugins;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Core.Endpoints.Plugins.ReorderLibraryMetadataProviders;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Plugins.ReorderLibraryMetadataProviders;
/// <summary>
/// Contains unit tests for the <see cref="ReorderLibraryMetadataProvidersEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ReorderLibraryMetadataProvidersEndpointTests
{
private readonly ICommandHandler<ReorderLibraryMetadataProvidersCommand, Result<Success>> _mockHandler;
private readonly ReorderLibraryMetadataProvidersEndpoint _sut;
private readonly ReorderLibraryMetadataProvidersRequestFixture _reorderLibraryMetadataProvidersRequestFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="ReorderLibraryMetadataProvidersEndpointTests"/> class.
/// </summary>
public ReorderLibraryMetadataProvidersEndpointTests()
{
_mockHandler = Substitute.For<ICommandHandler<ReorderLibraryMetadataProvidersCommand, Result<Success>>>();
_sut = FastEndpoints.Factory.Create<ReorderLibraryMetadataProvidersEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithSuccess()
{
// Arrange
ReorderLibraryMetadataProvidersRequest request = _reorderLibraryMetadataProvidersRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<ReorderLibraryMetadataProvidersCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Success));
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
Ok<Success> okResult = Assert.IsType<Ok<Success>>(result);
Assert.Equal(Result.Success, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
ReorderLibraryMetadataProvidersRequest request = _reorderLibraryMetadataProvidersRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = Error.NotFound("LibraryMetadataProviderConfiguration.NotFound", "The metadata provider configuration was not found.");
_mockHandler.HandleAsync(Arg.Any<ReorderLibraryMetadataProvidersCommand>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemDetails = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status404NotFound, problemDetails.StatusCode);
Assert.Equal("application/problem+json", problemDetails.ContentType);
Assert.IsType<Microsoft.AspNetCore.Mvc.ProblemDetails>(problemDetails.ProblemDetails);
Assert.Equal("LibraryMetadataProviderConfiguration.NotFound", problemDetails.ProblemDetails.Title);
Assert.Equal("The metadata provider configuration was not found.", problemDetails.ProblemDetails.Detail);
Assert.Equal(StatusCodes.Status404NotFound, problemDetails.ProblemDetails.Status);
Assert.Equal("https://tools.ietf.org/html/rfc9110#section-15.5.5", problemDetails.ProblemDetails.Type);
Assert.NotNull(problemDetails.ProblemDetails.Extensions["traceId"]);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendReorderLibraryMetadataProvidersCommandToSender()
{
// Arrange
ReorderLibraryMetadataProvidersRequest request = _reorderLibraryMetadataProvidersRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<ReorderLibraryMetadataProvidersCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Success));
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<ReorderLibraryMetadataProvidersCommand>(command =>
command.LibraryId == request.LibraryId &&
command.PluginIds.SequenceEqual(request.PluginIds)),
Arg.Is(cancellationToken));
}
[Fact]
public async Task ExecuteAsync_WhenCancellationRequested_ShouldCancelOperation()
{
// Arrange
ReorderLibraryMetadataProvidersRequest request = _reorderLibraryMetadataProvidersRequestFixture.Create();
CancellationTokenSource cts = new();
TaskCompletionSource<bool> operationStarted = new();
TaskCompletionSource<bool> cancellationRequested = new();
_mockHandler.HandleAsync(Arg.Any<ReorderLibraryMetadataProvidersCommand>(), Arg.Any<CancellationToken>())
.Returns(callInfo => Task.Run(async () =>
{
operationStarted.SetResult(true);
await cancellationRequested.Task;
callInfo.Arg<CancellationToken>().ThrowIfCancellationRequested();
return Result.From(Result.Success);
}, 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,129 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.Plugins.Commands.SetLibraryMetadataProviderEnabled;
using Lumina.Contracts.Fixtures.Core.Requests.Plugins;
using Lumina.Contracts.Requests.Plugins;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Core.Endpoints.Plugins.SetLibraryMetadataProviderEnabled;
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.Plugins.SetLibraryMetadataProviderEnabled;
/// <summary>
/// Contains unit tests for the <see cref="SetLibraryMetadataProviderEnabledEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class SetLibraryMetadataProviderEnabledEndpointTests
{
private readonly ICommandHandler<SetLibraryMetadataProviderEnabledCommand, Result<Success>> _mockHandler;
private readonly SetLibraryMetadataProviderEnabledEndpoint _sut;
private readonly SetLibraryMetadataProviderEnabledRequestFixture _setLibraryMetadataProviderEnabledRequestFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="SetLibraryMetadataProviderEnabledEndpointTests"/> class.
/// </summary>
public SetLibraryMetadataProviderEnabledEndpointTests()
{
_mockHandler = Substitute.For<ICommandHandler<SetLibraryMetadataProviderEnabledCommand, Result<Success>>>();
_sut = FastEndpoints.Factory.Create<SetLibraryMetadataProviderEnabledEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithSuccess()
{
// Arrange
SetLibraryMetadataProviderEnabledRequest request = _setLibraryMetadataProviderEnabledRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<SetLibraryMetadataProviderEnabledCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Success));
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
Ok<Success> okResult = Assert.IsType<Ok<Success>>(result);
Assert.Equal(Result.Success, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
SetLibraryMetadataProviderEnabledRequest request = _setLibraryMetadataProviderEnabledRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = Error.NotFound("LibraryMetadataProviderConfiguration.NotFound", "The metadata provider configuration was not found.");
_mockHandler.HandleAsync(Arg.Any<SetLibraryMetadataProviderEnabledCommand>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemDetails = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status404NotFound, problemDetails.StatusCode);
Assert.Equal("application/problem+json", problemDetails.ContentType);
Assert.IsType<Microsoft.AspNetCore.Mvc.ProblemDetails>(problemDetails.ProblemDetails);
Assert.Equal("LibraryMetadataProviderConfiguration.NotFound", problemDetails.ProblemDetails.Title);
Assert.Equal("The metadata provider configuration was not found.", problemDetails.ProblemDetails.Detail);
Assert.Equal(StatusCodes.Status404NotFound, problemDetails.ProblemDetails.Status);
Assert.Equal("https://tools.ietf.org/html/rfc9110#section-15.5.5", problemDetails.ProblemDetails.Type);
Assert.NotNull(problemDetails.ProblemDetails.Extensions["traceId"]);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendSetLibraryMetadataProviderEnabledCommandToSender()
{
// Arrange
SetLibraryMetadataProviderEnabledRequest request = _setLibraryMetadataProviderEnabledRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<SetLibraryMetadataProviderEnabledCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Success));
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<SetLibraryMetadataProviderEnabledCommand>(command =>
command.LibraryId == request.LibraryId &&
command.PluginId == request.PluginId &&
command.IsEnabled == request.IsEnabled),
Arg.Is(cancellationToken));
}
[Fact]
public async Task ExecuteAsync_WhenCancellationRequested_ShouldCancelOperation()
{
// Arrange
SetLibraryMetadataProviderEnabledRequest request = _setLibraryMetadataProviderEnabledRequestFixture.Create();
CancellationTokenSource cts = new();
TaskCompletionSource<bool> operationStarted = new();
TaskCompletionSource<bool> cancellationRequested = new();
_mockHandler.HandleAsync(Arg.Any<SetLibraryMetadataProviderEnabledCommand>(), Arg.Any<CancellationToken>())
.Returns(callInfo => Task.Run(async () =>
{
operationStarted.SetResult(true);
await cancellationRequested.Task;
callInfo.Arg<CancellationToken>().ThrowIfCancellationRequested();
return Result.From(Result.Success);
}, 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,128 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.CQRS;
using Lumina.Application.Core.Plugins.Commands.UpdatePluginSettings;
using Lumina.Contracts.Fixtures.Core.Requests.Plugins;
using Lumina.Contracts.Requests.Plugins;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Core.Endpoints.Plugins.UpdatePluginSettings;
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.Plugins.UpdatePluginSettings;
/// <summary>
/// Contains unit tests for the <see cref="UpdatePluginSettingsEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class UpdatePluginSettingsEndpointTests
{
private readonly ICommandHandler<UpdatePluginSettingsCommand, Result<Success>> _mockHandler;
private readonly UpdatePluginSettingsEndpoint _sut;
private readonly UpdatePluginSettingsRequestFixture _updatePluginSettingsRequestFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="UpdatePluginSettingsEndpointTests"/> class.
/// </summary>
public UpdatePluginSettingsEndpointTests()
{
_mockHandler = Substitute.For<ICommandHandler<UpdatePluginSettingsCommand, Result<Success>>>();
_sut = FastEndpoints.Factory.Create<UpdatePluginSettingsEndpoint>(_mockHandler);
}
[Fact]
public async Task ExecuteAsync_WhenSuccessful_ShouldReturnOkResultWithSuccess()
{
// Arrange
UpdatePluginSettingsRequest request = _updatePluginSettingsRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<UpdatePluginSettingsCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Success));
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
Ok<Success> okResult = Assert.IsType<Ok<Success>>(result);
Assert.Equal(Result.Success, okResult.Value);
}
[Fact]
public async Task ExecuteAsync_WhenHandlerReturnsError_ShouldReturnProblemResult()
{
// Arrange
UpdatePluginSettingsRequest request = _updatePluginSettingsRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
Error expectedError = Error.NotFound("Plugin.NotFound", "The requested plugin was not found.");
_mockHandler.HandleAsync(Arg.Any<UpdatePluginSettingsCommand>(), Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
IResult result = await _sut.ExecuteAsync(request, cancellationToken);
// Assert
ProblemHttpResult problemDetails = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status404NotFound, problemDetails.StatusCode);
Assert.Equal("application/problem+json", problemDetails.ContentType);
Assert.IsType<Microsoft.AspNetCore.Mvc.ProblemDetails>(problemDetails.ProblemDetails);
Assert.Equal("Plugin.NotFound", problemDetails.ProblemDetails.Title);
Assert.Equal("The requested plugin was not found.", problemDetails.ProblemDetails.Detail);
Assert.Equal(StatusCodes.Status404NotFound, problemDetails.ProblemDetails.Status);
Assert.Equal("https://tools.ietf.org/html/rfc9110#section-15.5.5", problemDetails.ProblemDetails.Type);
Assert.NotNull(problemDetails.ProblemDetails.Extensions["traceId"]);
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldSendUpdatePluginSettingsCommandToSender()
{
// Arrange
UpdatePluginSettingsRequest request = _updatePluginSettingsRequestFixture.Create();
CancellationToken cancellationToken = CancellationToken.None;
_mockHandler.HandleAsync(Arg.Any<UpdatePluginSettingsCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Success));
// Act
await _sut.ExecuteAsync(request, cancellationToken);
// Assert
await _mockHandler.Received(1).HandleAsync(
Arg.Is<UpdatePluginSettingsCommand>(command =>
command.PluginId == request.PluginId &&
command.Settings == request.Settings),
Arg.Is(cancellationToken));
}
[Fact]
public async Task ExecuteAsync_WhenCancellationRequested_ShouldCancelOperation()
{
// Arrange
UpdatePluginSettingsRequest request = _updatePluginSettingsRequestFixture.Create();
CancellationTokenSource cts = new();
TaskCompletionSource<bool> operationStarted = new();
TaskCompletionSource<bool> cancellationRequested = new();
_mockHandler.HandleAsync(Arg.Any<UpdatePluginSettingsCommand>(), Arg.Any<CancellationToken>())
.Returns(callInfo => Task.Run(async () =>
{
operationStarted.SetResult(true);
await cancellationRequested.Task;
callInfo.Arg<CancellationToken>().ThrowIfCancellationRequested();
return Result.From(Result.Success);
}, 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);
}
}