Search in sources :

Example 11 with DBObject

use of com.mongodb.DBObject in project camel by apache.

the class MongoDbOperationsTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    // Prepare test
    assertEquals(0, testCollection.count());
    for (int i = 1; i <= 100; i++) {
        String body = null;
        Formatter f = new Formatter();
        if (i % 2 == 0) {
            body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString();
        } else {
            body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\", \"extraField\": true}", i).toString();
        }
        f.close();
        template.requestBody("direct:insert", body);
    }
    assertEquals(100L, testCollection.count());
    // Testing the update logic
    BasicDBObject extraField = new BasicDBObject("extraField", true);
    assertEquals("Number of records with 'extraField' flag on must equal 50", 50L, testCollection.count(extraField));
    assertEquals("Number of records with 'scientist' field = Darwin on must equal 0", 0, testCollection.count(new BasicDBObject("scientist", "Darwin")));
    DBObject updateObj = new BasicDBObject("$set", new BasicDBObject("scientist", "Darwin"));
    Exchange resultExchange = template.request("direct:update", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(new Object[] { extraField, updateObj });
            exchange.getIn().setHeader(MongoDbConstants.MULTIUPDATE, true);
        }
    });
    Object result = resultExchange.getOut().getBody();
    assertTrue(result instanceof UpdateResult);
    assertEquals("Number of records updated header should equal 50", 50L, resultExchange.getOut().getHeader(MongoDbConstants.RECORDS_AFFECTED));
    assertEquals("Number of records with 'scientist' field = Darwin on must equal 50 after update", 50, testCollection.count(new BasicDBObject("scientist", "Darwin")));
}
Also used : Exchange(org.apache.camel.Exchange) BasicDBObject(com.mongodb.BasicDBObject) Processor(org.apache.camel.Processor) Formatter(java.util.Formatter) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.junit.Test)

Example 12 with DBObject

use of com.mongodb.DBObject in project camel by apache.

the class MongoDbOperationsTest method testStoreOidOnInsert.

@Test
public void testStoreOidOnInsert() throws Exception {
    DBObject dbObject = new BasicDBObject();
    ObjectId oid = template.requestBody("direct:testStoreOidOnInsert", dbObject, ObjectId.class);
    assertEquals(dbObject.get("_id"), oid);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 13 with DBObject

use of com.mongodb.DBObject in project camel by apache.

the class MongoDbOperationsTest method testCommand.

@Test
public void testCommand() throws Exception {
    //Call hostInfo, command working with every configuration
    Object result = template.requestBody("direct:command", "{\"hostInfo\":\"1\"}");
    assertTrue("Result is not of type DBObject", result instanceof Document);
    assertTrue("The result should contain keys", ((Document) result).keySet().size() > 0);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Document(org.bson.Document) Test(org.junit.Test)

Example 14 with DBObject

use of com.mongodb.DBObject in project camel by apache.

the class MongoDbOutputTypeTest method testFindAllDBObjectList.

@Test
public void testFindAllDBObjectList() {
    // Test that the collection has 0 documents in it
    assertEquals(0, testCollection.count());
    pumpDataIntoTestCollection();
    Object result = template.requestBody("direct:findAllDBObjectList", (Object) null);
    assertTrue("Result is not of type List", result instanceof List);
    @SuppressWarnings("unchecked") List<DBObject> resultList = (List<DBObject>) result;
    assertListSize("Result does not contain 1000 elements", resultList, 1000);
    // Ensure that all returned documents contain all fields
    for (DBObject dbObject : resultList) {
        assertNotNull("DBObject in returned list should contain all fields", dbObject.get("_id"));
        assertNotNull("DBObject in returned list should contain all fields", dbObject.get("scientist"));
        assertNotNull("DBObject in returned list should contain all fields", dbObject.get("fixedField"));
    }
    for (Exchange resultExchange : getMockEndpoint("mock:resultFindAll").getReceivedExchanges()) {
        assertEquals("Result total size header should equal 1000", 1000, resultExchange.getIn().getHeader(MongoDbConstants.RESULT_TOTAL_SIZE));
    }
}
Also used : Exchange(org.apache.camel.Exchange) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) List(java.util.List) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 15 with DBObject

use of com.mongodb.DBObject in project camel by apache.

the class MongoDbOutputTypeTest method testFindAllDBCursor.

@Test
public void testFindAllDBCursor() {
    // Test that the collection has 0 documents in it
    assertEquals(0, testCollection.count());
    pumpDataIntoTestCollection();
    // Repeat ten times, obtain 10 batches of 100 results each time
    int numToSkip = 0;
    final int limit = 100;
    for (int i = 0; i < 10; i++) {
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put(MongoDbConstants.NUM_TO_SKIP, numToSkip);
        headers.put(MongoDbConstants.LIMIT, 100);
        Object result = template.requestBodyAndHeaders("direct:findAllDBCursor", (Object) null, headers);
        assertTrue("Result is not of type DBCursor", result instanceof MongoIterable);
        MongoIterable<BasicDBObject> resultCursor = (MongoIterable<BasicDBObject>) result;
        // Ensure that all returned documents contain all fields
        for (DBObject dbObject : resultCursor) {
            assertNotNull("DBObject in returned list should contain all fields", dbObject.get("_id"));
            assertNotNull("DBObject in returned list should contain all fields", dbObject.get("scientist"));
            assertNotNull("DBObject in returned list should contain all fields", dbObject.get("fixedField"));
        }
        numToSkip = numToSkip + limit;
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) HashMap(java.util.HashMap) MongoIterable(com.mongodb.client.MongoIterable) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Aggregations

DBObject (com.mongodb.DBObject)646 BasicDBObject (com.mongodb.BasicDBObject)445 Test (org.junit.Test)267 YearFilterPagingRequest (org.devgateway.ocds.web.rest.controller.request.YearFilterPagingRequest)95 DBCollection (com.mongodb.DBCollection)84 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)79 ApiOperation (io.swagger.annotations.ApiOperation)71 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)70 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)63 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)53 ArrayList (java.util.ArrayList)44 DBCursor (com.mongodb.DBCursor)42 HashMap (java.util.HashMap)40 List (java.util.List)35 ObjectId (org.bson.types.ObjectId)30 BasicDBList (com.mongodb.BasicDBList)29 Map (java.util.Map)28 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)20 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)20 BSONObject (org.bson.BSONObject)19