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")));
}
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);
}
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);
}
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));
}
}
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;
}
}
Aggregations