use of com.mongodb.client.result.UpdateResult in project camel by apache.
the class MongoDbOperationsTest method testSaveWithoutId.
@Test
public void testSaveWithoutId() {
// Prepare test
assertEquals(0, testCollection.count());
// This document should not be modified
Document doc = new Document("scientist", "Copernic");
template.requestBody("direct:insert", doc);
// save (upsert) a document without Id => insert with new Id
doc = new Document("scientist", "Einstein");
assertNull(doc.get(MONGO_ID));
UpdateResult result = template.requestBody("direct:save", doc, UpdateResult.class);
assertNotNull(result.getUpsertedId());
// Without Id save perform an insert not an update.
assertEquals(0, result.getModifiedCount());
// Testing the save logic
Document record1 = testCollection.find(eq(MONGO_ID, result.getUpsertedId())).first();
assertEquals("Scientist field of '" + result.getUpsertedId() + "' must equal 'Einstein'", "Einstein", record1.get("scientist"));
}
use of com.mongodb.client.result.UpdateResult in project camel by apache.
the class MongoDbHeaderHandlingTest method testWriteResultAsHeaderWithWriteOp.
@Test
public void testWriteResultAsHeaderWithWriteOp() {
// Prepare test
assertEquals(0, testCollection.count());
Object[] req = new Object[] { "{\"_id\":\"testSave1\", \"scientist\":\"Einstein\"}", "{\"_id\":\"testSave2\", \"scientist\":\"Copernicus\"}" };
Object result = template.requestBody("direct:insert", req);
//assertTrue(result instanceof WriteResult);
assertEquals("Number of records persisted must be 2", 2, testCollection.count());
// Testing the save logic
final DBObject record1 = testCollection.find(new BasicDBObject("_id", "testSave1")).first();
assertEquals("Scientist field of 'testSave1' must equal 'Einstein'", "Einstein", record1.get("scientist"));
record1.put("scientist", "Darwin");
// test that as a payload, we get back exactly our input, but enriched with the CamelMongoDbWriteResult header
Exchange resultExch = template.request("direct:save", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(record1);
}
});
assertTrue(resultExch.getOut().getBody() instanceof BasicDBObject);
assertTrue(resultExch.getOut().getBody().equals(record1));
assertTrue(resultExch.getOut().getHeader(MongoDbConstants.WRITERESULT) instanceof UpdateResult);
DBObject record2 = testCollection.find(new BasicDBObject("_id", "testSave1")).first();
assertEquals("Scientist field of 'testSave1' must equal 'Darwin' after save operation", "Darwin", record2.get("scientist"));
}
use of com.mongodb.client.result.UpdateResult in project camel by apache.
the class MongoDbOperationsTest method testSave.
@Test
public void testSave() throws Exception {
// Prepare test
assertEquals(0, testCollection.count());
Object[] req = new Object[] { "{\"_id\":\"testSave1\", \"scientist\":\"Einstein\"}", "{\"_id\":\"testSave2\", \"scientist\":\"Copernicus\"}" };
Object result = template.requestBody("direct:insert", req);
assertTrue(result instanceof List);
assertEquals("Number of records persisted must be 2", 2, testCollection.count());
// Testing the save logic
DBObject record1 = testCollection.find(new BasicDBObject("_id", "testSave1")).first();
assertEquals("Scientist field of 'testSave1' must equal 'Einstein'", "Einstein", record1.get("scientist"));
record1.put("scientist", "Darwin");
result = template.requestBody("direct:save", record1);
assertTrue(result instanceof UpdateResult);
record1 = testCollection.find(new BasicDBObject("_id", "testSave1")).first();
assertEquals("Scientist field of 'testSave1' must equal 'Darwin' after save operation", "Darwin", record1.get("scientist"));
}
use of com.mongodb.client.result.UpdateResult in project camel by apache.
the class MongoDbHeaderHandlingTest method testWriteResultAsHeaderWithWriteOp.
@Test
public void testWriteResultAsHeaderWithWriteOp() {
// Prepare test
assertEquals(0, testCollection.count());
Object[] req = new Object[] { new Document(MONGO_ID, "testSave1").append("scientist", "Einstein").toJson(), new Document(MONGO_ID, "testSave2").append("scientist", "Copernicus").toJson() };
// Object result =
template.requestBody("direct:insert", req);
// assertTrue(result instanceof WriteResult);
assertEquals("Number of records persisted must be 2", 2, testCollection.count());
// Testing the save logic
final Document record1 = testCollection.find(eq(MONGO_ID, "testSave1")).first();
assertEquals("Scientist field of 'testSave1' must equal 'Einstein'", "Einstein", record1.get("scientist"));
record1.put("scientist", "Darwin");
// test that as a payload, we get back exactly our input, but enriched
// with the CamelMongoDbWriteResult header
Exchange resultExch = template.request("direct:save", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(record1);
}
});
assertTrue(resultExch.getOut().getBody() instanceof Document);
assertTrue(resultExch.getOut().getBody().equals(record1));
assertTrue(resultExch.getOut().getHeader(MongoDbConstants.WRITERESULT) instanceof UpdateResult);
Document record2 = testCollection.find(eq(MONGO_ID, "testSave1")).first();
assertEquals("Scientist field of 'testSave1' must equal 'Darwin' after save operation", "Darwin", record2.get("scientist"));
}
use of com.mongodb.client.result.UpdateResult in project sling by apache.
the class MongoDBNoSqlAdapter method store.
@Override
public boolean store(NoSqlData data) {
Document envelope = new Document();
envelope.put(PN_PATH, data.getPath());
envelope.put(PN_DATA, new Document(data.getProperties(MultiValueMode.LISTS)));
// for list-children query efficiency store parent path as well
String parentPath = ResourceUtil.getParent(data.getPath());
if (parentPath != null) {
envelope.put(PN_PARENT_PATH, parentPath);
}
UpdateResult result = collection.replaceOne(Filters.eq(PN_PATH, data.getPath()), envelope, new UpdateOptions().upsert(true));
// return true if a new entry was inserted, false if an existing was replaced
return (result.getMatchedCount() == 0);
}
Aggregations