使用Java中的Rally Rest API修改集合属性

时间:2015-02-27 06:00:48

标签: java api rest collections rally

我试图修改TestCase的TestSets属性。

JsonArray newTestSets = new JsonArray();

...根据需要添加值(在最简单的情况下,我清除属性)

JsonObject updates = new JsonObject();
updates.add("TestSets", newTestSets);

我像所有其他更新一样创建updateRequest

UpdateRequest updateRequest = new UpdateRequest(ref, updates)

我没有收到任何错误但没有任何改变。 TestCase仍在几个TestSet中

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我验证了它可以在TestCase上更新TestSets集合。请参阅this github repo

public class UpdateTestSetsOnTestCase {

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


        String host = "https://rally1.rallydev.com";
        String apiKey = "_abc123";
        String workspaceRef = "/workspace/12352608129";
        String applicationName = "RestExample_updateTestSetsOnTC";


        RallyRestApi restApi = new RallyRestApi(new URI(host),apiKey);
        restApi.setApplicationName(applicationName);

        try {
            String setID = "TS24";
            String testid = "TC3";
            QueryRequest  tsRequest = new QueryRequest("TestSet");
            tsRequest.setWorkspace(workspaceRef);
            tsRequest.setQueryFilter(new QueryFilter("FormattedID", "=", setID));
            QueryResponse tsQueryResponse = restApi.query(tsRequest);
            if(tsQueryResponse.getTotalResultCount() == 0){
                System.out.println("Cannot find tag: " + setID);
                return;
            }
            JsonObject tsJsonObject = tsQueryResponse.getResults().get(0).getAsJsonObject();
            String tsRef = tsJsonObject.get("_ref").getAsString();
            System.out.println(tsRef);

            QueryRequest testCaseRequest = new QueryRequest("TestCase");
            testCaseRequest.setWorkspace(workspaceRef);
            testCaseRequest.setFetch(new Fetch("FormattedID", "Name", "TestSets"));
            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 numberOfTestSets = testCaseJsonObject.getAsJsonObject("TestSets").get("Count").getAsInt();
            System.out.println(numberOfTestSets + " testset(s) on " + testid);
            QueryRequest testsetCollectionRequest = new QueryRequest(testCaseJsonObject.getAsJsonObject("TestSets"));
            testsetCollectionRequest.setFetch(new Fetch("FormattedID"));
            JsonArray testsets = restApi.query(testsetCollectionRequest).getResults();

            for (int j=0;j<numberOfTestSets;j++){
                System.out.println("FormattedID: " + testsets.get(j).getAsJsonObject().get("FormattedID"));
            }
            testsets.add(tsJsonObject);
            JsonObject testCaseUpdate = new JsonObject();
            testCaseUpdate.add("TestSets", testsets);
            UpdateRequest updateTestCaseRequest = new UpdateRequest(testCaseRef,testCaseUpdate);
            UpdateResponse updateTestCaseResponse = restApi.update(updateTestCaseRequest);
            if (updateTestCaseResponse.wasSuccessful()) {
                QueryRequest testsetCollectionRequest2 = new QueryRequest(testCaseJsonObject.getAsJsonObject("TestSets"));
                testsetCollectionRequest2.setFetch(new Fetch("FormattedID"));
                JsonArray testsetsAfterUpdate = restApi.query(testsetCollectionRequest2).getResults();
                int numberOfTestSetsAfterUpdate = restApi.query(testsetCollectionRequest2).getResults().size();
                System.out.println("Successfully updated : " + testid + " TestSets after update: " + numberOfTestSetsAfterUpdate);
                for (int j=0;j<numberOfTestSetsAfterUpdate;j++){
                    System.out.println("FormattedID: " + testsetsAfterUpdate.get(j).getAsJsonObject().get("FormattedID"));
                }
            }
        } finally {
            restApi.close();
        }

    }
}