11package com .hyperwallet .android .ui .transfermethod ;
22
3+ import static org .hamcrest .MatcherAssert .assertThat ;
4+ import static org .hamcrest .Matchers .is ;
35import static org .mockito .Matchers .any ;
46import static org .mockito .Matchers .anyString ;
57import static org .mockito .Matchers .eq ;
1113import static org .mockito .Mockito .when ;
1214import static org .mockito .MockitoAnnotations .initMocks ;
1315
16+ import static com .hyperwallet .android .ExceptionMapper .EC_UNEXPECTED_EXCEPTION ;
17+
1418import com .hyperwallet .android .model .HyperwalletError ;
1519import com .hyperwallet .android .model .HyperwalletErrors ;
1620import com .hyperwallet .android .model .graphql .HyperwalletTransferMethodConfigurationKey ;
2327import com .hyperwallet .android .ui .repository .UserRepositoryImpl ;
2428import com .hyperwallet .android .ui .rule .HyperwalletExternalResourceManager ;
2529
30+ import org .hamcrest .Matchers ;
2631import org .json .JSONObject ;
2732import org .junit .Before ;
2833import org .junit .Rule ;
2934import org .junit .Test ;
3035import org .junit .runner .RunWith ;
36+ import org .mockito .ArgumentCaptor ;
3137import org .mockito .ArgumentMatchers ;
38+ import org .mockito .Captor ;
3239import org .mockito .Mock ;
3340import org .mockito .invocation .InvocationOnMock ;
3441import 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