2014-09-30 29 views
1

我想用現有標籤更新Rally中的TestCase,但是我從服務器收到錯誤。 我的步驟是:使用Rally Java API更新帶標籤的TestCase時出錯

  1. 查找基於測試ID的測試用例,保持裁判和標籤對象
  2. 找到標記,保持基準
  3. 如果_tagsNameArray JSON數組不包含我的標籤,更新
  4. 發送更新反彈服務器

下面的代碼我有更新測試用例:

String tagref = getTagReference(tagname); //this is where I get the reference to the existing Tag 
QueryRequest testCaseRequest = new QueryRequest("TestCase"); 
testCaseRequest.setFetch(new Fetch("FormattedID", "Name", "Tags")); 
testCaseRequest.setQueryFilter(new QueryFilter("FormattedID", "=", testID)); 
QueryResponse testCaseQueryResponse; 
testCaseQueryResponse = restApi.query(testCaseRequest); 
JsonArray resultArray = testCaseQueryResponse.getResults(); 
if (resultArray.size() == 0) { 
System.out.println("Test " + testID + "does not exist anymore"); 
      return; 
} 
JsonObject testCaseJson = resultArray.get(0).getAsJsonObject(); 
String testCaseRef = testCaseJson.get("_ref").getAsString(); 
JsonObject tasks = testCaseJson.get("Tags").getAsJsonObject(); 
JsonArray taskarray = tasks.get("_tagsNameArray").getAsJsonArray(); 
boolean tagfound = false; 
for (JsonElement el : taskarray) { 
    JsonObject task = el.getAsJsonObject(); 
    String tgnm = task.get("Name").getAsString(); 
    if (tgnm.contentEquals(tagname)) { 
    tagfound = true; 
    break; 
    } 
} 
if (!tagfound) { 
    String shortref = Ref.getRelativeRef(tagref); 
    String shorttestref = Ref.getRelativeRef(testCaseRef); 
    JsonObject json = new JsonObject(); 
    json.addProperty("Name", tagname); // not mandatory, I guess 
    json.addProperty("_ref", shortref); 
    taskarray.add(json); 
    tasks.add("_tagsNameArray", taskarray); 
    JsonObject updateTCJson = new JsonObject(); 
    updateTCJson.add("Tags", tasks); 
    UpdateRequest updateTestCaseRequest = new UpdateRequest(testCaseRef, updateTCJson); 
    UpdateResponse updateTestCaseResponse = restApi.update(updateTestCaseRequest); 
    if (updateTestCaseResponse.wasSuccessful()) { 
    System.out.println("Updated Test Case with tag: " + tagname); 
    } else { 
    System.err.println("Cannot update test case tags pentru ca: "+updateTestCaseResponse.getErrors()[0]); 
} 

問題是,我從服務器獲取這樣的:

{"OperationResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": ["Could not read: Could not read referenced object 2"], "Warnings": ["It is no longer necessary to append \".js\" to WSAPI resources."]}} 

這是我的命,我花了很多時間的調試,我不能發現問題。有任何想法嗎?

+0

你檢查了taskarray和現有任務之間的區別嗎?也許嘗試評論'tasks.add(「_ tagsNameArray」,taskarray);'並報告你的結果? – pherris 2014-09-30 16:47:01

回答

1

這是一個應用程序示例,它更新了TestCase上的Tags集合。我注意到你正在使用_tagsNameArray_tagsNameArray集合在Rally架構中沒有定義,不應該依賴實現。來源是this github repo

public class UpdateTestCaseWithTag { 

    public static void main(String[] args) throws URISyntaxException, IOException { 


      String host = "https://rally1.rallydev.com"; 
      String apiKey = "_abc123"; 
      String workspaceRef = "/workspace/1234567"; 
      String applicationName = "RestExample_updateTCwithTag"; 


      RallyRestApi restApi = new RallyRestApi(new URI(host),apiKey); 
      restApi.setApplicationName(applicationName); 

     try { 
      String tagname = "tag1"; 
      String testid = "TC32"; 
      QueryRequest tagRequest = new QueryRequest("Tag"); 
      tagRequest.setWorkspace(workspaceRef); 
      tagRequest.setQueryFilter(new QueryFilter("Name", "=", tagname)); 
      QueryResponse tagQueryResponse = restApi.query(tagRequest); 
      if(tagQueryResponse.getTotalResultCount() == 0){ 
       System.out.println("Cannot find tag: " + tagname); 
       return; 
      } 
      JsonObject tagJsonObject = tagQueryResponse.getResults().get(0).getAsJsonObject(); 
      String tagRef = tagJsonObject.get("_ref").getAsString(); 
      System.out.println(tagRef); 
      QueryRequest testCaseRequest = new QueryRequest("TestCase"); 
      testCaseRequest.setWorkspace(workspaceRef); 
      testCaseRequest.setFetch(new Fetch("FormattedID", "Name", "Tags")); 
      testCaseRequest.setQueryFilter(new QueryFilter("FormattedID", "=", testid)); 
      QueryResponse testCaseQueryResponse = restApi.query(testCaseRequest);; 

      if (testCaseQueryResponse.getTotalResultCount() == 0) { 
      System.out.println("Cannot find test case : " + testid); 
      return; 
      } 
      JsonObject testCaseJsonObject = testCaseQueryResponse.getResults().get(0).getAsJsonObject(); 
      String testCaseRef = testCaseJsonObject.get("_ref").getAsString(); 
      System.out.println(testCaseRef); 
      int numberOfTags = testCaseJsonObject.getAsJsonObject("Tags").get("Count").getAsInt(); 
      System.out.println(numberOfTags + " tag(s) on " + testid); 
      QueryRequest tagCollectionRequest = new QueryRequest(testCaseJsonObject.getAsJsonObject("Tags")); 
      tagCollectionRequest.setFetch(new Fetch("Name")); 
      JsonArray tags = restApi.query(tagCollectionRequest).getResults(); 

      for (int j=0;j<numberOfTags;j++){ 
       System.out.println("Tag Name: " + tags.get(j).getAsJsonObject().get("Name")); 
      } 
      tags.add(tagJsonObject); 
      JsonObject testCaseUpdate = new JsonObject(); 
      testCaseUpdate.add("Tags", tags); 
      UpdateRequest updateTestCaseRequest = new UpdateRequest(testCaseRef,testCaseUpdate); 
      UpdateResponse updateTestCaseResponse = restApi.update(updateTestCaseRequest); 
      if (updateTestCaseResponse.wasSuccessful()) { 
       System.out.println("Successfully updated : " + testid + " Tags after update: "); 
       QueryRequest tagCollectionRequest2 = new QueryRequest(testCaseJsonObject.getAsJsonObject("Tags")); 
       tagCollectionRequest.setFetch(new Fetch("Name")); 
       JsonArray tagsAfterUpdate = restApi.query(tagCollectionRequest).getResults(); 
       int numberOfTagsAfterUpdate = restApi.query(tagCollectionRequest).getResults().size(); 
       for (int j=0;j<numberOfTagsAfterUpdate;j++){ 
        System.out.println("Tag Name: " + tagsAfterUpdate.get(j).getAsJsonObject().get("Name")); 
       } 
      } 
     } finally { 
      restApi.close(); 
     } 

    } 
} 
+0

是的,就是這樣,謝謝!我直接與收到的對象搞混了,沒有對標籤進行新的查詢。現在工作正常! – vicusbass 2014-10-01 07:34:50

相關問題