Search in sources :

Example 46 with UpdateResult

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"));
}
Also used : Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.Test)

Example 47 with UpdateResult

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"));
}
Also used : Exchange(org.apache.camel.Exchange) BasicDBObject(com.mongodb.BasicDBObject) Processor(org.apache.camel.Processor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.Test)

Example 48 with UpdateResult

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"));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) List(java.util.List) Arrays.asList(java.util.Arrays.asList) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.Test)

Example 49 with UpdateResult

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"));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.Test)

Example 50 with UpdateResult

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);
}
Also used : Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(com.mongodb.client.model.UpdateOptions)

Aggregations

UpdateResult (com.mongodb.client.result.UpdateResult)74 Document (org.bson.Document)31 Test (org.junit.Test)20 Bson (org.bson.conversions.Bson)16 Update (org.springframework.data.mongodb.core.query.Update)12 Test (org.testng.annotations.Test)11 UpdateOptions (com.mongodb.client.model.UpdateOptions)10 BasicDBObject (com.mongodb.BasicDBObject)9 FindOptions (dev.morphia.query.FindOptions)8 BsonDocument (org.bson.BsonDocument)8 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)7 DeleteResult (com.mongodb.client.result.DeleteResult)7 Query (org.springframework.data.mongodb.core.query.Query)7 ObjectId (org.bson.types.ObjectId)6 UpdateOptions (dev.morphia.UpdateOptions)5 ArrayList (java.util.ArrayList)5 Exchange (org.apache.camel.Exchange)5 Processor (org.apache.camel.Processor)5 DBObject (com.mongodb.DBObject)4 Formatter (java.util.Formatter)4