Search in sources :

Example 21 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class ClientsEntityTest method shouldUpdateClient.

@Test
public void shouldUpdateClient() throws Exception {
    Request<Client> request = api.clients().update("1", new Client("My Application"));
    assertThat(request, is(notNullValue()));
    server.jsonResponse(MGMT_CLIENT, 200);
    Client response = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest, hasMethodAndPath("PATCH", "/api/v2/clients/1"));
    assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
    assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
    Map<String, Object> body = bodyFromRequest(recordedRequest);
    assertThat(body.size(), is(1));
    assertThat(body, hasEntry("name", "My Application"));
    assertThat(response, is(notNullValue()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Client(com.auth0.json.mgmt.client.Client) Test(org.junit.Test)

Example 22 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class UsersEntity method delete.

/**
 * Delete an existing User.
 * A token with scope delete:users is needed.
 * See https://auth0.com/docs/api/management/v2#!/Users/delete_users_by_id
 *
 * @param userId the user id
 * @return a Request to execute.
 */
public Request<Void> delete(String userId) {
    Asserts.assertNotNull(userId, "user id");
    String url = baseUrl.newBuilder().addPathSegments("api/v2/users").addPathSegment(userId).build().toString();
    VoidRequest request = new VoidRequest(client, url, "DELETE");
    request.addHeader("Authorization", "Bearer " + apiToken);
    return request;
}
Also used : VoidRequest(com.auth0.net.VoidRequest)

Example 23 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class BaseRequestTest method alwaysCloseResponseOnAuth0Exception.

@Test
public void alwaysCloseResponseOnAuth0Exception() {
    Exception exception = null;
    try {
        new MockBaseRequest<String>(client) {

            @Override
            protected String parseResponse(Response response) throws Auth0Exception {
                throw new Auth0Exception("Auth0Exception");
            }
        }.execute();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(exception, is(notNullValue()));
    assertThat(exception, is(instanceOf(Auth0Exception.class)));
    assertThat(exception.getMessage(), is("Auth0Exception"));
    verify(response, times(1)).close();
}
Also used : Auth0Exception(com.auth0.exception.Auth0Exception) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) Test(org.junit.Test)

Example 24 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class BaseRequestTest method alwaysCloseResponseOnRateLimitException.

@Test
public void alwaysCloseResponseOnRateLimitException() {
    Exception exception = null;
    try {
        new MockBaseRequest<String>(client) {

            @Override
            protected String parseResponse(Response response) throws Auth0Exception {
                throw new RateLimitException(-1, -1, -1);
            }
        }.execute();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(exception, is(notNullValue()));
    assertThat(exception, is(instanceOf(RateLimitException.class)));
    assertThat(exception.getMessage(), is("Request failed with status code 429: Rate limit reached"));
    verify(response, times(1)).close();
}
Also used : RateLimitException(com.auth0.exception.RateLimitException) Auth0Exception(com.auth0.exception.Auth0Exception) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) Test(org.junit.Test)

Example 25 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class BaseRequestTest method asyncCompletesWithExceptionWhenResponseParsingFails.

@Test
public void asyncCompletesWithExceptionWhenResponseParsingFails() throws Exception {
    doReturn(call).when(client).newCall(any());
    doAnswer(invocation -> {
        ((Callback) invocation.getArgument(0)).onResponse(call, response);
        return null;
    }).when(call).enqueue(any());
    CompletableFuture<?> request = new MockBaseRequest<String>(client) {

        @Override
        protected String parseResponse(Response response) throws Auth0Exception {
            throw new Auth0Exception("Response Parsing Error");
        }
    }.executeAsync();
    Exception exception = null;
    Object result = null;
    try {
        result = request.get();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(result, is(nullValue()));
    assertThat(exception, is(notNullValue()));
    assertThat(exception.getCause(), is(instanceOf(Auth0Exception.class)));
    assertThat(exception.getCause().getMessage(), is("Response Parsing Error"));
}
Also used : Auth0Exception(com.auth0.exception.Auth0Exception) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)36 APIException (com.auth0.exception.APIException)27 Auth0Exception (com.auth0.exception.Auth0Exception)27 RateLimitException (com.auth0.exception.RateLimitException)27 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)26 List (java.util.List)25 Test (org.junit.Test)25 VoidRequest (com.auth0.net.VoidRequest)24 TokenHolder (com.auth0.json.auth.TokenHolder)22 JsonParseException (com.fasterxml.jackson.core.JsonParseException)19 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)19 ExpectedException (org.junit.rules.ExpectedException)19 RecordedMultipartRequest (com.auth0.net.multipart.RecordedMultipartRequest)16 Test (org.junit.jupiter.api.Test)14 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)14 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)13 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)9 AuthAPI (com.auth0.client.auth.AuthAPI)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 OkHttpClient (okhttp3.OkHttpClient)7