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
39 changes: 36 additions & 3 deletions core/src/main/java/com/hyperwallet/android/Hyperwallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ 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 payPalAccount the {@code PayPalAccount} to be created; must not be null
Expand Down Expand Up @@ -341,7 +341,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 payPalAccount the {@code PayPalAccount} to be created; must not be null
Expand Down Expand Up @@ -388,7 +388,7 @@ public void deactivateBankAccount(@NonNull final String transferMethodToken, @Nu
new TypeReference<HyperwalletStatusTransition>() {
}, listener).jsonModel(deactivatedStatusTransition);

performRestTransaction(builder, listener);
performRestTransaction(builder, listener);
}

/**
Expand Down Expand Up @@ -423,6 +423,39 @@ public void deactivateBankCard(@NonNull final String transferMethodToken, @Nulla
performRestTransaction(builder, listener);
}

/**
* Deactivates the {@link PayPalAccount} linked to the transfer method token specified. The
* {@code PayPalAccount} being deactivated must belong to the User that is associated with the
* authentication token returned from
* {@link HyperwalletAuthenticationTokenProvider#retrieveAuthenticationToken(HyperwalletAuthenticationTokenListener)}.
*
* <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 deactivated; must not be null
* @param notes a note regarding the status change
* @param listener the callback handler of responses from the Hyperwallet platform; must not be null
*/
public void deactivatePayPalAccount(@NonNull final String transferMethodToken, @Nullable final String notes,
@NonNull final HyperwalletListener<HyperwalletStatusTransition> listener) {
PathFormatter pathFormatter = new PathFormatter("users/{0}/paypal-accounts/{1}/status-transitions",
transferMethodToken);

final HyperwalletStatusTransition deactivatedStatusTransition = new HyperwalletStatusTransition(
HyperwalletStatusTransition.StatusDefinition.DE_ACTIVATED);
deactivatedStatusTransition.setNotes(notes);
RestTransaction.Builder builder = new RestTransaction.Builder<>(POST, pathFormatter,
new TypeReference<HyperwalletStatusTransition>() {
}, listener).jsonModel(deactivatedStatusTransition);

performRestTransaction(builder, listener);
}


/**
* Returns the {@link HyperwalletTransferMethod} (Bank Account, Bank Card, PayPay Account, Prepaid Card,
* Paper Checks) for the User associated with the authentication token returned from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.hyperwallet.android.transfermethod.HyperwalletCreatePayPalAccountTest;
import com.hyperwallet.android.transfermethod.HyperwalletDeactivateBankAccountTest;
import com.hyperwallet.android.transfermethod.HyperwalletDeactivateBankCardTest;
import com.hyperwallet.android.transfermethod.HyperwalletDeactivatePayPalAccountTest;
import com.hyperwallet.android.transfermethod.HyperwalletGetBankAccountTest;
import com.hyperwallet.android.transfermethod.HyperwalletGetBankCardTest;
import com.hyperwallet.android.transfermethod.HyperwalletGetPayPalTest;
Expand Down Expand Up @@ -35,6 +36,7 @@
HyperwalletUpdatePayPalAccountTest.class,
HyperwalletDeactivateBankAccountTest.class,
HyperwalletDeactivateBankCardTest.class,
HyperwalletDeactivatePayPalAccountTest.class,
HyperwalletListTransferMethodsTest.class,
HyperwalletListBankCardsTest.class,
HyperwalletRetrieveTransferMethodConfigurationKeysTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.hyperwallet.android.transfermethod;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

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.HyperwalletStatusTransition;
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.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@RunWith(RobolectricTestRunner.class)
public class HyperwalletDeactivatePayPalAccountTest {

@Rule
public HyperwalletMockWebServer mServer = new HyperwalletMockWebServer();
@Rule
public HyperwalletSdkMock mSdkMock = new HyperwalletSdkMock(mServer);
@Rule
public HyperwalletExternalResourceManager mExternalResourceManager = new HyperwalletExternalResourceManager();
@Rule
public MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private HyperwalletListener<HyperwalletStatusTransition> mListener;
@Captor
private ArgumentCaptor<HyperwalletStatusTransition> mStatusTransitionCaptor;
@Captor
private ArgumentCaptor<HyperwalletException> mExceptionArgumentCaptor;

private final CountDownLatch mAwait = new CountDownLatch(1);

@Test
public void testDeactivatePayPalAccount_successfulStatusTransition() throws Exception {

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

Hyperwallet.getDefault().deactivatePayPalAccount("trm-da21954a-3910-4d70-b83d-0c2d96104394", null,
mListener);
mAwait.await(50, TimeUnit.MILLISECONDS);

verify(mListener).onSuccess(mStatusTransitionCaptor.capture());
verify(mListener, never()).onFailure(any(HyperwalletException.class));
HyperwalletStatusTransition statusTransitionResponse = mStatusTransitionCaptor.getValue();
assertNotNull(statusTransitionResponse);
assertThat(statusTransitionResponse.getFromStatus(),
is(HyperwalletStatusTransition.StatusDefinition.ACTIVATED));
assertThat(statusTransitionResponse.getToStatus(),
is(HyperwalletStatusTransition.StatusDefinition.DE_ACTIVATED));
assertThat(statusTransitionResponse.getTransition(),
is(HyperwalletStatusTransition.StatusDefinition.DE_ACTIVATED));
assertThat(statusTransitionResponse.getToken(), is("sts-70ddc78a-0c14-4a72-8390-75d49ff376f2"));
assertNotNull(statusTransitionResponse.getCreatedOn());
assertThat(mServer.getRequest().getPath(), endsWith(
"users/usr-fbfd5848-60d0-43c5-8462-099c959b49c7/paypal-accounts/trm-da21954a-3910-4d70-b83d"
+ "-0c2d96104394/status-transitions"));
}

@Test
public void testDeactivatePayPalAccount_unsuccessfulStatusTransition() throws Exception {

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

Hyperwallet.getDefault().deactivatePayPalAccount("trm-fake-token", null, mListener);
mAwait.await(100, TimeUnit.MILLISECONDS);

verify(mListener).onFailure(mExceptionArgumentCaptor.capture());
verify(mListener, never()).onSuccess(any(HyperwalletStatusTransition.class));
HyperwalletException hyperwalletException = mExceptionArgumentCaptor.getValue();
assertNotNull(hyperwalletException);
final HyperwalletErrors hyperwalletErrors = hyperwalletException.getHyperwalletErrors();
assertNotNull(hyperwalletErrors);
final List<HyperwalletError> statusTransitionErrorList = hyperwalletErrors.getErrors();
assertNotNull(statusTransitionErrorList);
assertThat(statusTransitionErrorList, hasSize(1));
HyperwalletError statusTransitionError = statusTransitionErrorList.get(0);
assertNotNull(statusTransitionError);
assertThat(statusTransitionError.getCode(), is("INVALID_FIELD_VALUE"));
assertThat(statusTransitionError.getFieldName(), is("transition"));
assertThat(statusTransitionError.getMessage(), is("transition is invalid"));
assertThat(mServer.getRequest().getPath(),
endsWith(
"users/usr-fbfd5848-60d0-43c5-8462-099c959b49c7/paypal-accounts/trm-fake-token/status"
+ "-transitions"));
}
}
16 changes: 16 additions & 0 deletions core/src/test/resources/deactivated_paypal_account_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"token": "sts-70ddc78a-0c14-4a72-8390-75d49ff376f2",
"createdOn": "2017-10-30T18:50:20",
"transition": "DE_ACTIVATED",
"fromStatus": "ACTIVATED",
"toStatus": "DE_ACTIVATED",
"notes": "Closing this account.",
"links": [
{
"params": {
"rel": "self"
},
"href": "https://api.sandbox.hyperwallet.com/rest/v3/users/usr-f695ef43-9614-4e17-9269-902c234616c3/paypal-accounts/trm-da21954a-3910-4d70-b83d-0c2d96104394/status-transitions/sts-70ddc78a-0c14-4a72-8390-75d49ff376f2"
}
]
}