Skip to content

Commit d678348

Browse files
Peter Joseph Olamitskoong
authored andcommitted
HW-52639 default to wallet country for tmc selection (#62)
1 parent 68b492a commit d678348

File tree

11 files changed

+569
-20
lines changed

11 files changed

+569
-20
lines changed

ui/src/androidTest/java/com/hyperwallet/android/ui/transfermethod/SelectTransferMethodTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,43 @@ public void testSelectTransferMethod_clickBankCardOpensAddTransferMethodUi() {
404404
onView(allOf(instanceOf(TextView.class), withParent(withId(R.id.toolbar)))).check(
405405
matches(withText(R.string.title_add_bank_card)));
406406
}
407+
408+
@Test
409+
public void testSelectTransferMethod_verifyThatCountryIsFromUserProfile() {
410+
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
411+
.getResourceContent("user_ca_response.json")).mock();
412+
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
413+
.getResourceContent("successful_tmc_keys_response.json")).mock();
414+
415+
mActivityTestRule.launchActivity(null);
416+
417+
onView(withId(R.id.select_transfer_method_country_value)).check(matches(withText("Canada")));
418+
onView(withId(R.id.select_transfer_method_currency_value)).check(matches(withText("CAD")));
419+
}
420+
421+
@Test
422+
public void testSelectTransferMethod_verifyDefaultsToUSWhenUserProfileCountryIsNotConfigured() {
423+
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
424+
.getResourceContent("user_not_configured_country_response.json")).mock();
425+
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
426+
.getResourceContent("successful_tmc_keys_response.json")).mock();
427+
428+
mActivityTestRule.launchActivity(null);
429+
430+
onView(withId(R.id.select_transfer_method_country_value)).check(matches(withText("United States")));
431+
onView(withId(R.id.select_transfer_method_currency_value)).check(matches(withText("USD")));
432+
}
433+
434+
@Test
435+
public void testSelectTransferMethod_verifyDefaultsToUSWhenUserProfileDoesNotHaveCountry() {
436+
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
437+
.getResourceContent("user_no_country_response.json")).mock();
438+
mMockWebServer.mockResponse().withHttpResponseCode(HTTP_OK).withBody(sResourceManager
439+
.getResourceContent("successful_tmc_keys_response.json")).mock();
440+
441+
mActivityTestRule.launchActivity(null);
442+
443+
onView(withId(R.id.select_transfer_method_country_value)).check(matches(withText("United States")));
444+
onView(withId(R.id.select_transfer_method_currency_value)).check(matches(withText("USD")));
445+
}
407446
}

ui/src/main/java/com/hyperwallet/android/ui/transfermethod/SelectTransferMethodContract.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
package com.hyperwallet.android.ui.transfermethod;
2929

3030
import androidx.annotation.NonNull;
31+
import androidx.annotation.Nullable;
3132

3233
import com.hyperwallet.android.model.HyperwalletError;
3334

@@ -89,8 +90,8 @@ interface Presenter {
8990

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

92-
void loadTransferMethodConfigurationKeys(final boolean forceUpdate, @NonNull final String countryCode,
93-
@NonNull final String currencyCode);
93+
void loadTransferMethodConfigurationKeys(final boolean forceUpdate, @Nullable final String countryCode,
94+
@Nullable final String currencyCode);
9495

9596
void loadTransferMethodTypes(final boolean forceUpdate, @NonNull final String countryCode,
9697
@NonNull final String currencyCode);

ui/src/main/java/com/hyperwallet/android/ui/transfermethod/SelectTransferMethodFragment.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public class SelectTransferMethodFragment extends Fragment implements SelectTran
5555

5656
private static final String ARGUMENT_COUNTRY_CODE_SELECTED = "ARGUMENT_COUNTRY_CODE_SELECTED";
5757
private static final String ARGUMENT_CURRENCY_CODE_SELECTED = "ARGUMENT_CURRENCY_CODE_SELECTED";
58-
private static final String DEFAULT_COUNTRY_CODE = "US";
59-
private static final String DEFAULT_CURRENCY_CODE = "USD";
6058
private static final boolean FORCE_UPDATE = false;
6159
private static final String TAG = SelectTransferMethodFragment.class.getName();
6260

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

8179
public static SelectTransferMethodFragment newInstance() {
8280
SelectTransferMethodFragment selectTransferMethodFragment = new SelectTransferMethodFragment();
83-
selectTransferMethodFragment.mSelectedCountryCode = DEFAULT_COUNTRY_CODE;
84-
selectTransferMethodFragment.mSelectedCurrencyCode = DEFAULT_CURRENCY_CODE;
8581

8682
Bundle arguments = new Bundle();
8783
arguments.putString(ARGUMENT_COUNTRY_CODE_SELECTED, selectTransferMethodFragment.mSelectedCountryCode);

ui/src/main/java/com/hyperwallet/android/ui/transfermethod/SelectTransferMethodPresenter.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
*/
1818
package com.hyperwallet.android.ui.transfermethod;
1919

20+
import static com.hyperwallet.android.ExceptionMapper.EC_UNEXPECTED_EXCEPTION;
21+
22+
import android.text.TextUtils;
23+
2024
import androidx.annotation.NonNull;
2125
import androidx.annotation.Nullable;
2226

27+
import com.hyperwallet.android.model.HyperwalletError;
2328
import com.hyperwallet.android.model.HyperwalletErrors;
2429
import com.hyperwallet.android.model.graphql.HyperwalletTransferMethodConfigurationKey;
2530
import com.hyperwallet.android.model.graphql.keyed.Country;
@@ -30,13 +35,16 @@
3035
import com.hyperwallet.android.ui.repository.UserRepository;
3136

3237
import java.util.ArrayList;
38+
import java.util.Arrays;
3339
import java.util.HashSet;
3440
import java.util.List;
3541
import java.util.Set;
3642
import java.util.TreeMap;
3743

3844
public class SelectTransferMethodPresenter implements SelectTransferMethodContract.Presenter {
3945

46+
private static final String DEFAULT_COUNTRY_CODE = "US";
47+
4048
private final TransferMethodConfigurationRepository mTransferMethodConfigurationRepository;
4149
private final UserRepository mUserRepository;
4250
private final SelectTransferMethodContract.View mView;
@@ -50,8 +58,8 @@ public class SelectTransferMethodPresenter implements SelectTransferMethodContra
5058
}
5159

5260
@Override
53-
public void loadTransferMethodConfigurationKeys(final boolean forceUpdate, @NonNull final String countryCode,
54-
@NonNull final String currencyCode) {
61+
public void loadTransferMethodConfigurationKeys(final boolean forceUpdate, @Nullable final String countryCode,
62+
@Nullable final String currencyCode) {
5563

5664
mView.showProgressBar();
5765

@@ -69,16 +77,41 @@ public void onKeysLoaded(@Nullable final HyperwalletTransferMethodConfigurationK
6977
if (!mView.isActive()) {
7078
return;
7179
}
80+
81+
Country country = TextUtils.isEmpty(countryCode) ? key.getCountry(user.getCountry())
82+
: key.getCountry(countryCode);
83+
84+
if (country == null) { // param and user country is null
85+
country = key.getCountry(DEFAULT_COUNTRY_CODE);
86+
}
87+
88+
String currencyCodeString = currencyCode;
89+
Set<Currency> currencies = key.getCurrencies(country.getCode());
90+
if (TextUtils.isEmpty(currencyCodeString) && currencies != null
91+
&& !currencies.isEmpty()) {
92+
currencyCodeString = ((Currency) currencies.toArray()[0]).getCode();
93+
}
94+
95+
// at this point if currencyCodeString is still null/empty, that means program is
96+
// mis-configured
97+
if (TextUtils.isEmpty(currencyCodeString)) {
98+
HyperwalletError error = new HyperwalletError(
99+
"Can't get Currency based from Country: " + country.getCode(),
100+
EC_UNEXPECTED_EXCEPTION);
101+
showErrorLoadTransferMethods(new HyperwalletErrors(Arrays.asList(error)));
102+
return;
103+
}
104+
72105
mView.hideProgressBar();
73106
Set<HyperwalletTransferMethodType> transferMethodTypes =
74-
key.getTransferMethodType(countryCode, currencyCode) != null
75-
? key.getTransferMethodType(countryCode, currencyCode) :
107+
key.getTransferMethodType(country.getCode(), currencyCodeString) != null
108+
? key.getTransferMethodType(country.getCode(), currencyCodeString) :
76109
new HashSet<HyperwalletTransferMethodType>();
77110

78-
mView.showTransferMethodCountry(countryCode);
79-
mView.showTransferMethodCurrency(currencyCode);
111+
mView.showTransferMethodCountry(country.getCode());
112+
mView.showTransferMethodCurrency(currencyCodeString);
80113
mView.showTransferMethodTypes(
81-
getTransferMethodSelectionItems(countryCode, currencyCode,
114+
getTransferMethodSelectionItems(country.getCode(), currencyCodeString,
82115
user.getProfileType(), transferMethodTypes));
83116
}
84117

ui/src/test/java/com/hyperwallet/android/ui/transfermethod/SelectTransferMethodPresenterTest.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.hyperwallet.android.ui.transfermethod;
22

3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.is;
35
import static org.mockito.Matchers.any;
46
import static org.mockito.Matchers.anyString;
57
import static org.mockito.Matchers.eq;
@@ -11,6 +13,8 @@
1113
import static org.mockito.Mockito.when;
1214
import static org.mockito.MockitoAnnotations.initMocks;
1315

16+
import static com.hyperwallet.android.ExceptionMapper.EC_UNEXPECTED_EXCEPTION;
17+
1418
import com.hyperwallet.android.model.HyperwalletError;
1519
import com.hyperwallet.android.model.HyperwalletErrors;
1620
import com.hyperwallet.android.model.graphql.HyperwalletTransferMethodConfigurationKey;
@@ -23,12 +27,15 @@
2327
import com.hyperwallet.android.ui.repository.UserRepositoryImpl;
2428
import com.hyperwallet.android.ui.rule.HyperwalletExternalResourceManager;
2529

30+
import org.hamcrest.Matchers;
2631
import org.json.JSONObject;
2732
import org.junit.Before;
2833
import org.junit.Rule;
2934
import org.junit.Test;
3035
import org.junit.runner.RunWith;
36+
import org.mockito.ArgumentCaptor;
3137
import org.mockito.ArgumentMatchers;
38+
import org.mockito.Captor;
3239
import org.mockito.Mock;
3340
import org.mockito.invocation.InvocationOnMock;
3441
import org.mockito.stubbing.Answer;
@@ -50,7 +57,11 @@ public class SelectTransferMethodPresenterTest {
5057
private TransferMethodConfigurationRepositoryImpl mTransferMethodConfigurationRepository;
5158
@Mock
5259
private UserRepositoryImpl mUserRepository;
60+
@Captor
61+
private ArgumentCaptor<List<HyperwalletError>> mErrorCaptor;
62+
5363
private HyperwalletTransferMethodConfigurationKey mResult;
64+
private HyperwalletTransferMethodConfigurationKey mPartialResult;
5465
private HyperwalletUser mUser;
5566
private SelectTransferMethodPresenter selectTransferMethodPresenter;
5667

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

75+
String partialResponseBody = externalResourceManager.getResourceContent(
76+
"partial_success_tmc_keys_response.json");
77+
mPartialResult = new HyperwalletTransferMethodConfigurationKeyResult(new JSONObject(partialResponseBody));
78+
6479
String userResponseBody = externalResourceManager.getResourceContent("user_response.json");
6580
final JSONObject userJsonObject = new JSONObject(userResponseBody);
6681
mUser = new HyperwalletUser(userJsonObject);
@@ -187,6 +202,94 @@ public Object answer(InvocationOnMock invocation) {
187202
verify(view, never()).showAddTransferMethod(anyString(), anyString(), anyString(), anyString());
188203
}
189204

205+
@Test
206+
public void testLoadTransferMethodConfigurationKeys_loadsKeysIntoUserProfileCountryViewOnSuccess() {
207+
// When
208+
when(view.isActive()).thenReturn(true);
209+
210+
doAnswer(new Answer() {
211+
@Override
212+
public Object answer(InvocationOnMock invocation) {
213+
TransferMethodConfigurationRepository.LoadKeysCallback callback =
214+
(TransferMethodConfigurationRepository.LoadKeysCallback) invocation.getArguments()[0];
215+
callback.onKeysLoaded(mResult);
216+
return callback;
217+
}
218+
}).when(mTransferMethodConfigurationRepository).getKeys(any(
219+
TransferMethodConfigurationRepository.LoadKeysCallback.class));
220+
221+
doAnswer(new Answer() {
222+
@Override
223+
public Object answer(InvocationOnMock invocation) {
224+
UserRepository.LoadUserCallback userCallback =
225+
(UserRepository.LoadUserCallback) invocation.getArguments()[0];
226+
userCallback.onUserLoaded(mUser);
227+
return userCallback;
228+
}
229+
}).when(mUserRepository).loadUser(any(
230+
UserRepository.LoadUserCallback.class));
231+
232+
// Then
233+
selectTransferMethodPresenter.loadTransferMethodConfigurationKeys(false, null, null);
234+
235+
verify(view).showTransferMethodCountry("US");
236+
verify(view).showTransferMethodCurrency("USD");
237+
verify(view).showTransferMethodTypes(ArgumentMatchers.<TransferMethodSelectionItem>anyList());
238+
verify(view, never()).showErrorLoadTransferMethodConfigurationKeys(
239+
ArgumentMatchers.<HyperwalletError>anyList());
240+
verify(view, never()).showErrorLoadCurrency(ArgumentMatchers.<HyperwalletError>anyList());
241+
verify(view, never()).showErrorLoadTransferMethodTypes(ArgumentMatchers.<HyperwalletError>anyList());
242+
verify(view, never()).showErrorLoadCountrySelection(ArgumentMatchers.<HyperwalletError>anyList());
243+
verify(view, never()).showErrorLoadCurrencySelection(ArgumentMatchers.<HyperwalletError>anyList());
244+
verify(view, never()).showAddTransferMethod(anyString(), anyString(), anyString(), anyString());
245+
}
246+
247+
@Test
248+
public void testLoadTransferMethodConfigurationKeys_loadsKeysIntoUserProfileCountryViewOnError() {
249+
// When
250+
when(view.isActive()).thenReturn(true);
251+
252+
doAnswer(new Answer() {
253+
@Override
254+
public Object answer(InvocationOnMock invocation) {
255+
TransferMethodConfigurationRepository.LoadKeysCallback callback =
256+
(TransferMethodConfigurationRepository.LoadKeysCallback) invocation.getArguments()[0];
257+
callback.onKeysLoaded(mPartialResult);
258+
return callback;
259+
}
260+
}).when(mTransferMethodConfigurationRepository).getKeys(any(
261+
TransferMethodConfigurationRepository.LoadKeysCallback.class));
262+
263+
doAnswer(new Answer() {
264+
@Override
265+
public Object answer(InvocationOnMock invocation) {
266+
UserRepository.LoadUserCallback userCallback =
267+
(UserRepository.LoadUserCallback) invocation.getArguments()[0];
268+
userCallback.onUserLoaded(mUser);
269+
return userCallback;
270+
}
271+
}).when(mUserRepository).loadUser(any(
272+
UserRepository.LoadUserCallback.class));
273+
274+
// Then
275+
selectTransferMethodPresenter.loadTransferMethodConfigurationKeys(false, null, null);
276+
277+
278+
verify(view).showProgressBar();
279+
verify(view, times(2)).isActive();
280+
verify(view).hideProgressBar();
281+
verify(view).showErrorLoadTransferMethodConfigurationKeys(mErrorCaptor.capture());
282+
verify(view, never()).showTransferMethodCountry(anyString());
283+
verify(view, never()).showTransferMethodCurrency(anyString());
284+
verify(view, never()).showTransferMethodTypes(ArgumentMatchers.<TransferMethodSelectionItem>anyList());
285+
286+
// Assert
287+
List<HyperwalletError> errors = mErrorCaptor.getValue();
288+
assertThat(errors, Matchers.<HyperwalletError>hasSize(1));
289+
assertThat(errors.get(0).getCode(), is(EC_UNEXPECTED_EXCEPTION));
290+
assertThat(errors.get(0).getMessage(), is("Can't get Currency based from Country: US"));
291+
}
292+
190293
@Test
191294
public void testLoadCurrency_loadsCurrenciesIntoViewOnSuccess() {
192295
// When

0 commit comments

Comments
 (0)