Class ResponseMatchers

java.lang.Object
com._4point.testing.matchers.jaxrs.ResponseMatchers

public class ResponseMatchers extends Object
Response Matchers for testing an JAX-RS HTTP Response object. Based on https://www.planetgeek.ch/2012/03/07/create-your-own-matcher/
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Content-type string for "application/pdf"
    static final jakarta.ws.rs.core.MediaType
    JAX-RS MedisType object for "application/pdf"
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.hamcrest.Matcher<jakarta.ws.rs.core.Response>
    Creates a Matcher that validates that a Response does not have an entity..
    static HtmlForm
    expectingHtmlForm(jakarta.ws.rs.core.Response response, URI baseUri)
    Performs a series of checks on the Response object to validate that it is a response containing HTML and then returns an HtmlForm object for further validation.
    static Pdf
    expectingPdf(jakarta.ws.rs.core.Response response)
    Performs a series of checks on the Response object to validate that it is a response containing a PDF and then returns a Pdf object for further validation.
    static org.hamcrest.Matcher<jakarta.ws.rs.core.Response>
    Creates a Matcher that validates that a Response has an entity.
    static org.hamcrest.Matcher<jakarta.ws.rs.core.Response>
    hasEntityEqualTo(byte[] bytes)
    Creates a matcher that compares the Response bytes with a provided bute array to see if they match.
    static org.hamcrest.Matcher<jakarta.ws.rs.core.Response>
    hasEntityMatching(org.hamcrest.Matcher<byte[]> byteMatcher)
    Creates a matcher that allows someone to validate the bytes in the response entity.
    static org.hamcrest.Matcher<jakarta.ws.rs.core.Response>
    hasMediaType(jakarta.ws.rs.core.MediaType expectedMediaType)
    Creates a Matcher that compares a Response object's MediaType to the one provided.
    static org.hamcrest.Matcher<jakarta.ws.rs.core.Response>
    hasStringEntityMatching(Charset charset, org.hamcrest.Matcher<String> stringMatcher)
    Creates a matcher that allows someone to validate the response entity as a Srting.
    static org.hamcrest.Matcher<jakarta.ws.rs.core.Response>
    hasStringEntityMatching(org.hamcrest.Matcher<String> stringMatcher)
    Creates a matcher that allows someone to validate the response entity as a Srting.
    static org.hamcrest.Matcher<jakarta.ws.rs.core.Response>
    isStatus(jakarta.ws.rs.core.Response.Status expectedStatus)
    Creates a Matcher that compares a Response object's status to the one provided.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • APPLICATION_PDF

      public static final String APPLICATION_PDF
      Content-type string for "application/pdf"
      See Also:
    • APPLICATION_PDF_TYPE

      public static final jakarta.ws.rs.core.MediaType APPLICATION_PDF_TYPE
      JAX-RS MedisType object for "application/pdf"
  • Method Details

    • isStatus

      public static org.hamcrest.Matcher<jakarta.ws.rs.core.Response> isStatus(jakarta.ws.rs.core.Response.Status expectedStatus)
      Creates a Matcher that compares a Response object's status to the one provided.
      Parameters:
      expectedStatus - the expected Status
      Returns:
      The matcher
    • hasMediaType

      public static org.hamcrest.Matcher<jakarta.ws.rs.core.Response> hasMediaType(jakarta.ws.rs.core.MediaType expectedMediaType)
      Creates a Matcher that compares a Response object's MediaType to the one provided.
      Parameters:
      expectedMediaType - the expected MediaType
      Returns:
      the matcher
    • hasEntity

      public static org.hamcrest.Matcher<jakarta.ws.rs.core.Response> hasEntity()
      Creates a Matcher that validates that a Response has an entity.
      Returns:
      the matcher
    • doesNotHaveEntity

      public static org.hamcrest.Matcher<jakarta.ws.rs.core.Response> doesNotHaveEntity()
      Creates a Matcher that validates that a Response does not have an entity..
      Returns:
      the matcher
    • hasEntityMatching

      public static org.hamcrest.Matcher<jakarta.ws.rs.core.Response> hasEntityMatching(org.hamcrest.Matcher<byte[]> byteMatcher)
      Creates a matcher that allows someone to validate the bytes in the response entity. The matcher fails if the Response has no entity to match against.
      Parameters:
      byteMatcher - a matcher that validates the bytes in the response enrity
      Returns:
      the matcher
    • hasStringEntityMatching

      public static org.hamcrest.Matcher<jakarta.ws.rs.core.Response> hasStringEntityMatching(Charset charset, org.hamcrest.Matcher<String> stringMatcher)
      Creates a matcher that allows someone to validate the response entity as a Srting. The matcher fails if the Response has no entity to match against.
      Parameters:
      charset - the character set the incoming data should be in
      stringMatcher - a matcher that validates the response entity as a String
      Returns:
      the matcher
    • hasStringEntityMatching

      public static org.hamcrest.Matcher<jakarta.ws.rs.core.Response> hasStringEntityMatching(org.hamcrest.Matcher<String> stringMatcher)
      Creates a matcher that allows someone to validate the response entity as a Srting. The matcher fails if the Response has no entity to match against. Current assumes that the incoming data is in UTF-9.
      Parameters:
      stringMatcher - a matcher that validates the response entity as a String
      Returns:
      the matcher
    • hasEntityEqualTo

      public static org.hamcrest.Matcher<jakarta.ws.rs.core.Response> hasEntityEqualTo(byte[] bytes)
      Creates a matcher that compares the Response bytes with a provided bute array to see if they match.
      Parameters:
      bytes - a byte array to be matched against
      Returns:
      the matcher
    • expectingPdf

      public static Pdf expectingPdf(jakarta.ws.rs.core.Response response) throws Pdf.PdfException
      Performs a series of checks on the Response object to validate that it is a response containing a PDF and then returns a Pdf object for further validation. * it ensures the ContentType header is application/pdf * it ensures the Status is OK * it ensures there is a body in the response * it reads the body into a PDF object which parses the body using a PDF parser.
      Parameters:
      response - the Response object to be validated
      Returns:
      the valid Pdf object
      Throws:
      Pdf.PdfException - thrown if there are any I/O or parsing errors when validating the Pdf
    • expectingHtmlForm

      public static HtmlForm expectingHtmlForm(jakarta.ws.rs.core.Response response, URI baseUri)
      Performs a series of checks on the Response object to validate that it is a response containing HTML and then returns an HtmlForm object for further validation. * it ensures the ContentType header is test/html * it ensures the Status is OK * it ensures there is a body in the response * it reads the body into a HtmlForm object which parses the body using an Html parser.
      Parameters:
      response - the Response object to be validated
      baseUri - base location of the form
      Returns:
      the valid HtmlForm object
      Throws:
      IllegalStateException - thrown if there are any I/O or parsing errors when validating the Html