127 lines
4.3 KiB
C#
127 lines
4.3 KiB
C#
using FluentValidation;
|
|
using Heimdall.Application.Behaviors;
|
|
using Heimdall.Application.Common;
|
|
using MediatR;
|
|
using NSubstitute;
|
|
|
|
namespace Heimdall.Application.Tests.Behaviors;
|
|
|
|
public class ValidationBehaviorTests
|
|
{
|
|
public sealed class TestCommand : IRequest<ServiceResult>
|
|
{
|
|
public string Name { get; init; } = string.Empty;
|
|
}
|
|
|
|
public sealed class TestGenericCommand : IRequest<ServiceResult<string>>
|
|
{
|
|
public string Name { get; init; } = string.Empty;
|
|
}
|
|
|
|
private sealed class TestValidator : AbstractValidator<TestCommand>
|
|
{
|
|
public TestValidator()
|
|
{
|
|
RuleFor(x => x.Name).NotEmpty();
|
|
}
|
|
}
|
|
|
|
private sealed class TestGenericValidator : AbstractValidator<TestGenericCommand>
|
|
{
|
|
public TestGenericValidator()
|
|
{
|
|
RuleFor(x => x.Name).NotEmpty();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task No_validators_should_pass_through()
|
|
{
|
|
var validators = Enumerable.Empty<IValidator<TestCommand>>();
|
|
var behavior = new ValidationBehavior<TestCommand, ServiceResult>(validators);
|
|
var next = Substitute.For<RequestHandlerDelegate<ServiceResult>>();
|
|
next().Returns(ServiceResult.Success());
|
|
|
|
var result = await behavior.Handle(new TestCommand { Name = "test" }, next, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
await next.Received(1)();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Valid_request_should_pass_through()
|
|
{
|
|
var validators = new[] { new TestValidator() };
|
|
var behavior = new ValidationBehavior<TestCommand, ServiceResult>(validators);
|
|
var next = Substitute.For<RequestHandlerDelegate<ServiceResult>>();
|
|
next().Returns(ServiceResult.Success());
|
|
|
|
var result = await behavior.Handle(new TestCommand { Name = "valid" }, next, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
await next.Received(1)();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Invalid_request_should_return_failure()
|
|
{
|
|
var validators = new[] { new TestValidator() };
|
|
var behavior = new ValidationBehavior<TestCommand, ServiceResult>(validators);
|
|
var next = Substitute.For<RequestHandlerDelegate<ServiceResult>>();
|
|
|
|
var result = await behavior.Handle(new TestCommand { Name = "" }, next, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
result.StatusCode.ShouldBe(400);
|
|
result.Error.ShouldNotBeNullOrEmpty();
|
|
await next.DidNotReceive()();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Multiple_validators_should_aggregate_errors()
|
|
{
|
|
var validators = new IValidator<TestCommand>[]
|
|
{
|
|
new TestValidator(),
|
|
new TestValidator() // duplicate
|
|
};
|
|
var behavior = new ValidationBehavior<TestCommand, ServiceResult>(validators);
|
|
var next = Substitute.For<RequestHandlerDelegate<ServiceResult>>();
|
|
|
|
var result = await behavior.Handle(new TestCommand { Name = "" }, next, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
result.Error.ShouldNotBeNull();
|
|
result.Error.ShouldContain("'Name'");
|
|
await next.DidNotReceive()();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Generic_ServiceResult_T_should_also_work()
|
|
{
|
|
// Test with ServiceResult<T> using a dedicated command type
|
|
var validators = new[] { new TestGenericValidator() };
|
|
var behavior = new ValidationBehavior<TestGenericCommand, ServiceResult<string>>(validators);
|
|
var next = Substitute.For<RequestHandlerDelegate<ServiceResult<string>>>();
|
|
next().Returns(ServiceResult<string>.Success("ok"));
|
|
|
|
var result = await behavior.Handle(new TestGenericCommand { Name = "valid" }, next, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
result.Data.ShouldBe("ok");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Generic_ServiceResult_T_failure_should_return_error()
|
|
{
|
|
var validators = new[] { new TestGenericValidator() };
|
|
var behavior = new ValidationBehavior<TestGenericCommand, ServiceResult<string>>(validators);
|
|
var next = Substitute.For<RequestHandlerDelegate<ServiceResult<string>>>();
|
|
|
|
var result = await behavior.Handle(new TestGenericCommand { Name = "" }, next, CancellationToken.None);
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
result.StatusCode.ShouldBe(400);
|
|
}
|
|
}
|