【问题标题】:Rally Rest API code unable to create new Test Case Results added添加了无法创建新测试用例结果的 Rally Rest API 代码
【发布时间】:2014-03-07 16:51:56
【问题描述】:

我最近创建了一个带有相关 RallyRestToolkit jar 文件的 java 项目。我的代码的目的是为指定的测试用例创建一个新的 TestCaseResult。该代码当前在 Eclipse 中工作,没有任何错误。但是,当我在 Rally 的 UI 中查看结果时,我没有看到它添加了。我的代码中是否缺少某些内容?

import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.request.DeleteRequest;
import com.rallydev.rest.request.GetRequest;
import com.rallydev.rest.request.UpdateRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.response.DeleteResponse;
import com.rallydev.rest.response.GetResponse;
import com.rallydev.rest.response.UpdateResponse;
import com.rallydev.rest.util.Ref;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URI;

//import rally.TestCaseResult;

//import java.net.URISyntaxException;

public class RallyUpdater {


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

        //Create and configure a new instance of RallyRestApi
        RallyRestApi restApi = new RallyRestApi(new URI("https://rally1.rallydev.com"),"user@company.com", "password");
        restApi.setWsapiVersion("1.38");
        restApi.setApplicationName("Add Test Case Result");

            // Query for Test Case to which we want to add results
            QueryRequest testCaseRequest = new QueryRequest("TestCase");
            testCaseRequest.setFetch(new Fetch("FormattedID","Rally REST API Enhancment Object ID to Formatted ID"));
            testCaseRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "TC7020"));
            QueryResponse testCaseQueryResponse = restApi.query(testCaseRequest);
            JsonObject testCaseJsonObject = testCaseQueryResponse.getResults().get(0).getAsJsonObject();
            String testCaseRef = testCaseQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").getAsString();
            //Add a Test Case Result
            System.out.println("Creating TestCase Result...");
            JsonObject newTestCaseResult = new JsonObject();
            //newTestCaseResult.addProperty("Notes", "Test Case Result");
            newTestCaseResult.addProperty("Verdict", "Pass");
            newTestCaseResult.addProperty("Notes", "Test");

            newTestCaseResult.addProperty("Build", "2012.05.31.0020101");
            newTestCaseResult.addProperty("Tester", "Sam Adams");
            newTestCaseResult.addProperty("TestCase", "/testcase/14321026920");
            System.out.println("added tester");
            //newTestCaseResult.addProperty("FormattedID", "TC7020");

            CreateRequest createRequest = new CreateRequest("testcaseresult", newTestCaseResult);
            CreateResponse createResponse = restApi.create(createRequest);
            System.out.println("Done");
            System.out.println(String.format( "Created %s", createResponse.getObject().get("_ref").getAsString()));

            restApi.close();
    }
}

新代码:

import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.request.DeleteRequest;
import com.rallydev.rest.request.GetRequest;
import com.rallydev.rest.request.UpdateRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.response.DeleteResponse;
import com.rallydev.rest.response.GetResponse;
import com.rallydev.rest.response.UpdateResponse;
import com.rallydev.rest.util.Ref;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URI;



public class RallyUpdater {


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

        //Create and configure a new instance of RallyRestApi
        RallyRestApi restApi = new RallyRestApi(new URI("https://rally1.rallydev.com"),"username@company.com", "password");
        restApi.setWsapiVersion("1.36");
        restApi.setApplicationName("Add Test Case Result");

            // Query for Test Case to which we want to add results
            QueryRequest testCaseRequest = new QueryRequest("TestCase");
            testCaseRequest.setFetch(new Fetch("FormattedID","Rally REST API Enhancment Object ID to Formatted ID"));
            testCaseRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "TC7xxx"));
            QueryResponse testCaseQueryResponse = restApi.query(testCaseRequest);
            JsonObject testCaseJsonObject = testCaseQueryResponse.getResults().get(0).getAsJsonObject();
            String testCaseRef = testCaseQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").getAsString();`
            //Add a Test Case Result
            System.out.println("Creating TestCase Result...");
            JsonObject newTestCaseResult = new JsonObject();
            newTestCaseResult.addProperty("Verdict", "Pass");
            newTestCaseResult.addProperty("Notes", "Test");

            newTestCaseResult.addProperty("Build", "2012.05.31.0020101");
            newTestCaseResult.addProperty("Tester", "Joe Smith");
            newTestCaseResult.addProperty("TestCase", "/testcase/14321026920");
            System.out.println("added tester");
            //newTestCaseResult.addProperty("FormattedID", "TC7xxx");

            CreateRequest createRequest = new CreateRequest("testcaseresult", newTestCaseResult);
            CreateResponse createResponse = restApi.create(createRequest);
            System.out.println("Done");
            System.out.println(String.format( "Created %s", createResponse.getObject().get("_ref").getAsString()));

            restApi.close();
    }
}

新代码 03-11-14

import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.request.DeleteRequest;
import com.rallydev.rest.request.GetRequest;
import com.rallydev.rest.request.UpdateRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.response.DeleteResponse;
import com.rallydev.rest.response.GetResponse;
import com.rallydev.rest.response.UpdateResponse;
import com.rallydev.rest.util.Ref;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URI;

public class RallyNewTestCase {

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


     RallyRestApi restApi = new RallyRestApi(new URI("https://rally1.rallydev.com"),"jsmith@company.com", "password");
     restApi.setWsapiVersion("1.38");
     restApi.setApplicationName("Add Test Case Result");

     QueryRequest userRequest = new QueryRequest("User");
     userRequest.setFetch(new Fetch("UserName", "Subscription", "Joe Smith"));
     userRequest.setQueryFilter(new QueryFilter("UserName", "=", "jsmith@company.com"));
     QueryResponse userQueryResponse = restApi.query(userRequest);
     JsonArray userQueryResults = userQueryResponse.getResults();
     JsonElement userQueryElement = userQueryResults.get(0);
     JsonObject userQueryObject = userQueryElement.getAsJsonObject();
     String userRef = userQueryObject.get("_ref").toString();

     if (userQueryResponse.wasSuccessful()) {
         System.out.println("User Found"); 
     } else {
         System.out.println("User not found");
     }
     System.out.println("Creating a test case result...");

     QueryRequest testCaseRequest = new QueryRequest("TestCase");
     testCaseRequest.setFetch(new Fetch("FormattedID","Rally REST API Enhancment Object ID to Formatted ID"));
     testCaseRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "TCxxx"));
     QueryResponse testCaseQueryResponse = restApi.query(testCaseRequest);
     JsonObject testCaseJsonObject = testCaseQueryResponse.getResults().get(0).getAsJsonObject();
     String testCaseRef = testCaseQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").getAsString();
     if (testCaseQueryResponse.wasSuccessful()) {
         System.out.println("Test Case Found"); 
     } else {
         System.out.println("Test Case not found");
     }


     try {
            //Add a Test Case Result
             System.out.println("Creating TestCase Result...");
             JsonObject newTestCaseResult = new JsonObject();
             newTestCaseResult.addProperty("Verdict", "Pass");
             newTestCaseResult.addProperty("Notes", "Test");
             newTestCaseResult.addProperty("Build", "2012.05.31.0020101");
             newTestCaseResult.addProperty("Tester", "Joe Smith");
             newTestCaseResult.addProperty("TestCase", "/testcae/17080774372");
             System.out.println("added testcase info");

             CreateRequest createRequest = new CreateRequest("newtestcaseresult", newTestCaseResult);
             CreateResponse createResponse = restApi.create(createRequest);  

             if (createResponse.wasSuccessful()) {
                 System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));          

                 //Read Test Case
                 String ref = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
                 System.out.println(String.format("\nReading Test Case Result %s...", ref));
                 GetRequest getRequest = new GetRequest(ref);
                 getRequest.setFetch(new Fetch("Date", "Verdict"));
                 GetResponse getResponse = restApi.get(getRequest);
                 JsonObject obj = getResponse.getObject();
                 System.out.println(String.format("Read Test Case Result. Date = %s, Verdict = %s",
                         obj.get("Date").getAsString(), obj.get("Verdict").getAsString())); 

             } else {
                 String[] createErrors;
                 createErrors = createResponse.getErrors();
                 System.out.println("Error occurred creating Test Case: ");
                 for (int i=0; i<createErrors.length;i++) {
                     System.out.println(createErrors[i]);
                System.out.println(createErrors[j]);                 
             }
         }
             }finally {
         //Release all resources
         restApi.close();
     }   

 } 

}

【问题讨论】:

  • 谢谢马克,我会尽快尝试。 “..../testcase/12345678910”部分中有什么数字?是对象 ID 还是格式化 ID?只是想仔细检查一下。

标签: api rally


【解决方案1】:

我认为缺少的是 TestCaseResult 与 TestCase 所需的关联:

newTestCaseResult.addProperty("TestCase", "/testcase/12345678910");

这个答案:

Rally toolKit for Java how do i create and then attach a TestCaseResult to a TestCase

提供如何创建与现有 TestCase 关联的 TCR 的示例。

【讨论】:

  • 我想出了上面的命名约定(带有 URL 代码)。但是,我在其中添加了那部分代码,并且发生了相同的结果。有什么其他的想法可以尝试吗?感谢帮助 – user2697781 刚刚编辑
  • 创建响应是否有警告或错误?您是如何获取 TestCase 对象 id 的?
  • 控制台上看不到任何警告。它只是遍历代码并显示代码中的 System.out.println 消息。在 Ecipe 之外运行我的代码的任何其他工具建议将不胜感激(因此我可以为您提供)。
  • 还有第二个问题。我目前没有对象 ID。只是 UI 中的 Formatted ID 和 URL 中的 Ref #。对象ID是否需要进入代码?
  • 谢谢马克,更新后的代码附加在旧代码下面。我已经按照您的指示添加了 restApi.create() 调用。现在在 "System.out.println(String.format( "Created %s", createResponse.getObject().get("_ref").getAsString())) 行上发生了一个实际的 java.lang.NullPointerException 错误;"。有任何想法吗?越来越近。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 2017-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多