diff --git a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java index 2a8f4dff2..d2ad4355a 100644 --- a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java +++ b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java @@ -67,6 +67,17 @@ public static ManagementAPI.Builder newBuilder(String domain, String apiToken) { return new ManagementAPI.Builder(domain, apiToken); } + /** + * Instantiate a new {@link Builder} to configure and build a new ManagementAPI client. + * + * @param domain the tenant's domain. Must be a non-null valid HTTPS domain. + * @param tokenProvider the API Token provider to use when making requests. + * @return a Builder for further configuration. + */ + public static ManagementAPI.Builder newBuilder(String domain, TokenProvider tokenProvider) { + return new ManagementAPI.Builder(domain, tokenProvider); + } + private ManagementAPI(String domain, TokenProvider tokenProvider, Auth0HttpClient httpClient) { Asserts.assertNotNull(domain, "domain"); Asserts.assertNotNull(tokenProvider, "token provider"); @@ -409,7 +420,7 @@ public NetworkAclsEntity networkAcls() { */ public static class Builder { private final String domain; - private final String apiToken; + private final TokenProvider tokenProvider; private Auth0HttpClient httpClient = DefaultHttpClient.newBuilder().build(); /** @@ -418,8 +429,17 @@ public static class Builder { * @param apiToken the API token used to make requests to the Auth0 Management API. */ public Builder(String domain, String apiToken) { + this(domain, SimpleTokenProvider.create(apiToken)); + } + + /** + * Create a new Builder + * @param domain the domain of the tenant. + * @param tokenProvider the API Token provider to use when making requests. + */ + public Builder(String domain, TokenProvider tokenProvider) { this.domain = domain; - this.apiToken = apiToken; + this.tokenProvider = tokenProvider; } /** @@ -438,7 +458,7 @@ public Builder withHttpClient(Auth0HttpClient httpClient) { * @return the configured {@code ManagementAPI} instance. */ public ManagementAPI build() { - return new ManagementAPI(domain, SimpleTokenProvider.create(apiToken), httpClient); + return new ManagementAPI(domain, tokenProvider, httpClient); } } } diff --git a/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java b/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java index 45712d66c..419f8511a 100644 --- a/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java +++ b/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java @@ -54,7 +54,7 @@ public void shouldCreateWithDomainAndToken() { } @Test - public void shouldCreateWithHttpClient() { + public void shouldCreateWithHttpClientWithApiToken() { Auth0HttpClient httpClient = new Auth0HttpClient() { @Override public Auth0HttpResponse sendRequest(Auth0HttpRequest request) { @@ -69,6 +69,31 @@ public CompletableFuture sendRequestAsync(Auth0HttpRequest re ManagementAPI api = ManagementAPI.newBuilder(DOMAIN, API_TOKEN) .withHttpClient(httpClient).build(); + + assertThat(api, is(notNullValue())); + assertThat(api.getHttpClient(), is(httpClient)); + } + + @Test + public void shouldCreateWithHttpClientWithTokenProvider() { + Auth0HttpClient httpClient = new Auth0HttpClient() { + @Override + public Auth0HttpResponse sendRequest(Auth0HttpRequest request) { + return null; + } + + @Override + public CompletableFuture sendRequestAsync(Auth0HttpRequest request) { + return null; + } + }; + + ManagementAPI api = ManagementAPI.newBuilder( + DOMAIN, + SimpleTokenProvider.create(API_TOKEN) + ) + .withHttpClient(httpClient) + .build(); assertThat(api, is(notNullValue())); assertThat(api.getHttpClient(), is(httpClient)); } @@ -98,10 +123,17 @@ public void shouldThrowWhenDomainIsNull() { @Test public void shouldThrowWhenApiTokenIsNull() { verifyThrows(IllegalArgumentException.class, - () -> ManagementAPI.newBuilder(DOMAIN, null).build(), + () -> ManagementAPI.newBuilder(DOMAIN, (String) null).build(), "'api token' cannot be null!"); } + @Test + public void shouldThrowWhenTokenProviderIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> ManagementAPI.newBuilder(DOMAIN, (TokenProvider) null).build(), + "'token provider' cannot be null!"); + } + @Test public void shouldThrowOnUpdateWhenApiTokenIsNull() { ManagementAPI api = ManagementAPI.newBuilder(DOMAIN, API_TOKEN).build();