Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Rest calls in Java using RestAssured

When you make a rest request using RestAssured, it appears to wait for a response. I need to make a POST request in RestAssured and then while it waits for a response, I need to make a GET request. I'm using Java and RestAssured. I've also tried creating a second thread and it still has not worked. This is for testing purposes.

Here's where it waits:

given().auth().preemptive().basic(userCreds[0], userCreds[1]).contentType("application/json;").and().post(baseURL + resourcePath + this.act.getId() + "/run");

I would like this to run while the previous request is running as well (asynchronous request?):

given().auth().preemptive().basic(userCreds[0], userCreds[1]).when().get(baseURL + resourcePath + this.connect.getId() + "/outgoing/" + fileId);

I've also read that RestAssured supports asynchronous requests, but I've been unsuccessful to get it to work. My project is mavenized. I'm just a lowly QA guy so any help would be greatly appreciated.

like image 590
mmyers Avatar asked Nov 21 '22 05:11

mmyers


1 Answers

Completable Future can be used to make calls async.

List<CompletableFuture<Pair<String,String>>> futures =
    Arrays.stream(endpoints)
        .map(endpoint -> restCallAsync(endpoint))
        .collect(Collectors.toList());

List<Pair<String,String>> result =
    futures.stream()
        .map(CompletableFuture::join)
        .collect(Collectors.toList());
CompletableFuture<Pair<String,String>> restCallAsync(String endpoint) {
    CompletableFuture<Pair<String,String>> future = CompleteableFuture.supplyAsync(new Supplier<Pair<String,String>>() {
        @Override
        public Pair<String,String> get() {
            Response response = given()
                .relaxedHTTPSValidation()
                .get(endpoint)
                .then()
                .extract().response();
            LOG.info("Endpoint : " + endpoint + " Status : " + response.getStatusCode());
            ...
        }
    });
}
like image 70
Amrit Avatar answered Mar 08 '23 09:03

Amrit