re.assertContentType("application/json"); //currently only application/json is supported
if(!test.expectsSuccess()){
return; //no further checks for tests that expect failure
}
JSONObject jso = new JSONObject(re.getContent());
JSONArray results = jso.getJSONArray("results");
if(test.expectesResults()){
assertTrue("Missing Results for Query: \n "+test+
"\n Result:\n "+results.toString(4),
results.length() > 0);
} else {
assertTrue("Unexpected Results for Query:\n "+test+
"\n Result:\n "+results.toString(4),
results.length() == 0);
}
Set<String> expectedIds;
if(test.getExpectedResultIds() != null && !test.getExpectedResultIds().isEmpty()){
expectedIds = new HashSet<String>(test.getExpectedResultIds());
} else {
expectedIds = null;
}
//iterate over the results
//General NOTE:
// use opt**(..) methods to avoid JSON Exception. We want to parse
// everything and than do asserts!
for(int i=0;i<results.length();i++){
JSONObject result = results.getJSONObject(i);
String id = result.optString("id", null);
log.info("({}) {}",i,id);
assertNotNull("ID missing for an Result", id);
if(expectedIds != null){
expectedIds.remove(id); //not all results must be in the list