@Test
public void testQuery() throws Exception {
// normal query
Response response = client.query(new Query("test", "testQuery", 0, 100, new BasicBSONObject()));
Assert.assertEquals(100, response.getNumberReturned());
Assert.assertEquals(100, response.getDocuments().size());
for (BSONObject doc : response.getDocuments()) {
int id = ((Number) doc.get("_id")).intValue();
Assert.assertNotNull(doc);
Assert.assertEquals(id, doc.get("_id"));
Assert.assertEquals("name-" + id, doc.get("name"));
Assert.assertEquals("value-" + (id*100), doc.get("value"));
}
List<BSONObject> first10 = response.getDocuments().subList(0, 10);
List<BSONObject> next10 = response.getDocuments().subList(10, 20);
// limited query
response = client.query(new Query("test", "testQuery", 0, 10, new BasicBSONObject()));
Assert.assertEquals(10, response.getNumberReturned());
Assert.assertEquals(10, response.getDocuments().size());
Assert.assertEquals(first10, response.getDocuments());
// skipped query
response = client.query(new Query("test", "testQuery", 10, 10, new BasicBSONObject()));
Assert.assertEquals(10, response.getNumberReturned());
Assert.assertEquals(10, response.getDocuments().size());
Assert.assertEquals(next10, response.getDocuments());
// over skipped query
response = client.query(new Query("test", "testQuery", 10, 100, new BasicBSONObject()));
Assert.assertEquals(90, response.getNumberReturned());
Assert.assertEquals(90, response.getDocuments().size());
// equals query
response = client.query(new Query("test", "testQuery", 0, 1, new BasicBSONObject("_id", 50)));
Assert.assertEquals(1, response.getNumberReturned());
Assert.assertEquals(1, response.getDocuments().size());
BSONObject doc = response.getDocuments().get(0);
Assert.assertEquals(50, doc.get("_id"));
// greater than query
response = client.query(new Query("test", "testQuery", 0, 100,
new BasicBSONObject("_id", new BasicBSONObject("$gt", 50))));
Assert.assertEquals(49, response.getNumberReturned());
Assert.assertEquals(49, response.getDocuments().size());
response = client.query(new Query("test", "testQuery", 0, 100,
new BasicBSONObject("_id", new BasicBSONObject("$gte", 50))));
Assert.assertEquals(50, response.getNumberReturned());
Assert.assertEquals(50, response.getDocuments().size());
// lesser than query
response = client.query(new Query("test", "testQuery", 0, 100,
new BasicBSONObject("_id", new BasicBSONObject("$lt", 50))));
Assert.assertEquals(50, response.getNumberReturned());
Assert.assertEquals(50, response.getDocuments().size());
response = client.query(new Query("test", "testQuery", 0, 100,
new BasicBSONObject("_id", new BasicBSONObject("$lte", 50))));
Assert.assertEquals(51, response.getNumberReturned());
Assert.assertEquals(51, response.getDocuments().size());
// combined query
response = client.query(new Query("test", "testQuery", 0, 100,
new BasicBSONObject("_id", new BasicBSONObject("$gt", 20).append("$lt", 80))));
Assert.assertEquals(59, response.getNumberReturned());
Assert.assertEquals(59, response.getDocuments().size());
}