Search in sources :

Example 36 with HttpResponseCallback

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

the class MockFragmentBuilder method setupSuccessResponses.

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

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((HttpResponseCallback) invocation.getArguments()[1]).success(mSuccessResponse);
            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]).success(mSuccessResponse);
            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)

Example 37 with HttpResponseCallback

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

the class AnalyticsSenderUnitTest method deletesDatabaseEventsOnAsynchronousSuccessResponse.

@Test
public void deletesDatabaseEventsOnAsynchronousSuccessResponse() throws InterruptedException {
    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]).success("");
            return null;
        }
    }).when(mHttpClient).post(anyString(), anyString(), any(HttpResponseCallback.class));
    AnalyticsSender.send(RuntimeEnvironment.application, mAuthorization, mHttpClient, "", false);
    List<List<AnalyticsEvent>> pendingEvents = database.getPendingRequests();
    assertEquals(0, pendingEvents.size());
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JSONObject(org.json.JSONObject) List(java.util.List) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) Test(org.junit.Test)

Example 38 with HttpResponseCallback

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

the class BraintreeApiHttpClientTest method setsBaseUrl.

@Test(timeout = 1000)
public void setsBaseUrl() throws IOException, InterruptedException {
    BraintreeApiHttpClient client = spy(new BraintreeApiHttpClient("http://localhost", null));
    final CountDownLatch latch = new CountDownLatch(1);
    client.get("/some-path", new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            latch.countDown();
        }

        @Override
        public void failure(Exception exception) {
            latch.countDown();
        }
    });
    latch.await();
    verify(client).init(eq("http://localhost/some-path"));
}
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 39 with HttpResponseCallback

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

the class BraintreeApiHttpClientTest method getRequestSslCertificateSuccessfulInProduction.

@Test(timeout = 5000)
public void getRequestSslCertificateSuccessfulInProduction() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    BraintreeApiHttpClient client = new BraintreeApiHttpClient("https://payments.braintree-api.com", null);
    client.get("/ping", new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            try {
                assertEquals("OK", new JSONObject(responseBody).getJSONObject("data").getString("status"));
            } catch (JSONException e) {
                fail("Response invalid");
            }
            latch.countDown();
        }

        @Override
        public void failure(Exception exception) {
            fail("Request failed");
        }
    });
    latch.await();
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) IOException(java.io.IOException) JSONException(org.json.JSONException) Test(org.junit.Test)

Example 40 with HttpResponseCallback

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

the class BraintreeGraphQLHttpClientTest method getRequestSslCertificateSuccessfulInSandbox.

@Test(timeout = 5000)
public void getRequestSslCertificateSuccessfulInSandbox() throws InterruptedException, InvalidArgumentException {
    BraintreeGraphQLHttpClient httpClient = new BraintreeGraphQLHttpClient("https://payments.sandbox.braintree-api.com/graphql", TOKENIZATION_KEY);
    httpClient.get("/", new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            mCountDownLatch.countDown();
        }

        @Override
        public void failure(Exception exception) {
            assertFalse(exception instanceof SSLException);
            mCountDownLatch.countDown();
        }
    });
    mCountDownLatch.await();
}
Also used : HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) SSLException(javax.net.ssl.SSLException) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) IOException(java.io.IOException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) SSLException(javax.net.ssl.SSLException) 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