Renames project paths from Lumina to Nemera

This commit is contained in:
BinaryAlley committed 2026-11-27 15:34:43 +02:00
1 parent ce6fddb4c9
commit 0f659a4877
6450 files changed

No files matched your search

@@ -0,0 +1,282 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.Primitives;
using Lumina.Presentation.Api.Fixtures.Core.Endpoints.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Common;
/// <summary>
/// Contains unit tests for the <see cref="BaseEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class BaseEndpointTests
{
private readonly BaseEndpointFixture _sut;
/// <summary>
/// Initializes a new instance of the <see cref="BaseEndpointTests"/> class.
/// </summary>
public BaseEndpointTests()
{
_sut = Factory.Create<BaseEndpointFixture>();
}
[Fact]
public void Problem_WhenCalledWithNoErrors_ShouldReturnGenericProblem()
{
// Act
IResult result = _sut.TestProblem([]);
// Assert
Assert.IsType<ProblemHttpResult>(result);
}
[Fact]
public void Problem_WhenCalledWithValidationErrors_ShouldReturnValidationProblem()
{
// Arrange
List<Error> errors = [
Error.Validation("Code1", "Description1"),
Error.Validation("Code2", "Description2")
];
// Act
IResult result = _sut.TestProblem(errors);
// Assert
ProblemHttpResult problemDetails = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails.StatusCode);
Assert.Equal("application/problem+json", problemDetails.ContentType);
HttpValidationProblemDetails validationProblemDetails = Assert.IsType<HttpValidationProblemDetails>(problemDetails.ProblemDetails);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, validationProblemDetails.Status);
Assert.Equal("General.Validation", validationProblemDetails.Title);
Assert.Equal("OneOrMoreValidationErrorsOccurred", validationProblemDetails.Detail);
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", validationProblemDetails.Type);
Assert.Equal(2, validationProblemDetails.Errors.Count);
Assert.Equal(new[] { "Description1" }, validationProblemDetails.Errors["Code1"]);
Assert.Equal(new[] { "Description2" }, validationProblemDetails.Errors["Code2"]);
}
[Theory]
[InlineData(ErrorType.Conflict, StatusCodes.Status409Conflict)]
[InlineData(ErrorType.NotFound, StatusCodes.Status404NotFound)]
[InlineData(ErrorType.Failure, StatusCodes.Status403Forbidden)]
[InlineData(ErrorType.Unexpected, StatusCodes.Status500InternalServerError)]
public void Problem_WhenCalledWithSingleError_ShouldReturnCorrectStatusCode(ErrorType errorType, int expectedStatusCode)
{
// Arrange
Error error = errorType switch
{
ErrorType.Conflict => Error.Conflict("ErrorCode", "ErrorDescription"),
ErrorType.NotFound => Error.NotFound("ErrorCode", "ErrorDescription"),
ErrorType.Validation => Error.Validation("ErrorCode", "ErrorDescription"),
ErrorType.Failure => Error.Failure("ErrorCode", "ErrorDescription"),
_ => Error.Unexpected("ErrorCode", "ErrorDescription"),
};
List<Error> errors = [error];
// Act
IResult result = _sut.TestProblem(errors);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(expectedStatusCode, problemResult.StatusCode);
Assert.Equal(expectedStatusCode, problemResult.ProblemDetails.Status);
Assert.Equal("ErrorCode", problemResult.ProblemDetails.Title);
Assert.Equal("ErrorDescription", problemResult.ProblemDetails.Detail);
Assert.NotNull(problemResult.ProblemDetails.Instance);
Assert.Contains("traceId", problemResult.ProblemDetails.Extensions.Keys);
}
[Fact]
public void Problem_WhenCalledWithSingleValidationError_ShouldReturnCorrectStatusCode()
{
// Arrange
Error error = Error.Validation();
List<Error> errors = [error];
// Act
IResult result = _sut.TestProblem(errors);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemResult.StatusCode);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemResult.ProblemDetails.Status);
Assert.Equal("General.Validation", problemResult.ProblemDetails.Title);
Assert.Equal("OneOrMoreValidationErrorsOccurred", problemResult.ProblemDetails.Detail);
Assert.NotNull(problemResult.ProblemDetails.Instance);
Assert.Contains("traceId", problemResult.ProblemDetails.Extensions.Keys);
}
[Fact]
public void Problem_WhenCalledWithMixedErrors_ShouldReturnProblemResult()
{
// Arrange
List<Error> errors = [
Error.NotFound("FailureCode", "Failure Error"),
Error.Failure("FailureCode", "Failure Error")
];
// Act
IResult result = _sut.TestProblem(errors);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Contains(problemResult.StatusCode, new[] { StatusCodes.Status404NotFound, StatusCodes.Status400BadRequest });
int?[] allowedStatusCodes = [StatusCodes.Status404NotFound, StatusCodes.Status400BadRequest];
Assert.Contains(problemResult.ProblemDetails.Status, allowedStatusCodes);
Assert.Equal("FailureCode", problemResult.ProblemDetails.Title);
Assert.Equal("Failure Error", problemResult.ProblemDetails.Detail);
}
[Fact]
public void ValidationProblem_WhenCalled_ShouldReturnValidationProblemResult()
{
// Arrange
List<Error> errors = [
Error.Validation("Code1", "Description1"),
Error.Validation("Code2", "Description2")
];
// Act
IResult result = _sut.TestValidationProblem(errors);
// Assert
ProblemHttpResult problemDetails = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, problemDetails.StatusCode);
Assert.Equal("application/problem+json", problemDetails.ContentType);
HttpValidationProblemDetails validationProblemDetails = Assert.IsType<HttpValidationProblemDetails>(problemDetails.ProblemDetails);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, validationProblemDetails.Status);
Assert.Equal("General.Validation", validationProblemDetails.Title);
Assert.Equal("OneOrMoreValidationErrorsOccurred", validationProblemDetails.Detail);
Assert.Equal("https://tools.ietf.org/html/rfc4918#section-11.2", validationProblemDetails.Type);
Assert.Equal(2, validationProblemDetails.Errors.Count);
Assert.Equal(new[] { "Description1" }, validationProblemDetails.Errors["Code1"]);
Assert.Equal(new[] { "Description2" }, validationProblemDetails.Errors["Code2"]);
}
[Fact]
public void ValidationProblem_WhenCalledWithDuplicateErrorCodes_ShouldMergeDescriptions()
{
// Arrange
List<Error> errors = [
Error.Validation("Code1", "Description1"),
Error.Validation("Code1", "Description2"),
Error.Validation("Code2", "Description3")
];
// Act
IResult result = _sut.TestValidationProblem(errors);
// Assert
ProblemHttpResult problemDetails = Assert.IsType<ProblemHttpResult>(result);
HttpValidationProblemDetails validationProblemDetails = Assert.IsType<HttpValidationProblemDetails>(problemDetails.ProblemDetails);
Assert.Contains("Code1", validationProblemDetails.Errors.Keys);
Assert.Contains("Code2", validationProblemDetails.Errors.Keys);
Assert.Equal(new[] { "Description1", "Description2" }, validationProblemDetails.Errors["Code1"]);
Assert.Equal(new[] { "Description3" }, validationProblemDetails.Errors["Code2"]);
}
[Fact]
public void Problem_WhenCalledWithSingleUnauthorizedError_ShouldReturnForbiddenStatusCode()
{
// Act
IResult result = _sut.TestProblem([Error.Unauthorized("ErrorCode", "ErrorDescription")]);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemResult.StatusCode);
}
[Fact]
public void Problem_WhenCalledWithSingleForbiddenError_ShouldReturnForbiddenStatusCode()
{
// Act
IResult result = _sut.TestProblem([Error.Forbidden("ErrorCode", "ErrorDescription")]);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status403Forbidden, problemResult.StatusCode);
}
[Fact]
public void Problem_WhenCalledWithResourceUnavailableError_ShouldReturnServiceUnavailableStatusCode()
{
// Arrange
List<Error> errors = [Error.ResourceUnavailable("ErrorCode", "ErrorDescription")];
// Act
IResult result = _sut.TestProblem(errors);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status503ServiceUnavailable, problemResult.StatusCode);
Assert.Equal(StatusCodes.Status503ServiceUnavailable, problemResult.ProblemDetails.Status);
Assert.Equal("ErrorCode", problemResult.ProblemDetails.Title);
Assert.Equal("ErrorDescription", problemResult.ProblemDetails.Detail);
Assert.NotNull(problemResult.ProblemDetails.Instance);
Assert.Contains("traceId", problemResult.ProblemDetails.Extensions.Keys);
}
[Fact]
public void Problem_WhenCalledWithMixedErrorsStartingWithValidation_ShouldReturnTheValidationStatusCode()
{
// Arrange
List<Error> errors = [
Error.Validation("Code1", "Description1"),
Error.Conflict("Code2", "Description2")
];
// Act
IResult result = _sut.TestProblem(errors);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(StatusCodes.Status400BadRequest, problemResult.StatusCode);
Assert.Equal("Code1", problemResult.ProblemDetails.Title);
}
[Fact]
public void Problem_WhenActivityIsCurrent_ShouldUseTheActivityIdAsTraceId()
{
// Arrange
using (Activity activity = new Activity("TestActivity"))
{
activity.Start();
List<Error> errors = [Error.NotFound("ErrorCode", "ErrorDescription")];
// Act
IResult result = _sut.TestProblem(errors);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
Assert.Equal(activity.Id, problemResult.ProblemDetails.Extensions["traceId"]);
}
}
[Fact]
public void ValidationProblem_WhenActivityIsCurrent_ShouldUseTheActivityIdAsTraceId()
{
// Arrange
using (Activity activity = new Activity("TestActivity"))
{
activity.Start();
List<Error> errors = [Error.Validation("Code1", "Description1")];
// Act
IResult result = _sut.TestValidationProblem(errors);
// Assert
ProblemHttpResult problemResult = Assert.IsType<ProblemHttpResult>(result);
HttpValidationProblemDetails validationProblemDetails = Assert.IsType<HttpValidationProblemDetails>(problemResult.ProblemDetails);
Assert.Equal(activity.Id, validationProblemDetails.Extensions["traceId"]);
}
}
}
@@ -0,0 +1,222 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Api.Core.Endpoints.Common;
using Microsoft.AspNetCore.Http;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.UnitTests.Core.Endpoints.Common;
/// <summary>
/// Contains unit tests for the <see cref="DisposingStreamResult"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class DisposingStreamResultTests
{
/// <summary>
/// Initializes a new instance of the <see cref="DisposingStreamResultTests"/> class.
/// </summary>
public DisposingStreamResultTests()
{
}
[Fact]
public async Task ExecuteAsync_WhenContentIsProvided_ShouldWriteEveryByteToResponseBody()
{
// Arrange
byte[] expectedBytes = CreateBytes(512);
using MemoryStream content = new(expectedBytes);
DisposingStreamResult sut = new(content, "audio/mpeg", expectedBytes.Length);
DefaultHttpContext httpContext = CreateHttpContext();
// Act
await sut.ExecuteAsync(httpContext);
// Assert
httpContext.Response.Body.Position = 0;
using MemoryStream writtenBody = new();
await httpContext.Response.Body.CopyToAsync(writtenBody);
Assert.Equal(expectedBytes, writtenBody.ToArray());
}
[Fact]
public async Task ExecuteAsync_WhenContentLengthIsProvided_ShouldSetContentTypeAndContentLength()
{
// Arrange
byte[] contentBytes = CreateBytes(128);
using MemoryStream content = new(contentBytes);
DisposingStreamResult sut = new(content, "audio/mp4", contentBytes.Length);
DefaultHttpContext httpContext = CreateHttpContext();
// Act
await sut.ExecuteAsync(httpContext);
// Assert
Assert.Equal("audio/mp4", httpContext.Response.ContentType);
Assert.Equal(contentBytes.Length, httpContext.Response.ContentLength);
}
[Fact]
public async Task ExecuteAsync_WhenContentLengthIsNull_ShouldLeaveResponseContentLengthUnset()
{
// Arrange
byte[] contentBytes = CreateBytes(64);
using MemoryStream content = new(contentBytes);
DisposingStreamResult sut = new(content, "audio/ogg", null);
DefaultHttpContext httpContext = CreateHttpContext();
// Act
await sut.ExecuteAsync(httpContext);
// Assert
Assert.Equal("audio/ogg", httpContext.Response.ContentType);
Assert.Null(httpContext.Response.ContentLength);
}
[Fact]
public async Task ExecuteAsync_WhenWriteCompletes_ShouldDisposeTheContentStream()
{
// Arrange
byte[] contentBytes = CreateBytes(32);
DisposalTrackingStream content = new(contentBytes);
DisposingStreamResult sut = new(content, "audio/mpeg", contentBytes.Length);
DefaultHttpContext httpContext = CreateHttpContext();
// Act
await sut.ExecuteAsync(httpContext);
// Assert
Assert.True(content.IsDisposed);
}
[Fact]
public async Task ExecuteAsync_WhenCopyToAsyncThrows_ShouldDisposeTheContentStreamAndPropagateTheException()
{
// Arrange
ThrowingCopyStream content = new(CreateBytes(32));
DisposingStreamResult sut = new(content, "audio/mpeg", null);
DefaultHttpContext httpContext = CreateHttpContext();
// Act
Task operationTask = sut.ExecuteAsync(httpContext);
// Assert
await Assert.ThrowsAsync<IOException>(() => operationTask);
Assert.True(content.IsDisposed);
}
[Fact]
public async Task ExecuteAsync_WhenStreamWasPartiallyRead_ShouldCopyFromTheCurrentPosition()
{
// Arrange
byte[] contentBytes = CreateBytes(100);
using MemoryStream content = new(contentBytes);
content.Position = 37;
DisposingStreamResult sut = new(content, "audio/wav", contentBytes.Length - 37);
DefaultHttpContext httpContext = CreateHttpContext();
// Act
await sut.ExecuteAsync(httpContext);
// Assert
httpContext.Response.Body.Position = 0;
using MemoryStream writtenBody = new();
await httpContext.Response.Body.CopyToAsync(writtenBody);
Assert.Equal(contentBytes[37..], writtenBody.ToArray());
}
/// <summary>
/// Creates a <see cref="DefaultHttpContext"/> with a writable response body.
/// </summary>
/// <returns>The configured HTTP context.</returns>
private static DefaultHttpContext CreateHttpContext()
{
DefaultHttpContext httpContext = new();
httpContext.Response.Body = new MemoryStream();
return httpContext;
}
/// <summary>
/// Creates a deterministic byte array that stands in for streamed content.
/// </summary>
/// <param name="length">The number of bytes to create.</param>
/// <returns>The created bytes.</returns>
private static byte[] CreateBytes(int length)
{
return [.. Enumerable.Range(0, length).Select(index => (byte)(index % 256))];
}
/// <summary>
/// A memory stream that records whether it was disposed.
/// </summary>
private sealed class DisposalTrackingStream : MemoryStream
{
/// <summary>
/// Gets whether the stream was disposed.
/// </summary>
public bool IsDisposed { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="DisposalTrackingStream"/> class.
/// </summary>
/// <param name="content">The bytes of the stream.</param>
public DisposalTrackingStream(byte[] content) : base(content)
{
}
/// <summary>
/// Releases the resources of the stream and records the disposal.
/// </summary>
/// <param name="disposing">Whether managed resources are being disposed.</param>
protected override void Dispose(bool disposing)
{
IsDisposed = true;
base.Dispose(disposing);
}
}
/// <summary>
/// A memory stream whose copy operation always fails.
/// </summary>
private sealed class ThrowingCopyStream : MemoryStream
{
/// <summary>
/// Gets whether the stream was disposed.
/// </summary>
public bool IsDisposed { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="ThrowingCopyStream"/> class.
/// </summary>
/// <param name="content">The bytes of the stream.</param>
public ThrowingCopyStream(byte[] content) : base(content)
{
}
/// <summary>
/// Simulates a failed write to the destination stream.
/// </summary>
/// <param name="destination">The destination stream.</param>
/// <param name="bufferSize">The size of the buffer used by the copy.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>This method never returns, it always throws.</returns>
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{
throw new IOException("The destination stream rejected the content.");
}
/// <summary>
/// Releases the resources of the stream and records the disposal.
/// </summary>
/// <param name="disposing">Whether managed resources are being disposed.</param>
protected override void Dispose(bool disposing)
{
IsDisposed = true;
base.Dispose(disposing);
}
}
}