使用Rally Java API使用Tag更新TestCase时出错

时间:2014-09-30 14:57:15

标签: java rally

我正在尝试使用现有标记更新Rally中的TestCase,但是我从服务器收到错误。 我的步骤是:

  1. 根据测试ID找到测试用例,保留ref和Tags对象
  2. 找到标签,保留参考
  3. 如果_tagsNameArray json数组不包含我的标记,请更新它
  4. 将更新发送到Rally服务器
  5. 以下是我更新测试用例的代码:

    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."]}}
    
    这是杀了我,我花了很多时间调试,我找不到问题。有什么想法吗?

1 个答案:

答案 0 :(得分: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();
        }   

    } 
}