Search in sources :

Example 46 with HttpResponseCallback

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

the class BraintreeHttpClientTest method postsErrorWhenPathIsNull.

@Test(timeout = 1000)
public void postsErrorWhenPathIsNull() throws InterruptedException, InvalidArgumentException {
    BraintreeHttpClient httpClient = new BraintreeHttpClient(TokenizationKey.fromString(TOKENIZATION_KEY));
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    httpClient.get(null, new HttpResponseCallback() {

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

        @Override
        public void failure(Exception exception) {
            assertEquals(IllegalArgumentException.class, exception.getClass());
            assertEquals("Path cannot be null", exception.getMessage());
            countDownLatch.countDown();
        }
    });
    httpClient.post(null, "{}", new HttpResponseCallback() {

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

        @Override
        public void failure(Exception exception) {
            assertEquals(IllegalArgumentException.class, exception.getClass());
            assertEquals("Path cannot be null", 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) Test(org.junit.Test)

Example 47 with HttpResponseCallback

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

the class HttpClientTest method assertExceptionIsPosted.

/* helpers */
private void assertExceptionIsPosted(HttpClient httpClient, final Class<? extends Exception> exceptionType, final String exceptionMessage) throws IOException, InterruptedException {
    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 : Matchers.anyString(org.mockito.Matchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) DownForMaintenanceException(com.braintreepayments.api.exceptions.DownForMaintenanceException) AuthenticationException(com.braintreepayments.api.exceptions.AuthenticationException) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) UnprocessableEntityException(com.braintreepayments.api.exceptions.UnprocessableEntityException) UpgradeRequiredException(com.braintreepayments.api.exceptions.UpgradeRequiredException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ServerException(com.braintreepayments.api.exceptions.ServerException) RateLimitException(com.braintreepayments.api.exceptions.RateLimitException) SSLException(javax.net.ssl.SSLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 48 with HttpResponseCallback

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

the class HttpClientTest method successCallbacksHappenOnMainThread.

@Test(timeout = 1000)
public void successCallbacksHappenOnMainThread() throws IOException, InterruptedException {
    HttpClient httpClient = clientWithExpectedResponse(200, "");
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    httpClient.get("/", new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            assertEquals(Looper.getMainLooper(), Looper.myLooper());
            countDownLatch.countDown();
        }

        @Override
        public void failure(Exception exception) {
            fail("Request failed");
        }
    });
    httpClient.post("/", "{}", new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            assertEquals(Looper.getMainLooper(), Looper.myLooper());
            countDownLatch.countDown();
        }

        @Override
        public void failure(Exception exception) {
            fail("Request failed");
        }
    });
    countDownLatch.await();
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) DownForMaintenanceException(com.braintreepayments.api.exceptions.DownForMaintenanceException) AuthenticationException(com.braintreepayments.api.exceptions.AuthenticationException) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) UnprocessableEntityException(com.braintreepayments.api.exceptions.UnprocessableEntityException) UpgradeRequiredException(com.braintreepayments.api.exceptions.UpgradeRequiredException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ServerException(com.braintreepayments.api.exceptions.ServerException) RateLimitException(com.braintreepayments.api.exceptions.RateLimitException) SSLException(javax.net.ssl.SSLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 49 with HttpResponseCallback

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

the class HttpClientTest method failureCallbacksHappenOnMainThread.

@Test(timeout = 1000)
public void failureCallbacksHappenOnMainThread() throws Exception {
    HttpClient httpClient = spy(new HttpClient());
    doThrow(new IOException()).when(httpClient).parseResponse(any(HttpsURLConnection.class));
    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(Looper.getMainLooper(), Looper.myLooper());
            countDownLatch.countDown();
        }
    });
    httpClient.post("/", "{}", new HttpResponseCallback() {

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

        @Override
        public void failure(Exception exception) {
            assertEquals(Looper.getMainLooper(), Looper.myLooper());
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
}
Also used : IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) DownForMaintenanceException(com.braintreepayments.api.exceptions.DownForMaintenanceException) AuthenticationException(com.braintreepayments.api.exceptions.AuthenticationException) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) UnprocessableEntityException(com.braintreepayments.api.exceptions.UnprocessableEntityException) UpgradeRequiredException(com.braintreepayments.api.exceptions.UpgradeRequiredException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ServerException(com.braintreepayments.api.exceptions.ServerException) RateLimitException(com.braintreepayments.api.exceptions.RateLimitException) SSLException(javax.net.ssl.SSLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 50 with HttpResponseCallback

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

the class PayPal method requestOneTimePayment.

private static void requestOneTimePayment(final BraintreeFragment fragment, final PayPalRequest paypalRequest, final boolean isBillingAgreement, final PayPalApprovalHandler handler) {
    final HttpResponseCallback callback = new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            final PayPalPaymentResource paypalPaymentResource;
            try {
                paypalPaymentResource = PayPalPaymentResource.fromJson(responseBody);
            } catch (JSONException e) {
                fragment.postCallback(e);
                return;
            }
            String redirectUrl = Uri.parse(paypalPaymentResource.getRedirectUrl()).buildUpon().appendQueryParameter(USER_ACTION_KEY, paypalRequest.getUserAction()).toString();
            Request request;
            if (isBillingAgreement) {
                request = getBillingAgreementRequest(fragment, redirectUrl);
            } else {
                request = getCheckoutRequest(fragment, redirectUrl);
            }
            startPayPal(fragment, request, handler);
        }

        @Override
        public void failure(Exception e) {
            fragment.postCallback(e);
        }
    };
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            if (!configuration.isPayPalEnabled()) {
                fragment.postCallback(new BraintreeException("PayPal is not enabled"));
                return;
            }
            if (!isManifestValid(fragment)) {
                fragment.sendAnalyticsEvent("paypal.invalid-manifest");
                fragment.postCallback(new BraintreeException("BraintreeBrowserSwitchActivity missing, " + "incorrectly configured in AndroidManifest.xml or another app defines the same browser " + "switch url as this app. See " + "https://developers.braintreepayments.com/guides/client-sdk/android/v2#browser-switch " + "for the correct configuration"));
                return;
            }
            try {
                persistPayPalRequest(fragment.getApplicationContext(), paypalRequest);
                createPaymentResource(fragment, paypalRequest, isBillingAgreement, callback);
            } catch (JSONException | ErrorWithResponse | BraintreeException ex) {
                fragment.postCallback(ex);
            }
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) PayPalPaymentResource(com.braintreepayments.api.models.PayPalPaymentResource) PayPalConfiguration(com.braintreepayments.api.models.PayPalConfiguration) Configuration(com.braintreepayments.api.models.Configuration) Request(com.paypal.android.sdk.onetouch.core.Request) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) BillingAgreementRequest(com.paypal.android.sdk.onetouch.core.BillingAgreementRequest) AuthorizationRequest(com.paypal.android.sdk.onetouch.core.AuthorizationRequest) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) PendingRequest(com.paypal.android.sdk.onetouch.core.sdk.PendingRequest) JSONException(org.json.JSONException) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) JSONException(org.json.JSONException) BrowserSwitchException(com.braintreepayments.api.exceptions.BrowserSwitchException)

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