无法使用Rally Rest API从TestCase上的TestSets集合中删除TestSet

时间:2015-03-13 22:43:47

标签: java api rest collections rally

添加到TestSet的属性工作,但不是减法。清除TestSets属性如下是NO-OP - no。

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

但是由于添加工作,我知道该属性不是只读的。

1 个答案:

答案 0 :(得分:0)

我测试了使用具有此端点和有效负载的Chrome Advanced Rest Client从TestCase上的TestSets集合中删除TestSet,并且它有效:

https://rally1.rallydev.com/slm/webservice/v2.0/testcase/12458269829/testsets/remove

{"CollectionItems":[{"_ref":"https://rally1.rallydev.com/slm/webservice/v2.0/testset/19255956341"}]}

enter image description here

我尝试使用Rally Java工具包,它需要gson-2.2.4.jar。那个版本的gson没有remove方法。 gson-2.3.1有remove方法。

我使用了gson-2.3.1,其代码试图从测试用例的TestSets集合中删除测试集。

现在,自动填充功能会显示'删除'并且没有“没有方法”。错误,但从集合中删除元素无声地失败。我提交了一个错误。

以下是我测试过的代码:

public class RemoveTestSetFromCollection {

    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 = "TS25";
            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.remove(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();
        }

    }
}