@Test(groups = {"dataset_service"}, dependsOnMethods = {"searchRead_Domain"})
public void searchRead_Paginated() {
final String[] fieldNames = {"id", "name", "login"};
OpenERPJSONRPCClient server = new OpenERPJSONRPCClient(SERVER_URL+":"+SERVER_PORT);
JSONObject sessionInfo = server.sessionAuthenticate("openerp_jsonrpc_client", "admin", "admin", null, null);
/*
* Warning: with limit and offset, result.length still contains total number of records returned by the search.
* But result.records contains only limit record.
* Example with limit=2 offset=10
{
"id": "r2",
"jsonrpc": "2.0",
"result": {
"length": 562,
"records": [
{
"id": 30,
"login": "user3_1382175375491",
"name": "Althea-Daisey NATIVIDADLAVONANGELINE"
},
{
"id": 31,
"login": "user4_1382175375558",
"name": "Amy-Roberto MAUDPARTHENIA"
}
]
}
}
*/
/*
* To test offset and limit we search to overlapping pages and we check that the first id of the second page equals
* the last id of the first page.
*/
// First page: offset = 0 and limit = 2 (so response is printable)
JSONObject paginatedObjects = server.modelSearchRead("res.users", fieldNames, 0, 2, null, "id", null);
// We test limit
Integer paginatedObjectCount = ((JSONArray)paginatedObjects.get("records")).length();
System.out.println("We got "+paginatedObjectCount + " paginatedObjects:");
System.out.println(paginatedObjects);
Assert.assertEquals( paginatedObjectCount.intValue(), 2, "Humm search_read() with limit and offset failed on the limit).");
int idOfLastObjectOfPage1 = ((Integer)((JSONObject)((JSONArray)paginatedObjects.get("records")).get(1)).get("id")).intValue();
// Second page: offset = 1 and limit = 2
JSONObject page2Objects = server.modelSearchRead("res.users", fieldNames, 1, 2, null, "id", null);
int idOfFirstObjectOfPage2 = ((Integer)((JSONObject)((JSONArray)page2Objects.get("records")).get(0)).get("id")).intValue();
// We test offset
Assert.assertEquals( idOfLastObjectOfPage1, idOfFirstObjectOfPage2, "Humm search_read() with limit and offset failed (offset).");