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
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,43 @@ public void testSelectTransferMethod_clickBankCardOpensAddTransferMethodUi() {
onView(allOf(instanceOf(TextView.class), withParent(withId(R.id.toolbar)))).check(
matches(withText(R.string.title_add_bank_card)));
}

@Test
public void testSelectTransferMethod_verifyThatCountryIsFromUserProfile() {
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
.getResourceContent("user_ca_response.json")).mock();
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
.getResourceContent("successful_tmc_keys_response.json")).mock();

mActivityTestRule.launchActivity(null);

onView(withId(R.id.select_transfer_method_country_value)).check(matches(withText("Canada")));
onView(withId(R.id.select_transfer_method_currency_value)).check(matches(withText("CAD")));
}

@Test
public void testSelectTransferMethod_verifyDefaultsToUSWhenUserProfileCountryIsNotConfigured() {
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
.getResourceContent("user_not_configured_country_response.json")).mock();
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
.getResourceContent("successful_tmc_keys_response.json")).mock();

mActivityTestRule.launchActivity(null);

onView(withId(R.id.select_transfer_method_country_value)).check(matches(withText("United States")));
onView(withId(R.id.select_transfer_method_currency_value)).check(matches(withText("USD")));
}

@Test
public void testSelectTransferMethod_verifyDefaultsToUSWhenUserProfileDoesNotHaveCountry() {
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
.getResourceContent("user_no_country_response.json")).mock();
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
.getResourceContent("successful_tmc_keys_response.json")).mock();

mActivityTestRule.launchActivity(null);

onView(withId(R.id.select_transfer_method_country_value)).check(matches(withText("United States")));
onView(withId(R.id.select_transfer_method_currency_value)).check(matches(withText("USD")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package com.hyperwallet.android.ui.transfermethod;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hyperwallet.android.model.HyperwalletError;

Expand Down Expand Up @@ -89,8 +90,8 @@ interface Presenter {

void loadCurrency(final boolean forceUpdate, @NonNull final String countryCode);

void loadTransferMethodConfigurationKeys(final boolean forceUpdate, @NonNull final String countryCode,
@NonNull final String currencyCode);
void loadTransferMethodConfigurationKeys(final boolean forceUpdate, @Nullable final String countryCode,
@Nullable final String currencyCode);

void loadTransferMethodTypes(final boolean forceUpdate, @NonNull final String countryCode,
@NonNull final String currencyCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public class SelectTransferMethodFragment extends Fragment implements SelectTran

private static final String ARGUMENT_COUNTRY_CODE_SELECTED = "ARGUMENT_COUNTRY_CODE_SELECTED";
private static final String ARGUMENT_CURRENCY_CODE_SELECTED = "ARGUMENT_CURRENCY_CODE_SELECTED";
private static final String DEFAULT_COUNTRY_CODE = "US";
private static final String DEFAULT_CURRENCY_CODE = "USD";
private static final boolean FORCE_UPDATE = false;
private static final String TAG = SelectTransferMethodFragment.class.getName();

Expand All @@ -80,8 +78,6 @@ public SelectTransferMethodFragment() {

public static SelectTransferMethodFragment newInstance() {
SelectTransferMethodFragment selectTransferMethodFragment = new SelectTransferMethodFragment();
selectTransferMethodFragment.mSelectedCountryCode = DEFAULT_COUNTRY_CODE;
selectTransferMethodFragment.mSelectedCurrencyCode = DEFAULT_CURRENCY_CODE;

Bundle arguments = new Bundle();
arguments.putString(ARGUMENT_COUNTRY_CODE_SELECTED, selectTransferMethodFragment.mSelectedCountryCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
*/
package com.hyperwallet.android.ui.transfermethod;

import static com.hyperwallet.android.ExceptionMapper.EC_UNEXPECTED_EXCEPTION;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hyperwallet.android.model.HyperwalletError;
import com.hyperwallet.android.model.HyperwalletErrors;
import com.hyperwallet.android.model.graphql.HyperwalletTransferMethodConfigurationKey;
import com.hyperwallet.android.model.graphql.keyed.Country;
Expand All @@ -30,13 +35,16 @@
import com.hyperwallet.android.ui.repository.UserRepository;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

public class SelectTransferMethodPresenter implements SelectTransferMethodContract.Presenter {

private static final String DEFAULT_COUNTRY_CODE = "US";

private final TransferMethodConfigurationRepository mTransferMethodConfigurationRepository;
private final UserRepository mUserRepository;
private final SelectTransferMethodContract.View mView;
Expand All @@ -50,8 +58,8 @@ public class SelectTransferMethodPresenter implements SelectTransferMethodContra
}

@Override
public void loadTransferMethodConfigurationKeys(final boolean forceUpdate, @NonNull final String countryCode,
@NonNull final String currencyCode) {
public void loadTransferMethodConfigurationKeys(final boolean forceUpdate, @Nullable final String countryCode,
@Nullable final String currencyCode) {

mView.showProgressBar();

Expand All @@ -69,16 +77,41 @@ public void onKeysLoaded(@Nullable final HyperwalletTransferMethodConfigurationK
if (!mView.isActive()) {
return;
}

Country country = TextUtils.isEmpty(countryCode) ? key.getCountry(user.getCountry())
: key.getCountry(countryCode);

if (country == null) { // param and user country is null
country = key.getCountry(DEFAULT_COUNTRY_CODE);
}

String currencyCodeString = currencyCode;
Set<Currency> currencies = key.getCurrencies(country.getCode());
if (TextUtils.isEmpty(currencyCodeString) && currencies != null
&& !currencies.isEmpty()) {
currencyCodeString = ((Currency) currencies.toArray()[0]).getCode();
}

// at this point if currencyCodeString is still null/empty, that means program is
// mis-configured
if (TextUtils.isEmpty(currencyCodeString)) {
HyperwalletError error = new HyperwalletError(
"Can't get Currency based from Country: " + country.getCode(),
EC_UNEXPECTED_EXCEPTION);
showErrorLoadTransferMethods(new HyperwalletErrors(Arrays.asList(error)));
return;
}

mView.hideProgressBar();
Set<HyperwalletTransferMethodType> transferMethodTypes =
key.getTransferMethodType(countryCode, currencyCode) != null
? key.getTransferMethodType(countryCode, currencyCode) :
key.getTransferMethodType(country.getCode(), currencyCodeString) != null
? key.getTransferMethodType(country.getCode(), currencyCodeString) :
new HashSet<HyperwalletTransferMethodType>();

mView.showTransferMethodCountry(countryCode);
mView.showTransferMethodCurrency(currencyCode);
mView.showTransferMethodCountry(country.getCode());
mView.showTransferMethodCurrency(currencyCodeString);
mView.showTransferMethodTypes(
getTransferMethodSelectionItems(countryCode, currencyCode,
getTransferMethodSelectionItems(country.getCode(), currencyCodeString,
user.getProfileType(), transferMethodTypes));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hyperwallet.android.ui.transfermethod;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
Expand All @@ -11,6 +13,8 @@
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import static com.hyperwallet.android.ExceptionMapper.EC_UNEXPECTED_EXCEPTION;

import com.hyperwallet.android.model.HyperwalletError;
import com.hyperwallet.android.model.HyperwalletErrors;
import com.hyperwallet.android.model.graphql.HyperwalletTransferMethodConfigurationKey;
Expand All @@ -23,12 +27,15 @@
import com.hyperwallet.android.ui.repository.UserRepositoryImpl;
import com.hyperwallet.android.ui.rule.HyperwalletExternalResourceManager;

import org.hamcrest.Matchers;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
Expand All @@ -50,7 +57,11 @@ public class SelectTransferMethodPresenterTest {
private TransferMethodConfigurationRepositoryImpl mTransferMethodConfigurationRepository;
@Mock
private UserRepositoryImpl mUserRepository;
@Captor
private ArgumentCaptor<List<HyperwalletError>> mErrorCaptor;

private HyperwalletTransferMethodConfigurationKey mResult;
private HyperwalletTransferMethodConfigurationKey mPartialResult;
private HyperwalletUser mUser;
private SelectTransferMethodPresenter selectTransferMethodPresenter;

Expand All @@ -61,6 +72,10 @@ public void initialize() throws Exception {
final JSONObject jsonObject = new JSONObject(responseBody);
mResult = new HyperwalletTransferMethodConfigurationKeyResult(jsonObject);

String partialResponseBody = externalResourceManager.getResourceContent(
"partial_success_tmc_keys_response.json");
mPartialResult = new HyperwalletTransferMethodConfigurationKeyResult(new JSONObject(partialResponseBody));

String userResponseBody = externalResourceManager.getResourceContent("user_response.json");
final JSONObject userJsonObject = new JSONObject(userResponseBody);
mUser = new HyperwalletUser(userJsonObject);
Expand Down Expand Up @@ -187,6 +202,94 @@ public Object answer(InvocationOnMock invocation) {
verify(view, never()).showAddTransferMethod(anyString(), anyString(), anyString(), anyString());
}

@Test
public void testLoadTransferMethodConfigurationKeys_loadsKeysIntoUserProfileCountryViewOnSuccess() {
// When
when(view.isActive()).thenReturn(true);

doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
TransferMethodConfigurationRepository.LoadKeysCallback callback =
(TransferMethodConfigurationRepository.LoadKeysCallback) invocation.getArguments()[0];
callback.onKeysLoaded(mResult);
return callback;
}
}).when(mTransferMethodConfigurationRepository).getKeys(any(
TransferMethodConfigurationRepository.LoadKeysCallback.class));

doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
UserRepository.LoadUserCallback userCallback =
(UserRepository.LoadUserCallback) invocation.getArguments()[0];
userCallback.onUserLoaded(mUser);
return userCallback;
}
}).when(mUserRepository).loadUser(any(
UserRepository.LoadUserCallback.class));

// Then
selectTransferMethodPresenter.loadTransferMethodConfigurationKeys(false, null, null);

verify(view).showTransferMethodCountry("US");
verify(view).showTransferMethodCurrency("USD");
verify(view).showTransferMethodTypes(ArgumentMatchers.<TransferMethodSelectionItem>anyList());
verify(view, never()).showErrorLoadTransferMethodConfigurationKeys(
ArgumentMatchers.<HyperwalletError>anyList());
verify(view, never()).showErrorLoadCurrency(ArgumentMatchers.<HyperwalletError>anyList());
verify(view, never()).showErrorLoadTransferMethodTypes(ArgumentMatchers.<HyperwalletError>anyList());
verify(view, never()).showErrorLoadCountrySelection(ArgumentMatchers.<HyperwalletError>anyList());
verify(view, never()).showErrorLoadCurrencySelection(ArgumentMatchers.<HyperwalletError>anyList());
verify(view, never()).showAddTransferMethod(anyString(), anyString(), anyString(), anyString());
}

@Test
public void testLoadTransferMethodConfigurationKeys_loadsKeysIntoUserProfileCountryViewOnError() {
// When
when(view.isActive()).thenReturn(true);

doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
TransferMethodConfigurationRepository.LoadKeysCallback callback =
(TransferMethodConfigurationRepository.LoadKeysCallback) invocation.getArguments()[0];
callback.onKeysLoaded(mPartialResult);
return callback;
}
}).when(mTransferMethodConfigurationRepository).getKeys(any(
TransferMethodConfigurationRepository.LoadKeysCallback.class));

doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
UserRepository.LoadUserCallback userCallback =
(UserRepository.LoadUserCallback) invocation.getArguments()[0];
userCallback.onUserLoaded(mUser);
return userCallback;
}
}).when(mUserRepository).loadUser(any(
UserRepository.LoadUserCallback.class));

// Then
selectTransferMethodPresenter.loadTransferMethodConfigurationKeys(false, null, null);


verify(view).showProgressBar();
verify(view, times(2)).isActive();
verify(view).hideProgressBar();
verify(view).showErrorLoadTransferMethodConfigurationKeys(mErrorCaptor.capture());
verify(view, never()).showTransferMethodCountry(anyString());
verify(view, never()).showTransferMethodCurrency(anyString());
verify(view, never()).showTransferMethodTypes(ArgumentMatchers.<TransferMethodSelectionItem>anyList());

// Assert
List<HyperwalletError> errors = mErrorCaptor.getValue();
assertThat(errors, Matchers.<HyperwalletError>hasSize(1));
assertThat(errors.get(0).getCode(), is(EC_UNEXPECTED_EXCEPTION));
assertThat(errors.get(0).getMessage(), is("Can't get Currency based from Country: US"));
}

@Test
public void testLoadCurrency_loadsCurrenciesIntoViewOnSuccess() {
// When
Expand Down
Loading