net.mguenther.kafka.junit.ExternalKafkaCluster Maven / Gradle / Ivy
package net.mguenther.kafka.junit;
import kafka.api.LeaderAndIsr;
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;
import java.util.Map;
import java.util.Properties;
@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);
}
@Override
public Map fetchLeaderAndIsr(final String topic) {
return topicManagerDelegate.fetchLeaderAndIsr(topic);
}
@Override
public Properties fetchTopicConfig(final String topic) {
return topicManagerDelegate.fetchTopicConfig(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);
}
}