Shankar Daitha
Shankar Daitha

Reputation: 243

How to prepare dynamic request in rest Assured

I am new to rest assured. Currently, I am working on API automation using rest assured. I have the following scenario to handle

We have two APIs (eg: API1, API2) , API1 will give the list of userdetails . I need to send these details as part of the second API request.

API1 - Response

    [ {
  "userSourceMeta" : {
    "userId" : "[email protected]",
    "source" : "BOX",
    "organisationId" : 1,
    "emailId" : "[email protected]",
    "sourceUserId" : "16231222289",
    "sourceTeamId" : null
  },
  "connectionStatus" : null
}, {
  "userSourceMeta" : {
    "userId" : "[email protected]",
    "source" : "DROPBOX",
    "organisationId" : 1,
    "emailId" : "[email protected]",
    "sourceUserId" : "88888222768",
    "sourceTeamId" : null
  },
  "connectionStatus" : null
}, {
  "userSourceMeta" : {
    "userId" : "[email protected]",
    "source" : "GDRIVE",
    "organisationId" : 1,
    "emailId" : "[email protected]",
    "sourceUserId" : "8888873554753473",
    "sourceTeamId" : null
  },
  "connectionStatus" : null
}]

API2 - Request :. : In API2 request I need to send "sourceUserId", "source" details.

    {
  "query": "hi",
  "timeZone": "Asia/Calcutta",
  "sourceFilterInfo": [
    {
      "sourceUserId": "16055292089",
      "source": "BOX"
    },
    {
      "sourceUserId": "88888222768",
      "source": "DROPBOX"
    },
    {
      "sourceUserId": "8888873554753473",
      "source": "GDRIVE"
    }
  ],
  "contextIds": []
}

userSourceMeta details are dynamically change based on the user. Please suggest to me how to prepare the API2 request based on the previous API responses.

Upvotes: 0

Views: 2355

Answers (2)

Swapnil Sonawane
Swapnil Sonawane

Reputation: 5

In my case I have two vId(1 & 2). I want to send it dynamically, if response is null for vId 1 then set vId as 2.

Response response = given().header("Content-Type", "application/json")
                   .param("name", Name).param("vIds", vId)
                   .when().get(EndPoint).then()
                   .extract().response();   

Upvotes: 0

lucas-nguyen-17
lucas-nguyen-17

Reputation: 5917

I don't know whether you're fimilar with POJO approach or not, but personally, it'd be the easiest way.

Note:

  • you can extract static class to stand-alond class, no required it must be here.
  • I use Lombok and Jackson as external lib.
public class DynamicResponse {

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    static class SourceMeta{
        private UserSource userSourceMeta;
    }

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    static class UserSource {
        private String userId;
        private String source;
    }

    @Data
    static class Request2 {
        private String query;
        private String timeZone;
        private List<UserSource> sourceFilterInfo;
    }

    @Test
    void test() {
        //Save response of request 1
        List<SourceMeta> res1 = given().get("http://localhost:8000/req1")
                .as(new TypeRef<>() {});

        // Convert response 1 to list of UserSource
        List<UserSource> userSource = res1.stream()
                .map(SourceMeta::getUserSourceMeta)
                .collect(Collectors.toList());

        // Add list of UserSource to Request 2
        Request2 req2 = new Request2();
        req2.setQuery("hi");
        req2.setTimeZone("Asia/Calcutta");
        req2.setSourceFilterInfo(userSource);

        // Send the Request 2
        given().log().body().contentType(ContentType.JSON)
                .body(req2)
                .post("http://localhost:8000/echo");
    }
}

Upvotes: 1

Related Questions