Skip to content

Commit

Permalink
Add unit tests for deepEncodeKeyWhenUseObjectId for MongoClient
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchong committed Oct 24, 2023
1 parent 49adb45 commit b008bd0
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
57 changes: 57 additions & 0 deletions src/test/java/io/vertx/ext/mongo/MongoClientWithObjectIdTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.vertx.ext.mongo;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.bson.types.ObjectId;
import org.junit.Test;
Expand Down Expand Up @@ -31,6 +32,7 @@ public void tearDown() throws Exception {
protected static JsonObject getConfig() {
JsonObject config = MongoClientTestBase.getConfig();
config.put("useObjectId", true);
System.out.println("!!!!!!!!!!!!!!!!!!!!!!! config " + config);
return config;
}

Expand Down Expand Up @@ -77,6 +79,32 @@ public void testFindOneReturnsStringId() throws Exception {
await();
}

@Test
public void testFindOneWithNestedQueryReturnsStringId() throws Exception {
String collection = randomCollection();
mongoClient.createCollection(collection).onComplete(onSuccess(res -> {
JsonObject orig = createDoc();
JsonObject doc = orig.copy();
String objectId = getObjectId(doc);
JsonObject query = JsonObject.of("$and", JsonArray.of(
JsonObject.of("foo", "bar"),
JsonObject.of("_id", objectId)));
mongoClient.insert(collection, doc).onComplete(onSuccess(id -> {
// no auto-generated objectId from mongo
assertNull(id);
mongoClient.findOne(collection, query, null).onComplete(onSuccess(obj -> {
assertTrue(obj.containsKey("_id"));
assertTrue(obj.getValue("_id") instanceof String);
obj.remove("_id");
// nested "_id" will not be modified when insert
assertEquals(orig, obj);
testComplete();
}));
}));
}));
await();
}

@Test
public void testFindOneReturnsNothing() throws Exception {
String collection = randomCollection();
Expand Down Expand Up @@ -116,6 +144,35 @@ public void testFindReturnsStringId() throws Exception {
await();
}


@Test
public void testFindWithNestedQueryReturnsStringId() throws Exception {
String collection = randomCollection();
mongoClient.createCollection(collection).onComplete(onSuccess(res -> {
JsonObject orig = createDoc();
JsonObject doc = orig.copy();
String objectId = getObjectId(doc);
JsonObject query = JsonObject.of("$and", JsonArray.of(
JsonObject.of("foo", "bar"),
JsonObject.of("_id", objectId)));
mongoClient.insert(collection, doc).onComplete(onSuccess(id -> {
// no auto-generated objectId from mongo
assertNull(id);
mongoClient.find(collection, query).onComplete(onSuccess(list -> {
assertTrue(list.size() == 1);
JsonObject obj = list.get(0);
assertTrue(obj.containsKey("_id"));
assertTrue(obj.getValue("_id") instanceof String);
obj.remove("_id");
// nested "_id" will not be modified when insert
assertEquals(orig, obj);
testComplete();
}));
}));
}));
await();
}

@Test
@Override
public void testInsertPreexistingObjectID() throws Exception {
Expand Down
33 changes: 32 additions & 1 deletion src/test/java/io/vertx/ext/mongo/MongoTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,14 @@ protected JsonObject createDoc() {
.put("myarr", new JsonArray()
.add("blah")
.add(true)
.add(312)));
.add(312)))
.put("nested_id1", new JsonObject()
.put("_id", new ObjectId().toHexString()))
.put("nested_id2", new JsonArray()
.add(new JsonObject()
.put("_id", new ObjectId().toHexString()))
.add(new JsonObject()
.put("_id", new ObjectId().toHexString())));
}

protected JsonObject createDoc(int num) {
Expand Down Expand Up @@ -210,4 +217,28 @@ protected JsonObject createDocWithAmbiguitiesDependingOnLocale(int num) {
.put("longval", 123456789L).put("dblval", 1.23);
}

// WARN: try to getObjectId from doc will generate new objectId on doc if not exists
protected String getObjectId(JsonObject doc) {
Object idVal = doc.getValue("_id");

// auto generate when not exists
if(idVal == null) {
String _id = new ObjectId().toHexString();
doc.put("_id", JsonObject.of("$oid", _id));
return _id;
}

// return string
if(idVal instanceof String) {
return (String) idVal;
}

// return $oid from ObjectId object
if(idVal instanceof JsonObject) {
return ((JsonObject) idVal).getString("$oid");
}

return null;
}

}

0 comments on commit b008bd0

Please sign in to comment.