public void when_create_producer_without_required_serializers_it_should_fail()
        {
            var serializerFactoryMock = new Mock <ISerializerFactory>();

            serializerFactoryMock.Setup(factory => factory.Create <string>()).Returns(Mock.Of <ISerializer <string> >);
            serializerFactoryMock.Setup(factory => factory.Create <int>()).Returns(() => null);

            var producerFactory = new DefaultProducerFactory(Mock.Of <IProducerConfigurator>(), serializerFactoryMock.Object);

            Action sutBadKey = () => producerFactory.Create <int, string>(CreateProducerConfig());

            sutBadKey.Should().ThrowExactly <PoezdOperationException>().Where(exception => exception.Message.Contains("key"));

            Action sutBadValue = () => producerFactory.Create <string, int>(CreateProducerConfig());

            sutBadValue.Should().ThrowExactly <PoezdOperationException>().Where(exception => exception.Message.Contains("value"));
        }
        public async Task when_create_producer_it_should_configure_it_using_configurator()
        {
            var configuratorMock = new Mock <IProducerConfigurator>();

            configuratorMock
            .Setup(
                configurator => configurator.Configure(
                    It.IsAny <ProducerBuilder <string, byte[]> >(),
                    It.IsAny <ISerializer <string> >(),
                    It.IsAny <ISerializer <byte[]> >()))
            .Returns(
                (
                    ProducerBuilder <string, byte[]> builder,
                    ISerializer <string> keySerializer,
                    ISerializer <byte[]> valueSerializer) =>
            {
                builder.SetKeySerializer(keySerializer);
                builder.SetValueSerializer(valueSerializer);
                return(builder);
            })
            .Verifiable("configurator should be called");

            var serializerFactoryMock = new Mock <ISerializerFactory>();

            serializerFactoryMock.Setup(factory => factory.Create <string>()).Returns(Serializers.Utf8)
            .Verifiable("serializer for the key should be gotten");
            serializerFactoryMock.Setup(factory => factory.Create <byte[]>()).Returns(Serializers.ByteArray)
            .Verifiable("serializer for the value should be gotten");

            var producerFactory = new DefaultProducerFactory(configuratorMock.Object, serializerFactoryMock.Object);
            var producer        = producerFactory.Create <string, byte[]>(CreateProducerConfig());

            var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(value: 5)).Token;

            await using var kafkaTestContext = _kafkaTestContextFactory.Create <string, byte[]>(timeout);
            var topic = new StringCreator().Get(length: 10);
            await kafkaTestContext.CreateTopics(topic);

            var key     = StringCreator.Get(length: 10);
            var value   = Encoding.UTF8.GetBytes("test-message");
            var message = new Message <string, byte[]> {
                Headers = new Headers(), Key = key, Timestamp = Timestamp.Default, Value = value
            };

            Func <Task> sut = () => producer.ProduceAsync(
                topic,
                message,
                timeout);

            sut.Should().NotThrow();
            configuratorMock.Verify();
            serializerFactoryMock.Verify();

            var consumeResult = kafkaTestContext.Consume(topic);

            consumeResult.Message.Key.Should().Be(key);
            consumeResult.Message.Value.Should().BeEquivalentTo(value);
        }
        public void when_create_producer_with_invalid_arguments_it_should_fail()
        {
            var configurator      = Mock.Of <IProducerConfigurator>();
            var serializerFactory = Mock.Of <ISerializerFactory>();

            var factory = new DefaultProducerFactory(configurator, serializerFactory);

            Action sut = () => factory.Create <int, string>(config: null);

            sut.Should().ThrowExactly <ArgumentNullException>();
        }