Class RestClient


  • public class RestClient
    extends java.lang.Object
    REST client for test stubbed rest (StubRest).

    client() provides a raw client, configured with: - Random port - Requests logging (@StubRest(logRequests = true), disabled by default) - Enabled restricted headers and method workaround (for url connection, used by in-memory test container) - Set default timeouts to avoid infinite calls - Enabled multipart support (if available in classpath)

    target(String...) - shortcut for calling rest services with a relative urls (no server, port or rest prefix). Without default* configuration (see below).

    Shortcut rest call methods with response mapping: - get(String, Class) - post(String, Object, Class) - put(String, Object, Class) - delete(String, Class) Putting null instead of result class implies void response. In this case, response status checked to be 200 or 204 (could be changed with defaultOk(Integer...)).

    To verify response headers use generic request method (request(String...)): for example, to request get response: Response res = rest.request("/path/").get().

    To simplify default shortcut methods usage, additional parameters like custom headers and query parameter are configured as defaults: - defaultHeader(String, String) - defaultQueryParam(String, String) - defaultAccept(String...) - defaultOk(Integer...) used only to verify correct OK codes for void responses (for methods returning mapped a mapped result: get("/path/", null)) - fail if the response status was not specified in defaultOk (200, 204 by default). For methods returning a real result, status is not checked (result presence already means correct execution)

    By default, defaults are reset after each test. So defaults could be specified in test setup method (to apply the same for all tests in class) or just before method call (in method test directly). Automatic rest could be disabled with @StubRest(autoReset = false).

    Since:
    20.02.2025
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      javax.ws.rs.client.Client client()
      Returns the pre-configured Client for this test.
      RestClient defaultAccept​(java.lang.String... accept)
      Apply default Accept header for shortcut rest call methods.
      RestClient defaultHeader​(java.lang.String name, java.lang.String value)
      Apply a default header for shortcut rest call methods.
      RestClient defaultOk​(java.lang.Integer... codes)
      Set allowed response codes for void calls (by default, 200 and 204).
      RestClient defaultQueryParam​(java.lang.String name, java.lang.String value)
      Apply a default query param for shortcut rest call methods.
      <T> T delete​(java.lang.String path, java.lang.Class<T> result)
      Simple DELETE call shortcut.
      <T> T get​(java.lang.String path, java.lang.Class<T> result)
      Simple GET call shortcut.
      java.net.URI getBaseUri()
      Just in case, it is not required when using any rest call method.
      boolean hasDefaultAccepts()
      Could be used for verifications in tests to avoid defaults collide.
      boolean hasDefaultHeaders()
      Could be used for verifications in tests to avoid defaults collide.
      boolean hasDefaultQueryParams()
      Could be used for verifications in tests to avoid defaults collide.
      boolean isDefaultStatusChanged()
      Could be used for verifications in tests to avoid defaults collide.
      <T> T post​(java.lang.String path, java.lang.Object body, java.lang.Class<T> result)
      Simple POST call shortcut.
      <T> T post​(java.lang.String rootPath, javax.ws.rs.client.Entity<?> body, java.lang.Class<T> result)
      Same as post(String, Object, Class), but accepts generic entity.
      <T> T put​(java.lang.String path, java.lang.Object body, java.lang.Class<T> result)
      Simple PUT call shortcut.
      <T> T put​(java.lang.String path, javax.ws.rs.client.Entity<?> body, java.lang.Class<T> result)
      Same as put(String, Object, Class), but accepts generic entity.
      javax.ws.rs.client.Invocation.Builder request​(java.lang.String... paths)
      Create request for provided target with all defaults applied.
      RestClient reset()
      Reset configured defaults.
      javax.ws.rs.client.WebTarget target​(java.lang.String... paths)
      Creates a web target to be sent to the resource under testing.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RestClient

        public RestClient​(GuiceyJerseyTest jerseyTest)
        Create client.
        Parameters:
        jerseyTest - jersey test instance
    • Method Detail

      • target

        public javax.ws.rs.client.WebTarget target​(java.lang.String... paths)
        Creates a web target to be sent to the resource under testing. When multiple parameters provided, they are connected with "/", avoiding duplicate slash appearances so, for example, "app, path", "app/, /path" or any other variation would always lead to correct "app/path"). Essentially this is the same as using WebTarget.path(String) multiple times (after initial target creation).

        Example: .target("/smth/").request().buildGet().invoke()

        WARNING: any specified defaults do not affect this method!

        This is a generic method. Provided shortcuts (like get(String, Class) should simplify usage.

        Parameters:
        paths - one or more path parts (assumed to be joined with '/') - overall, relative path (from tested application base URI) this web target should point to
        Returns:
        jersey web target object
      • client

        public javax.ws.rs.client.Client client()
        Returns the pre-configured Client for this test. For sending requests prefer target(String...). Use target(String...) method to avoid specifying full target path.
        Returns:
        the JerseyTest configured Client
      • getBaseUri

        public java.net.URI getBaseUri()
        Just in case, it is not required when using any rest call method.
        Returns:
        root rest url
      • defaultHeader

        public RestClient defaultHeader​(java.lang.String name,
                                        java.lang.String value)
        Apply a default header for shortcut rest call methods. Does not apply for target(String...) method.
        Parameters:
        name - header name
        value - header value
        Returns:
        client itself for chained calls
      • defaultQueryParam

        public RestClient defaultQueryParam​(java.lang.String name,
                                            java.lang.String value)
        Apply a default query param for shortcut rest call methods. Does not apply for target(String...) method.
        Parameters:
        name - parameter name
        value - parameter value
        Returns:
        client itself for chained calls
      • defaultOk

        public RestClient defaultOk​(java.lang.Integer... codes)
        Set allowed response codes for void calls (by default, 200 and 204). For example, get("/path/", null) would fail if response differs from specified. Note that status is not checked for responses, returning result (result presence already indicates correct execution).

        Does not apply for target(String...) method.

        Override previous setting.

        Parameters:
        codes - response codes allowed for void rest calls
        Returns:
        client itself for chained calls
      • hasDefaultHeaders

        public boolean hasDefaultHeaders()
        Could be used for verifications in tests to avoid defaults collide.
        Returns:
        true if default headers specified
      • hasDefaultQueryParams

        public boolean hasDefaultQueryParams()
        Could be used for verifications in tests to avoid defaults collide.
        Returns:
        true if default query params specified
      • hasDefaultAccepts

        public boolean hasDefaultAccepts()
        Could be used for verifications in tests to avoid defaults collide.
        Returns:
        true if default accepts specified
      • isDefaultStatusChanged

        public boolean isDefaultStatusChanged()
        Could be used for verifications in tests to avoid defaults collide.
        Returns:
        true if default void statuses changed
      • defaultAccept

        public RestClient defaultAccept​(java.lang.String... accept)
        Apply default Accept header for shortcut rest call methods. Does not apply for target(String...) method.
        Parameters:
        accept - accept values
        Returns:
        client itself for chained calls
        See Also:
        MediaType
      • request

        public javax.ws.rs.client.Invocation.Builder request​(java.lang.String... paths)
        Create request for provided target with all defaults applied. Use to get the complete response object to validate response headers: Response res = request("/path/").get().
        Parameters:
        paths - target path, relative to rest root
        Returns:
        request object, ready to be sent
      • get

        public <T> T get​(java.lang.String path,
                         @Nullable
                         java.lang.Class<T> result)
        Simple GET call shortcut. Provided path should include only the target rest path.

        To provide additional headers and query params see defaultHeader(String, String) (defaultAccept(String...)) and defaultQueryParam(String, String). For void responses (result class null) checks response status correctness (see defaultOk(Integer...)).

        For response headers validation, use raw Response res = request("/path/").get().

        Type Parameters:
        T - result type
        Parameters:
        path - target path, relative to rest root
        result - result type (when null, accepts any 200 or 204 responses)
        Returns:
        mapped result object or null (if class not declared)
      • post

        public <T> T post​(java.lang.String path,
                          @Nullable
                          java.lang.Object body,
                          @Nullable
                          java.lang.Class<T> result)
        Simple POST call shortcut. Provided path should include only the target rest path. Body object assumed to be a json entity (would be serialized as json). For file sending use method with generic entity post(String, javax.ws.rs.client.Entity, Class).

        To provide additional headers and query params see defaultHeader(String, String) (defaultAccept(String...)) and defaultQueryParam(String, String). For void responses (result class null) checks response status correctness (see defaultOk(Integer...)).

        For response headers validation, use raw Response res = request("/path/").post(Entity.json(body).

        Type Parameters:
        T - result type
        Parameters:
        path - target path, relative to rest root
        body - post body object (serialized as json)
        result - result type (when null, accepts any 200 or 204 responses)
        Returns:
        mapped result object or null (if class not declared)
      • post

        public <T> T post​(java.lang.String rootPath,
                          @Nullable
                          javax.ws.rs.client.Entity<?> body,
                          @Nullable
                          java.lang.Class<T> result)
        Same as post(String, Object, Class), but accepts generic entity. Useful for sending multipart requests.
        Type Parameters:
        T - result type
        Parameters:
        rootPath - target path, relative to rest root
        body - entity object
        result - result type (when null, accepts any 200 or 204 responses)
        Returns:
        mapped result object or null (if class not declared)
      • put

        public <T> T put​(java.lang.String path,
                         @Nullable
                         java.lang.Object body,
                         @Nullable
                         java.lang.Class<T> result)
        Simple PUT call shortcut. Provided path should include only the target rest path. Body object assumed to be a json entity (would be serialized as json). For file sending use method with generic entity put(String, javax.ws.rs.client.Entity, Class).

        To provide additional headers and query params see defaultHeader(String, String) (defaultAccept(String...)) and defaultQueryParam(String, String). For void responses (result class null) checks response status correctness (see defaultOk(Integer...)).

        For response headers validation, use raw Response res = request("/path/").put(Entity.json(body)).

        Type Parameters:
        T - result type
        Parameters:
        path - target path, relative to rest root
        body - put body object (serialized as json)
        result - result type (when null, accepts any 200 or 204 responses)
        Returns:
        mapped result object or null (if class not declared)
      • put

        public <T> T put​(java.lang.String path,
                         javax.ws.rs.client.Entity<?> body,
                         @Nullable
                         java.lang.Class<T> result)
        Same as put(String, Object, Class), but accepts generic entity. Useful for sending multipart requests.
        Type Parameters:
        T - result type
        Parameters:
        path - target path, relative to rest root
        body - post body object (serialized as json)
        result - result type (when null, accepts any 200 or 204 responses)
        Returns:
        mapped result object or null (if class not declared)
      • delete

        public <T> T delete​(java.lang.String path,
                            @Nullable
                            java.lang.Class<T> result)
        Simple DELETE call shortcut. Provided path should include only the target rest path.

        To provide additional headers and query params see defaultHeader(String, String) (defaultAccept(String...)) and defaultQueryParam(String, String). For void responses (result class null) checks response status correctness (see defaultOk(Integer...)).

        For response headers validation, use raw Response res = request("/path/").delete().

        Type Parameters:
        T - result type
        Parameters:
        path - target path, relative to rest root
        result - result type (when null, accepts any 200 or 204 responses)
        Returns:
        mapped result object or null (if class not declared)
      • reset

        public RestClient reset()
        Reset configured defaults.
        Returns:
        rest client itself for chained calls