Search in sources :

Example 1 with UnexpectedException

use of com.braintreepayments.api.exceptions.UnexpectedException in project braintree_android by braintree.

the class ConfigurationManagerUnitTest method isFetchingConfiguration_isFalseInErrorCallback.

@Test(timeout = 1000)
public void isFetchingConfiguration_isFalseInErrorCallback() throws InterruptedException {
    when(mBraintreeFragment.getHttpClient()).thenReturn(new BraintreeHttpClient(mTokenizationKey) {

        @Override
        public void get(String path, HttpResponseCallback callback) {
            if (path.contains(mTokenizationKey.getConfigUrl())) {
                callback.failure(new UnexpectedException("Something bad happened"));
            }
        }
    });
    ConfigurationManager.getConfiguration(mBraintreeFragment, new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            fail("Success listener should not have been called for bad request");
        }
    }, new BraintreeResponseListener<Exception>() {

        @Override
        public void onResponse(Exception e) {
            assertFalse(ConfigurationManager.isFetchingConfiguration());
            mCountDownLatch.countDown();
        }
    });
    mCountDownLatch.await();
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) SharedPreferencesHelper.writeMockConfiguration(com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration) Configuration(com.braintreepayments.api.models.Configuration) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) BraintreeHttpClient(com.braintreepayments.api.internal.BraintreeHttpClient) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) Test(org.junit.Test)

Example 2 with UnexpectedException

use of com.braintreepayments.api.exceptions.UnexpectedException in project braintree_android by braintree.

the class ConfigurationManagerUnitTest method getConfiguration_callsErrorListenerWhenHttpFails.

@Test(timeout = 1000)
public void getConfiguration_callsErrorListenerWhenHttpFails() throws InterruptedException {
    BraintreeHttpClient fakeClient = new BraintreeHttpClient(mTokenizationKey) {

        @Override
        public void get(String path, HttpResponseCallback callback) {
            if (path.contains(mTokenizationKey.getConfigUrl())) {
                callback.failure(new UnexpectedException("Something bad happened"));
            }
        }
    };
    when(mBraintreeFragment.getHttpClient()).thenReturn(fakeClient);
    ConfigurationManager.getConfiguration(mBraintreeFragment, new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            fail("Success listener should not have been called for bad request");
        }
    }, new BraintreeResponseListener<Exception>() {

        @Override
        public void onResponse(Exception e) {
            assertTrue(e instanceof UnexpectedException);
            assertEquals("Something bad happened", e.getMessage());
            mCountDownLatch.countDown();
        }
    });
    mCountDownLatch.await();
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) SharedPreferencesHelper.writeMockConfiguration(com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration) Configuration(com.braintreepayments.api.models.Configuration) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) BraintreeHttpClient(com.braintreepayments.api.internal.BraintreeHttpClient) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) Test(org.junit.Test)

Example 3 with UnexpectedException

use of com.braintreepayments.api.exceptions.UnexpectedException in project braintree_android by braintree.

the class BraintreeGraphQLHttpClientTest method parseResponseFailsWithCoercionError.

@Test
public void parseResponseFailsWithCoercionError() throws Exception {
    String baseUrl = "http://example.com/graphql";
    HttpClient httpClient = new BraintreeGraphQLHttpClient(baseUrl, TOKENIZATION_KEY);
    httpClient = HttpClientTestUtils.stubResponse(httpClient, 200, FixturesHelper.stringFromFixture("errors/graphql/coercion_error.json"));
    HttpURLConnection connection = httpClient.init(baseUrl);
    try {
        httpClient.parseResponse(connection);
        fail("No exception was thrown");
    } catch (UnexpectedException e) {
        assertEquals("An unexpected error occurred", e.getMessage());
    }
}
Also used : UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 4 with UnexpectedException

use of com.braintreepayments.api.exceptions.UnexpectedException in project braintree_android by braintree.

the class BraintreeGraphQLHttpClient method parseResponse.

@Override
protected String parseResponse(HttpURLConnection connection) throws Exception {
    String response = super.parseResponse(connection);
    JSONArray errors = new JSONObject(response).optJSONArray(ErrorWithResponse.GRAPHQL_ERRORS_KEY);
    if (errors != null) {
        for (int i = 0; i < errors.length(); i++) {
            JSONObject error = errors.getJSONObject(i);
            JSONObject extensions = error.optJSONObject("extensions");
            if (extensions == null) {
                throw new UnexpectedException("An unexpected error occurred");
            }
            if (Json.optString(extensions, "legacyCode", "").equals("50000")) {
                throw new AuthorizationException(error.getString("message"));
            } else if (!Json.optString(extensions, "errorType", "").equals("user_error")) {
                throw new UnexpectedException("An unexpected error occurred");
            }
        }
        throw ErrorWithResponse.fromGraphQLJson(response);
    }
    return response;
}
Also used : UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) JSONObject(org.json.JSONObject) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) JSONArray(org.json.JSONArray)

Aggregations

UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)4 Test (org.junit.Test)3 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)2 ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)2 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)2 BraintreeHttpClient (com.braintreepayments.api.internal.BraintreeHttpClient)2 Configuration (com.braintreepayments.api.models.Configuration)2 SharedPreferencesHelper.writeMockConfiguration (com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration)2 AuthorizationException (com.braintreepayments.api.exceptions.AuthorizationException)1 HttpURLConnection (java.net.HttpURLConnection)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1