How to use testExternalZookeeperWithExternalNetwork method of org.testcontainers.containers.KafkaContainerTest class

Best Testcontainers-java code snippet using org.testcontainers.containers.KafkaContainerTest.testExternalZookeeperWithExternalNetwork

Source:KafkaContainerTest.java Github

copy

Full Screen

...30 kafka.start();31 }32 }33 @Test34 public void testExternalZookeeperWithExternalNetwork() throws Exception {35 try (36 Network network = Network.newNetwork();37 KafkaContainer kafka = new KafkaContainer(KAFKA_TEST_IMAGE)38 .withNetwork(network)39 .withExternalZookeeper("zookeeper:2181");40 GenericContainer<?> zookeeper = new GenericContainer<>(ZOOKEEPER_TEST_IMAGE)41 .withNetwork(network)42 .withNetworkAliases("zookeeper")43 .withEnv("ZOOKEEPER_CLIENT_PORT", "2181");44 GenericContainer<?> application = new GenericContainer<>(DockerImageName.parse("alpine"))45 .withNetwork(network)46 .withNetworkAliases("dummy")47 .withCommand("sleep 10000")48 ) {...

Full Screen

Full Screen

testExternalZookeeperWithExternalNetwork

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;3import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;4import java.io.IOException;5import java.util.Arrays;6import java.util.List;7import java.util.concurrent.TimeUnit;8import java.util.stream.Collectors;9import org.apache.kafka.clients.consumer.ConsumerConfig;10import org.apache.kafka.clients.consumer.KafkaConsumer;11import org.apache.kafka.clients.producer.KafkaProducer;12import org.apache.kafka.clients.producer.ProducerConfig;13import org.apache.kafka.clients.producer.ProducerRecord;14import org.apache.kafka.common.serialization.StringDeserializer;15import org.apache.kafka.common.serialization.StringSerializer;16import org.junit.Test;17import org.rnorth.ducttape.unreliables.Unreliables;18import org.rnorth.visibleassertions.VisibleAssertions;19public class KafkaContainerTest {20 public void testSimple() throws Exception {21 try (KafkaContainer kafka = new KafkaContainer()) {22 kafka.start();23 String bootstrapServers = kafka.getBootstrapServers();24 VisibleAssertions.info("Kafka brokers: " + bootstrapServers);25 try (KafkaConsumer<String, String> consumer = createConsumer(bootstrapServers)) {26 consumer.subscribe(Arrays.asList("topic1"));27 try (KafkaProducer<String, String> producer = createProducer(bootstrapServers)) {28 producer.send(new ProducerRecord<>("topic1", "key1", "value1")).get();29 producer.send(new ProducerRecord<>("topic1", "key2", "value2")).get();30 }31 Unreliables.retryUntilTrue(10, TimeUnit.SECONDS, () -> {32 List<String> records = consumer.poll(1000).stream()33 .map(r -> r.key() + ":" + r.value())34 .collect(Collectors.toList());35 return records.equals(Arrays.asList("key1:value1", "key2:value2"));36 });37 }38 }39 }40 public void testWithExternalZookeeper() throws Exception {41 try (ZooKeeperContainer zookeeper = new ZooKeeperContainer()) {42 zookeeper.start();43 try (KafkaContainer kafka = new KafkaContainer(zookeeper.getConnectString())) {44 kafka.start();45 String bootstrapServers = kafka.getBootstrapServers();46 VisibleAssertions.info("Kafka brokers: " + bootstrapServers);47 try (KafkaConsumer<String, String> consumer = createConsumer(bootstrapServers)) {48 consumer.subscribe(Arrays.asList

Full Screen

Full Screen

testExternalZookeeperWithExternalNetwork

Using AI Code Generation

copy

Full Screen

1 public void testExternalZookeeperWithExternalNetwork() throws Exception {2 try (Network network = Network.newNetwork()) {3 String zookeeperContainerName = "zookeeper";4 String kafkaContainerName = "kafka";5 String zookeeperNetworkAlias = "zookeeper";6 String kafkaNetworkAlias = "kafka";7 GenericContainer zookeeper = new GenericContainer(DockerImageName.parse("confluentinc/cp-zookeeper:5.5.1"))8 .withNetwork(network)9 .withNetworkAliases(zookeeperNetworkAlias)10 .withEnv("ZOOKEEPER_CLIENT_PORT", "2181")11 .withEnv("ZOOKEEPER_TICK_TIME", "2000")12 .withExposedPorts(2181)13 .withName(zookeeperContainerName);14 GenericContainer kafka = new GenericContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"))15 .withNetwork(network)16 .withNetworkAliases(kafkaNetworkAlias)17 .withEnv("KAFKA_BROKER_ID", "1")18 .withEnv("KAFKA_ZOOKEEPER_CONNECT", zookeeperNetworkAlias + ":2181")19 .withEnv("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", "1")20 .withExposedPorts(9092)21 .withName(kafkaContainerName);22 zookeeper.start();23 kafka.start();24 String zookeeperHost = zookeeper.getContainerIpAddress();25 Integer zookeeperPort = zookeeper.getMappedPort(2181);26 String kafkaHost = kafka.getContainerIpAddress();27 Integer kafkaPort = kafka.getMappedPort(9092);28 assertThat(zookeeperHost).isEqualTo(kafkaHost);29 assertThat(zookeeperPort).isEqualTo(2181);30 assertThat(kafkaPort).isEqualTo(9092);31 }32 }33 @DisplayName("should be able to use external kafka with external network")34 @Disabled("Requires external Kafka. Run manually.")35 public void testExternalKafkaWithExternalNetwork() throws Exception {36 try (Network network = Network.newNetwork()) {37 String zookeeperContainerName = "zookeeper";38 String kafkaContainerName = "kafka";39 String zookeeperNetworkAlias = "zookeeper";

Full Screen

Full Screen

testExternalZookeeperWithExternalNetwork

Using AI Code Generation

copy

Full Screen

1import org.junit.jupiter.api.Test;2import org.testcontainers.containers.KafkaContainer;3import org.testcontainers.containers.Network;4import org.testcontainers.containers.wait.strategy.Wait;5import org.testcontainers.utility.DockerImageName;6public class KafkaContainerTest {7 private static final DockerImageName ZOOKEEPER_IMAGE = DockerImageName.parse("confluentinc/cp-zookeeper:5.5.1");8 private static final DockerImageName KAFKA_IMAGE = DockerImageName.parse("confluentinc/cp-kafka:5.5.1");9 public void testExternalZookeeperWithExternalNetwork() {10 Network network = Network.newNetwork();11 KafkaContainer kafkaContainer = new KafkaContainer(KAFKA_IMAGE)12 .withNetwork(network)13 .withNetworkAliases("kafka")14 .withExternalZookeeper("zookeeper:2181")15 .waitingFor(Wait.forLogMessage(".*started.*", 1));16 kafkaContainer.start();17 kafkaContainer.stop();18 }19}20import org.junit.jupiter.api.Test;21import org.testcontainers.containers.KafkaContainer;22import org.testcontainers.containers.Network;23import org.testcontainers.containers.wait.strategy.Wait;24import org.testcontainers.utility.DockerImageName;25public class KafkaContainerTest {26 private static final DockerImageName ZOOKEEPER_IMAGE = DockerImageName.parse("confluentinc/cp-zookeeper:5.5.1");27 private static final DockerImageName KAFKA_IMAGE = DockerImageName.parse("confluentinc/cp-kafka:5.5.1");28 public void testExternalZookeeperWithExternalNetwork() {29 Network network = Network.newNetwork();30 KafkaContainer kafkaContainer = new KafkaContainer(KAFKA_IMAGE)31 .withNetwork(network)32 .withNetworkAliases("kafka")33 .withExternalZookeeper("zookeeper:2181")34 .withEnv("KAFKA_ZOOKEEPER_CONNECT", "zookeeper:2181")35 .waitingFor(Wait.forLogMessage(".*started.*", 1));36 kafkaContainer.start();37 kafkaContainer.stop();38 }39}

Full Screen

Full Screen

testExternalZookeeperWithExternalNetwork

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.KafkaContainer2import org.testcontainers.containers.Network3import org.testcontainers.containers.output.Slf4jLogConsumer4import org.testcontainers.containers.wait.strategy.Wait5import org.testcontainers.utility.DockerImageName6import org.testcontainers.utility.MountableFile7import org.slf4j.LoggerFactory8import static org.testcontainers.containers.KafkaContainer.KAFKA_PORT9import static org.testcontainers.containers.KafkaContainer.ZOOKEEPER_PORT10class KafkaContainerTest {

Full Screen

Full Screen

testExternalZookeeperWithExternalNetwork

Using AI Code Generation

copy

Full Screen

1public class KafkaContainerTest {2 private static final String KAFKA_VERSION = "5.2.1";3 private static final String ZOOKEEPER_VERSION = "3.4.13";4 public static GenericContainer zookeeper = new GenericContainer("zookeeper:" + ZOOKEEPER_VERSION)5 .withExposedPorts(2181);6 public static KafkaContainer kafka = new KafkaContainer(KAFKA_VERSION)7 .withExternalZookeeper("localhost:" + zookeeper.getMappedPort(2181));8 public void testKafka() throws Exception {9 try (KafkaProducer<String, String> producer = createProducer()) {10 try (KafkaConsumer<String, String> consumer = createConsumer()) {11 String topic = "test-" + System.currentTimeMillis();12 consumer.subscribe(Collections.singletonList(topic));13 producer.send(new ProducerRecord<>(topic, "key", "value"));14 producer.flush();15 ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(30));16 assertThat(records.count(), is(1));17 }18 }19 }20 private KafkaProducer<String, String> createProducer() {21 Properties properties = new Properties();22 properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers());23 properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());24 properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());25 return new KafkaProducer<>(properties);26 }27 private KafkaConsumer<String, String> createConsumer() {28 Properties properties = new Properties();29 properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers());30 properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "test-" + System.currentTimeMillis());31 properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());32 properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());33 return new KafkaConsumer<>(properties);34 }35 public void testExternalZookeeperWithExternalNetwork() throws Exception {36 try (Network network = Network.newNetwork()) {37 try (GenericContainer zookeeper = new GenericContainer("zookeeper:" + ZOOKEEPER_VERSION)

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