How to use KafkaContainerCluster method of com.example.kafkacluster.KafkaContainerCluster class

Best Testcontainers-java code snippet using com.example.kafkacluster.KafkaContainerCluster.KafkaContainerCluster

Source:KafkaContainerCluster.java Github

copy

Full Screen

...16import java.util.stream.Stream;17import static java.util.concurrent.TimeUnit.SECONDS;18/**19 * Provides an easy way to launch a Kafka cluster with multiple brokers. Copied from20 * https://github.com/testcontainers/testcontainers-java/blob/master/examples/kafka-cluster/src/test/java/com/example/kafkacluster/KafkaContainerCluster.java21 * <p>22 * Since it was copied from the link above, we decided to not make any major changes to it and use it as is.23 */24public class KafkaContainerCluster implements Startable {25 private final int brokersNum;26 private final Network network;27 private final GenericContainer<?> zookeeper;28 private final Collection<KafkaContainer> brokers;29 /**30 * Default constructor for building a kafka cluster to be used for tests.31 *32 * @param confluentPlatformVersion version of the kafka image33 * @param brokersNum number of brokers34 * @param internalTopicsRf replication factor35 */36 public KafkaContainerCluster(String confluentPlatformVersion, int brokersNum, int internalTopicsRf) {37 if (brokersNum < 0) {38 throw new IllegalArgumentException("brokersNum '" + brokersNum + "' must be greater than 0");39 }40 if (internalTopicsRf < 0 || internalTopicsRf > brokersNum) {41 throw new IllegalArgumentException(42 "internalTopicsRf '" + internalTopicsRf + "' must be less than brokersNum and greater than 0");43 }44 this.brokersNum = brokersNum;45 this.network = Network.newNetwork();46 this.zookeeper = new GenericContainer<>(DockerImageName.parse("confluentinc/cp-zookeeper")47 .withTag(confluentPlatformVersion))48 .withNetwork(network)49 .withNetworkAliases("zookeeper")50 .withEnv("ZOOKEEPER_CLIENT_PORT", String.valueOf(KafkaContainer.ZOOKEEPER_PORT));...

Full Screen

Full Screen

Source:KafkaContainerClusterTest.java Github

copy

Full Screen

...20import java.util.UUID;21import java.util.concurrent.TimeUnit;22import static org.assertj.core.api.Assertions.assertThat;23import static org.assertj.core.api.Assertions.tuple;24public class KafkaContainerClusterTest {25 @Test26 public void testKafkaContainerCluster() throws Exception {27 try (28 KafkaContainerCluster cluster = new KafkaContainerCluster("6.2.1", 3, 2)29 ) {30 cluster.start();31 String bootstrapServers = cluster.getBootstrapServers();32 assertThat(cluster.getBrokers()).hasSize(3);33 testKafkaFunctionality(bootstrapServers, 3, 2);34 }35 }36 protected void testKafkaFunctionality(String bootstrapServers, int partitions, int rf) throws Exception {37 try (38 AdminClient adminClient = AdminClient.create(ImmutableMap.of(39 AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers40 ));41 KafkaProducer<String, String> producer = new KafkaProducer<>(42 ImmutableMap.of(...

Full Screen

Full Screen

KafkaContainerCluster

Using AI Code Generation

copy

Full Screen

1import com.example.kafkacluster.KafkaContainerCluster;2public class KafkaTest {3 public static void main(String[] args) {4 KafkaContainerCluster kafkaContainerCluster = new KafkaContainerCluster();5 kafkaContainerCluster.start();6 }7}8import com.example.kafkacluster.KafkaContainerCluster;9public class KafkaTest {10 public static void main(String[] args) {11 KafkaContainerCluster kafkaContainerCluster = new KafkaContainerCluster();12 kafkaContainerCluster.stop();13 }14}15import com.example.kafkacluster.KafkaContainerCluster;16public class KafkaTest {17 public static void main(String[] args) {18 KafkaContainerCluster kafkaContainerCluster = new KafkaContainerCluster();19 kafkaContainerCluster.restart();20 }21}22import com.example.kafkacluster.KafkaContainerCluster;23public class KafkaTest {24 public static void main(String[] args) {25 KafkaContainerCluster kafkaContainerCluster = new KafkaContainerCluster();26 kafkaContainerCluster.delete();27 }28}29import com.example.kafkacluster.KafkaContainerCluster;30public class KafkaTest {31 public static void main(String[] args) {32 KafkaContainerCluster kafkaContainerCluster = new KafkaContainerCluster();33 kafkaContainerCluster.status();34 }35}36import com.example.kafkacluster.KafkaContainerCluster;37public class KafkaTest {38 public static void main(String[] args) {39 KafkaContainerCluster kafkaContainerCluster = new KafkaContainerCluster();40 kafkaContainerCluster.logs();41 }42}43import com.example.kafkacluster.KafkaContainerCluster;44public class KafkaTest {45 public static void main(String[] args) {46 KafkaContainerCluster kafkaContainerCluster = new KafkaContainerCluster();47 kafkaContainerCluster.restart();48 }49}

Full Screen

Full Screen

KafkaContainerCluster

Using AI Code Generation

copy

Full Screen

1package com.example.kafkacluster;2import org.apache.kafka.clients.admin.AdminClient;3import org.apache.kafka.clients.admin.AdminClientConfig;4import org.apache.kafka.clients.admin.NewTopic;5import org.apache.kafka.clients.producer.KafkaProducer;6import org.apache.kafka.clients.producer.ProducerRecord;7import org.apache.kafka.common.serialization.StringSerializer;8import org.junit.jupiter.api.AfterAll;9import org.junit.jupiter.api.BeforeAll;10import org.junit.jupiter.api.Test;11import org.testcontainers.containers.KafkaContainer;12import org.testcontainers.containers.Network;13import org.testcontainers.utility.DockerImageName;14import java.util.Collections;15import java.util.List;16import java.util.Properties;17import java.util.concurrent.ExecutionException;18import java.util.stream.Collectors;19import java.util.stream.Stream;20import static org.junit.jupiter.api.Assertions.assertEquals;21public class KafkaContainerClusterTest {22 private static KafkaContainerCluster kafkaContainerCluster;23 private static List<KafkaContainer> kafkaContainers;24 private static Network network;25 public static void setup() {26 network = Network.newNetwork();27 kafkaContainerCluster = new KafkaContainerCluster(3, network);28 kafkaContainers = kafkaContainerCluster.getKafkaContainers();29 }30 public static void tearDown() {31 kafkaContainerCluster.stop();32 }33 public void whenCreatingKafkaCluster_thenAllBrokersShouldBeUpAndRunning() {34 List<String> brokers = kafkaContainers.stream()35 .map(KafkaContainer::getBootstrapServers)36 .collect(Collectors.toList());37 Properties properties = new Properties();38 properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, brokers);39 AdminClient adminClient = AdminClient.create(properties);40 try {41 assertEquals(3, adminClient.listTopics().names().get().size());42 } catch (InterruptedException | ExecutionException e) {43 e.printStackTrace();44 }45 }46 public void whenCreatingKafkaCluster_thenAllBrokersShouldBeAbleToCommunicate() {47 String topicName = "test";48 String message = "Hello World!";49 kafkaContainers.forEach(50 kafkaContainer -> {51 String broker = kafkaContainer.getBootstrapServers();52 Properties properties = new Properties();53 properties.put(AdminClientConfig.BOOTSTRAP_SERVERS

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful