Skip to content

Commit

Permalink
Fix #44.
Browse files Browse the repository at this point in the history
Allow saving of documents with an existing _id
  • Loading branch information
johnoliver committed Oct 22, 2015
1 parent e5ece0e commit 2872455
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Expand Up @@ -100,7 +100,10 @@ public io.vertx.ext.mongo.MongoClient saveWithOptions(String collection, JsonObj
JsonObject encodedDocument = encodeKeyWhenUseObjectId(document);
filter.put(ID_FIELD, encodedDocument.getValue(ID_FIELD));

coll.replaceOne(wrap(filter), encodedDocument, convertCallback(resultHandler, result -> null));
com.mongodb.client.model.UpdateOptions updateOptions = new com.mongodb.client.model.UpdateOptions()
.upsert(true);

coll.replaceOne(wrap(filter), encodedDocument, updateOptions, convertCallback(resultHandler, result -> null));
}
return this;
}
Expand Down
Expand Up @@ -127,6 +127,16 @@ public void testInsertNoCollection() {
await();
}

public void assertDocumentWithIdIsPresent(String collection, Object id) {
mongoClient.find(collection,
new JsonObject()
.put("_id", id),
onSuccess(result -> {
assertEquals(1, result.size());
testComplete();
}));
}

@Test
public void testInsertNoPreexistingID() throws Exception {
String collection = randomCollection();
Expand All @@ -148,8 +158,7 @@ public void testInsertPreexistingID() throws Exception {
String genID = TestUtils.randomAlphaString(100);
doc.put("_id", genID);
mongoClient.insert(collection, doc, onSuccess(id -> {
assertNull(id);
testComplete();
assertDocumentWithIdIsPresent(collection, genID);
}));
}));
await();
Expand All @@ -163,8 +172,7 @@ public void testInsertPreexistingLongID() throws Exception {
Long genID = TestUtils.randomLong();
doc.put("_id", genID);
mongoClient.insertWithOptions(collection, doc, ACKNOWLEDGED, onSuccess(id -> {
assertNull(id);
testComplete();
assertDocumentWithIdIsPresent(collection, genID);
}));
}));
await();
Expand All @@ -178,8 +186,7 @@ public void testSavePreexistingLongID() throws Exception {
Long genID = TestUtils.randomLong();
doc.put("_id", genID);
mongoClient.saveWithOptions(collection, doc, ACKNOWLEDGED, onSuccess(id -> {
assertNull(id);
testComplete();
assertDocumentWithIdIsPresent(collection, genID);
}));
}));
await();
Expand All @@ -193,8 +200,7 @@ public void testInsertPreexistingObjectID() throws Exception {
JsonObject genID = new JsonObject().put("id", TestUtils.randomAlphaString(100));
doc.put("_id", genID);
mongoClient.insertWithOptions(collection, doc, ACKNOWLEDGED, onSuccess(id -> {
assertNull(id);
testComplete();
assertDocumentWithIdIsPresent(collection, genID);
}));
}));
await();
Expand Down

0 comments on commit 2872455

Please sign in to comment.