Search in sources :

Example 11 with HttpResponseCallback

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

the class ConfigurationManagerUnitTest method isFetchingConfiguration_isTrueWhenFetchingConfiguration.

@Test
public void isFetchingConfiguration_isTrueWhenFetchingConfiguration() throws InterruptedException {
    when(mBraintreeFragment.getHttpClient()).thenReturn(new BraintreeHttpClient(mTokenizationKey) {

        @Override
        public void get(String path, HttpResponseCallback callback) {
            mThreadPool.submit(new Runnable() {

                @Override
                public void run() {
                    SystemClock.sleep(1000);
                }
            });
        }
    });
    ConfigurationManager.getConfiguration(mBraintreeFragment, new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
        }
    }, new BraintreeResponseListener<Exception>() {

        @Override
        public void onResponse(Exception e) {
        }
    });
    assertTrue(ConfigurationManager.isFetchingConfiguration());
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) 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 12 with HttpResponseCallback

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

the class ConfigurationManagerUnitTest method stubConfigurationFromGateway.

private void stubConfigurationFromGateway(final String responseString) {
    BraintreeHttpClient fakeClient = new BraintreeHttpClient(mBraintreeFragment.getAuthorization()) {

        @Override
        public void get(String path, HttpResponseCallback callback) {
            if (path.contains(mBraintreeFragment.getAuthorization().getConfigUrl())) {
                callback.success(responseString);
            }
        }
    };
    when(mBraintreeFragment.getHttpClient()).thenReturn(fakeClient);
}
Also used : HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) BraintreeHttpClient(com.braintreepayments.api.internal.BraintreeHttpClient)

Example 13 with HttpResponseCallback

use of com.braintreepayments.api.interfaces.HttpResponseCallback 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 14 with HttpResponseCallback

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

the class BraintreeApiHttpClientTest method getRequestBadCertificateCheck.

@Test(timeout = 5000)
public void getRequestBadCertificateCheck() throws InterruptedException {
    if (!BuildConfig.RUN_ALL_TESTS) {
        return;
    }
    final CountDownLatch latch = new CountDownLatch(1);
    BraintreeApiHttpClient client = new BraintreeApiHttpClient("https://" + EnvironmentHelper.getLocalhostIp() + ":9443", null);
    client.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());
            latch.countDown();
        }
    });
    latch.await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) IOException(java.io.IOException) JSONException(org.json.JSONException) Test(org.junit.Test)

Example 15 with HttpResponseCallback

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

the class BraintreeApiHttpClientTest method throwsBraintreeApiErrorResponseExceptionOn400.

@Test(timeout = 5000)
public void throwsBraintreeApiErrorResponseExceptionOn400() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    final Class expectedException = BraintreeApiErrorResponse.class;
    final String expectedMessage = "The provided parameters are invalid; see details for field-specific error messages.";
    BraintreeApiHttpClient client = new BraintreeApiHttpClient(null, null);
    client = (BraintreeApiHttpClient) stubResponse(client, 400, stringFromFixture("errors/braintree_api_error_response.json"));
    client.get("/", new HttpResponseCallback() {

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

        @Override
        public void failure(Exception exception) {
            assertEquals(expectedException, exception.getClass());
            assertEquals(expectedMessage, exception.getMessage());
            countDownLatch.countDown();
        }
    });
    client.post("/", "{}", new HttpResponseCallback() {

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

        @Override
        public void failure(Exception exception) {
            assertEquals(expectedException, exception.getClass());
            assertEquals(expectedMessage, exception.getMessage());
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
}
Also used : BraintreeApiErrorResponse(com.braintreepayments.api.exceptions.BraintreeApiErrorResponse) CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) IOException(java.io.IOException) JSONException(org.json.JSONException) Test(org.junit.Test)

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