|
| 1 | +package com.stripe.model; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import com.google.gson.JsonObject; |
| 9 | +import com.google.gson.JsonParser; |
| 10 | +import com.stripe.BaseStripeTest; |
| 11 | +import com.stripe.net.ApiResource; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +public class GsonRoundTripTest extends BaseStripeTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void testUnexpandedExpandableField() { |
| 18 | + String json = "{\"id\":\"in_123\",\"object\":\"invoice\",\"customer\":\"cus_456\"}"; |
| 19 | + Invoice invoice = ApiResource.GSON.fromJson(json, Invoice.class); |
| 20 | + |
| 21 | + assertEquals("cus_456", invoice.getCustomer()); |
| 22 | + assertNull(invoice.getCustomerObject()); |
| 23 | + |
| 24 | + String serialized = ApiResource.GSON.toJson(invoice); |
| 25 | + Invoice roundTripped = ApiResource.GSON.fromJson(serialized, Invoice.class); |
| 26 | + |
| 27 | + assertEquals("cus_456", roundTripped.getCustomer()); |
| 28 | + assertNull(roundTripped.getCustomerObject()); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testExpandedExpandableField() { |
| 33 | + String json = |
| 34 | + "{\"id\":\"in_123\",\"object\":\"invoice\"," |
| 35 | + + "\"customer\":{\"id\":\"cus_456\",\"object\":\"customer\"," |
| 36 | + + "\"name\":\"John Doe\",\"metadata\":{\"key\":\"value\"}}}"; |
| 37 | + Invoice invoice = ApiResource.GSON.fromJson(json, Invoice.class); |
| 38 | + |
| 39 | + assertEquals("cus_456", invoice.getCustomer()); |
| 40 | + Customer customer = invoice.getCustomerObject(); |
| 41 | + assertNotNull(customer); |
| 42 | + assertEquals("cus_456", customer.getId()); |
| 43 | + assertEquals("John Doe", customer.getName()); |
| 44 | + |
| 45 | + String serialized = ApiResource.GSON.toJson(invoice); |
| 46 | + Invoice roundTripped = ApiResource.GSON.fromJson(serialized, Invoice.class); |
| 47 | + |
| 48 | + assertEquals("cus_456", roundTripped.getCustomer()); |
| 49 | + Customer rtCustomer = roundTripped.getCustomerObject(); |
| 50 | + assertNotNull(rtCustomer); |
| 51 | + assertEquals("cus_456", rtCustomer.getId()); |
| 52 | + assertEquals("John Doe", rtCustomer.getName()); |
| 53 | + assertEquals("value", rtCustomer.getMetadata().get("key")); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testNullExpandableField() { |
| 58 | + String json = "{\"id\":\"in_123\",\"object\":\"invoice\",\"customer\":null}"; |
| 59 | + Invoice invoice = ApiResource.GSON.fromJson(json, Invoice.class); |
| 60 | + |
| 61 | + assertNull(invoice.getCustomer()); |
| 62 | + assertNull(invoice.getCustomerObject()); |
| 63 | + |
| 64 | + String serialized = ApiResource.GSON.toJson(invoice); |
| 65 | + Invoice roundTripped = ApiResource.GSON.fromJson(serialized, Invoice.class); |
| 66 | + |
| 67 | + assertNull(roundTripped.getCustomer()); |
| 68 | + assertNull(roundTripped.getCustomerObject()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testPaymentSourceDirectField() { |
| 73 | + // Charge.source is a direct PaymentSource field (not ExpandableField) |
| 74 | + String json = |
| 75 | + "{\"id\":\"ch_123\",\"object\":\"charge\"," |
| 76 | + + "\"source\":{\"id\":\"card_789\",\"object\":\"card\"," |
| 77 | + + "\"brand\":\"Visa\",\"last4\":\"4242\"}}"; |
| 78 | + Charge charge = ApiResource.GSON.fromJson(json, Charge.class); |
| 79 | + |
| 80 | + assertNotNull(charge.getSource()); |
| 81 | + assertTrue(charge.getSource() instanceof Card); |
| 82 | + assertEquals("card_789", charge.getSource().getId()); |
| 83 | + |
| 84 | + String serialized = ApiResource.GSON.toJson(charge); |
| 85 | + Charge roundTripped = ApiResource.GSON.fromJson(serialized, Charge.class); |
| 86 | + |
| 87 | + assertNotNull(roundTripped.getSource()); |
| 88 | + assertTrue(roundTripped.getSource() instanceof Card); |
| 89 | + assertEquals("card_789", roundTripped.getSource().getId()); |
| 90 | + assertEquals("Visa", ((Card) roundTripped.getSource()).getBrand()); |
| 91 | + assertEquals("4242", ((Card) roundTripped.getSource()).getLast4()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testStripeRawJsonObjectRoundTrip() { |
| 96 | + String innerJson = "{\"id\":\"unknown_123\",\"object\":\"unknown_type\",\"foo\":\"bar\"}"; |
| 97 | + StripeRawJsonObject raw = new StripeRawJsonObject(); |
| 98 | + raw.json = JsonParser.parseString(innerJson).getAsJsonObject(); |
| 99 | + |
| 100 | + String serialized = ApiResource.GSON.toJson(raw); |
| 101 | + // Should serialize as the raw JSON, not wrapped in {"json":{...}} |
| 102 | + JsonObject parsed = JsonParser.parseString(serialized).getAsJsonObject(); |
| 103 | + assertEquals("unknown_123", parsed.get("id").getAsString()); |
| 104 | + assertEquals("bar", parsed.get("foo").getAsString()); |
| 105 | + |
| 106 | + StripeRawJsonObject roundTripped = |
| 107 | + ApiResource.GSON.fromJson(serialized, StripeRawJsonObject.class); |
| 108 | + assertNotNull(roundTripped.json); |
| 109 | + assertEquals("unknown_123", roundTripped.json.get("id").getAsString()); |
| 110 | + assertEquals("bar", roundTripped.json.get("foo").getAsString()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testInvoiceWithExpandedCustomerRoundTrip() throws Exception { |
| 115 | + // Realistic scenario from RUN_DEVSDK-2253 |
| 116 | + final String[] expansions = {"customer"}; |
| 117 | + final String data = getFixture("/v1/invoices/in_123", expansions); |
| 118 | + final Invoice original = ApiResource.GSON.fromJson(data, Invoice.class); |
| 119 | + |
| 120 | + assertNotNull(original.getCustomerObject()); |
| 121 | + assertEquals(original.getCustomer(), original.getCustomerObject().getId()); |
| 122 | + |
| 123 | + String serialized = ApiResource.GSON.toJson(original); |
| 124 | + Invoice roundTripped = ApiResource.GSON.fromJson(serialized, Invoice.class); |
| 125 | + |
| 126 | + assertEquals(original.getId(), roundTripped.getId()); |
| 127 | + assertEquals(original.getCustomer(), roundTripped.getCustomer()); |
| 128 | + assertNotNull(roundTripped.getCustomerObject()); |
| 129 | + assertEquals(original.getCustomerObject().getId(), roundTripped.getCustomerObject().getId()); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testSubscriptionWithDefaultSourceRoundTrip() throws Exception { |
| 134 | + // Realistic scenario from DEVSDK-2319 |
| 135 | + final String[] expansions = {"default_source"}; |
| 136 | + final String data = getFixture("/v1/subscriptions/sub_123", expansions); |
| 137 | + final Subscription original = ApiResource.GSON.fromJson(data, Subscription.class); |
| 138 | + |
| 139 | + String serialized = ApiResource.GSON.toJson(original); |
| 140 | + Subscription roundTripped = ApiResource.GSON.fromJson(serialized, Subscription.class); |
| 141 | + |
| 142 | + assertEquals(original.getId(), roundTripped.getId()); |
| 143 | + } |
| 144 | +} |
0 commit comments