All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.mguenther.kafka.junit.SendValues Maven / Gradle / Ivy

Go to download

Provides an embedded Kafka cluster consisting of Apache ZooKeeper, Apache Kafka Brokers and Kafka Connect workers in distributed mode along with a rich set of convenient accessors and fault injectors to interact with the embedded Kafka cluster. Supports working against external clusters as well.

There is a newer version: 3.6.0
Show newest version
package net.mguenther.kafka.junit;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;

@Getter
@ToString
@RequiredArgsConstructor
public class SendValues {

    public static class SendValuesBuilder {

        private final String topic;
        private final Collection values = new ArrayList<>();
        private final Properties producerProps = new Properties();

        SendValuesBuilder(final String topic, final Collection values) {
            this.topic = topic;
            this.values.addAll(values);
        }

        public  SendValuesBuilder with(final String propertyName, final T value) {
            producerProps.put(propertyName, value);
            return this;
        }

        public  SendValuesBuilder withAll(final Properties producerProps) {
            this.producerProps.putAll(producerProps);
            return this;
        }

        private  void ifNonExisting(final String propertyName, final T value) {
            if (producerProps.get(propertyName) != null) return;
            producerProps.put(propertyName, value);
        }

        public SendValues useDefaults() {
            producerProps.clear();
            return build();
        }

        public SendValues build() {
            ifNonExisting(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
            ifNonExisting(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
            return new SendValues<>(topic, values, producerProps);
        }
    }

    private final String topic;
    private final Collection values;
    private final Properties producerProps;

    public static  SendValuesBuilder to(final String topic, final Collection values) {
        return new SendValuesBuilder<>(topic, values);
    }

    @SafeVarargs
    public static  SendValuesBuilder to(final String topic, final V... values) {
        return new SendValuesBuilder<>(topic, Arrays.asList(values));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy