Reputation: 1
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?
Upvotes: 0
Views: 6647
Reputation: 21
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)
}
Upvotes: 2