Best Testcontainers-java code snippet using org.testcontainers.redpanda.RedpandaContainerTest.testKafkaFunctionality
Source:RedpandaContainerTest.java
...31 @Test32 public void testUsage() throws Exception {33 try (RedpandaContainer container = new RedpandaContainer(REDPANDA_DOCKER_IMAGE)) {34 container.start();35 testKafkaFunctionality(container.getBootstrapServers());36 }37 }38 @Test39 public void testUsageWithStringImage() throws Exception {40 try (41 // constructorWithVersion {42 RedpandaContainer container = new RedpandaContainer("docker.redpanda.com/vectorized/redpanda:v22.2.1")43 // }44 ) {45 container.start();46 testKafkaFunctionality(47 // getBootstrapServers {48 container.getBootstrapServers()49 // }50 );51 }52 }53 @Test54 public void testNotCompatibleVersion() {55 assertThatThrownBy(() -> new RedpandaContainer("docker.redpanda.com/vectorized/redpanda:v21.11.19"))56 .isInstanceOf(IllegalArgumentException.class)57 .hasMessageContaining("Redpanda version must be >= v22.2.1");58 }59 @Test60 public void testSchemaRegistry() {61 try (RedpandaContainer container = new RedpandaContainer(REDPANDA_DOCKER_IMAGE)) {62 container.start();63 String subjectsEndpoint = String.format(64 "%s/subjects",65 // getSchemaRegistryAddress {66 container.getSchemaRegistryAddress()67 // }68 );69 String subjectName = String.format("test-%s-value", UUID.randomUUID());70 Response createSubject = RestAssured71 .given()72 .contentType("application/vnd.schemaregistry.v1+json")73 .pathParam("subject", subjectName)74 .body("{\"schema\": \"{\\\"type\\\": \\\"string\\\"}\"}")75 .when()76 .post(subjectsEndpoint + "/{subject}/versions")77 .thenReturn();78 assertThat(createSubject.getStatusCode()).isEqualTo(200);79 Response allSubjects = RestAssured.given().when().get(subjectsEndpoint).thenReturn();80 assertThat(allSubjects.getStatusCode()).isEqualTo(200);81 assertThat(allSubjects.jsonPath().getList("$")).contains(subjectName);82 }83 }84 private void testKafkaFunctionality(String bootstrapServers) throws Exception {85 testKafkaFunctionality(bootstrapServers, 1, 1);86 }87 private void testKafkaFunctionality(String bootstrapServers, int partitions, int rf) throws Exception {88 try (89 AdminClient adminClient = AdminClient.create(90 ImmutableMap.of(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers)91 );92 KafkaProducer<String, String> producer = new KafkaProducer<>(93 ImmutableMap.of(94 ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,95 bootstrapServers,96 ProducerConfig.CLIENT_ID_CONFIG,97 UUID.randomUUID().toString()98 ),99 new StringSerializer(),100 new StringSerializer()101 );...
testKafkaFunctionality
Using AI Code Generation
1import org.testcontainers.containers.Network;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.testcontainers.containers.wait.strategy.Wait;4import org.testcontainers.junit.jupiter.Container;5import org.testcontainers.junit.jupiter.Testcontainers;6import org.testcontainers.redpanda.RedpandaContainer;7import org.testcontainers.utility.DockerImageName;8class RedpandaContainerTest {9 private static final RedpandaContainer redpanda = new RedpandaContainer(DockerImageName.parse("vectorized/redpanda"))10 .withNetwork(Network.newNetwork())11 .withNetworkAliases("redpanda")12 .withExposedPorts(9092)13 .waitingFor(Wait.forLogMessage(".*redpanda::main - application.cc:101 - Red Panda started.*", 1));14 void testKafkaFunctionality() throws Exception {15 redpanda.start();16 final String topic = "test-topic";17 final String message = "test-message";18 final String groupId = "test-group";19 try (final AdminClient admin = AdminClient.create(redpanda.getAdminClientConfig())) {20 admin.createTopics(Collections.singleton(new NewTopic(topic, 1, (short) 1))).all().get();21 }22 redpanda.followOutput(new Slf4jLogConsumer(LoggerFactory.getLogger(RedpandaContainerTest.class)));23 try (final KafkaProducer<String, String> producer = new KafkaProducer<>(redpanda.getProducerProperties())) {24 producer.send(new ProducerRecord<>(topic, message)).get();25 }26 try (final KafkaConsumer<String, String> consumer = new KafkaConsumer<>(redpanda.getConsumerProperties(groupId, "consumer-1"))) {27 consumer.subscribe(Collections.singleton(topic));28 final ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(30));29 assertThat(records.count()).isEqualTo(1);30 assertThat(records.iterator().next().value()).isEqualTo(message);31 }32 }33}
testKafkaFunctionality
Using AI Code Generation
1import org.testcontainers.containers.KafkaContainer;2import org.testcontainers.containers.Network;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import org.testcontainers.containers.output.WaitingConsumer;5import org.testcontainers.utility.DockerImageName;6import org.testcontainers.utility.MountableFile;7import java.io.IOException;8import java.nio.file.Files;9import java.nio.file.Path;10import java.util.concurrent.TimeUnit;11public class RedpandaContainerTest {12 private static final String REDPANDA_IMAGE = "vectorized/redpanda:v21.1.1";13 public static void main(String[] args) throws IOException, InterruptedException {14 Path tempDir = Files.createTempDirectory("redpanda-test");15 System.out.println("Temp dir: " + tempDir);16 try (RedpandaContainer redpanda = new RedpandaContainer(DockerImageName.parse(REDPANDA_IMAGE))17 .withNetwork(Network.newNetwork())18 .withNetworkAliases("redpanda")19 .withExposedPorts(9092)20 .withFileSystemBind(tempDir.toString(), "/var/lib/redpanda/data")21 .withFileSystemBind(tempDir.toString(), "/var/lib/redpanda/conf")22 .withFileSystemBind(tempDir.toString(), "/var/lib/redpanda/logs")23 .withFileSystemBind(tempDir.toString(), "/var/lib/redpanda/ntp")24 .withFileSystemBind(tempDir.toString(), "/var/lib/redpanda/txn")25 .withFileSystemBind(tempDir.toString(), "/var/lib/redpanda/coproc")26 .withFileSystemBind(tempDir.toString(), "/var/lib/redpanda/offsets")
testKafkaFunctionality
Using AI Code Generation
1public class TestKafkaFunctionality {2 public void testKafkaFunctionality() throws Exception {3 try (RedPandaContainer redPanda = new RedPandaContainer()) {4 redPanda.start();5 redPanda.testKafkaFunctionality();6 }7 }8}
testKafkaFunctionality
Using AI Code Generation
1> import org.testcontainers.containers.KafkaContainer2> import org.testcontainers.containers.Network3> import org.testcontainers.containers.output.Slf4jLogConsumer4> import org.testcontainers.utility.DockerImageName5> import org.testcontainers.utility.MountableFile6> import org.testcontainers.containers.GenericContainer7> import org.testcontainers.containers.wait.strategy.Wait8> import org.testcontainers.containers.wait.strategy.WaitAllStrategy9> import org.testcontainers.containers.wait.strategy.WaitStrategy10> import org.testcontainers.containers.wait.strategy.Wait11> import org.testcontainers.containers.wait.strategy.WaitAllStrategy12> import org.testcontainers.containers.wait.strategy.WaitStrategy13> import org.testcontainers.containers.wait.strategy.Wait14> import org.testcontainers.containers.wait.strategy.WaitAllStrategy15> import org.testcontainers.containers.wait.strategy.WaitStrategy16> import org.testcontainers.containers.wait.strategy.Wait17> import org.testcontainers.containers.wait.strategy.WaitAllStrategy18> import org.testcontainers.containers.wait.strategy.WaitStrategy19> import org.testcontainers.containers.wait.strategy.Wait20> import org.testcontainers.containers.wait.strategy.WaitAllStrategy21> import org.testcontainers.containers.wait.strategy.WaitStrategy22> import org.testcontainers.containers.wait.strategy.Wait23> import org.testcontainers.containers.wait.strategy.WaitAllStrategy24> import org.testcontainers.containers.wait.strategy.WaitStrategy25> import org.testcontainers.containers.wait.strategy.Wait26> import org.testcontainers.containers.wait.strategy.WaitAllStrategy27> import org.testcontainers.containers.wait.strategy.WaitStrategy28> import org.testcontainers.containers.wait.strategy.Wait29> import org.testcontainers.containers.wait.strategy.WaitAllStrategy30> import org.testcontainers.containers.wait.strategy.WaitStrategy31> import org.testcontainers.containers.wait.strategy.Wait32> import org.testcontainers.containers.wait.strategy.WaitAllStrategy33> import org.testcontainers.containers.wait.strategy.WaitStrategy34> import org.testcontainers.containers.wait.strategy.Wait35> import org.testcontainers.containers.wait.strategy.WaitAllStrategy36> import org.testcontainers.containers.wait.strategy.WaitStrategy37> import org.testcontainers.containers.wait.strategy.Wait38> import org.testcontainers.containers.wait.strategy.WaitAllStrategy39> import org.testcontainers.containers.wait.strategy.WaitStrategy40> import org.testcontainers.containers.wait.strategy
testKafkaFunctionality
Using AI Code Generation
1public class TestRedPandaContainer {2 public void testKafkaFunctionality() throws Exception {3 RedPandaContainer redPandaContainer = new RedPandaContainer();4 redPandaContainer.start();5 redPandaContainer.testKafkaFunctionality();6 redPandaContainer.stop();7 }8}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!