Reputation: 409
I wrote a script, about a month ago and it could successfully create tags and add them to testcases. But for past few days the script is unable to add tags to testcases, it is still able to create tags though. Here is the source code I wrote:
//Looks for a specific tag
public static void ChkTag(RallyRestApi r, String tagname) throws IOException{
boolean tagfound = false;
QueryRequest alltags = new QueryRequest("Tag");
QueryResponse resp = r.query(alltags);
if(resp.wasSuccessful()){
//System.out.println("query for all tags was successful.");
for(JsonElement result : resp.getResults()){
JsonObject temp = result.getAsJsonObject();
if(temp.get("Name").getAsString().equals(tagname)){
tagfound =true;
}
}
if(tagfound==false){
System.out.println("Tag for this testcase is missing..creating now...");
ExportTCtoRally_adk.createTag(r, tagname);
}
}
}
public static void createTag(RallyRestApi r, String name) throws IOException{
//Create Tag
JsonObject mytag = new JsonObject();
mytag.addProperty("Name", name);
CreateRequest mytagc = new CreateRequest("Tag", mytag);
CreateResponse mytagcc = r.create(mytagc);
String ref_tag = Ref.getRelativeRef(mytagcc.getObject().get("_ref").getAsString());
if(mytagcc.wasSuccessful())
System.out.println("Tag successfully created.");
return;
}
and then While creating the testcase i run the following code:
//-----------------FOR TAGS----------------------
boolean tagfound = false;
if (creating1.wasSuccessful()){
System.out.println("testcase created.");
QueryRequest alltags = new QueryRequest("Tag");
QueryResponse resp = r.query(alltags);
JsonElement out = null;
if(resp.wasSuccessful()){
System.out.println("query for all tags was successful.");
for(JsonElement result : resp.getResults()){
JsonObject temp = result.getAsJsonObject();
if(temp.get("Name").getAsString().equals(tagx)){
System.out.println("desired tag found.");
tagfound=true;
out=result;
}
}
if(tagfound==false){
System.out.println("Tag wasn't found.");
ExportTCtoRally_adk.createTag(r, tagx);
System.out.println("New Tag created.");
}
JsonArray tiger = new JsonArray();
tiger.add(out);
System.out.println("Updating testcase tags...");
JsonObject updatedtesttag = new JsonObject();
updatedtesttag.add("Tags", tiger);
UpdateRequest updatetag = new UpdateRequest(ref_testcase, updatedtesttag);
UpdateResponse updatetagresp = r.update(updatetag);
if(updatetagresp.wasSuccessful())
System.out.println("Tag succeccfully added to the test case");
}
//-----------------------------------------------
I know somethings are running twice here, but since it doesn't cause any problem, I left it like that.
So my Question is is there anything wrong with my code?
Thanks.
Upvotes: 1
Views: 194
Reputation: 8400
Do you have more than one page (200) of tags in your workspace now? (You can check by looking at getTotalResultCount() on the response). By default querying will only return one page of data. It would probably be more efficient to just add a filter to search for the tag by name instead:
QueryRequest alltags = new QueryRequest("Tag");
alltags.setQueryFilter(new QueryFilter("Name", "=", tagx);
QueryResponse resp = r.query(alltags);
if(resp.wasSuccessful() && resp.getResults().size() == 1) {
//tag is at index 0 in results
}
Upvotes: 1