Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions core/src/main/java/com/hyperwallet/android/Hyperwallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static Hyperwallet getDefault() {
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param bankAccount the {@code HyperwalletBankAccount} to be created; must not be null
Expand Down Expand Up @@ -161,7 +161,7 @@ public void createBankAccount(@NonNull final HyperwalletBankAccount bankAccount,
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* * processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param bankAccountPagination the ordering and filtering criteria
Expand All @@ -186,7 +186,7 @@ public void listBankAccounts(@Nullable final HyperwalletBankAccountPagination ba
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* * processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param bankCard the {@code HyperwalletBankCard} to be created; must not be null
Expand All @@ -209,14 +209,14 @@ public void createBankCard(@NonNull final HyperwalletBankCard bankCard,
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param transferMethodToken the Hyperwallet specific unique identifier for the {@code HyperwalletBankAccount}
* being requested; must not be null
* @param listener the callback handler of responses from the Hyperwallet platform; must not be null
*/
public void getBankAccount(@NonNull String transferMethodToken,
public void getBankAccount(@NonNull final String transferMethodToken,
@NonNull final HyperwalletListener<HyperwalletBankAccount> listener) {
PathFormatter pathFormatter = new PathFormatter("users/{0}/bank-accounts/{1}", transferMethodToken);

Expand All @@ -233,7 +233,7 @@ public void getBankAccount(@NonNull String transferMethodToken,
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param transferMethodToken the Hyperwallet specific unique identifier for the {@code HyperwalletBankCard}
Expand All @@ -260,7 +260,7 @@ public void getBankCard(@NonNull final String transferMethodToken,
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param bankAccount the {@code HyperwalletBankAccount} to be created; must not be null
Expand Down Expand Up @@ -289,7 +289,7 @@ public void updateBankAccount(@NonNull final HyperwalletBankAccount bankAccount,
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param bankCard the {@code HyperwalletBankCard} to be created; must not be null
Expand All @@ -316,7 +316,7 @@ public void updateBankCard(@NonNull final HyperwalletBankCard bankCard,
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param transferMethodToken the Hyperwallet specific unique identifier for the {@code HyperwalletBankAccount}
Expand Down Expand Up @@ -348,7 +348,7 @@ public void deactivateBankAccount(@NonNull final String transferMethodToken, @Nu
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param transferMethodToken the Hyperwallet specific unique identifier for the {@code HyperwalletBankCard} being
Expand Down Expand Up @@ -394,7 +394,7 @@ public void deactivateBankCard(@NonNull final String transferMethodToken, @Nulla
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param transferMethodPagination the ordering and filtering criteria
Expand Down Expand Up @@ -434,7 +434,7 @@ public void listTransferMethods(@Nullable final HyperwalletTransferMethodPaginat
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param bankCardPagination the ordering and filtering criteria
Expand Down Expand Up @@ -473,7 +473,7 @@ public void listBankCards(@Nullable final HyperwalletBankCardPagination bankCard
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param bankCardPagination the ordering and filtering criteria
Expand All @@ -490,6 +490,30 @@ public void listPayPalAccounts(@Nullable final PayPalAccountPagination bankCardP
performRestTransaction(builder, listener);
}

/**
* Returns the {@link PayPalAccount} linked to the transfer method token specified, or null if none exists.
*
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param transferMethodToken the Hyperwallet specific unique identifier for the {@code PayPalAccount}
* being requested; must not be null
* @param listener the callback handler of responses from the Hyperwallet platform; must not be null
*/
public void getPayPalAccount(@NonNull final String transferMethodToken,
@NonNull final HyperwalletListener<PayPalAccount> listener) {
PathFormatter pathFormatter = new PathFormatter("users/{0}/paypal-accounts/{1}", transferMethodToken);

RestTransaction.Builder builder = new RestTransaction.Builder<>(GET, pathFormatter,
new TypeReference<PayPalAccount>() {
}, listener);

performRestTransaction(builder, listener);
}

/**
* Returns the transfer method configuration key set, processing times, and fees for the User that is associated
* with the authentication token returned from
Expand All @@ -498,7 +522,7 @@ public void listPayPalAccounts(@Nullable final PayPalAccountPagination bankCardP
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param transferMethodConfigurationKeysQuery containing the transfer method configuration key query,
Expand All @@ -525,7 +549,7 @@ public void retrieveTransferMethodConfigurationKeys(
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
* processing the request.</p>
*
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
* if the current one is expired or about to expire.</p>
*
* @param transferMethodConfigurationFieldQuery containing a transfer method configuration key tuple of
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.hyperwallet.android.transfermethod;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import static com.hyperwallet.android.model.HyperwalletStatusTransition.StatusDefinition.ACTIVATED;
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.CREATED_ON;
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.EMAIL;
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.STATUS;
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.TOKEN;
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.TRANSFER_METHOD_COUNTRY;
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.TRANSFER_METHOD_CURRENCY;
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.TYPE;
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodTypes.PAYPAL_ACCOUNT;

import com.hyperwallet.android.Hyperwallet;
import com.hyperwallet.android.exception.HyperwalletException;
import com.hyperwallet.android.listener.HyperwalletListener;
import com.hyperwallet.android.model.HyperwalletError;
import com.hyperwallet.android.model.HyperwalletErrors;
import com.hyperwallet.android.model.PayPalAccount;
import com.hyperwallet.android.rule.HyperwalletExternalResourceManager;
import com.hyperwallet.android.rule.HyperwalletMockWebServer;
import com.hyperwallet.android.rule.HyperwalletSdkMock;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;

import java.net.HttpURLConnection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import okhttp3.mockwebserver.RecordedRequest;

@RunWith(RobolectricTestRunner.class)
public class HyperwalletGetPayPalTest {
@Rule
public HyperwalletMockWebServer mServer = new HyperwalletMockWebServer();
@Rule
public HyperwalletSdkMock mSdkMock = new HyperwalletSdkMock(mServer);
@Rule
public HyperwalletExternalResourceManager mExternalResourceManager = new HyperwalletExternalResourceManager();
@Rule
public MockitoRule mMockito = MockitoJUnit.rule();

@Mock
private HyperwalletListener<PayPalAccount> mListener;
@Captor
private ArgumentCaptor<PayPalAccount> mPayPalAccountArgumentCaptor;
@Captor
private ArgumentCaptor<HyperwalletException> mExceptionCaptor;

private final CountDownLatch mAwait = new CountDownLatch(1);
private static final long COUNTDOWN_TIMEOUT_MILLIS = 100L;

@Test
public void testGetGetPayPalAccount_returnsAccount() throws InterruptedException {

String responseBody = mExternalResourceManager.getResourceContent("paypal_account_response.json");
mServer.mockResponse().withHttpResponseCode(HttpURLConnection.HTTP_OK).withBody(responseBody).mock();

Hyperwallet.getDefault().getPayPalAccount("trm-ac5727ac-8fe7-42fb-b69d-977ebdd7b48b", mListener);
mAwait.await(COUNTDOWN_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);

RecordedRequest recordedRequest = mServer.getRequest();
assertThat(recordedRequest.getPath(),
is("/rest/v3/users/usr-fbfd5848-60d0-43c5-8462-099c959b49c7/paypal-accounts/trm-ac5727ac-8fe7-42fb"
+ "-b69d-977ebdd7b48b"));

verify(mListener).onSuccess(mPayPalAccountArgumentCaptor.capture());
verify(mListener, never()).onFailure(any(HyperwalletException.class));

PayPalAccount payPalAccountResponse = mPayPalAccountArgumentCaptor.getValue();
assertThat(payPalAccountResponse, is(notNullValue()));
assertThat(payPalAccountResponse.getField(TYPE), is(PAYPAL_ACCOUNT));
assertThat(payPalAccountResponse.getField(TOKEN), is("trm-ac5727ac-8fe7-42fb-b69d-977ebdd7b48b"));
assertThat(payPalAccountResponse.getField(STATUS), is(ACTIVATED));

assertThat(payPalAccountResponse.getField(CREATED_ON), is("2019-01-09T22:50:14"));
assertThat(payPalAccountResponse.getField(TRANSFER_METHOD_COUNTRY), is("US"));
assertThat(payPalAccountResponse.getField(TRANSFER_METHOD_CURRENCY), is("USD"));
assertThat(payPalAccountResponse.getField(EMAIL), is("jsmith@paypal.com"));

}

@Test
public void testGetGetPayPalAccount_returnsNoAccount() throws InterruptedException {

String responseBody = "";
mServer.mockResponse().withHttpResponseCode(HttpURLConnection.HTTP_NO_CONTENT).withBody(responseBody).mock();

Hyperwallet.getDefault().getPayPalAccount("trm-ac5727ac-8fe7-42fb-b69d-977ebdd7b48b", mListener);
mAwait.await(COUNTDOWN_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);

RecordedRequest recordedRequest = mServer.getRequest();
assertThat(recordedRequest.getPath(),
is("/rest/v3/users/usr-fbfd5848-60d0-43c5-8462-099c959b49c7/paypal-accounts/trm-ac5727ac-8fe7-42fb"
+ "-b69d-977ebdd7b48b"));

verify(mListener).onSuccess(mPayPalAccountArgumentCaptor.capture());
verify(mListener, never()).onFailure(any(HyperwalletException.class));

PayPalAccount payPalAccount = mPayPalAccountArgumentCaptor.getValue();
assertThat(payPalAccount, is(nullValue()));
}

@Test
public void testGetGetPayPalAccount_returnsError() throws InterruptedException {

String responseBody = mExternalResourceManager.getResourceContentError("system_error_response.json");
mServer.mockResponse().withHttpResponseCode(HttpURLConnection.HTTP_BAD_REQUEST).withBody(responseBody).mock();

Hyperwallet.getDefault().getPayPalAccount("trm-854c4ec1-9161-49d6-92e2-b8d15aa4bf56", mListener);
mAwait.await(COUNTDOWN_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);

RecordedRequest recordedRequest = mServer.getRequest();
assertThat(recordedRequest.getPath(),
is("/rest/v3/users/usr-fbfd5848-60d0-43c5-8462-099c959b49c7/paypal-accounts/trm-854c4ec1-9161-49d6-92e2"
+ "-b8d15aa4bf56"));

verify(mListener, never()).onSuccess(any(PayPalAccount.class));
verify(mListener).onFailure(mExceptionCaptor.capture());

HyperwalletException exception = mExceptionCaptor.getValue();
assertThat(exception, is(notNullValue()));
HyperwalletErrors errors = exception.getHyperwalletErrors();
assertThat(errors, is(notNullValue()));
assertThat(errors.getErrors(), hasSize(1));

HyperwalletError error = errors.getErrors().get(0);
assertThat(error.getCode(), is("SYSTEM_ERROR"));
assertThat(error.getMessage(),
is("A system error has occurred. Please try again. If you continue to receive this error, please "
+ "contact customer support for assistance (Ref ID: 99b4ad5c-4aac-4cc2-aa9b-4b4f4844ac9b)."));

}
}