无法使用对指定TestSet的引用创建/更新Rally TestCaseResult

时间:2012-11-18 19:04:56

标签: rest rally

使用拉力赛,我需要

  

使用新的TestSet Ref更新TestCaseResult。

     

OR

     

通过复制先前TestCaseResult中的所有内容并仅更改TestSet Ref。来创建一个新的TestCaseResult。

我试图通过Java REST toolkit from Rally做同样的事情。它似乎在内部使用JSON REST API。

当我尝试使用CreateRequest或UpdateRequest执行此操作时,我从API中收到错误“无法为测试集设置值:null

是否无法更新TestCaseResult的TestSet(无论是现有的还是新创建的)?

这是我正在使用的一些示例代码(通过更改testset显示来自现有的create testcaseresult)。

        //get testcaseresult object
        GetRequest tcrReq = new GetRequest("/testcaseresult/12345.js"); 
        tcrReq.setFetch(new Fetch("FormattedID", "Name"));
        GetResponse tcrResponse = restApi.get(tcrReq);

        //update testcaseresult object with new testset
        JsonObject tsRef = new JsonObject();
        tsRef.addProperty("_ref", "https://rally1.rallydev.com/slm/webservice/1.39/testset/1029348.js");
        tcrResponse.getObject().add("TestSet",tsRef);
        tcrResponse.getObject().remove("_ref");

        //Create API for new testcaseresult object
        CreateRequest createRequest = new CreateRequest("testcaseresult", tcrResponse.getObject());
        CreateResponse createResponse = restApi.create(createRequest);
        if(createResponse.wasSuccessful()){
            System.out.println(createResponse.getObject());
        }else{
            String[] ss = createResponse.getErrors();
            for(int i=0; i<ss.length; i++){
                System.out.println(ss[i]);
            }
        }

你能帮助我理解我是做错了还是这是一个拉力限制?

2 个答案:

答案 0 :(得分:3)

我相信你得到&#34;无法为测试集设置值:null&#34; 错误消息的原因是有一个&#34;不可见&#34;对TestCaseResults的约束,其中必须将与它们相关联的TestCase调度到感兴趣的TestSet中,然后才能将TestCaseResult指定为TestSet作为属性。

不幸的是,TestCase上没有TestSet属性,所以你必须从TestSet中查询TestCases集合,然后循环遍历该集合以检查并查看父TestCase是否是该集合的成员。一旦验证TestCase位于TestSet的TestCase集合中,您就可以继续使用感兴趣的TestSet属性成功更新成员TestCaseResult。我测试了下面的内容,它按预期工作。

以下是说明如何完成此操作的代码段:

    // Create and configure a new instance of RallyRestApi
    // Connection parameters
    String rallyURL = "https://rally1.rallydev.com";
    String wsapiVersion = "1.38";
    String applicationName = "RestExample_UpdateTestSetOnTestCaseResult";

    // Credentials
    String userName = "user@company.com";
    String userPassword = "password";

    RallyRestApi restApi = new RallyRestApi(
            new URI(rallyURL),
            userName,
            userPassword);
    restApi.setWsapiVersion(wsapiVersion);
    restApi.setApplicationName(applicationName);

    // Ref to Test Case Result of Interest
    String testCaseResultRef = "/testcaseresult/1234567891.js";

    GetRequest testCaseResultRequest = new GetRequest(testCaseResultRef);
    GetResponse testCaseResultResponse = restApi.get(testCaseResultRequest);
    JsonObject testCaseResultObj = testCaseResultResponse.getObject();

    // Get the Test Case Result's Parent Test Case        
    JsonObject testCase = testCaseResultObj.get("TestCase").getAsJsonObject();

    String testCaseRef = testCase.get("_ref").getAsString();
    GetRequest testCaseRequest = new GetRequest(testCaseRef);
    GetResponse testCaseResponse = restApi.get(testCaseRequest);
    JsonObject testCaseObj = testCaseResponse.getObject();

    System.out.println(testCaseRef);

    // Ref to Test Set of Interest
    String testSetRef = "/TestSet/12345678910.js";

    // Get the Test Set of interest
    GetRequest testSetRequest = new GetRequest(testSetRef);
    GetResponse testSetResponse = restApi.get(testSetRequest);
    JsonObject testSetObject = testSetResponse.getObject();

    // Grab the Test Cases in this Test Set
    JsonArray testCasesInTestSet = testSetObject.get("TestCases").getAsJsonArray();

    // Loop through and see if our Test Case of interest is a member
    boolean testCaseIsInSet = false;
    for (int i=0; i<testCasesInTestSet.size(); i++) {
        JsonObject thisTestCase = testCasesInTestSet.get(i).getAsJsonObject();
        String thisTestCaseRef = thisTestCase.get("_ref").getAsString();

        if (thisTestCaseRef.equals(testCaseRef)) {
            testCaseIsInSet = true;
        }
    }

    if (testCaseIsInSet) {
        // Update Test Set on Existing Test Case Result
        try {            

            //Add Test Set
            System.out.println("\nUpdating Existing Test Case Result's Test Set attribute...");
            testCaseResultObj.addProperty("TestSet", testSetRef);

            UpdateRequest updateExistTestCaseResultRequest = new UpdateRequest(testCaseResultRef, testCaseResultObj);
            UpdateResponse updateExistTestCaseResultResponse = restApi.update(updateExistTestCaseResultRequest);

            if (updateExistTestCaseResultResponse.wasSuccessful()) {
                System.out.println("Updated Test Case Result with new Test Set");
                String[] updateExistTestCaseResultWarnings;

                updateExistTestCaseResultWarnings = updateExistTestCaseResultResponse.getWarnings();
                    System.out.println("Warning(s) occurred updating Test Case Result: ");
                for (int i=0; i<updateExistTestCaseResultWarnings.length;i++) {
                    System.out.println(updateExistTestCaseResultWarnings[i]);
                }

            } else {
                String[] updateExistTestCaseResultErrors;
                updateExistTestCaseResultErrors = updateExistTestCaseResultResponse.getErrors();
                    System.out.println("Error occurred updating Test Case Result: ");
                for (int i=0; i<updateExistTestCaseResultErrors.length;i++) {
                    System.out.println(updateExistTestCaseResultErrors[i]);
                }                   
            }
        } catch (Exception e) {
            System.out.println("Exception occurred while updating Tags on existing Test Case: ");
            e.printStackTrace();            
        }

        finally {
            //Release all resources
            restApi.close();
        }
    } else {
        System.out.println("Unable to Update Test Case Result with specified Test Set");
        System.out.println("Parent Test Case is not a member of this Test Set");
    }
}

答案 1 :(得分:0)

更新TestSet时,您可以将值设置为ref-您不需要包装器对象。

tcrResponse.getObject().add("TestSet", "/testset/1029348.js");