How to use executeTx method of org.testcontainers.containers.MongoDBContainerTest class

Best Testcontainers-java code snippet using org.testcontainers.containers.MongoDBContainerTest.executeTx

Source:MongoDBContainerTest.java Github

copy

Full Screen

...25 ) {26 // startingMongoDBContainer {27 mongoDBContainer.start();28 // }29 executeTx(mongoDBContainer);30 }31 }32 private void executeTx(MongoDBContainer mongoDBContainer) {33 final String mongoRsUrl = mongoDBContainer.getReplicaSetUrl();34 assertThat(mongoRsUrl).isNotNull();35 final String connectionString = mongoDBContainer.getConnectionString();36 final MongoClient mongoSyncClientBase = MongoClients.create(connectionString);37 final MongoClient mongoSyncClient = MongoClients.create(mongoRsUrl);38 mongoSyncClient39 .getDatabase("mydb1")40 .getCollection("foo")41 .withWriteConcern(WriteConcern.MAJORITY)42 .insertOne(new Document("abc", 0));43 mongoSyncClient44 .getDatabase("mydb2")45 .getCollection("bar")46 .withWriteConcern(WriteConcern.MAJORITY)47 .insertOne(new Document("xyz", 0));48 mongoSyncClientBase49 .getDatabase("mydb3")50 .getCollection("baz")51 .withWriteConcern(WriteConcern.MAJORITY)52 .insertOne(new Document("def", 0));53 final ClientSession clientSession = mongoSyncClient.startSession();54 final TransactionOptions txnOptions = TransactionOptions55 .builder()56 .readPreference(ReadPreference.primary())57 .readConcern(ReadConcern.LOCAL)58 .writeConcern(WriteConcern.MAJORITY)59 .build();60 final String trxResult = "Inserted into collections in different databases";61 TransactionBody<String> txnBody = () -> {62 final MongoCollection<Document> coll1 = mongoSyncClient.getDatabase("mydb1").getCollection("foo");63 final MongoCollection<Document> coll2 = mongoSyncClient.getDatabase("mydb2").getCollection("bar");64 coll1.insertOne(clientSession, new Document("abc", 1));65 coll2.insertOne(clientSession, new Document("xyz", 999));66 return trxResult;67 };68 try {69 final String trxResultActual = clientSession.withTransaction(txnBody, txnOptions);70 assertThat(trxResultActual).isEqualTo(trxResult);71 } catch (RuntimeException re) {72 throw new IllegalStateException(re.getMessage(), re);73 } finally {74 clientSession.close();75 mongoSyncClient.close();76 }77 }78 @Test79 public void supportsMongoDB_4_4() {80 try (final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.4"))) {81 mongoDBContainer.start();82 }83 }84 @Test85 public void shouldTestDatabaseName() {86 try (final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10"))) {87 mongoDBContainer.start();88 final String databaseName = "my-db";89 assertThat(mongoDBContainer.getReplicaSetUrl(databaseName)).endsWith(databaseName);90 }91 }92 @Test93 public void supportsMongoDB_6() {94 try (final MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:6.0.1")) {95 mongoDBContainer.start();96 executeTx(mongoDBContainer);97 }98 }99}...

Full Screen

Full Screen

executeTx

Using AI Code Generation

copy

Full Screen

1[INFO] [INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ testcontainers ---2[INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ testcontainers ---3[INFO] [INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ testcontainers ---4[INFO] [INFO] --- maven-source-plugin:3.0.1:jar-no-fork (attach-sources) @ testcontainers ---5[INFO] [INFO] --- maven-javadoc-plugin:3.1.0:jar (attach-javadocs) @ testcontainers ---6[INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ testcontainers ---

Full Screen

Full Screen

executeTx

Using AI Code Generation

copy

Full Screen

1 public void testExecuteTx() throws IOException {2 try (MongoDBContainer mongoDBContainer = new MongoDBContainer()) {3 mongoDBContainer.start();4 mongoDBContainer.executeTx("db.test.insertOne({foo: 'bar'})");5 mongoDBContainer.executeTx("db.test.insertOne({foo: 'baz'})");6 mongoDBContainer.executeTx("db.test.insertOne({foo: 'qux'})");7 try (MongoClient mongoClient = mongoDBContainer.getNewMongoClient()) {8 MongoDatabase testDb = mongoClient.getDatabase("test");9 MongoCollection<Document> testCollection = testDb.getCollection("test");10 assertEquals(3, testCollection.count());11 }12 }13 }14 @DisplayName("should run a command in a running container")15 public void testExecuteCommand() throws IOException {16 try (MongoDBContainer mongoDBContainer = new MongoDBContainer()) {17 mongoDBContainer.start();18 mongoDBContainer.executeCommand("mongo test --eval \"db.test.insertOne({foo: 'bar'})\"");19 mongoDBContainer.executeCommand("mongo test --eval \"db.test.insertOne({foo: 'baz'})\"");20 mongoDBContainer.executeCommand("mongo test --eval \"db.test.insertOne({foo: 'qux'})\"");21 try (MongoClient mongoClient = mongoDBContainer.getNewMongoClient()) {22 MongoDatabase testDb = mongoClient.getDatabase("test");23 MongoCollection<Document> testCollection = testDb.getCollection("test");24 assertEquals(3, testCollection.count());25 }26 }27 }28 @DisplayName("should run a command in a running container with a non-default user")29 public void testExecuteCommandWithCustomUser() throws IOException {30 try (MongoDBContainer mongoDBContainer = new MongoDBContainer()) {31 mongoDBContainer.start();32 mongoDBContainer.executeCommand("mongo test --eval \"db.test.insertOne({foo: 'bar'})\"");33 mongoDBContainer.executeCommand("mongo test --eval \"db.test.insertOne({foo: 'baz'})\"");34 mongoDBContainer.executeCommand("mongo test --eval \"db.test.insertOne({foo: 'qux'})\"");35 try (MongoClient mongoClient = mongoDBContainer.getNewMongoClient()) {36 MongoDatabase testDb = mongoClient.getDatabase("test");37 MongoCollection<Document> testCollection = testDb.getCollection("test");38 assertEquals(

Full Screen

Full Screen

executeTx

Using AI Code Generation

copy

Full Screen

1public class MongoDBContainerTest {2 private MongoDBContainer<?> container;3 public void setUp() {4 container = new MongoDBContainer<>(DockerImageName.parse("mongo:latest"));5 container.start();6 }7 public void tearDown() {8 container.stop();9 }10 public void testExecuteTx() throws TimeoutException, InterruptedException {11 String result = container.executeTx("db", "db.stats().ok");12 assertEquals("1", result);13 }14}15package org.testcontainers.containers;16import java.util.concurrent.TimeUnit;17import java.util.concurrent.TimeoutException;18import org.junit.After;19import org.junit.Before;20import org.junit.Test;21import static org.junit.Assert.assertEquals;22import static org.junit.Assert.assertTrue;23public class MongoDBContainerTest {24 private MongoDBContainer<?> container;25 public void setUp() {26 container = new MongoDBContainer<>(DockerImageName.parse("mongo:latest"));27 container.start();28 }29 public void tearDown() {30 container.stop();31 }32 public void testExecuteTx() throws TimeoutException, InterruptedException {33 String result = container.executeTx("db", "db.stats().ok");34 assertEquals("1", result);35 }36}37package org.testcontainers.containers;38import java.util.concurrent.TimeUnit;39import java.util.concurrent.TimeoutException;40import org.junit.After;41import org.junit.Before;42import org.junit.Test;43import static org.junit.Assert.assertEquals;44import static org.junit.Assert.assertTrue;45public class MongoDBContainerTest {46 private MongoDBContainer<?> container;47 public void setUp() {

Full Screen

Full Screen

executeTx

Using AI Code Generation

copy

Full Screen

1 final Document document = new Document("name", "test");2 final Document insertDocument = new Document("$set", document);3 final String collectionName = "test";4 mongoDBContainer.executeTx(mongoDatabase -> {5 MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);6 collection.insertOne(insertDocument);7 });8 final Document document1 = new Document("name", "test1");9 final Document insertDocument1 = new Document("$set", document1);10 final String collectionName1 = "test1";11 mongoDBContainer.executeTx(mongoDatabase -> {12 MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName1);13 collection.insertOne(insertDocument1);14 });15 final Document document2 = new Document("name", "test2");16 final Document insertDocument2 = new Document("$set", document2);17 final String collectionName2 = "test2";18 mongoDBContainer.executeTx(mongoDatabase -> {19 MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName2);20 collection.insertOne(insertDocument2);21 });22 final Document document3 = new Document("name", "test3");23 final Document insertDocument3 = new Document("$set", document3);24 final String collectionName3 = "test3";25 mongoDBContainer.executeTx(mongoDatabase -> {26 MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName3);27 collection.insertOne(insertDocument3);28 });29 final Document document4 = new Document("name", "test4");30 final Document insertDocument4 = new Document("$set", document4);31 final String collectionName4 = "test4";32 mongoDBContainer.executeTx(mongoDatabase -> {33 MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName4);34 collection.insertOne(insertDocument4);35 });

Full Screen

Full Screen

executeTx

Using AI Code Generation

copy

Full Screen

1 public void shouldExecuteTransaction() {2 MongoDBContainer mongoDBContainer = new MongoDBContainer();3 mongoDBContainer.start();4 MongoClient mongoClient = mongoDBContainer.getMongoClient();5 MongoDatabase database = mongoClient.getDatabase("mydatabase");6 MongoCollection<Document> collection = database.getCollection("mycollection");7 collection.insertOne(new Document("foo", "bar"));8 mongoDBContainer.executeTx("mydatabase", "mycollection", "db.mycollection.insertOne({foo: 'bar2'})");9 assertThat(collection.countDocuments()).isEqualTo(2);10 }11 public void shouldExecuteTransaction2() {12 MongoDBContainer mongoDBContainer = new MongoDBContainer();13 mongoDBContainer.start();14 MongoClient mongoClient = mongoDBContainer.getMongoClient();15 MongoDatabase database = mongoClient.getDatabase("mydatabase");16 MongoCollection<Document> collection = database.getCollection("mycollection");17 collection.insertOne(new Document("foo", "bar"));18 mongoDBContainer.executeTx("mydatabase", "mycollection", "db.mycollection.insertOne({foo: 'bar2'})");19 assertThat(collection.countDocuments()).isEqualTo(2);20 }21 public void shouldExecuteTransaction3() {22 MongoDBContainer mongoDBContainer = new MongoDBContainer();23 mongoDBContainer.start();24 MongoClient mongoClient = mongoDBContainer.getMongoClient();25 MongoDatabase database = mongoClient.getDatabase("mydatabase");26 MongoCollection<Document> collection = database.getCollection("mycollection");27 collection.insertOne(new Document("foo", "bar"));28 mongoDBContainer.executeTx("mydatabase", "mycollection", "db.mycollection.insertOne({foo: 'bar2'})");29 assertThat(collection.countDocuments()).isEqualTo(2);30 }31 public void shouldExecuteTransaction4() {

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