Search in sources :

Example 6 with BraintreeHttpClient

use of com.braintreepayments.api.internal.BraintreeHttpClient 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 7 with BraintreeHttpClient

use of com.braintreepayments.api.internal.BraintreeHttpClient in project braintree_android by braintree.

the class BraintreeFragment method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    if (mContext == null) {
        mContext = getActivity().getApplicationContext();
    }
    mNewActivityNeedsConfiguration = false;
    mCrashReporter = CrashReporter.setup(this);
    mSessionId = getArguments().getString(EXTRA_SESSION_ID);
    mIntegrationType = getArguments().getString(EXTRA_INTEGRATION_TYPE);
    mAuthorization = getArguments().getParcelable(EXTRA_AUTHORIZATION_TOKEN);
    mAnalyticsDatabase = AnalyticsDatabase.getInstance(getApplicationContext());
    if (mHttpClient == null) {
        mHttpClient = new BraintreeHttpClient(mAuthorization);
    }
    if (savedInstanceState != null) {
        List<PaymentMethodNonce> paymentMethodNonces = savedInstanceState.getParcelableArrayList(EXTRA_CACHED_PAYMENT_METHOD_NONCES);
        if (paymentMethodNonces != null) {
            mCachedPaymentMethodNonces.addAll(paymentMethodNonces);
        }
        mHasFetchedPaymentMethodNonces = savedInstanceState.getBoolean(EXTRA_FETCHED_PAYMENT_METHOD_NONCES);
        try {
            setConfiguration(Configuration.fromJson(savedInstanceState.getString(EXTRA_CONFIGURATION)));
        } catch (JSONException ignored) {
        }
    } else {
        if (mAuthorization instanceof TokenizationKey) {
            sendAnalyticsEvent("started.client-key");
        } else {
            sendAnalyticsEvent("started.client-token");
        }
    }
    fetchConfiguration();
}
Also used : TokenizationKey(com.braintreepayments.api.models.TokenizationKey) JSONException(org.json.JSONException) PaymentMethodNonce(com.braintreepayments.api.models.PaymentMethodNonce) BraintreeHttpClient(com.braintreepayments.api.internal.BraintreeHttpClient)

Example 8 with BraintreeHttpClient

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

use of com.braintreepayments.api.internal.BraintreeHttpClient in project braintree_android by braintree.

the class MockFragmentBuilder method build.

public BraintreeFragment build() {
    BraintreeFragment fragment = mock(BraintreeFragment.class);
    when(fragment.getApplicationContext()).thenReturn(mContext);
    when(fragment.getAuthorization()).thenReturn(mAuthorization);
    when(fragment.getSessionId()).thenReturn(mSessionId);
    when(fragment.getReturnUrlScheme()).thenReturn("com.braintreepayments.api.braintree");
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((ConfigurationListener) invocation.getArguments()[0]).onConfigurationFetched(mConfiguration);
            return null;
        }
    }).when(fragment).waitForConfiguration(any(ConfigurationListener.class));
    when(fragment.getConfiguration()).thenReturn(mConfiguration);
    BraintreeHttpClient httpClient = mock(BraintreeHttpClient.class);
    if (mSuccessResponse != null) {
        setupSuccessResponses(httpClient);
    } else if (mErrorResponse != null) {
        setupErrorResponses(httpClient);
    }
    when(fragment.getHttpClient()).thenReturn(httpClient);
    BraintreeGraphQLHttpClient graphQLHttpClient = mock(BraintreeGraphQLHttpClient.class);
    if (mGraphQLSuccessResponse != null) {
        setupGraphQLSuccessResponses(graphQLHttpClient);
    } else if (mGraphQLErrorResponse != null) {
        setupGraphQLErrorResponses(graphQLHttpClient);
    }
    when(fragment.getGraphQLHttpClient()).thenReturn(graphQLHttpClient);
    return fragment;
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) BraintreeGraphQLHttpClient(com.braintreepayments.api.internal.BraintreeGraphQLHttpClient) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BraintreeHttpClient(com.braintreepayments.api.internal.BraintreeHttpClient)

Example 10 with BraintreeHttpClient

use of com.braintreepayments.api.internal.BraintreeHttpClient in project braintree_android by braintree.

the class UnionPayUnitTest method enroll_sendsPayloadToEndpoint.

@Test
public void enroll_sendsPayloadToEndpoint() throws JSONException {
    UnionPayCardBuilder unionPayCardBuilder = new UnionPayCardBuilder().cardNumber("someCardNumber").expirationMonth("expirationMonth").expirationYear("expirationYear").mobileCountryCode("mobileCountryCode").mobilePhoneNumber("mobilePhoneNumber");
    BraintreeHttpClient httpClient = mock(BraintreeHttpClient.class);
    doNothing().when(httpClient).get(anyString(), any(HttpResponseCallback.class));
    when(mBraintreeFragment.getHttpClient()).thenReturn(httpClient);
    ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
    UnionPay.enroll(mBraintreeFragment, unionPayCardBuilder);
    verify(httpClient).post(eq("/v1/union_pay_enrollments"), argumentCaptor.capture(), any(HttpResponseCallback.class));
    JSONObject enrollPayload = new JSONObject(argumentCaptor.getValue());
    JSONObject unionPayEnrollment = enrollPayload.getJSONObject("unionPayEnrollment");
    assertEquals("someCardNumber", unionPayEnrollment.getString("number"));
    assertEquals("expirationMonth", unionPayEnrollment.getString("expirationMonth"));
    assertEquals("expirationYear", unionPayEnrollment.getString("expirationYear"));
    assertEquals("mobileCountryCode", unionPayEnrollment.getString("mobileCountryCode"));
    assertEquals("mobilePhoneNumber", unionPayEnrollment.getString("mobileNumber"));
}
Also used : JSONObject(org.json.JSONObject) UnionPayCardBuilder(com.braintreepayments.api.models.UnionPayCardBuilder) Matchers.anyString(org.mockito.Matchers.anyString) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) BraintreeHttpClient(com.braintreepayments.api.internal.BraintreeHttpClient) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

BraintreeHttpClient (com.braintreepayments.api.internal.BraintreeHttpClient)12 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)9 Test (org.junit.Test)8 Matchers.anyString (org.mockito.Matchers.anyString)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)4 UnionPayCardBuilder (com.braintreepayments.api.models.UnionPayCardBuilder)4 JSONObject (org.json.JSONObject)4 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)3 UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)3 Configuration (com.braintreepayments.api.models.Configuration)3 SharedPreferencesHelper.writeMockConfiguration (com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration)3 BraintreeGraphQLHttpClient (com.braintreepayments.api.internal.BraintreeGraphQLHttpClient)1 PaymentMethodNonce (com.braintreepayments.api.models.PaymentMethodNonce)1 TokenizationKey (com.braintreepayments.api.models.TokenizationKey)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 JSONException (org.json.JSONException)1 Before (org.junit.Before)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1