Reputation: 409
I am sending a query to check whether there is a testcaseresult associated with a testcase. I am using this:
QueryRequest c = new QueryRequest("testcaseresult");
c.setFetch(new Fetch("testcase"));
c.setQueryFilter(new QueryFilter("testcase", "=", testCaseRef));
QueryResponse cc = r.query(c);
//String testresultRef = cc.getResults().get(0).getAsJsonObject().get("_ref").toString();
I want to create a new testcaseresult only if there is no testcaseresult in the testcase so far. how can i use the query to do it? Thank you.
Upvotes: 0
Views: 163
Reputation: 409
Thanks Mark. That makes sense. I however found another way, as shown below:
try{
QueryRequest c = new QueryRequest("testcaseresult");
c.setFetch(new Fetch("testcase"));
c.setQueryFilter(new QueryFilter("testcase", "=", testCaseRef));
QueryResponse cc = r.query(c);
String testresultRef = cc.getResults().get(0).getAsJsonObject().get("_ref").toString();
} catch(IndexOutOfBoundsException e){
//Create a testcaseresult here.
}
I used a try-catch block. I query for a test result in a try block. If this throws an IndexOutofboundexception, it means that there is no such test result. In that case I can create a new test result in the catch block.
Upvotes: 0
Reputation:
I think you're on the right track in doing a followup Query. You can't query on Ref's however. However since you have the ref, and presumably also have the TestCase JsonObject, you could do something like the following where you would query on the FormattedID of the parent TestCase:
// Query for Test Case to which we want to add results
QueryRequest testCaseRequest = new QueryRequest("TestCase");
testCaseRequest.setFetch(new Fetch("FormattedID","Name"));
testCaseRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "TC4"));
QueryResponse testCaseQueryResponse = restApi.query(testCaseRequest);
JsonObject testCaseJsonObject = testCaseQueryResponse.getResults().get(0).getAsJsonObject();
String testCaseRef = testCaseQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").toString();
String testCaseFormattedID = testCaseQueryResponse.getResults().get(0).getAsJsonObject.get("FormattedID").toString();
// Query for Test Case Results that are associated to this particular Test Case
QueryRequest testCaseResultsRequest = new QueryRequest("TestCaseResult");
testCaseResultsRequest.setFetch(new Fetch("Build","TestCase","Verdict","FormattedID"));
testCaseResultsRequest.setQueryFilter(new QueryFilter("TestCase.FormattedID", "=", testCaseFormattedID));
QueryResponse testCaseResultResponse = restApi.query(testCaseResultsRequest);
int numberTestCaseResults = testCaseResultResponse.getTotalResultCount();
Upvotes: 1