简体   繁体   中英

RestAssured with Awaitility

I currently have some code which needs some retry logic. As some remote requests and database updates might take some time. I don't want to add a specific wait for this. I want to retry until it is matching my specific response. Currently it is using awaitility and it will retry until a specific field is matching.

private static RequestSpecification REQUEST_SPEC(String baseUri) {
     return new RequestSpecBuilder()
         .setBaseUri(baseUri)
         .setBasePath(DEFAULT_PATH)
         .setAccept(ContentType.JSON)  
         .setContentType(ContentType.JSON)
         .setConfig(config().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails()))
         .build();
}
private static ResponseSpecBuilder PROFILE_RESPONSE() {
    return new ResponseSpecBuilder()
        .expectStatusCode(HTTP_OK)
        .expectBody("profileId", equalTo(PROFILE_ID))
        .expectBody("userKey", notNullValue());
}
await().timeout(10, SECONDS).untilAsserted(() -> assertThat("Profile not yet updated",
   given()
      .spec(REQUEST_SPEC)
      .pathParam("somePathParam", "somePathParamValue")
      .auth().oauth2("someAccessToken")
   .when() 
      .get("/someRequestPath")
   .then()
      .spec(PROFILE_RESPONSE)
   .extract() 
      .body().jsonPath().getString("profileId"), equalTo("updatedProfileId")));

I'm combining build-in RestAssured mechanisms (reusable ResponseSpecifications) and awaitility with hamcrest matcher. Is there a way to retry upon RestAssured ValidatableResponse instead of extracting it and validate it with an hamcrest assertThat() matcher?

Awaitility.await().atMost(30,SECONDS).until(()-> isStatus(expected));

private Boolean isStatus(String expected){
String url="/order/{id}";
Response response = RestAssured.given()
                    .header("header", "value")
                    .pathParam("id",id)
                    .when().get(url);
return response.then().extract().path("links").toString().equalsIgnoreCase(expected)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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