Skip to content

Commit

Permalink
[Host.FluentValidation] Duplicated error messages zarusz#192
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Maruszak <[email protected]>
  • Loading branch information
zarusz committed Aug 28, 2023
1 parent 12b92bc commit edf66f2
Show file tree
Hide file tree
Showing 39 changed files with 204 additions and 121 deletions.
2 changes: 1 addition & 1 deletion src/Host.Serialization.Properties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>2.0.1</Version>
<Version>2.0.2</Version>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Host.Transport.Properties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Import Project="Common.NuGet.Properties.xml" />

<PropertyGroup>
<Version>2.1.9</Version>
<Version>2.1.10</Version>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.4" />
<PackageReference Include="NSwag.AspNetCore" Version="13.18.2" />
<PackageReference Include="NSwag.AspNetCore" Version="13.20.0" />
<ProjectReference Include="..\..\SlimMessageBus.Host.AsyncApi\SlimMessageBus.Host.AsyncApi.csproj" />
<ProjectReference Include="..\..\SlimMessageBus.Host.AzureServiceBus\SlimMessageBus.Host.AzureServiceBus.csproj" />
<ProjectReference Include="..\..\SlimMessageBus.Host.Kafka\SlimMessageBus.Host.Kafka.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.5.1" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.7.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Namotion.Reflection" Version="2.1.1" />
<PackageReference Include="Saunter" Version="0.11.0" />
<PackageReference Include="Namotion.Reflection" Version="3.0.0" />
<PackageReference Include="Saunter" Version="0.12.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.7.5" />
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.9.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.12.0" />
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.16.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ protected AbstractValidationInterceptor(IEnumerable<IValidator<T>> validators, I
_errorsHandler = errorsHandler;
}

protected async Task OnValidate(T message, CancellationToken cancellationToken)
internal protected async Task OnValidate(T message, CancellationToken cancellationToken)
{
var context = new ValidationContext<T>(message);

var validationTasks = _validators
.Select(x => x.ValidateAsync(context, cancellationToken));

await Task.WhenAll(validationTasks);
var results = await Task.WhenAll(validationTasks);

var failures = validationTasks
.SelectMany(x => x.Result.Errors);
var failures = results.SelectMany(x => x.Errors);

if (failures.Any())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.5.1" />
<PackageReference Include="FluentValidation" Version="11.7.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SlimMessageBus.Host.Configuration\SlimMessageBus.Host.Configuration.csproj" />
<ProjectReference Include="..\SlimMessageBus.Host.Interceptor\SlimMessageBus.Host.Interceptor.csproj" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>SlimMessageBus.Host.FluentValidation.Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>


</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Confluent.Kafka" Version="2.0.2" />
<PackageReference Include="Confluent.Kafka" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion src/SlimMessageBus.Host.Mqtt/SlimMessageBus.Host.Mqtt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@

<ItemGroup>
<PackageReference Include="MQTTnet " Version="4.1.4.563" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.1.4.563" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.2.1.781" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SlimMessageBus.Host\SlimMessageBus.Host.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="MQTTnet" Version="4.2.1.781" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="StackExchange.Redis" Version="2.6.96" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Apache.Avro" Version="1.11.1" />
<PackageReference Include="Apache.Avro" Version="1.11.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.22.1" />
<PackageReference Include="Google.Protobuf" Version="3.24.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand All @@ -32,8 +32,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Update="FluentAssertions" Version="6.10.0" />
<PackageReference Update="Moq" Version="4.18.4" />
<PackageReference Update="FluentAssertions" Version="6.12.0" />
<PackageReference Update="Moq" Version="4.20.69" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -30,8 +30,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Update="FluentAssertions" Version="6.10.0" />
<PackageReference Update="Moq" Version="4.18.4" />
<PackageReference Update="FluentAssertions" Version="6.12.0" />
<PackageReference Update="Moq" Version="4.20.69" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.7" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\SlimMessageBus.Host\SlimMessageBus.Host.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="FluentAssertions" Version="6.10.0" />
<PackageReference Update="Moq" Version="4.18.4" />
<PackageReference Update="FluentAssertions" Version="6.12.0" />
<PackageReference Update="Moq" Version="4.20.69" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<Import Project="../Host.Test.Properties.xml" />

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand All @@ -19,8 +19,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Update="FluentAssertions" Version="6.10.0" />
<PackageReference Update="Moq" Version="4.18.4" />
<PackageReference Update="FluentAssertions" Version="6.12.0" />
<PackageReference Update="Moq" Version="4.20.69" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace SlimMessageBus.Host.FluentValidation.Test;

using global::FluentValidation;
using global::FluentValidation.Results;

public class AbstractValidationInterceptorTests
{
private readonly Message _message;
private readonly Mock<IValidator<Message>> _validatorMock;
private readonly CancellationToken _cancellationToken;
private readonly AbstractValidationInterceptor<Message> _subject;

public AbstractValidationInterceptorTests()
{
_message = new Message();
_validatorMock = new Mock<IValidator<Message>>();
_cancellationToken = new CancellationToken();
_subject = new Mock<AbstractValidationInterceptor<Message>>(new[] { _validatorMock.Object }, null) { CallBase = true }.Object;
}

public record Message;

[Fact]
public async Task When_OnValidate_Given_ValidationFails_Then_RaisesException()
{
// arrange
_validatorMock
.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken))
.ReturnsAsync(new ValidationResult { Errors = new List<ValidationFailure> { new() { ErrorMessage = "Something is Wrong" } } });

// act
Func<Task> act = () => _subject.OnValidate(_message, _cancellationToken);

// asset
await act.Should().ThrowAsync<ValidationException>();

_validatorMock
.Verify(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken), Times.Once);
}

[Fact]
public async Task When_OnValidate_Given_ValidationSucceeds_Then_CallsNext()
{
// arrange
_validatorMock
.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken))
.ReturnsAsync(new ValidationResult());

// act
await _subject.OnValidate(_message, _cancellationToken);

// asset
_validatorMock
.Verify(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken), Times.Once);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public record Message;
public async Task Given_Validator_When_ValidationFails_Then_RaisesException()
{
// arrange
_validatorMock.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken)).ThrowsAsync(new ValidationException("Bad message"));
_validatorMock
.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken))
.ThrowsAsync(new ValidationException("Bad message"));

// act
Func<Task> act = () => _subject.OnHandle(_message, _nextMock.Object, _consumerContextMock.Object);
Expand All @@ -41,7 +43,9 @@ public async Task Given_Validator_When_ValidationFails_Then_RaisesException()
public async Task Given_Validator_When_ValidationSucceeds_Then_CallsNext()
{
// arrange
_validatorMock.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken)).ReturnsAsync(new ValidationResult());
_validatorMock
.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken))
.ReturnsAsync(new ValidationResult());

// act
await _subject.OnHandle(_message, _nextMock.Object, _consumerContextMock.Object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public record ResponseMessage;
public async Task Given_Validator_When_ValidationFails_Then_RaisesException()
{
// arrange
_validatorMock.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken)).ThrowsAsync(new ValidationException("Bad message"));
_validatorMock
.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken))
.ThrowsAsync(new ValidationException("Bad message"));

// act
Func<Task> act = () => _subject.OnHandle(_message, _nextMock.Object, _consumerContextMock.Object);
Expand All @@ -42,7 +44,9 @@ public async Task Given_Validator_When_ValidationFails_Then_RaisesException()
public async Task Given_Validator_When_ValidationSucceeds_Then_CallsNext()
{
// arrange
_validatorMock.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken)).ReturnsAsync(new ValidationResult());
_validatorMock
.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken))
.ReturnsAsync(new ValidationResult());

// act
await _subject.OnHandle(_message, _nextMock.Object, _consumerContextMock.Object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public record Message;
public async Task Given_Validator_When_ValidationFails_Then_RaisesException()
{
// arrange
_validatorMock.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken)).ThrowsAsync(new ValidationException("Bad message"));
_validatorMock
.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken))
.ThrowsAsync(new ValidationException("Bad message"));

// act
Func<Task<object>> act = () => _subject.OnHandle(_message, _nextMock.Object, _producerContextMock.Object);
Expand All @@ -41,7 +43,9 @@ public async Task Given_Validator_When_ValidationFails_Then_RaisesException()
public async Task Given_Validator_When_ValidationSucceeds_Then_CallsNext()
{
// arrange
_validatorMock.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken)).ReturnsAsync(new ValidationResult());
_validatorMock
.Setup(x => x.ValidateAsync(It.IsAny<ValidationContext<Message>>(), _cancellationToken))
.ReturnsAsync(new ValidationResult());

// act
await _subject.OnHandle(_message, _nextMock.Object, _producerContextMock.Object);
Expand Down
Loading

0 comments on commit edf66f2

Please sign in to comment.