Search in sources :

Example 31 with HttpResponseCallback

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

the class IdealUnitTest method startPayment_startsBrowserWithProperRequestCode.

@Test
public void startPayment_startsBrowserWithProperRequestCode() throws InterruptedException, InvalidArgumentException {
    final CountDownLatch latch = new CountDownLatch(1);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[1];
            callback.success(stringFromFixture("payment_methods/ideal_issuing_banks.json"));
            return null;
        }
    }).when(mMockApiClient).get(eq("/issuers/ideal"), any(HttpResponseCallback.class));
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            String requestBody = (String) invocation.getArguments()[1];
            JSONObject json = new JSONObject(requestBody);
            assertEquals("some-route-id", json.getString("route_id"));
            String expectedRedirectUrl = Uri.parse("http://assets.example.com/mobile/ideal-redirect/0.0.0/index.html?redirect_url=" + mBraintreeFragment.getReturnUrlScheme() + "://").toString();
            assertEquals(expectedRedirectUrl, json.getString("redirect_url"));
            HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[2];
            callback.success(stringFromFixture("payment_methods/pending_ideal_bank_payment.json"));
            return null;
        }
    }).when(mMockApiClient).post(eq("/ideal-payments"), any(String.class), any(HttpResponseCallback.class));
    Ideal.fetchIssuingBanks(mBraintreeFragment, new BraintreeResponseListener<List<IdealBank>>() {

        @Override
        public void onResponse(List<IdealBank> idealBanks) {
            IdealBank bank = idealBanks.get(0);
            IdealRequest builder = new IdealRequest().issuerId(bank.getId()).amount("1.00").currency("EUR").orderId("abc-123");
            Ideal.startPayment(mBraintreeFragment, builder, null);
            verify(mBraintreeFragment).browserSwitch(eq(BraintreeRequestCodes.IDEAL), eq("http://approval.example.com/"));
            latch.countDown();
        }
    });
    latch.await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) JSONObject(org.json.JSONObject) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IdealRequest(com.braintreepayments.api.models.IdealRequest) IdealBank(com.braintreepayments.api.models.IdealBank) JSONObject(org.json.JSONObject) List(java.util.List) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 32 with HttpResponseCallback

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

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

the class MockFragmentBuilder method setupGraphQLErrorResponses.

private void setupGraphQLErrorResponses(BraintreeGraphQLHttpClient graphQLHttpClient) {
    Answer answer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((HttpResponseCallback) invocation.getArguments()[1]).failure(mGraphQLErrorResponse);
            return null;
        }
    };
    doAnswer(answer).when(graphQLHttpClient).get(any(String.class), any(HttpResponseCallback.class));
    doAnswer(answer).when(graphQLHttpClient).post(anyString(), anyString(), any(HttpResponseCallback.class));
    doAnswer(answer).when(graphQLHttpClient).post(anyString(), any(HttpResponseCallback.class));
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback)

Example 34 with HttpResponseCallback

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

the class MockFragmentBuilder method setupGraphQLSuccessResponses.

private void setupGraphQLSuccessResponses(BraintreeGraphQLHttpClient graphQLHttpClient) {
    Answer answer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((HttpResponseCallback) invocation.getArguments()[1]).success(mGraphQLSuccessResponse);
            return null;
        }
    };
    doAnswer(answer).when(graphQLHttpClient).get(any(String.class), any(HttpResponseCallback.class));
    doAnswer(answer).when(graphQLHttpClient).post(anyString(), anyString(), any(HttpResponseCallback.class));
    doAnswer(answer).when(graphQLHttpClient).post(anyString(), any(HttpResponseCallback.class));
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback)

Example 35 with HttpResponseCallback

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

the class MockFragmentBuilder method setupErrorResponses.

private void setupErrorResponses(BraintreeHttpClient httpClient) {
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((HttpResponseCallback) invocation.getArguments()[1]).failure(mErrorResponse);
            return null;
        }
    }).when(httpClient).get(any(String.class), any(HttpResponseCallback.class));
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((HttpResponseCallback) invocation.getArguments()[2]).failure(mErrorResponse);
            return null;
        }
    }).when(httpClient).post(anyString(), anyString(), any(HttpResponseCallback.class));
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback)

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