How to retrieve Test Cases associated with the Test Set

孤者浪人 提交于 2019-12-01 07:35:16

问题


I tried to run provided code for jar 2.0.2 and now for 2.0.4 Both times received an error at line: int numberOfTestCases = testSetJsonObject.get("TestCases").getAsJsonArray().size();

Exception in thread "main" java.lang.IllegalStateException: This is not a JSON Array. at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106) at GetTCofTS.main(GetTCofTS.java:50)

I can overcome this error (since it is really not an array that is coming back) , but it won't resolve my problem, that I am trying to get not only the number of TestCases associated with the current TestSet, but list of TestCases - at least their formatted ID. Is it a bag in Rally?

public class GetTCofTS {

public static void main(String[] args) throws Exception {

    String host = "https://rally1.rallydev.com";

    String username = "user@co.com";
    String password = "secret";

    String applicationName = "RESTExampleFindTestCasesOfTestSet";
    String workspaceRef = "/workspace/1111";
    String projectRef = "/project/2222";
    String wsapiVersion = "1.43";


    RallyRestApi restApi = null;
    try {
        restApi = new RallyRestApi(
                new URI(host),
                username,
                password);
        restApi.setApplicationName(applicationName); 

        QueryRequest testSetRequest = new QueryRequest("TestSet");
        testSetRequest.setWorkspace(workspaceRef);
        restApi.setWsapiVersion(wsapiVersion);
        testSetRequest.setFetch(new Fetch(new String[] {"Name", "TestCases", "FormattedID"}));

        testSetRequest.setQueryFilter(new QueryFilter("Name", "=", "someTS"));

        QueryResponse testSetQueryResponse = restApi.query(testSetRequest);
        System.out.println("Successful: " + testSetQueryResponse.wasSuccessful());
        System.out.println("Size: " + testSetQueryResponse.getTotalResultCount());
        for (int i=0; i<testSetQueryResponse.getResults().size();i++){
            JsonObject testSetJsonObject = testSetQueryResponse.getResults().get(i).getAsJsonObject();
            System.out.println("Name: " + testSetJsonObject.get("Name") + " ref: " + testSetJsonObject.get("_ref").getAsString() + " Test Cases: " + testSetJsonObject.get("TestCases"));
            int numberOfTestCases = testSetJsonObject.get("TestCases").getAsJsonArray().size();
            System.out.println(numberOfTestCases);
            if(numberOfTestCases>0){
                  for (int j=0;j<numberOfTestCases;j++){
                  System.out.println(testSetJsonObject.get("TestCases").getAsJsonArray().get(j).getAsJsonObject().get("FormattedID"));
                 }
            }

        }

    } finally {
        if (restApi != null) {
            restApi.close();
        }
    }
}
}

回答1:


v2.0 is the preferred version going forward. The following small tweaks should get you up and going.

Instead of this (which works in 1.43 because the collection is returned):

int numberOfTestCases = testSetJsonObject.get("TestCases").getAsJsonArray().size();
if(numberOfTestCases>0) {
    for (int j=0;j<numberOfTestCases;j++) {
       System.out.println(testSetJsonObject.get("TestCases").getAsJsonArray().get(j).getAsJsonObject().get("FormattedID"));
    }
}

Do this:

//TestCases is an object with a Count and a ref to load the collection
int numberOfTestCases = testSetJsonObject.getAsJsonObject("TestCases").get("Count").getAsInt();

if(numberOfTestCases > 0) {
    QueryRequest testCaseRequest = new QueryRequest(testSetJsonObject.getAsJsonObject("TestCases"));
    testCaseRequest.setFetch(new Fetch("FormattedID"));
    //load the collection
    JsonArray testCases = restApi.query(testCaseRequest).getResults();
    for (int j=0;j<numberOfTestCases;j++){
        System.out.println(testCases.get(j).getAsJsonObject().get("FormattedID").getAsString());
    }
}


来源:https://stackoverflow.com/questions/19934895/how-to-retrieve-test-cases-associated-with-the-test-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!