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

net.mguenther.kafka.junit.ExternalKafkaCluster 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.AccessLevel;
import lombok.RequiredArgsConstructor;
import net.mguenther.kafka.junit.provider.DefaultRecordConsumer;
import net.mguenther.kafka.junit.provider.DefaultRecordProducer;
import net.mguenther.kafka.junit.provider.DefaultTopicManager;
import net.mguenther.kafka.junit.provider.NoOpTopicManager;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.List;

@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class ExternalKafkaCluster implements RecordProducer, RecordConsumer, TopicManager {

    private final RecordProducer producerDelegate;

    private final RecordConsumer consumerDelegate;

    private final TopicManager topicManagerDelegate;

    private ExternalKafkaCluster(final String bootstrapServers) {
        this(bootstrapServers, StringUtils.EMPTY);
    }

    private ExternalKafkaCluster(final String bootstrapServers, final String zkConnectString) {
        producerDelegate = new DefaultRecordProducer(bootstrapServers);
        consumerDelegate = new DefaultRecordConsumer(bootstrapServers);
        topicManagerDelegate = StringUtils.isEmpty(zkConnectString) ?
                new NoOpTopicManager() : new DefaultTopicManager(zkConnectString);
    }

    @Override
    public  List readValues(final ReadKeyValues readRequest) throws InterruptedException {
        return consumerDelegate.readValues(readRequest);
    }

    @Override
    public  List> read(final ReadKeyValues readRequest) throws InterruptedException {
        return consumerDelegate.read(readRequest);
    }

    @Override
    public  List observeValues(final ObserveKeyValues observeRequest) throws InterruptedException {
        return consumerDelegate.observeValues(observeRequest);
    }

    @Override
    public  List> observe(final ObserveKeyValues observeRequest) throws InterruptedException {
        return consumerDelegate.observe(observeRequest);
    }

    @Override
    public  List send(final SendValues sendRequest) throws InterruptedException {
        return producerDelegate.send(sendRequest);
    }

    @Override
    public  List send(final SendValuesTransactional sendRequest) throws InterruptedException {
        return producerDelegate.send(sendRequest);
    }

    @Override
    public  List send(final SendKeyValues sendRequest) throws InterruptedException {
        return producerDelegate.send(sendRequest);
    }

    @Override
    public  List send(final SendKeyValuesTransactional sendRequest) throws InterruptedException {
        return producerDelegate.send(sendRequest);
    }

    @Override
    public void createTopic(final TopicConfig config) {
        topicManagerDelegate.createTopic(config);
    }

    @Override
    public void deleteTopic(final String topic) {
        topicManagerDelegate.deleteTopic(topic);
    }

    @Override
    public boolean exists(final String topic) {
        return topicManagerDelegate.exists(topic);
    }

    public static ExternalKafkaCluster at(final String bootstrapServers) {
        return new ExternalKafkaCluster(bootstrapServers);
    }

    public static ExternalKafkaCluster at(final String bootstrapServers, final String zkConnectString) {
        return new ExternalKafkaCluster(bootstrapServers, zkConnectString);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy