Search in sources :

Example 6 with ErrorWithResponse

use of com.braintreepayments.api.exceptions.ErrorWithResponse in project braintree_android by braintree.

the class BraintreeFragmentUnitTest method postCallback_ErrorWithResponseIsPostedToListeners.

@Test
public void postCallback_ErrorWithResponseIsPostedToListeners() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY);
    fragment.addListener(new BraintreeErrorListener() {

        @Override
        public void onError(Exception error) {
            assertTrue(error instanceof ErrorWithResponse);
            assertEquals(422, ((ErrorWithResponse) error).getStatusCode());
            mCalled.set(true);
        }
    });
    fragment.postCallback(new ErrorWithResponse(422, ""));
    assertTrue(mCalled.get());
}
Also used : ErrorWithResponse(com.braintreepayments.api.exceptions.ErrorWithResponse) BraintreeErrorListener(com.braintreepayments.api.interfaces.BraintreeErrorListener) JSONException(org.json.JSONException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with ErrorWithResponse

use of com.braintreepayments.api.exceptions.ErrorWithResponse in project braintree_android by braintree.

the class UnionPayUnitTest method mockFailureCallback.

private void mockFailureCallback() {
    mockStatic(TokenizationClient.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((PaymentMethodNonceCallback) invocation.getArguments()[2]).failure(new ErrorWithResponse(422, ""));
            return null;
        }
    }).when(TokenizationClient.class);
    TokenizationClient.tokenize(any(BraintreeFragment.class), any(UnionPayCardBuilder.class), any(PaymentMethodNonceCallback.class));
}
Also used : Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ErrorWithResponse(com.braintreepayments.api.exceptions.ErrorWithResponse) PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback) UnionPayCardBuilder(com.braintreepayments.api.models.UnionPayCardBuilder)

Example 8 with ErrorWithResponse

use of com.braintreepayments.api.exceptions.ErrorWithResponse in project braintree_android by braintree.

the class ThreeDSecureAuthenticationResponseUnitTest method getErrors_returnsErrorString.

@Test
public void getErrors_returnsErrorString() {
    ThreeDSecureAuthenticationResponse authResponse = ThreeDSecureAuthenticationResponse.fromJson(stringFromFixture("three_d_secure/authentication_response_with_error.json"));
    ErrorWithResponse errors = new ErrorWithResponse(0, authResponse.getErrors());
    assertNull(authResponse.getCardNonce());
    assertFalse(authResponse.isSuccess());
    assertEquals("Failed to authenticate, please try a different form of payment", errors.getMessage());
}
Also used : ErrorWithResponse(com.braintreepayments.api.exceptions.ErrorWithResponse) Test(org.junit.Test)

Example 9 with ErrorWithResponse

use of com.braintreepayments.api.exceptions.ErrorWithResponse in project braintree_android by braintree.

the class CardTest method tokenize_callsErrorCallbackForInvalidPostalCode.

@Test(timeout = 10000)
public void tokenize_callsErrorCallbackForInvalidPostalCode() throws Exception {
    CardBuilder cardBuilder = new CardBuilder().cardNumber(VISA).expirationDate("08/20").postalCode("20000");
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    BraintreeFragment fragment = setupBraintreeFragment(new TestClientTokenBuilder().withPostalCodeVerification().build());
    fragment.addListener(new BraintreeErrorListener() {

        @Override
        public void onError(Exception error) {
            assertEquals("Postal code verification failed", ((ErrorWithResponse) error).errorFor("creditCard").errorFor("billingAddress").getFieldErrors().get(0).getMessage());
            countDownLatch.countDown();
        }
    });
    Card.tokenize(fragment, cardBuilder);
    countDownLatch.await();
}
Also used : CardBuilder(com.braintreepayments.api.models.CardBuilder) TestClientTokenBuilder(com.braintreepayments.api.test.TestClientTokenBuilder) ErrorWithResponse(com.braintreepayments.api.exceptions.ErrorWithResponse) CountDownLatch(java.util.concurrent.CountDownLatch) BraintreeErrorListener(com.braintreepayments.api.interfaces.BraintreeErrorListener) JSONException(org.json.JSONException) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) Test(org.junit.Test)

Example 10 with ErrorWithResponse

use of com.braintreepayments.api.exceptions.ErrorWithResponse in project braintree_android by braintree.

the class BraintreeGraphQLHttpClientTest method parseResponseFailsWithUserErrors.

@Test
public void parseResponseFailsWithUserErrors() throws Exception {
    String baseUrl = "http://example.com/graphql";
    HttpClient httpClient = new BraintreeGraphQLHttpClient(baseUrl, TOKENIZATION_KEY);
    httpClient = HttpClientTestUtils.stubResponse(httpClient, 200, FixturesHelper.stringFromFixture("errors/graphql/credit_card_error.json"));
    HttpURLConnection connection = httpClient.init(baseUrl);
    try {
        httpClient.parseResponse(connection);
        fail("No exception was thrown");
    } catch (ErrorWithResponse e) {
        assertEquals("Input is invalid.", e.getMessage());
        assertNotNull(e.errorFor("creditCard"));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ErrorWithResponse(com.braintreepayments.api.exceptions.ErrorWithResponse) Test(org.junit.Test)

Aggregations

ErrorWithResponse (com.braintreepayments.api.exceptions.ErrorWithResponse)10 Test (org.junit.Test)7 BraintreeErrorListener (com.braintreepayments.api.interfaces.BraintreeErrorListener)5 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)4 JSONException (org.json.JSONException)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 PaymentMethodNonceCallback (com.braintreepayments.api.interfaces.PaymentMethodNonceCallback)2 UnionPayCardBuilder (com.braintreepayments.api.models.UnionPayCardBuilder)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)2 Uri (android.net.Uri)1 AuthorizationException (com.braintreepayments.api.exceptions.AuthorizationException)1 BraintreeException (com.braintreepayments.api.exceptions.BraintreeException)1 UnionPayListener (com.braintreepayments.api.interfaces.UnionPayListener)1 CardBuilder (com.braintreepayments.api.models.CardBuilder)1 PaymentMethodBuilder (com.braintreepayments.api.models.PaymentMethodBuilder)1 ThreeDSecureAuthenticationResponse (com.braintreepayments.api.models.ThreeDSecureAuthenticationResponse)1 UnionPayCapabilities (com.braintreepayments.api.models.UnionPayCapabilities)1 TestClientTokenBuilder (com.braintreepayments.api.test.TestClientTokenBuilder)1