public async Task Validation_None_InvalidMessageIsProduced() { var message = new TestValidationMessage { String10 = "1234567890abcd" }; Host.ConfigureServices( services => services .AddLogging() .AddSilverback() .UseModel() .WithConnectionToMessageBroker(options => options.AddMockedKafka()) .AddKafkaEndpoints( endpoints => endpoints .Configure( config => { config.BootstrapServers = "PLAINTEXT://tests"; }) .AddOutbound <IIntegrationEvent>( endpoint => endpoint .DisableMessageValidation() .ProduceTo(DefaultTopicName))) .AddIntegrationSpyAndSubscriber()) .Run(); var publisher = Host.ScopedServiceProvider.GetRequiredService <IEventPublisher>(); Func <Task> act = () => publisher.PublishAsync(message); await act.Should().NotThrowAsync <ValidationException>(); DefaultTopic.MessagesCount.Should().Be(1); }
public async Task Validation_ThrowException_InvalidMessageNotProduced() { var message = new TestValidationMessage { String10 = "1234567890abcd" }; var expectedMessage = $"The message is not valid:{Environment.NewLine}- The field String10 must be a string with a maximum length of 10."; Host.ConfigureServices( services => services .AddLogging() .AddSilverback() .UseModel() .WithConnectionToMessageBroker(options => options.AddMockedKafka()) .AddKafkaEndpoints( endpoints => endpoints .Configure( config => { config.BootstrapServers = "PLAINTEXT://tests"; }) .AddOutbound <IIntegrationEvent>( endpoint => endpoint .ValidateMessage(true) .ProduceTo(DefaultTopicName))) .AddIntegrationSpyAndSubscriber()) .Run(); var publisher = Host.ScopedServiceProvider.GetRequiredService <IEventPublisher>(); Func <Task> act = () => publisher.PublishAsync(message); await act.Should().ThrowAsync <MessageValidationException>() .WithMessage(expectedMessage); DefaultTopic.MessagesCount.Should().Be(0); }