public void GetProducerConfig_When_Auth_Defined_Should_Contain_Them()
        {
            var attribute = new KafkaAttribute("brokers:9092", "myTopic")
            {
                AuthenticationMode = BrokerAuthenticationMode.Plain,
                Protocol           = BrokerProtocol.SaslSsl,
                Username           = "******",
                Password           = "******",
            };

            var entity = new KafkaProducerEntity()
            {
                Attribute = attribute,
                ValueType = typeof(ProtoUser),
            };

            var factory = new KafkaProducerFactory(emptyConfiguration, new DefaultNameResolver(emptyConfiguration), NullLoggerProvider.Instance);
            var config  = factory.GetProducerConfig(entity);

            Assert.Equal(5, config.Count());
            Assert.Equal("brokers:9092", config.BootstrapServers);
            Assert.Equal(SecurityProtocol.SaslSsl, config.SecurityProtocol);
            Assert.Equal(SaslMechanism.Plain, config.SaslMechanism);
            Assert.Equal("secret", config.SaslPassword);
            Assert.Equal("myuser", config.SaslUsername);
        }
コード例 #2
0
        public void GetProducerConfig_When_Ssl_Auth_Defined_Should_Contain_Them()
        {
            var attribute = new KafkaAttribute("brokers:9092", "myTopic")
            {
                Protocol               = BrokerProtocol.Ssl,
                SslKeyLocation         = "path/to/key",
                SslKeyPassword         = "******",
                SslCertificateLocation = "path/to/cert",
                SslCaLocation          = "path/to/cacert"
            };

            var entity = new KafkaProducerEntity()
            {
                Attribute = attribute,
                ValueType = typeof(ProtoUser),
            };

            var factory = new KafkaProducerFactory(emptyConfiguration, new DefaultNameResolver(emptyConfiguration), NullLoggerProvider.Instance);
            var config  = factory.GetProducerConfig(entity);

            Assert.Equal(6, config.Count());
            Assert.Equal("brokers:9092", config.BootstrapServers);
            Assert.Equal(SecurityProtocol.Ssl, config.SecurityProtocol);
            Assert.Equal("path/to/key", config.SslKeyLocation);
            Assert.Equal("password1", config.SslKeyPassword);
            Assert.Equal("path/to/cert", config.SslCertificateLocation);
            Assert.Equal("path/to/cacert", config.SslCaLocation);
        }
        public void GetProducerConfig_When_Ssl_Locations_Resolve_InAzure_Should_Contain_Full_Path()
        {
            AzureEnvironment.SetRunningInAzureEnvVars();

            var currentFolder = Directory.GetCurrentDirectory();
            var folder1       = Directory.CreateDirectory(Path.Combine(currentFolder, AzureFunctionsFileHelper.AzureDefaultFunctionPathPart1));

            Directory.CreateDirectory(Path.Combine(folder1.FullName, AzureFunctionsFileHelper.AzureDefaultFunctionPathPart2));

            AzureEnvironment.SetEnvironmentVariable(AzureFunctionsFileHelper.AzureHomeEnvVarName, currentFolder);

            var sslCertificate = this.CreateFile(Path.Combine(currentFolder, AzureFunctionsFileHelper.AzureDefaultFunctionPathPart1, AzureFunctionsFileHelper.AzureDefaultFunctionPathPart2, "sslCertificate.pfx"));
            var sslCa          = this.CreateFile(Path.Combine(currentFolder, AzureFunctionsFileHelper.AzureDefaultFunctionPathPart1, AzureFunctionsFileHelper.AzureDefaultFunctionPathPart2, "sslCa.pem"));
            var sslKeyLocation = this.CreateFile(Path.Combine(currentFolder, AzureFunctionsFileHelper.AzureDefaultFunctionPathPart1, AzureFunctionsFileHelper.AzureDefaultFunctionPathPart2, "sslKey.key"));

            var attribute = new KafkaAttribute("brokers:9092", "myTopic")
            {
                SslCertificateLocation = "sslCertificate.pfx",
                SslCaLocation          = "sslCa.pem",
                SslKeyLocation         = "sslKey.key"
            };

            var entity = new KafkaProducerEntity
            {
                Attribute = attribute,
                ValueType = typeof(ProtoUser)
            };

            var factory = new KafkaProducerFactory(emptyConfiguration, new DefaultNameResolver(emptyConfiguration), NullLoggerProvider.Instance);
            var config  = factory.GetProducerConfig(entity);

            Assert.Equal(sslCertificate.FullName, config.SslCertificateLocation);
            Assert.Equal(sslCa.FullName, config.SslCaLocation);
            Assert.Equal(sslKeyLocation.FullName, config.SslKeyLocation);
        }
        public void GetProducerConfig_When_Ssl_Locations_Resolve_Should_Contain_Full_Path()
        {
            var sslCertificate = this.CreateFile("sslCertificate.pfx");
            var sslCa          = this.CreateFile("sslCa.pem");
            var sslKeyLocation = this.CreateFile("sslKey.key");

            var attribute = new KafkaAttribute("brokers:9092", "myTopic")
            {
                SslCertificateLocation = sslCertificate.FullName,
                SslCaLocation          = sslCa.FullName,
                SslKeyLocation         = sslKeyLocation.FullName
            };

            var entity = new KafkaProducerEntity
            {
                Attribute = attribute,
                ValueType = typeof(ProtoUser)
            };

            var factory = new KafkaProducerFactory(emptyConfiguration, new DefaultNameResolver(emptyConfiguration), NullLoggerProvider.Instance);
            var config  = factory.GetProducerConfig(entity);

            Assert.Equal(sslCertificate.FullName, config.SslCertificateLocation);
            Assert.Equal(sslCa.FullName, config.SslCaLocation);
            Assert.Equal(sslKeyLocation.FullName, config.SslKeyLocation);
        }
        public void GetProducerConfig_When_No_Auth_Defined_Should_Contain_Only_BrokerList()
        {
            var attribute = new KafkaAttribute("brokers:9092", "myTopic")
            {
            };

            var entity = new KafkaProducerEntity()
            {
                Attribute = attribute,
                ValueType = typeof(ProtoUser),
            };

            var factory = new KafkaProducerFactory(emptyConfiguration, new DefaultNameResolver(emptyConfiguration), NullLoggerProvider.Instance);
            var config  = factory.GetProducerConfig(entity);

            Assert.Single(config);
            Assert.Equal("brokers:9092", config.BootstrapServers);
        }
        public void When_No_Type_Is_Set_Should_Create_ByteArray_Producer()
        {
            var attribute = new KafkaAttribute("brokers:9092", "myTopic")
            {
            };

            var entity = new KafkaProducerEntity()
            {
                Attribute = attribute,
            };

            var factory  = new KafkaProducerFactory(emptyConfiguration, new DefaultNameResolver(emptyConfiguration), NullLoggerProvider.Instance);
            var producer = factory.Create(entity);

            Assert.NotNull(producer);
            Assert.IsType <KafkaProducer <Null, byte[]> >(producer);
            var typedProducer = (KafkaProducer <Null, byte[]>)producer;

            Assert.Null(typedProducer.ValueSerializer);
        }
        public void When_Value_Type_Is_Protobuf_Should_Create_Protobuf_Listener()
        {
            var attribute = new KafkaAttribute("brokers:9092", "myTopic")
            {
            };

            var entity = new KafkaProducerEntity()
            {
                Attribute = attribute,
                ValueType = typeof(ProtoUser),
            };

            var factory  = new KafkaProducerFactory(emptyConfiguration, new DefaultNameResolver(emptyConfiguration), NullLoggerProvider.Instance);
            var producer = factory.Create(entity);

            Assert.NotNull(producer);
            Assert.IsType <KafkaProducer <Null, ProtoUser> >(producer);
            var typedProducer = (KafkaProducer <Null, ProtoUser>)producer;

            Assert.NotNull(typedProducer.ValueSerializer);
            Assert.IsType <ProtobufSerializer <ProtoUser> >(typedProducer.ValueSerializer);
        }
        public void When_Value_Type_Is_Specific_Record_Should_Create_SpecificRecord_Listener()
        {
            var attribute = new KafkaAttribute("brokers:9092", "myTopic")
            {
            };


            var entity = new KafkaProducerEntity()
            {
                Attribute  = attribute,
                ValueType  = typeof(MyAvroRecord),
                AvroSchema = MyAvroRecord.SchemaText,
            };

            var factory  = new KafkaProducerFactory(emptyConfiguration, new DefaultNameResolver(emptyConfiguration), NullLoggerProvider.Instance);
            var producer = factory.Create(entity);

            Assert.NotNull(producer);
            Assert.IsType <KafkaProducer <Null, MyAvroRecord> >(producer);
            var typedProducer = (KafkaProducer <Null, MyAvroRecord>)producer;

            Assert.NotNull(typedProducer.ValueSerializer);
            Assert.IsType <AvroSerializer <MyAvroRecord> >(typedProducer.ValueSerializer);
        }
        public void When_Avro_Schema_Is_Provided_Should_Create_GenericRecord_Listener()
        {
            var attribute = new KafkaAttribute("brokers:9092", "myTopic")
            {
                AvroSchema = "fakeAvroSchema"
            };

            var entity = new KafkaProducerEntity()
            {
                Attribute  = attribute,
                ValueType  = typeof(GenericRecord),
                AvroSchema = attribute.AvroSchema,
            };

            var factory  = new KafkaProducerFactory(emptyConfiguration, new DefaultNameResolver(emptyConfiguration), NullLoggerProvider.Instance);
            var producer = factory.Create(entity);

            Assert.NotNull(producer);
            Assert.IsType <KafkaProducer <Null, GenericRecord> >(producer);
            var typedProducer = (KafkaProducer <Null, GenericRecord>)producer;

            Assert.NotNull(typedProducer.ValueSerializer);
            Assert.IsType <AvroSerializer <GenericRecord> >(typedProducer.ValueSerializer);
        }