0

我正在使用 rally dev api 2.2.1,并且我正在尝试一次性创建步骤。而不是循环并逐个创建。

我写了这个,它工作正常,但是一步一步地创建并且需要时间。

private void updateSteps(TestCase testCase, JsonObject createResponse) {
        try {
            String testCaseRef = createResponse.get("_ref").getAsString();
            String testCaseID = createResponse.get("FormattedID").getAsString();
            final int[] stepCount = {1};
            LOGGER.info("*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Creating steps for " + testCaseID + " *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
            testCase.getTestCaseSteps().forEach(step -> {
                try {
                    StopWatch timer = new StopWatch();
                    timer.start();
                    executeDBGetStep(step);
                    JsonObject stepRequest = new JsonObject();
                    stepRequest.addProperty("TestCase", testCaseRef);
                    stepRequest.addProperty("StepIndex", stepCount[0]);
                    stepRequest.addProperty("Input", step.getTestStepDescription() + "<br/><br/>" + format(this.getObjectAsString(step.getTestStepDataRequest(true))));
                    if (step.getTestStepAssert() == null) {
                        stepRequest.addProperty("ExpectedResult", "As Expected");
                    } else {
                        JsonNode assertObject;
                        try {
                            assertObject = step.getTestStepAssert(true);
                        } catch (Exception ex) {
                            assertObject = step.getTestStepAssert(false);
                        }
                        stepRequest.addProperty("ExpectedResult", format(this.getObjectAsString(assertObject)));
                    }
                    CreateRequest createStepRequest = new CreateRequest("testCaseStep", stepRequest);
                    CreateResponse createStepResponse = this.rallyClient.getRestApi().create(createStepRequest);
                    timer.stop();
                    if (ExceptionHandlers.isResponseSuccessful(createStepResponse)) {
                        LOGGER.info("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
                        LOGGER.info("Step " + stepCount[0] + " created for test case " + testCaseID + " in " + timer.getLastTaskTimeMillis() + " milliseconds");
                        LOGGER.info("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
                    }
                    stepCount[0]++;
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            });
            LOGGER.info("*-*-*-*-*-*-*-*-*-*-*-* Finished creating steps for " + testCaseID + " -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

所以,到目前为止,我已经尝试过这个并且我得到 404 NOT FOUND 因为我假设在执行 collectionUpdate 时它正在请求中寻找 _ref 对象,并且由于我是第一次创建它,所以引用不存在并且它正在抛出此错误。

private void updateSteps(TestCase testCase, JsonObject createResponse) {
        try {
            String testCaseRef = createResponse.get("_ref").getAsString();
            String testCaseID = createResponse.get("FormattedID").getAsString();
            final int[] stepCount = {1};
            LOGGER.info("*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Creating steps for " + testCaseID + " *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
            StopWatch timer = new StopWatch();
            timer.start();
            JsonArray stepsTobeUpdated = new JsonArray();
            testCase.getTestCaseSteps().forEach(step -> {
                executeDBGetStep(step);
                JsonObject stepRequest = new JsonObject();
                JsonObject testCaseObject = new JsonObject();
                testCaseObject.addProperty("_ref", testCaseRef);
                stepRequest.add("TestCase", testCaseObject);
                stepRequest.addProperty("StepIndex", stepCount[0]);
                stepRequest.addProperty("Input", step.getTestStepDescription() + "<br/><br/>" + format(this.getObjectAsString(step.getTestStepDataRequest(true))));
                if (step.getTestStepAssert() == null) {
                    stepRequest.addProperty("ExpectedResult", "As Expected");
                } else {
                    JsonNode assertObject;
                    try {
                        assertObject = step.getTestStepAssert(true);
                    } catch (Exception ex) {
                        assertObject = step.getTestStepAssert(false);
                    }
                    stepRequest.addProperty("ExpectedResult", format(this.getObjectAsString(assertObject)));
                }
                stepsTobeUpdated.add(stepRequest);
                stepCount[0]++;
            });


            CollectionUpdateRequest testStepCollectionAddRequest = new CollectionUpdateRequest(testCaseRef + "/steps", stepsTobeUpdated, true);
            try {
                CollectionUpdateResponse testStepCollectionAddResponse = rallyClient.getRestApi().updateCollection(testStepCollectionAddRequest);
                if (ExceptionHandlers.isResponseSuccessful(testStepCollectionAddResponse)) {
                    LOGGER.info(stepsTobeUpdated.size() + " steps updated/created in test case " + testCase.getTestCaseCode());
                } else {
                    ExceptionHandlers.getErrors("Error while updating/creating steps in test case ").forEach(LOGGER::error);
                }
            } catch (IOException exception) {
                throw new IllegalStateException(exception);
            }
            timer.stop();

            LOGGER.info("*-*-*-*-*-*-*-*-*-*-*-* Finished creating steps for " + testCaseID + " -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

做了很多谷歌搜索,似乎以前没有人问过这个问题,可能是因为不可能。

任何曾经尝试过这个的人都可以帮助我找到解决方案。

4

0 回答 0