@Nullable public static CustomerSource fromString(@Nullable String jsonString) { try { return fromJson(new JSONObject(jsonString)); } catch (JSONException ignored) { return null; } }
try { JSONObject customerSourceObject = dataArray.getJSONObject(i); CustomerSource sourceData = CustomerSource.fromJson(customerSourceObject); if (sourceData == null || VALUE_APPLE_PAY.equals(sourceData.getTokenizationMethod())) {
@Test public void getSourceType_whenCard_returnsCard() { try { JSONObject jsonCard = new JSONObject(JSON_CARD); CustomerSource sourceData = CustomerSource.fromJson(jsonCard); assertNotNull(sourceData); assertEquals(Source.CARD, sourceData.getSourceType()); } catch (JSONException jsonException) { fail("Test data failure: " + jsonException.getMessage()); } }
@Test public void fromExampleJsonSource_toJson_createsSameObject() { try { JSONObject original = new JSONObject(EXAMPLE_JSON_SOURCE_WITHOUT_NULLS); CustomerSource sourceData = CustomerSource.fromJson(original); assertNotNull(sourceData); JsonTestUtils.assertJsonEquals(original, sourceData.toJson()); } catch (JSONException exception) { fail("Test data failure: " + exception.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_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()); } }