Search code examples
javatestingpactpact-jvm

How to update dynamic date on body for post params within JVM PACT contract?


I have a POST request that takes date as a param from within my contract file for a PACT test.

return builder
    .uponReceiving("A request to create a Zoom meeting")
    .path(createMeeting)
    .method("POST")
    .headers(headers)
    .body("{\"title\":\"My title\",\"start_time\":\"2020-08-28T14:30:00Z+01:00\",\"duration\":30,\"provider\":\"ZOOM\"}")
    .willRespondWith()
    .body(body)
    .toPact();

But I'd like this to be dynamic, having perhaps today's or tomorrow's date, otherwise it would have an expired date. Could you please advise on how to do do this and if possible to keep it from the consumer side.

These are both Consumer and Provider samples for my request.

Consumer

@ExtendWith(PactConsumerTestExt.class)
public class PACTConsumerEdUiVcTest {

Map<String, String> headers = new HashMap<>();

String createMeeting = "/manage/create-meeting";

@Pact(provider = VC, consumer = ED_UI)
public RequestResponsePact createPact(PactDslWithProvider builder) {

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

    DslPart body = new PactDslJsonBody()
            .date("start_time", "yyyy-MM-dd'T'HH:mm:ss.000'Z'", new Date());


    return builder
            .uponReceiving("A request to create a Zoom meeting")
            .path(createMeeting)
            .method("POST")
            .headers(headers)
            .body("{\"title\":\"My title\",\"start_time\":\"2020-08-28T14:30:00Z+01:00\",\"duration\":30,\"provider\":\"ZOOM\"}")
            .willRespondWith()
            .body(body)
            .toPact();
}

@Test
@PactTestFor(providerName = VC, port = "8080")
public void runTest() {

    //Mock url
    RestAssured.baseURI = "http://localhost:8080";

    Response response = RestAssured //todo: dynamic start time that won't expire. 27/08/2020
            .given()
            .headers(headers)
            .when()
            .body("{\"title\":\"My title\",\"start_time\":\"2020-08-28T14:30:00Z+01:00\",\"duration\":30,\"provider\":\"ZOOM\"}")
            .post(createMeeting);

    assert (response.getStatusCode() == 200);
}

}

Provider

@Provider(VC)
@PactFolder("target/pacts")

public class PactProviderEdUiVcTest {

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactTestTemplate(PactVerificationContext context, HttpRequest request) {
    request.addHeader("Authorization", AUTHORIZATION_TOKEN);
    context.verifyInteraction();
}

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

    getAuthorizationToken(UserType.TEACHER);
}

@State("A request to create a Zoom meeting")
public void sampleState() {
}

}

Many thanks.


Solution

  • Able to make it working changing the RestAssured implementation to accept a map and use the date as its value.

    map.put("start_time", new Date().toString());
    

    Here the full piece.

    @Test
    @PactTestFor(providerName = VC, port = "8080")
    public void runTest() {
    
        //Mock url
        RestAssured.baseURI = "http://localhost:8080";
    
        Map<String, Object> map = new HashMap<>();
        map.put("title", "MyTitle");
        map.put("start_time", new Date().toString());
        map.put("duration", 30);
        map.put("provider", "ZOOM");
    
        Response response = RestAssured //todo: dynamic start time that won't expire. 27/08/2020
                .given()
                .headers(headers)
                .when()
                .body(map)
                .post(createMeeting);
    
        assert (response.getStatusCode() == 200);
    }