Class RestActions

java.lang.Object
com.shaft.api.RestActions

public class RestActions extends Object
Core REST API automation engine for SHAFT, built on top of REST Assured.

This class handles HTTP request construction, execution, response parsing, and validation for RESTful web services. It supports GET, POST, PUT, PATCH, and DELETE methods, along with GraphQL queries.

For new code, prefer using SHAFT.API which provides a cleaner, session-scoped fluent interface. This class remains available for legacy compatibility and for its static utility methods such as getResponseJSONValue(Response, String).

Usage example (via SHAFT.API):

SHAFT.API api = new SHAFT.API("https://jsonplaceholder.typicode.com");
api.get("/posts/1").setTargetStatusCode(200).performRequest();
String title = api.getResponseJSONValue("$.title");
See Also:
  • Constructor Details

    • RestActions

      public RestActions(String serviceURI, SHAFT.API driver)
    • RestActions

      public RestActions(String serviceURI)
  • Method Details

    • buildNewRequest

      public static RequestBuilder buildNewRequest(String serviceURI, String serviceName, RestActions.RequestType requestType)
      Builds a new API request using a static factory approach. Prefer using SHAFT.API and its instance-level buildNewRequest(String, RequestType) for new tests.
      Parameters:
      serviceURI - the base URI of the service (e.g. "https://api.example.com/")
      serviceName - the endpoint path (e.g. "users/1")
      requestType - the HTTP method to use (e.g. RestActions.RequestType.GET)
      Returns:
      a RequestBuilder ready for further configuration and execution
      See Also:
    • parseBodyToJson

      public static InputStream parseBodyToJson(io.restassured.response.Response response)
      Parses a REST Assured Response body into a JSON InputStream. Delegates to parseBodyToJson(Object) using the response's raw body.
      Parameters:
      response - the API response whose body will be parsed
      Returns:
      an InputStream containing the JSON-formatted body
    • parseBodyToJson

      public static InputStream parseBodyToJson(Object body)
      Converts an arbitrary response body object to a JSON InputStream. If the object cannot be serialized as JSON, it is serialized using Java object serialization, or returned as a raw byte stream for REST-Assured response bodies.
      Parameters:
      body - the response body object to convert — may be a REST-Assured Response, a String, or any serializable Java object
      Returns:
      an InputStream representing the body content
    • getResponseBody

      public static String getResponseBody(io.restassured.response.Response response)
      Extracts the response body and returns it as a plain string
      Parameters:
      response - the target API response object
      Returns:
      a string value that represents the response body
    • getResponseJSONValue

      public static String getResponseJSONValue(io.restassured.response.Response response, String jsonPath)
      Extracts a string value from the response body by parsing the target jsonpath
      Parameters:
      response - the full response object returned by 'performRequest()' method
      jsonPath - the JSONPath expression that will be evaluated in order to extract the desired value [without the trailing $.], please refer to these urls for examples: https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html http://jsonpath.com/
      Returns:
      a string value that contains the extracted object
    • getResponseJSONValue

      public static String getResponseJSONValue(Object response, String jsonPath)
      Extracts a string value from an arbitrary response object by evaluating a JSONPath expression. Supports REST-Assured Response, String, JSONObject, JSONArray, List, and HashMap inputs.
      Parameters:
      response - the response object to parse — not limited to REST-Assured Response
      jsonPath - the JSONPath expression (e.g. "$.id" or "id")
      Returns:
      the extracted string value, or null if not found
      See Also:
    • getResponseJSONValueAsList

      public static List<Object> getResponseJSONValueAsList(io.restassured.response.Response response, String jsonPath)
      Extracts a list of values from the response body by evaluating a JSONPath expression.
      Parameters:
      response - the REST-Assured response object
      jsonPath - the JSONPath expression that resolves to an array (e.g. "$.items[*].id")
      Returns:
      a List of matched objects, or null if none are found
      See Also:
    • getResponseJSONValueFromList

      public static String getResponseJSONValueFromList(io.restassured.response.Response response, String jsonPathToList, String jsonPathToValueNeeded, String jsonPathToValueReference, String valueReference)
      Extracts a string value from an object of a list by reference of another attribute inside the same object
      Parameters:
      response - The target API response object
      jsonPathToList - The JSON path to the list of object inside the full response
      jsonPathToValueNeeded - The JSON path to the attribute value you need to extract inside an object from the list. for example: id
      jsonPathToValueReference - The JSON path that refers to the needed attribute value inside an object from the list. for example: username
      valueReference - The attribute value of the reference JSON path
      Returns:
      A string value from the object of the list which represents the first match of the reference attribute value
    • getResponseXMLValue

      public static String getResponseXMLValue(io.restassured.response.Response response, String xmlPath)
      Extracts a string value from the XML response body using the given XPath expression.
      Parameters:
      response - the REST-Assured response object with an XML body
      xmlPath - the XPath expression (e.g. "root.item.name")
      Returns:
      the extracted string value, or null if not found
      See Also:
    • getResponseXMLValue

      public static String getResponseXMLValue(Object response, String xmlPath)
      Extracts a string attribute value from an XML Node object.
      Parameters:
      response - an XML Node (typically obtained from getResponseXMLValueAsList(Response, String))
      xmlPath - the attribute name to retrieve from the node
      Returns:
      the attribute value string, or null if not found
    • getResponseXMLValueAsList

      public static List<Object> getResponseXMLValueAsList(io.restassured.response.Response response, String xmlPath)
      Extracts a list of XML Node children matching the given XPath expression.
      Parameters:
      response - the REST-Assured response object with an XML body
      xmlPath - the XPath expression to the desired node set (e.g. "root.items")
      Returns:
      a List of matched Object nodes, or null if not found
    • getResponseStatusCode

      public static int getResponseStatusCode(io.restassured.response.Response response)
      Returns the HTTP status code of the given API response and logs it.
      Parameters:
      response - the REST-Assured response object
      Returns:
      the HTTP status code (e.g. 200, 404)
    • getResponseTime

      public static long getResponseTime(io.restassured.response.Response response)
      Returns the total elapsed time of the given API response in milliseconds.
      Parameters:
      response - the REST-Assured response object
      Returns:
      the response time in milliseconds
    • compareJSON

      public static boolean compareJSON(io.restassured.response.Response response, String referenceJsonFilePath, RestActions.ComparisonType comparisonType)
      Compares the Response object against the content of the referenceJsonFilePath
      Parameters:
      response - the full response object returned by performRequest method.
      referenceJsonFilePath - the full absolute path to the test data file that will be used as a reference for this comparison
      comparisonType - ComparisonType.EQUALS, CONTAINS, MATCHES, EQUALS_STRICT; Note that MATCHES ignores the content ordering inside the JSON
      Returns:
      a boolean value that is TRUE in case the comparison passed, or FALSE in case it failed
    • compareJSON

      public static boolean compareJSON(io.restassured.response.Response response, String referenceJsonFilePath, RestActions.ComparisonType comparisonType, String jsonPathToTargetArray)
      Compares the Response object against the content of the referenceJsonFilePath
      Parameters:
      response - the full response object returned by performRequest method.
      referenceJsonFilePath - the full absolute path to the test data file that will be used as a reference for this comparison
      comparisonType - ComparisonType.EQUALS, CONTAINS; Note that MATCHES ignores the content ordering inside the JSON
      jsonPathToTargetArray - a jsonpath that will be parsed to point to the target JSON Array
      Returns:
      a boolean value that is TRUE in case the comparison passed, or FALSE in case it failed
    • formatXML

      public static String formatXML(String input)
      Pretty-prints an XML string by re-formatting it with consistent indentation.
      Parameters:
      input - the raw XML string to format
      Returns:
      a neatly indented XML string, or the original input if formatting fails
    • sendGraphQlRequest

      public static io.restassured.response.Response sendGraphQlRequest(String base_URI, String query)
      Perform Graphql Request using Query - WITHOUT Header.
      Parameters:
      base_URI - The Base URI without "graphql". example:: "https://api.example.com/"
      query - graphql query or mutation.
      Returns:
      Graphql Response
    • sendGraphQlRequest

      public static io.restassured.response.Response sendGraphQlRequest(String base_URI, String query, String variables)
      Perform Graphql Request using Query and Variables - WITHOUT Header.
      Parameters:
      base_URI - The Base URI without "graphql". example:: "https://api.example.com/"
      query - graphql query or mutation.
      variables - graphql variables; dynamic values of the query. please refer to this url for examples:: https://graphql.org/learn/queries/#variables
      Returns:
      Graphql Response
    • sendGraphQlRequest

      public static io.restassured.response.Response sendGraphQlRequest(String base_URI, String query, String variables, String fragment)
      Perform Graphql Request using Query, Variables, and Fragments - WITHOUT Header.
      Parameters:
      base_URI - The Base URI without "graphql". example:: "https://api.example.com/"
      query - graphql query or mutation.
      variables - graphql variables; dynamic values of the query. please refer to this url for examples:: https://graphql.org/learn/queries/#variables
      fragment - graphql fragment; reusable units let you construct sets of fields, and then include them in queries where you need to. please refer to this url for examples:: https://graphql.org/learn/queries/#fragments
      Returns:
      Graphql Response
    • getResponse

      public io.restassured.response.Response getResponse()
    • sendGraphQlRequestWithHeader

      public static io.restassured.response.Response sendGraphQlRequestWithHeader(String base_URI, String query, String header_key, String header_value)
      Perform Graphql Request using Query - WITH Header.
      Parameters:
      base_URI - The Base URI without "graphql". example:: "https://api.example.com/"
      query - graphql query or mutation.
      header_key - the name of the header that you want to add. example:: "Authorization"
      header_value - the value that will be put inside the key. example:: "bearer ${token}"
      Returns:
      Graphql Response
    • sendGraphQlRequestWithHeader

      public static io.restassured.response.Response sendGraphQlRequestWithHeader(String base_URI, String query, String variables, String header_key, String header_value)
      Perform Graphql Request using Query and Variables - WITH Header.
      Parameters:
      base_URI - The Base URI without "graphql". example:: "https://api.example.com/"
      query - graphql query or mutation.
      variables - graphql variables; dynamic values of the query. please refer to this url for examples:: https://graphql.org/learn/queries/#variables
      header_key - the name of the header that you want to add. example:: "Authorization"
      header_value - the value that will be put inside the key. example:: "bearer ${token}"
      Returns:
      Graphql Response
    • sendGraphQlRequestWithHeader

      public static io.restassured.response.Response sendGraphQlRequestWithHeader(String base_URI, String query, String variables, String fragment, String header_key, String header_value)
      Perform Graphql Request using Query, Variables, and Fragments - WITH Header.
      Parameters:
      base_URI - The Base URI without "graphql". example:: "https://api.example.com/"
      query - graphql query or mutation.
      variables - graphql variables; dynamic values of the query. please refer to this url for examples:: https://graphql.org/learn/queries/#variables
      fragment - graphql fragment; reusable units let you construct sets of fields, and then include them in queries where you need to. please refer to this url for examples:: https://graphql.org/learn/queries/#fragments
      header_key - the name of the header that you want to add. example:: "Authorization"
      header_value - the value that will be put inside the key. example:: "bearer ${token}"
      Returns:
      Graphql Response
    • buildNewRequest

      public RequestBuilder buildNewRequest(String serviceName, RestActions.RequestType requestType)
      Builds a new API request within this session, inheriting all session-level headers, cookies, and configuration.
      Parameters:
      serviceName - the endpoint path relative to this instance's base URI
      requestType - the HTTP method to use (e.g. RestActions.RequestType.POST)
      Returns:
      a RequestBuilder for configuring and executing the request
      See Also:
    • addHeaderVariable

      public RestActions addHeaderVariable(String key, String value)
      Append a header to the current session to be used in all the following requests. Note: This feature is commonly used for authentication tokens.
      Parameters:
      key - the name of the header that you want to add
      value - the value that will be put inside the key
      Returns:
      self-reference to be used for chaining actions
    • addCookieVariable

      public RestActions addCookieVariable(String key, String value)
      Appends a cookie to every subsequent request in this session.
      Parameters:
      key - the cookie name
      value - the cookie value
      Returns:
      self-reference to be used for chaining actions