@Nullable static Customer retrieveCustomer(@NonNull String customerId, @NonNull String secret) throws InvalidRequestException, APIConnectionException, APIException, AuthenticationException, CardException { final StripeResponse response = getStripeResponse( GET, getRetrieveCustomerUrl(customerId), null, RequestOptions.builder(secret).setApiVersion(API_VERSION).build()); convertErrorsToExceptionsAndThrowIfNecessary(response); return Customer.fromString(response.getResponseBody()); }
return Customer.fromString(response.getResponseBody());
return Customer.fromString(response.getResponseBody());
@Test public void fromString_whenStringIsNull_returnsNull() { assertNull(Customer.fromString(null)); }
@Test public void fromJson_whenNotACustomer_returnsNull() { assertNull(Customer.fromString(NON_CUSTOMER_OBJECT)); }
@Test public void fromJson_toJson_createsSameObject() { try { JSONObject rawJsonCustomer = new JSONObject(TEST_CUSTOMER_OBJECT); Customer customer = Customer.fromString(TEST_CUSTOMER_OBJECT); assertNotNull(customer); JsonTestUtils.assertJsonEquals(rawJsonCustomer, customer.toJson()); } catch (JSONException testDataException) { fail("Test data failure: " + testDataException.getMessage()); } }
@Test public void onClickAddSourceView_withoutPaymentSessoin_launchesAddSourceActivityWithoutLog() { Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(customer); mPaymentMethodsActivity.initializeCustomerSourceData(); mAddCardView.performClick(); ShadowActivity.IntentForResult intentForResult = mShadowActivity.getNextStartedActivityForResult(); assertNotNull(intentForResult); assertEquals(AddSourceActivity.class.getName(), intentForResult.intent.getComponent().getClassName()); assertFalse(intentForResult.intent.hasExtra(EXTRA_PAYMENT_SESSION_ACTIVE)); }
@Test public void fromJson_whenCustomerHasApplePay_returnsCustomerWithoutApplePaySources() { Customer customer = Customer.fromString(createTestCustomerObjectWithApplePaySource()); assertNotNull(customer); assertEquals(2, customer.getSources().size()); // Note that filtering the apple_pay sources intentionally does not change the total // count value. assertEquals(Integer.valueOf(5), customer.getTotalCount()); }
mActivity = Robolectric.buildActivity(AppCompatActivity.class).create().start().get(); Customer firstCustomer = Customer.fromString(FIRST_TEST_CUSTOMER_OBJECT); assertNotNull(firstCustomer); Customer secondCustomer = Customer.fromString(SECOND_TEST_CUSTOMER_OBJECT); assertNotNull(secondCustomer);
new IntentFilter(CustomerSession.ACTION_API_EXCEPTION)); mFirstCustomer = Customer.fromString(FIRST_TEST_CUSTOMER_OBJECT); assertNotNull(mFirstCustomer); mSecondCustomer = Customer.fromString(SECOND_TEST_CUSTOMER_OBJECT); assertNotNull(mSecondCustomer);
session.addProductUsageTokenIfValid("PaymentMethodsActivity"); Customer customerWithShippingInfo = Objects .requireNonNull(Customer.fromString(FIRST_TEST_CUSTOMER_OBJECT_WITH_SHIPPING_INFO)); ShippingInformation shippingInformation = Objects.requireNonNull(customerWithShippingInfo .getShippingInformation());
@Test public void onCreate_withCachedCustomer_showsUi() { Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(customer); mPaymentMethodsActivity.initializeCustomerSourceData(); assertNotNull(mProgressBar); assertNotNull(mRecyclerView); assertNotNull(mAddCardView); assertEquals(View.VISIBLE, mAddCardView.getVisibility()); assertEquals(View.VISIBLE, mRecyclerView.getVisibility()); assertEquals(View.GONE, mProgressBar.getVisibility()); }
@Test public void onActivityResult_withValidSource_refreshesCustomer() { Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(customer); mPaymentMethodsActivity.initializeCustomerSourceData(); Source source = Source.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE); assertNotNull(source); Intent resultIntent = new Intent(); resultIntent.putExtra(AddSourceActivity.EXTRA_NEW_SOURCE, source.toString()); ArgumentCaptor<CustomerSession.CustomerRetrievalListener> listenerArgumentCaptor = ArgumentCaptor.forClass(CustomerSession.CustomerRetrievalListener.class); mPaymentMethodsActivity.onActivityResult(REQUEST_CODE_ADD_CARD, RESULT_OK, resultIntent); assertEquals(View.VISIBLE, mProgressBar.getVisibility()); verify(mCustomerSessionProxy).updateCurrentCustomer(listenerArgumentCaptor.capture()); CustomerSession.CustomerRetrievalListener listener = listenerArgumentCaptor.getValue(); assertNotNull(listener); // Note - this doesn't make sense as the actual update; just testing that the customer // changes Customer updatedCustomer = Customer.fromString(TEST_CUSTOMER_OBJECT_WITH_SOURCES); assertNotNull(updatedCustomer); listener.onCustomerRetrieved(updatedCustomer); assertEquals(View.GONE, mProgressBar.getVisibility()); assertEquals(2, mRecyclerView.getAdapter().getItemCount()); }
@Test public void onCreate_withoutCacheCustomer_callsApiAndDisplaysProgressBarWhileWaiting() { Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); assertNotNull(customer); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(null); ArgumentCaptor<CustomerSession.CustomerRetrievalListener> listenerArgumentCaptor = ArgumentCaptor.forClass(CustomerSession.CustomerRetrievalListener.class); assertNotNull(mProgressBar); assertNotNull(mRecyclerView); assertNotNull(mAddCardView); mPaymentMethodsActivity.initializeCustomerSourceData(); verify(mCustomerSessionProxy).retrieveCurrentCustomer(listenerArgumentCaptor.capture()); assertEquals(View.VISIBLE, mProgressBar.getVisibility()); assertEquals(View.VISIBLE, mAddCardView.getVisibility()); assertEquals(View.VISIBLE, mRecyclerView.getVisibility()); CustomerSession.CustomerRetrievalListener listener = listenerArgumentCaptor.getValue(); assertNotNull(listener); listener.onCustomerRetrieved(customer); assertEquals(View.GONE, mProgressBar.getVisibility()); }
@Test public void onClickAddSourceView_whenStartedFromPaymentSession_launchesActivityWithLog() { Intent intent = PaymentMethodsActivity .newIntent(ApplicationProvider.getApplicationContext()) .putExtra(EXTRA_PROXY_DELAY, true) .putExtra(EXTRA_PAYMENT_SESSION_ACTIVE, true); mPaymentMethodsActivity = createActivity(intent); mPaymentMethodsActivity.setCustomerSessionProxy(mCustomerSessionProxy); mShadowActivity = Shadows.shadowOf(mPaymentMethodsActivity); mAddCardView = mPaymentMethodsActivity.findViewById(R.id.payment_methods_add_payment_container); Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(customer); mPaymentMethodsActivity.initializeCustomerSourceData(); mAddCardView.performClick(); ShadowActivity.IntentForResult intentForResult = mShadowActivity.getNextStartedActivityForResult(); assertNotNull(intentForResult); assertEquals(AddSourceActivity.class.getName(), intentForResult.intent.getComponent().getClassName()); assertTrue(intentForResult.intent.hasExtra(EXTRA_PAYMENT_SESSION_ACTIVE)); }
@Test public void onActivityResult_whenOneSourceButNoSelection_updatesSelectedItem() { Customer customer = Customer.fromString(CustomerSessionTest.FIRST_TEST_CUSTOMER_OBJECT); when(mCustomerSessionProxy.getCachedCustomer()).thenReturn(customer); mPaymentMethodsActivity.initializeCustomerSourceData(); Customer.fromString(TEST_CUSTOMER_OBJECT_WITH_ONE_SOURCE_NO_SELECTION); assertNotNull(updatedCustomer); Customer anotherCustomer = Customer.fromString(TEST_CUSTOMER_OBJECT_WITH_SOURCES); assertNotNull(anotherCustomer);
@Test public void onSaveMenuItem_sendsSelectionToApi_finishedWithExpectedResult() { Customer customer = Customer.fromString(TEST_CUSTOMER_OBJECT_WITH_SOURCES); assertNotNull(customer); assertNotNull(customer.getDefaultSource());
@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(); }
@Test public void fromJson_whenCustomer_returnsExpectedCustomer() { Customer customer = Customer.fromString(TEST_CUSTOMER_OBJECT); assertNotNull(customer); assertEquals("cus_AQsHpvKfKwJDrF", customer.getId()); assertEquals("abc123", customer.getDefaultSource()); assertNull(customer.getShippingInformation()); assertNotNull(customer.getSources()); assertEquals("/v1/customers/cus_AQsHpvKfKwJDrF/sources", customer.getUrl()); assertFalse(customer.getHasMore()); assertEquals(Integer.valueOf(0), customer.getTotalCount()); }