@Nullable public CustomerSource getSourceById(@NonNull String sourceId) { for (CustomerSource source : mSources) { if (sourceId.equals(source.getId())) { return source; } } return null; }
/** * Sets the selected source to the one whose ID is identical to the input string, if such * a value is found. * * @param sourceId a stripe ID to search for among the list of {@link CustomerSource} objects * @return {@code true} if the value was found, {@code false} if not */ boolean setSelectedSource(@NonNull String sourceId) { for (int i = 0; i < mCustomerSourceList.size(); i++) { if (sourceId.equals(mCustomerSourceList.get(i).getId())) { updateSelectedIndex(i); return true; } } return false; }
if (customerSource == null || customerSource.getId() == null) { CustomerSession.getInstance().setCustomerDefaultSource( this, customerSource.getId(), customerSource.getSourceType(), listener); } else { mCustomerSessionProxy.setCustomerDefaultSource( customerSource.getId(), customerSource.getSourceType(), listener);
@Test public void setSelection_changesSelection() { CustomerSource cardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE); CustomerSource secondCardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE_SECOND); mMaskedCardAdapter.addCustomerSourceIfSupported(cardSource, secondCardSource); assertEquals(2, mMaskedCardAdapter.getItemCount()); verify(mAdapterDataObserver, times(1)).onChanged(); assertNull(mMaskedCardAdapter.getSelectedSource()); assertNotNull(secondCardSource); assertNotNull(secondCardSource.getId()); mMaskedCardAdapter.setSelectedSource(secondCardSource.getId()); verify(mAdapterDataObserver, times(2)).onChanged(); assertNotNull(mMaskedCardAdapter.getSelectedSource()); assertEquals(secondCardSource.getId(), mMaskedCardAdapter.getSelectedSource().getId()); }
@Test public void fromJson_whenCard_createsCustomerSourceData() { try { JSONObject jsonCard = new JSONObject(JSON_CARD); CustomerSource sourceData = CustomerSource.fromJson(jsonCard); assertNotNull(sourceData); assertNotNull(sourceData.asCard()); assertEquals("card_189fi32eZvKYlo2CHK8NPRME", sourceData.getId()); assertNull(sourceData.getTokenizationMethod()); } catch (JSONException jsonException) { fail("Test data failure: " + jsonException.getMessage()); } }
@Test public void fromJson_whenCardWithTokenization_createsSourceDataWithTokenization() { try { JSONObject jsonCard = new JSONObject(JSON_APPLE_PAY_CARD); CustomerSource sourceData = CustomerSource.fromJson(jsonCard); assertNotNull(sourceData); assertNotNull(sourceData.asCard()); assertEquals("card_189fi32eZvKYlo2CHK8NPRME", sourceData.getId()); assertEquals("apple_pay", sourceData.getTokenizationMethod()); } catch (JSONException jsonException) { fail("Test data failure: " + jsonException.getMessage()); } }
@Test public void fromJson_whenSource_createsCustomerSourceData() { CustomerSource sourceData = CustomerSource.fromString(EXAMPLE_JSON_SOURCE_WITHOUT_NULLS); assertNotNull(sourceData); assertNotNull(sourceData.asSource()); assertEquals("src_19t3xKBZqEXluyI4uz2dxAfQ", sourceData.getId()); }
assertEquals(updatedCustomer.getSources().get(0).getId(), stringArgumentCaptor.getValue()); assertNotNull(updateListener);
assertEquals(customer.getDefaultSource(), sourceList.get(0).getId());
@Test public void updateCustomer_removesExistingSourcesAndAddsAllCustomerSources() { CustomerSource cardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE_SECOND); mMaskedCardAdapter.addCustomerSourceIfSupported(cardSource); assertEquals(1, mMaskedCardAdapter.getItemCount()); assertNull(mMaskedCardAdapter.getSelectedSource()); Customer customer = Customer.fromString(PaymentMethodsActivityTest.TEST_CUSTOMER_OBJECT_WITH_SOURCES); assertNotNull(customer); mMaskedCardAdapter.updateCustomer(customer); assertEquals(2, mMaskedCardAdapter.getItemCount()); assertNotNull(mMaskedCardAdapter.getSelectedSource()); assertEquals(customer.getDefaultSource(), mMaskedCardAdapter.getSelectedSource().getId()); verify(mAdapterDataObserver, times(4)).onChanged(); }