繁体   English   中英

如何为写在 Java 中的契约主体注入动态 ID?

[英]How to inject dynamic id for body of pact contract written in Java?

我们有一个 put api 将根据其 id 更新分配。 由于我们应该在测试后清理数据,我们的分配 ID 会在删除原始数据后发生变化,因此我们尝试将其动态注入到提供者端请求的正文中。 但是,我们似乎可能在这里遗漏了一些东西,因为它没有正确更新,并且仍然以设置为示例的 id 触发请求。

这是提供者 class:

@Slf4j
@Provider("Assignments API")
@Consumer("LTI-AGS-Tool")
//@PactBroker(url = BROKER_PACT_URL, authentication = @PactBrokerAuth(token = "${pactbroker.auth.token}"))
@VerificationReports(value = {"console", "markdown"}, reportDir = "target/pacts")
class PactProviderLTIAGSIT {

    private HashMap<String, String> headers = new HashMap<>();
    private String updateAssignmentId;
    private final String SERVICE_TOKEN = "myToken";

    @BeforeEach
    void createTeacherAssignment() {

        String assignmentBody = createBodyStringForStudentAssignmentSetup();

        assignmentBody = assignmentBody.replace("CPWAG", "OTHER_TEXT_RESOURCE");

        headers.put("Content-Type", "application/json");
        headers.put("Authorization", "myToken");

        RequestSpecification rq = Util.getRequestSpecification().baseUri(baseAssignmentUrl).headers(headers);
        Response response = rq.body(assignmentBody).post();
        assertEquals(201, response.getStatusCode());

        updateAssignmentId = response.jsonPath().get("assignments[0].refId");

        log.info("assignment id is " + updateAssignmentId);
    }

    @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void pactTestTemplate(PactVerificationContext context, HttpRequest request) {

        request.addHeader("Authorization", SERVICE_TOKEN);
        logCurlFromPact(context, request);
        context.verifyInteraction();
    }

    @BeforeEach
    void before(PactVerificationContext context) {
        context.setTarget(new HttpsTestTarget(BASE_PACT_TEACHER_ASSIGNMENTS_URL, 443, ""));
    }

    @State("Scoring info is passed between ags-tool and assignmentapi")
    Map<String, Object> getScoringInfo() {

        Map<String, Object> map = new HashMap<>();
        map.put("assignmentId", updateAssignmentId);
        return map;
    }
}

这里是消费者合同:

@ExtendWith(PactConsumerTestExt.class)
class PactConsumerSendScoreIT {

    private final Map<String, String> headers = new HashMap<>();
    private final String path = "/v5/assignmentStatus/update";

    @Pact(provider = PACT_PROVIDER, consumer = PACT_CONSUMER)
    public RequestResponsePact scoreConsumerPact(PactDslWithProvider builder) {

        headers.put("Content-Type", "application/json");

        //Body given and returned
        DslPart body = new PactDslJsonBody()
                .valueFromProviderState("assignmentId", "assignmentId", "c1ef3bbf-55a2-4638-8f93-22b2916fe085")
                .stringType("timestamp", DateTime.now().plusHours(3).toString())
                .decimalType("scoreGiven", 75.00)
                .decimalType("scoreMaximum", 100.00)
                .stringType("comment", "Good work!")
                .stringType("status", "IN_PROGRESS")
                .stringType("userId", "c2ef3bbf-55a2-4638-8f93-22b2916fe085")
                .close();

        return builder
                .given("Scoring info is passed between ags-tool and assignmentapi")
                .uponReceiving("Scoring info is passed between ags-tool and assignmentapi")
                .path(path)
                .method("POST")
                .body(body)
                .headers(headers)
                .willRespondWith()
                .status(201)
                .body(body)
                .toPact();

    }

    @Test
    @PactTestFor(pactMethod = "scoreConsumerPact", providerName = PACT_PROVIDER, port = "8080", pactVersion = PactSpecVersion.V3)
    void runTest(MockServer mockServer) {

        String updateAssignmentId = "c2ef3bbf-55a2-4638-8f93-22b2916fe085";

        HashMap<String, Object> map = new HashMap<>();
        map.put("timestamp", DateTime.now().plusHours(3).toString());
        map.put("scoreGiven", 75.00);
        map.put("scoreMaximum", 100.00);
        map.put("comment", "Good work!");
        map.put("status", "IN_PROGRESS");
        map.put("userId", "c2ef3bbf-55a2-4638-8f93-22b2916fe085");
        map.put("assignmentId", updateAssignmentId);

        //Mock url
        RequestSpecification rq = Util.getRequestSpecification().baseUri(mockServer.getUrl()).headers(headers);

        Response response = rq.body(map)
                .post(path);

        assertEquals(201, response.getStatusCode());
    }
}

谢谢你。

我们发现所需的表达式是包含 ${}(至少如果它是一个字符串)。 一旦我们将其更新为 valueFromProviderState("assignmentId", "${assignmentId}", "c1ef3bbf-55a2-4638-8f93-22b2916fe085") 它似乎就可以工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM