Class ClientSupport

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class ClientSupport
    extends java.lang.Object
    implements java.lang.AutoCloseable
    JerseyClient support for direct web tests (complete dropwizard startup).

    Client support maintains single JerseyClient instance. It may be used for calling any urls (not just application). Class provides many utility methods for automatic construction of base context paths, so tests could be completely independent from actual configuration.

    Client customization is possible through custom TestClientFactory implementation.

    See get(String, Class), post(String, Object, Class) and other simple methods as client api usage example (or use them directly if appropriate).

    Since:
    04.05.2020
    • Constructor Summary

      Constructors 
      Constructor Description
      ClientSupport​(io.dropwizard.testing.DropwizardTestSupport<?> support)  
      ClientSupport​(io.dropwizard.testing.DropwizardTestSupport<?> support, TestClientFactory factory)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String basePathAdmin()
      For example, with the default configuration it would be "http://localhost:8081/".
      java.lang.String basePathMain()
      For example, with default configuration it would be "http://localhost:8080/".
      java.lang.String basePathRest()
      For example, with the default configuration it would be "http://localhost:8080/".
      java.lang.String basePathRoot()  
      void checkVoidResponse​(java.util.function.Supplier<javax.ws.rs.core.Response> call)
      Validates response to be 200 or 204 (no content).
      void close()  
      <T> T delete​(java.lang.String rootPath, java.lang.Class<T> result)
      Simple DELETE call shortcut for server root.
      <T> T get​(java.lang.String rootPath, java.lang.Class<T> result)
      Simple GET call shortcut for server root.
      int getAdminPort()  
      org.glassfish.jersey.client.JerseyClient getClient()
      Single client instance maintained within test and method will always return the same instance.
      int getPort()  
      <T> T post​(java.lang.String rootPath, java.lang.Object body, java.lang.Class<T> result)
      Simple POST call shortcut for server root.
      <T> T put​(java.lang.String rootPath, java.lang.Object body, java.lang.Class<T> result)
      Simple PUT call shortcut for server root.
      javax.ws.rs.client.WebTarget target​(java.lang.String... paths)
      Unbounded (universal) WebTarget construction shortcut.
      javax.ws.rs.client.WebTarget targetAdmin​(java.lang.String... paths)
      Shortcut for WebTarget creation for admin context path.
      javax.ws.rs.client.WebTarget targetMain​(java.lang.String... paths)
      Shortcut for WebTarget creation for main context path.
      javax.ws.rs.client.WebTarget targetRest​(java.lang.String... paths)
      Shortcut for WebTarget creation for rest context path.
      • Methods inherited from class java.lang.Object

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

      • ClientSupport

        public ClientSupport​(io.dropwizard.testing.DropwizardTestSupport<?> support)
      • ClientSupport

        public ClientSupport​(io.dropwizard.testing.DropwizardTestSupport<?> support,
                             @Nullable
                             TestClientFactory factory)
    • Method Detail

      • getClient

        public org.glassfish.jersey.client.JerseyClient getClient()
        Single client instance maintained within test and method will always return the same instance.
        Returns:
        client instance
      • getPort

        public int getPort()
        Returns:
        main context port
        Throws:
        java.lang.NullPointerException - for guicey test
      • getAdminPort

        public int getAdminPort()
        Returns:
        admin context port
        Throws:
        java.lang.NullPointerException - for guicey test
      • basePathRoot

        public java.lang.String basePathRoot()
        Returns:
        root application path (localhost + port)
      • basePathMain

        public java.lang.String basePathMain()
        For example, with default configuration it would be "http://localhost:8080/". If "server.applicationContextPath" would be changed to "/someth" then method will return "http://localhost:8080/someth/".

        Returned path will always end with slash.

        Returns:
        base path for application main context
        Throws:
        java.lang.NullPointerException - for guicey test
      • basePathAdmin

        public java.lang.String basePathAdmin()
        For example, with the default configuration it would be "http://localhost:8081/". For "simple" server it would be "http://localhost:8080/adminPath/".

        Returned path will always end with slash.

        Returns:
        base path for admin context
        Throws:
        java.lang.NullPointerException - for guicey test
      • basePathRest

        public java.lang.String basePathRest()
        For example, with the default configuration it would be "http://localhost:8080/". If "server.rootPath" would be changed to "/someth" then method will return "http://localhost:8080/someth/". If main context mapping changed from root, then returned path will count it too (e.g. "http://localhost:8080/root/rest/", when "server.applicationContextPath" is "/root").

        Returned path will always end with slash.

        Returns:
        base path for rest
        Throws:
        java.lang.NullPointerException - for guicey test
      • target

        public javax.ws.rs.client.WebTarget target​(java.lang.String... paths)
        Unbounded (universal) WebTarget construction shortcut. First url part must contain host (port) target. 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("http://localhotst:8080/smth/").request().buildGet().invoke()

        NOTE: safe to use with guicey-only tests (when web part not started) to call any external url.

        Parameters:
        paths - one or more path parts (joined with '/')
        Returns:
        jersey web target object
      • targetMain

        public javax.ws.rs.client.WebTarget targetMain​(java.lang.String... paths)
        Shortcut for WebTarget creation for main context path. Method abstracts you from actual configuration so you can just call servlets by their registration uri.

        Without parameters it will target main context root: .targetMain().request().buildGet().invoke() would call "http://localhost:8080/".

        Additional paths may be provided to construct urls: .targetMain("something").request().buildGet().invoke() would call "http://localhost:8080/something" and .targetMain("foo", "bar").request().buildGet().invoke() would call "http://localhost:8080/foo/bar". Last example is equivalent to jersey api (kind of shortcut): .targetMain().path("foo").path("bar").request().buildGet().invoke().

        Parameters:
        paths - zero, one or more path parts (joined with '/') and appended to base path
        Returns:
        jersey web target object for main context
        Throws:
        java.lang.NullPointerException - for guicey test
        See Also:
        for base use construction details
      • targetAdmin

        public javax.ws.rs.client.WebTarget targetAdmin​(java.lang.String... paths)
        Shortcut for WebTarget creation for admin context path. Method abstracts you from actual configuration so you can just call servlets by their registration uri.

        Without parameters it will target admin context root: .targetAdmin().request().buildGet().invoke() would call "http://localhost:8081/". For simple server it would be "http://localhost:8080/admin/".

        Additional paths may be provided to construct urls: .targetAdmin("something").request().buildGet().invoke() would call "http://localhost:8081/something" and .targetAdmin("foo", "bar").request().buildGet().invoke() would call "http://localhost:8081/foo/bar". Last example is equivalent to jersey api (kind of shortcut): .targetAdmin().path("foo").path("bar").request().buildGet().invoke().

        Parameters:
        paths - zero, one or more path parts (joined with '/') and appended to base path
        Returns:
        jersey web target object for admin context
        Throws:
        java.lang.NullPointerException - for guicey test
        See Also:
        for base use construction details
      • targetRest

        public javax.ws.rs.client.WebTarget targetRest​(java.lang.String... paths)
        Shortcut for WebTarget creation for rest context path. Method abstracts you from actual configuration so you can just call rest resources by their registration uri.

        Without parameters it will target rest context root: .targetRest().request().buildGet().invoke() would call "http://localhost:8080/".

        Additional paths may be provided to construct urls: .targetRest("something").request().buildGet().invoke() would call "http://localhost:8080/something" and .targetRest("foo", "bar").request().buildGet().invoke() would call "http://localhost:8080/foo/bar". Last example is equivalent to jersey api (kind of shortcut): .targetRest().path("foo").path("bar").request().buildGet().invoke().

        Parameters:
        paths - zero, one or more path parts (joined with '/') and appended to base path
        Returns:
        jersey web target object for rest context
        Throws:
        java.lang.NullPointerException - for guicey test
        See Also:
        for base use construction details
      • get

        public <T> T get​(java.lang.String rootPath,
                         @Nullable
                         java.lang.Class<T> result)
        Simple GET call shortcut for server root. The path must include all required contexts: main context path and rest mapping (if it's a rest call). For example, if rest mapped to "rest/*" path then path parameter must include it like: "rest/my/api/smth".

        This method is very basic and could be used in the simplest cases. For other cases, it could be used as an example of api usage.

        Type Parameters:
        T - result type
        Parameters:
        rootPath - target path, relative to server root (everything after port)
        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
                          java.lang.Object body,
                          @Nullable
                          java.lang.Class<T> result)
        Simple POST call shortcut for server root. The path must include all required contexts: main context path and rest mapping (if it's a rest call). For example, if rest mapped to "rest/*" path then path parameter must include it like: "rest/my/api/smth".

        This method is very basic and could be used in the simplest cases. For other cases, it could be used as an example of api usage.

        Type Parameters:
        T - result type
        Parameters:
        rootPath - target path, relative to server root (everything after port)
        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)
      • put

        public <T> T put​(java.lang.String rootPath,
                         java.lang.Object body,
                         @Nullable
                         java.lang.Class<T> result)
        Simple PUT call shortcut for server root. The path must include all required contexts: main context path and rest mapping (if it's a rest call). For example, if rest mapped to "rest/*" path then path parameter must include it like: "rest/my/api/smth".

        This method is very basic and could be used in the simplest cases. For other cases, it could be used as an example of api usage.

        Type Parameters:
        T - result type
        Parameters:
        rootPath - target path, relative to server root (everything after port)
        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 rootPath,
                            @Nullable
                            java.lang.Class<T> result)
        Simple DELETE call shortcut for server root. The path must include all required contexts: main context path and rest mapping (if it's a rest call). For example, if rest mapped to "rest/*" path then path parameter must include it like: "rest/my/api/smth".

        This method is very basic and could be used in the simplest cases. For other cases, it could be used as an example of api usage.

        Type Parameters:
        T - result type
        Parameters:
        rootPath - target path, relative to server root (everything after port)
        result - result type (when null, accepts any 200 or 204 responses)
        Returns:
        mapped result object or null (if class not declared)
      • close

        public void close()
                   throws java.lang.Exception
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.lang.Exception
      • checkVoidResponse

        public void checkVoidResponse​(java.util.function.Supplier<javax.ws.rs.core.Response> call)
        Validates response to be 200 or 204 (no content). If not, throw exception with response body.

        Method is public to allow using it in custom calls.

        Parameters:
        call - supplier providing response