public async Task HandleAsync_ThrowException_ExceptionIsThrown()
        {
            var message = new TestValidationMessage
            {
                Id = "1", String10 = "123456789abc", IntRange = 5, NumbersOnly = "123"
            };
            var expectedMessage = $"The message is not valid:{Environment.NewLine}- The field String10 must be a string with a maximum length of 10.";
            var endpoint        = TestProducerEndpoint.GetDefault();

            endpoint.MessageValidationMode = MessageValidationMode.ThrowException;
            var envelope = new OutboundEnvelope(message, null, endpoint);

            IOutboundEnvelope?result = null;
            Func <Task>       act    = () => new ValidatorProducerBehavior(_outboundLogger).HandleAsync(
                new ProducerPipelineContext(
                    envelope,
                    Substitute.For <IProducer>(),
                    Substitute.For <IServiceProvider>()),
                context =>
            {
                result = context.Envelope;
                return(Task.CompletedTask);
            });

            result.Should().BeNull();
            await act.Should().ThrowAsync <MessageValidationException>().WithMessage(expectedMessage);
        }
        public async Task HandleAsync_ValidMessage_NoLogAndNoException(MessageValidationMode validationMode)
        {
            var message = new TestValidationMessage
            {
                Id = "1", String10 = "123", IntRange = 5, NumbersOnly = "123"
            };
            var endpoint = TestProducerEndpoint.GetDefault();

            endpoint.MessageValidationMode = validationMode;
            var envelope = new OutboundEnvelope(message, null, endpoint);

            IOutboundEnvelope?result = null;
            Func <Task>       act    = () => new ValidatorProducerBehavior(_outboundLogger).HandleAsync(
                new ProducerPipelineContext(
                    envelope,
                    Substitute.For <IProducer>(),
                    Substitute.For <IServiceProvider>()),
                context =>
            {
                result = context.Envelope;
                return(Task.CompletedTask);
            });

            await act.Should().NotThrowAsync <ValidationException>();

            result.Should().NotBeNull();
            result !.Message.Should().NotBeNull();
            _loggerSubstitute.DidNotReceive(LogLevel.Warning, null).Should().BeTrue();
        }
        public async Task HandleAsync_ValidMessage_NoLogAndNoException(MessageValidationMode validationMode)
        {
            var message = new TestValidationMessage
            {
                Id = "1", String10 = "123", IntRange = 5, NumbersOnly = "123"
            };
            var endpoint = TestConsumerEndpoint.GetDefault();

            endpoint.MessageValidationMode = validationMode;

            var envelope = new InboundEnvelope(
                message,
                null,
                null,
                new TestOffset(),
                endpoint,
                "source-endpoint");

            IRawInboundEnvelope?result = null;
            Func <Task>         act    = () => new ValidatorConsumerBehavior(_inboundLogger).HandleAsync(
                ConsumerPipelineContextHelper.CreateSubstitute(envelope, _serviceProvider),
                context =>
            {
                result = context.Envelope;
                return(Task.CompletedTask);
            });
            await act.Should().NotThrowAsync <ValidationException>();

            result.Should().NotBeNull();
            _loggerSubstitute.DidNotReceive(LogLevel.Warning, null).Should().BeTrue();
        }
 public MessageValidatorBenchmark()
 {
     _messageHavingSinglePropertyInvalid =
         TestValidationMessage.MessageHavingSinglePropertyInvalid;
     _messageHavingAllPropertiesInvalid =
         TestValidationMessage.MessageHavingAllPropertiesInvalid;
     _validMessage =
         TestValidationMessage.ValidMessage;
     _largeMessageHavingSeveralPropertiesInvalid =
         TestValidationLargeMessage.MessageHavingSeveralPropertiesInvalid;
     _largeMessageHavingSinglePropertyInvalid =
         TestValidationLargeMessage.MessageHavingSinglePropertyInvalid;
     _validLargeMessage =
         TestValidationLargeMessage.ValidMessage;
 }