void addCustomerSourceIfSupported(CustomerSource... customerSources) { if (customerSources == null) { return; } for (CustomerSource customerSource : customerSources) { if (customerSource.asCard() != null || canDisplaySource(customerSource.asSource())) { // If it's a card, we can display it. mCustomerSourceList.add(customerSource); } } notifyDataSetChanged(); }
@Nullable public String getTokenizationMethod() { Source paymentAsSource = asSource(); Card paymentAsCard = asCard(); if (paymentAsSource != null && paymentAsSource.getType().equals(Source.CARD)) { SourceCardData cardData = (SourceCardData) paymentAsSource.getSourceTypeModel(); if (cardData != null) { return cardData.getTokenizationMethod(); } } else if (paymentAsCard != null) { return paymentAsCard.getTokenizationMethod(); } return null; }
@Test public void setCustomerSourceList_filtersOutNonCardSources() { List<CustomerSource> customerSourceList = new ArrayList<>(); CustomerSource cardCustomerSource = Mockito.mock(CustomerSource.class); Card card = Mockito.mock(Card.class); Mockito.when(cardCustomerSource.asCard()).thenReturn(card); CustomerSource nonCardCustomerSource = Mockito.mock(CustomerSource.class); customerSourceList.add(cardCustomerSource); customerSourceList.add(nonCardCustomerSource); mMaskedCardAdapter.setCustomerSourceList(customerSourceList); assertEquals(1, mMaskedCardAdapter.getItemCount()); } }
@Test public void updateCustomer_filtersOutNonCardSources() { List<CustomerSource> customerSourceList = new ArrayList<>(); CustomerSource cardCustomerSource = Mockito.mock(CustomerSource.class); Card card = Mockito.mock(Card.class); Mockito.when(cardCustomerSource.asCard()).thenReturn(card); CustomerSource nonCardCustomerSource = Mockito.mock(CustomerSource.class); customerSourceList.add(cardCustomerSource); customerSourceList.add(nonCardCustomerSource); Customer customer = Mockito.mock(Customer.class); Mockito.when(customer.getSources()).thenReturn(customerSourceList); mMaskedCardAdapter.updateCustomer(customer); assertEquals(1, mMaskedCardAdapter.getItemCount()); }
/** * Set the card data displayed using a {@link CustomerSource} object. If the object * is not a {@link Source} object that represents a card and the object returns {@code null} * from its {@link CustomerSource#asCard()} method, then no values will be set on this control. * * @param customerSource the {@link CustomerSource} to be partially displayed */ void setCustomerSource(@NonNull CustomerSource customerSource) { Source source = customerSource.asSource(); if (source != null && source.getSourceTypeModel() != null && Source.CARD.equals(source.getType()) && source.getSourceTypeModel() instanceof SourceCardData) { SourceCardData sourceCardData = (SourceCardData) source.getSourceTypeModel(); setSourceCardData(sourceCardData); return; } Card card = customerSource.asCard(); if (card != null) { setCard(card); } }
@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 setCustomerSource_withCardObject_setsCorrectData() { CustomerSource customerSource = CustomerSource.fromString(JSON_CARD); assertNotNull(customerSource); assertNotNull(customerSource.asCard()); assertNull(customerSource.asSource()); mMaskedCardView.setCustomerSource(customerSource); assertEquals(Card.MASTERCARD, mMaskedCardView.getCardBrand()); assertEquals("5555", mMaskedCardView.getLast4()); assertFalse(mMaskedCardView.isSelected()); }