I have a step definition that is meant to perform a basic auth to get an access token. However I am not able to retrieve the string when it calls the authenticateWithBasicAuth method in the RestAssuredExtension class.
I am getting the error io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document
This is the Step definition:
@And("I perform basic auth operation to get access token")
public void i_Perform_Basic_Auth_Operation_To_Get_Access_Token() throws Throwable {
String username = CommonTestData.consumerKey;
String password = CommonTestData.consumerSecret;
String firstParameterName = CommonTestData.basicAuthQueryParamsKey1Variable;
String firstParameterValue = CommonTestData.basicAuthQueryParamsValue1Variable;
String secondParameterName = CommonTestData.basicAuthQueryParamsKey2Variable;
String secondParameterValue = CommonTestData.basicAuthQueryParamsValue2Variable;
RestAssuredExtension restAssuredExtension = new RestAssuredExtension(APIConstant.ApiMethods.POST,null);
token = restAssuredExtension.authenticateWithBasicAuth(username, password, firstParameterName, firstParameterValue, secondParameterName, secondParameterValue);
System.out.println("access_token is " + token);
}
This is the generic RestAssuredExtension class & methods:
I think the issue is return executeAPI().getBody().jsonPath().getString("access_token");
as I can't parse the JSON object? This is where I'm stuck at.
public class RestAssuredExtension {
private RequestSpecBuilder builder = new RequestSpecBuilder();
private String method;
private String url;
/**
* RestAssuredExtension constructor to pass the initial settings for the the following method
* @param method
* @param token
*/
public RestAssuredExtension(String method, String token) {
//Formulate the API url
this.url = "https://api.business.govt.nz/services/token";
this.method = method;
if(token != null)
builder.addHeader("Authorization", "Bearer " + token);
}
/**
* ExecuteAPI to execute the API for GET/POST/DELETE
* @return ResponseOptions<Response>
*/
private ResponseOptions<Response> executeAPI() {
RequestSpecification requestSpecification = builder.build();
RequestSpecification request = RestAssured.given();
request.contentType(ContentType.JSON);
request.spec(requestSpecification);
if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.POST))
return request.post(this.url);
else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.DELETE))
return request.delete(this.url);
else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.GET))
return request.get(this.url);
else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.PUT))
return request.get(this.url);
return null;
}
/**
* Authenticate to get the token variable
* @return string token
*/
public String authenticateWithBasicAuth(String username, String password, String firstParameterName, String firstParameterValue, String secondParameterName, String secondParameterValue){
builder.setAuth(RestAssured.preemptive().basic(username, password)).addQueryParam(firstParameterName, firstParameterValue).addQueryParam(secondParameterName, secondParameterValue);
return executeAPI().getBody().jsonPath().getString("access_token");
}