簡體   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