Skip to content

Commit

Permalink
Use test containers for MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Nov 6, 2024
1 parent 1632f89 commit e15a86c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
15 changes: 1 addition & 14 deletions mongo-examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,4 @@

Here you will find examples demonstrating the usage of the Vert.x Mongo Client.

* link:src/main/java/io/vertx/examples/mongo/MongoClientVerticle.java[MongoClientVerticle.java]
To run this example, you need a running Mongo instance. Once running, you can configure the
verticles with the mongo host:

----
{
"mongo_uri": "mongodb://localhost:27017",
"mongo_db" : "test"
}
----

By default, it uses `localhost` as host.
The port is set to `27017`.
link:src/main/java/io/vertx/examples/mongo/MongoClientVerticle.java[MongoClientVerticle.java]
5 changes: 5 additions & 0 deletions mongo-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>vertx-mongo-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<version>${testcontainers.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,29 @@

import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.mongo.MongoClient;
import io.vertx.launcher.application.VertxApplication;
import org.testcontainers.containers.MongoDBContainer;

public class MongoClientVerticle extends VerticleBase {

private static final MongoDBContainer MONGO_DB_CONTAINER = new MongoDBContainer("mongo:4.0.10");

public static void main(String[] args) {
MONGO_DB_CONTAINER.start();

VertxApplication.main(new String[]{MongoClientVerticle.class.getName()});
}

private MongoClient mongoClient;

@Override
public Future<?> start() throws Exception {

JsonObject config = Vertx.currentContext().config();

String uri = config.getString("mongo_uri");
if (uri == null) {
uri = "mongodb://localhost:27017";
}
String db = config.getString("mongo_db");
if (db == null) {
db = "test";
}
public Future<?> start() {

JsonObject mongoconfig = new JsonObject()
.put("connection_string", uri)
.put("db_name", db);
.put("connection_string", MONGO_DB_CONTAINER.getConnectionString())
.put("db_name", "test");

mongoClient = MongoClient.createShared(vertx, mongoconfig);

Expand Down

0 comments on commit e15a86c

Please sign in to comment.