Search in sources :

Example 6 with ConfigurationListener

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

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

the class ConfigurationManagerUnitTest method getConfiguration_writesConfigToDiskWithValidTimestampAfterFetch.

@Test(timeout = 1000)
public void getConfiguration_writesConfigToDiskWithValidTimestampAfterFetch() throws InterruptedException {
    stubConfigurationFromGateway(stringFromFixture("configuration/configuration.json"));
    ConfigurationManager.getConfiguration(mBraintreeFragment, new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            String key = Base64.encodeToString(Uri.parse(mTokenizationKey.getConfigUrl()).buildUpon().appendQueryParameter("configVersion", "3").build().toString().concat(mTokenizationKey.getBearer()).getBytes(), 0);
            assertEquals(stringFromFixture("configuration/configuration.json"), getSharedPreferences(RuntimeEnvironment.application).getString(key, ""));
            assertTrue(System.currentTimeMillis() - getSharedPreferences(RuntimeEnvironment.application).getLong(key + "_timestamp", 0) < 1000);
            mCountDownLatch.countDown();
        }
    }, new BraintreeResponseListener<Exception>() {

        @Override
        public void onResponse(Exception e) {
            fail(e.getMessage());
        }
    });
    mCountDownLatch.await();
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) SharedPreferencesHelper.writeMockConfiguration(com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration) Configuration(com.braintreepayments.api.models.Configuration) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) Test(org.junit.Test)

Example 8 with ConfigurationListener

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

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

the class ConfigurationManagerUnitTest method getConfiguration_getsConfigFromCacheWhenTimeoutHasNotExpired.

@Test(timeout = 1000)
public void getConfiguration_getsConfigFromCacheWhenTimeoutHasNotExpired() throws InterruptedException {
    writeMockConfiguration(RuntimeEnvironment.application, mTokenizationKey.getConfigUrl(), mTokenizationKey.getBearer(), stringFromFixture("configuration/configuration.json"), System.currentTimeMillis());
    ConfigurationManager.getConfiguration(mBraintreeFragment, new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            assertEquals(stringFromFixture("configuration/configuration.json"), configuration.toJson());
            mCountDownLatch.countDown();
        }
    }, new BraintreeResponseListener<Exception>() {

        @Override
        public void onResponse(Exception e) {
            fail(e.getMessage());
        }
    });
    mCountDownLatch.await();
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) SharedPreferencesHelper.writeMockConfiguration(com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration) Configuration(com.braintreepayments.api.models.Configuration) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) Test(org.junit.Test)

Example 10 with ConfigurationListener

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

the class BraintreeFragmentTest method fetchConfiguration_worksWithAClientToken.

@Test(timeout = 10000)
public void fetchConfiguration_worksWithAClientToken() throws InterruptedException {
    final BraintreeFragment fragment = getFragmentWithAuthorization(mActivity, mClientToken);
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            assertNotNull(configuration);
            mCountDownLatch.countDown();
        }
    });
    mCountDownLatch.await();
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) Test(org.junit.Test)

Aggregations

ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)37 Configuration (com.braintreepayments.api.models.Configuration)36 Test (org.junit.Test)21 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)15 JSONException (org.json.JSONException)12 UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)10 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)10 SharedPreferencesHelper.writeMockConfiguration (com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration)10 ConfigurationException (com.braintreepayments.api.exceptions.ConfigurationException)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 BraintreeFragmentTestUtils.getFragmentWithConfiguration (com.braintreepayments.api.BraintreeFragmentTestUtils.getFragmentWithConfiguration)4 BraintreeFragmentTestUtils.getMockFragmentWithConfiguration (com.braintreepayments.api.BraintreeFragmentTestUtils.getMockFragmentWithConfiguration)4 BraintreeException (com.braintreepayments.api.exceptions.BraintreeException)4 BraintreeHttpClient (com.braintreepayments.api.internal.BraintreeHttpClient)4 JSONObject (org.json.JSONObject)4 Bundle (android.os.Bundle)3 Intent (android.content.Intent)2 BraintreeResponseListener (com.braintreepayments.api.interfaces.BraintreeResponseListener)2 UnionPayConfiguration (com.braintreepayments.api.models.UnionPayConfiguration)2 Uri (android.net.Uri)1