Question

I've recently created a java project with the associated RallyRestToolkit jar files. The intent for my code is to create a new TestCaseResult to the specified Test Case. The code currently works in Eclipse without any errors. However when I go to view the result in the UI of Rally I don't see it added. Am I missing something in my code?

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();
    }
}

NEW CODE:

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();
    }
}

New Code 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();
     }   

 } 

}
Was it helpful?

Solution

I believe that what is missing is the needed association of the TestCaseResult to a TestCase:

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

This answer:

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

Provides an example of how to accomplish the creation of a TCR associated to an existing TestCase.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top