Search in sources :

Example 6 with HttpResponseCallback

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

the class IdealUnitTest method fetchIssuingBanks_postsErrorToListenerOnJsonParsingError.

@Test
public void fetchIssuingBanks_postsErrorToListenerOnJsonParsingError() throws InvalidArgumentException {
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[1];
            callback.success("gibberish");
            return null;
        }
    }).when(mMockApiClient).get(eq("/issuers/ideal"), any(HttpResponseCallback.class));
    Ideal.fetchIssuingBanks(mBraintreeFragment, new BraintreeResponseListener<List<IdealBank>>() {

        @Override
        public void onResponse(List<IdealBank> idealBanks) {
            fail("Success listener called");
        }
    });
    verify(mBraintreeFragment).postCallback(any(JSONException.class));
}
Also used : Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IdealBank(com.braintreepayments.api.models.IdealBank) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) List(java.util.List) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with HttpResponseCallback

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

the class IdealUnitTest method startPayment_callsResponseListenerWithPaymentId.

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

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            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));
    List<IdealBank> banks = IdealBank.fromJson(mConfiguration, stringFromFixture("payment_methods/ideal_issuing_banks.json"));
    IdealRequest builder = new IdealRequest().issuerId(banks.get(0).getId()).amount("1.00").currency("EUR").orderId("abc-123");
    Ideal.startPayment(mBraintreeFragment, builder, new BraintreeResponseListener<IdealResult>() {

        @Override
        public void onResponse(IdealResult idealResult) {
            assertEquals("ideal_payment_id", idealResult.getId());
            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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IdealRequest(com.braintreepayments.api.models.IdealRequest) IdealBank(com.braintreepayments.api.models.IdealBank) JSONObject(org.json.JSONObject) IdealResult(com.braintreepayments.api.models.IdealResult) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with HttpResponseCallback

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

the class IdealUnitTest method fetchIssuingBanks_success_sendsAnalyticsEvent.

@Test
public void fetchIssuingBanks_success_sendsAnalyticsEvent() {
    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));
    Ideal.fetchIssuingBanks(mBraintreeFragment, new BraintreeResponseListener<List<IdealBank>>() {

        @Override
        public void onResponse(List<IdealBank> idealBanks) {
            verify(mBraintreeFragment).sendAnalyticsEvent(eq("ideal.load.succeeded"));
        }
    });
}
Also used : Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IdealBank(com.braintreepayments.api.models.IdealBank) JSONObject(org.json.JSONObject) List(java.util.List) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with HttpResponseCallback

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

the class IdealUnitTest method startPayment_success_sendsAnalyticsEvent.

@Test
public void startPayment_success_sendsAnalyticsEvent() throws JSONException {
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            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));
    List<IdealBank> banks = IdealBank.fromJson(mConfiguration, stringFromFixture("payment_methods/ideal_issuing_banks.json"));
    IdealRequest builder = new IdealRequest().issuerId(banks.get(0).getId()).amount("1.00").currency("EUR").orderId("abc-123");
    Ideal.startPayment(mBraintreeFragment, builder, null);
    verify(mBraintreeFragment).sendAnalyticsEvent(eq("ideal.start-payment.selected"));
    verify(mBraintreeFragment).sendAnalyticsEvent(eq("ideal.webswitch.initiate.succeeded"));
}
Also used : Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IdealRequest(com.braintreepayments.api.models.IdealRequest) IdealBank(com.braintreepayments.api.models.IdealBank) JSONObject(org.json.JSONObject) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with HttpResponseCallback

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

the class AnalyticsSenderUnitTest method doesNothingOnAsynchronousErrorResponse.

@Test
public void doesNothingOnAsynchronousErrorResponse() throws Exception {
    AnalyticsEvent one = new AnalyticsEvent(RuntimeEnvironment.application, "sessionId", "custom", "started");
    AnalyticsEvent two = new AnalyticsEvent(RuntimeEnvironment.application, "sessionId", "custom", "finished");
    AnalyticsDatabase database = AnalyticsDatabase.getInstance(RuntimeEnvironment.application);
    database.addEvent(one);
    database.addEvent(two);
    awaitTasksFinished(database);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((HttpResponseCallback) invocation.getArguments()[2]).failure(new ServerException(""));
            return null;
        }
    }).when(mHttpClient).post(anyString(), anyString(), any(HttpResponseCallback.class));
    AnalyticsSender.send(RuntimeEnvironment.application, mAuthorization, mHttpClient, "", false);
    List<List<AnalyticsEvent>> pendingEvents = database.getPendingRequests();
    assertEquals(1, pendingEvents.size());
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ServerException(com.braintreepayments.api.exceptions.ServerException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JSONObject(org.json.JSONObject) List(java.util.List) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) 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