Rally Rest API代码无法创建新的测试用例结果

时间:2014-03-07 16:51:57

标签: api rally

我最近创建了一个带有相关RallyRestToolkit jar文件的java项目。我的代码的目的是为指定的测试用例创建一个新的TestCaseResult。该代码目前在Eclipse中运行,没有任何错误。但是,当我在拉力赛的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();
     }   

 } 

}

1 个答案:

答案 0 :(得分:0)

我相信缺少的是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创建的示例。