1

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");
    }

1 Answers1

1

After debugging the above a few times with a friend, we found that the issue was we were explicitly specifying ContentType.JSON as the Content type.

So what we did to ensure we could run the test successfully, we commented out the line request.contentType(ContentType.JSON) from the executeAPI method and added the Content type as a header in addHeader to the authenticateWithBasicAuth method:

/**
     * 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).addHeader("Content-Type", "application/x-www-form-urlencoded");
        builder.setAuth(RestAssured.preemptive().basic(username, password)).addQueryParam(firstParameterName, firstParameterValue).addQueryParam(secondParameterName, secondParameterValue);
        String token = executeAPI().getBody().jsonPath().getString("access_token");
        return executeAPI().getBody().jsonPath().getString("access_token");
    }

I then ran it again without adding the Content type header and it was working fine and I was able to get the access token as a String.