'How to parse JSON in rest assured stepdefination class?
Below is the JSON request body: { "memberId":"21bda54e-895a-4528-8788-698fce5b5", "prodList":[ { "productCode":"312" } ] }
Below is the code which I have written. Let me know How to parse the above JSON payload in the rest assured step definition file.
public class Generate_Bill {
private Response response;
private String accessToken;
private void jsonBodyUsingMap(String string, List singletonList) {
// TODO Auto-generated method stub
}
public String generateStringFromResource(String path) throws Throwable {
return new String(Files.readAllBytes(Paths.get(path)));
}
@Given("Post Generate Bill API")
public void post_Generate_Bill_API() {
RestAssured.baseURI = Initialization.url;
}
@When("call the Post Generate Bill API with valid token and details")
public void call_the_Post_Generate_Bill_API_with_valid_token_and_details() throws Throwable {
accessToken = TokenGeneration.accessToken;
String jsonBody = generateStringFromResource("src/test/java/request/Generate_Bill.json");
//Gson gson = new Gson();
//JsonObject inputObj = gson.fromJson(jsonBody, JsonObject.class);
Map<String, Object> jsonBodyUsingMap = new HashMap<String, Object>();
// singletonList() to return an immutable list containing only the specified object
jsonBodyUsingMap("prodList", Collections.singletonList(new HashMap<String, Object>() {
{
put("productCode", "31");
}}
));
jsonBodyUsingMap.put("memberId", "21bda54e-895a-4528-8788-6985f6fce5b5");
response = RestAssured.given()
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + accessToken)
.when()
.post(Initialization.Generate_Bill);
}
@Then("validate Generate Bill API for {int} status code")
public void validate_Generate_Bill_API_for_status_code(Integer int1) {
int status_code = response.getStatusCode();
System.out.println("status is: " +status_code);
Assert.assertEquals(200, status_code);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
