Search in sources :

Example 16 with HttpResponseCallback

use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.

the class BraintreeApiHttpClientTest method getRequestSslCertificateSuccessfulInSandbox.

@Test(timeout = 5000)
public void getRequestSslCertificateSuccessfulInSandbox() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    BraintreeApiHttpClient client = new BraintreeApiHttpClient("https://payments.sandbox.braintree-api.com", null);
    client.get("/ping", new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            try {
                assertEquals("OK", new JSONObject(responseBody).getJSONObject("data").getString("status"));
            } catch (JSONException e) {
                fail("Response invalid");
            }
            latch.countDown();
        }

        @Override
        public void failure(Exception exception) {
            fail("Request failed");
        }
    });
    latch.await();
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) IOException(java.io.IOException) JSONException(org.json.JSONException) Test(org.junit.Test)

Example 17 with HttpResponseCallback

use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.

the class BraintreeGraphQLHttpClientTest method getRequestSslCertificateSuccessfulInProduction.

@Test(timeout = 5000)
public void getRequestSslCertificateSuccessfulInProduction() throws InterruptedException, InvalidArgumentException {
    BraintreeGraphQLHttpClient httpClient = new BraintreeGraphQLHttpClient("https://payments.braintree-api.com/graphql", TOKENIZATION_KEY);
    httpClient.get("/", new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            mCountDownLatch.countDown();
        }

        @Override
        public void failure(Exception exception) {
            assertFalse(exception instanceof SSLException);
            mCountDownLatch.countDown();
        }
    });
    mCountDownLatch.await();
}
Also used : HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) SSLException(javax.net.ssl.SSLException) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) IOException(java.io.IOException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) SSLException(javax.net.ssl.SSLException) Test(org.junit.Test)

Example 18 with HttpResponseCallback

use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.

the class BraintreeHttpClientTest method getRequestBadCertificateCheck.

@Test(timeout = 5000)
public void getRequestBadCertificateCheck() throws InterruptedException, InvalidArgumentException {
    if (!BuildConfig.RUN_ALL_TESTS) {
        return;
    }
    BraintreeHttpClient httpClient = new BraintreeHttpClient(TokenizationKey.fromString(TOKENIZATION_KEY));
    httpClient.setBaseUrl("https://" + EnvironmentHelper.getLocalhostIp() + ":9443");
    httpClient.get("/", new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            fail("Request was successful");
        }

        @Override
        public void failure(Exception exception) {
            assertEquals("java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.", exception.getMessage());
            mCountDownLatch.countDown();
        }
    });
    mCountDownLatch.await();
}
Also used : HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) JSONException(org.json.JSONException) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) Test(org.junit.Test)

Example 19 with HttpResponseCallback

use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.

the class BraintreeHttpClientTest method assertExceptionIsPosted.

private void assertExceptionIsPosted(BraintreeHttpClient httpClient, final Class<? extends Exception> exceptionType, final String exceptionMessage) throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    httpClient.get("/", new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            fail("Request was successful");
        }

        @Override
        public void failure(Exception exception) {
            assertEquals(exceptionType, exception.getClass());
            if (exceptionMessage != null) {
                assertEquals(exceptionMessage, exception.getMessage());
            }
            countDownLatch.countDown();
        }
    });
    httpClient.post("/", "{}", new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            fail("Request was successful");
        }

        @Override
        public void failure(Exception exception) {
            assertEquals(exceptionType, exception.getClass());
            if (exceptionMessage != null) {
                assertEquals(exceptionMessage, exception.getMessage());
            }
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) JSONException(org.json.JSONException) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException)

Example 20 with HttpResponseCallback

use of com.braintreepayments.api.interfaces.HttpResponseCallback in project braintree_android by braintree.

the class Ideal method fetchIssuingBanks.

/**
 * Makes a call to fetch the list of potential issuing banks with which a customer can pay.
 *
 * @param fragment {@link BraintreeFragment}
 * @param listener {@link BraintreeResponseListener} the callback to which a list of issuing banks will be provided.
 */
public static void fetchIssuingBanks(final BraintreeFragment fragment, final BraintreeResponseListener<List<IdealBank>> listener) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(final Configuration configuration) {
            Exception configException = checkIdealEnabled(configuration);
            if (configException != null) {
                fragment.postCallback(configException);
                return;
            }
            fragment.getBraintreeApiHttpClient().get("/issuers/ideal", new HttpResponseCallback() {

                @Override
                public void success(String responseBody) {
                    fragment.sendAnalyticsEvent("ideal.load.succeeded");
                    try {
                        List<IdealBank> banks = IdealBank.fromJson(configuration, responseBody);
                        if (listener != null) {
                            listener.onResponse(banks);
                        }
                    } catch (JSONException jsonException) {
                        failure(jsonException);
                    }
                }

                @Override
                public void failure(Exception exception) {
                    fragment.sendAnalyticsEvent("ideal.load.failed");
                    fragment.postCallback(exception);
                }
            });
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) IdealBank(com.braintreepayments.api.models.IdealBank) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException)

Aggregations

HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)54 Test (org.junit.Test)36 JSONException (org.json.JSONException)26 IOException (java.io.IOException)22 JSONObject (org.json.JSONObject)22 InvocationOnMock (org.mockito.invocation.InvocationOnMock)20 Answer (org.mockito.stubbing.Answer)20 CountDownLatch (java.util.concurrent.CountDownLatch)18 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)16 AuthorizationException (com.braintreepayments.api.exceptions.AuthorizationException)14 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 Configuration (com.braintreepayments.api.models.Configuration)13 MalformedURLException (java.net.MalformedURLException)11 UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)10 ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)10 IdealBank (com.braintreepayments.api.models.IdealBank)9 Matchers.anyString (org.mockito.Matchers.anyString)8 List (java.util.List)7 SSLException (javax.net.ssl.SSLException)7