|
5 | 5 | import static org.junit.Assert.assertNull; |
6 | 6 | import static org.junit.Assert.assertTrue; |
7 | 7 |
|
| 8 | +import com.adyen.model.checkout.CardDetails; |
| 9 | +import com.adyen.model.checkout.CheckoutPaymentMethod; |
8 | 10 | import com.adyen.model.checkout.CreateCheckoutSessionResponse; |
| 11 | +import com.adyen.model.checkout.StoredPaymentMethodDetails; |
9 | 12 | import com.adyen.model.legalentitymanagement.*; |
10 | 13 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 14 | + |
11 | 15 | import java.time.Month; |
12 | 16 | import java.time.ZoneId; |
| 17 | + |
13 | 18 | import org.junit.Test; |
14 | 19 |
|
15 | | -/** Test model serialization (toJson) and deserialization (fromJson) */ |
| 20 | +/** |
| 21 | + * Test model serialization (toJson) and deserialization (fromJson) |
| 22 | + */ |
16 | 23 | public class ModelTest { |
17 | 24 |
|
18 | | - @Test |
19 | | - public void testFromJsonLegalEntity() throws JsonProcessingException { |
20 | | - String json = |
21 | | - "{\n" |
22 | | - + " \"id\": \"LE3227C223222B5BPCMFXD2XG\",\n" |
23 | | - + " \"reference\": \"YOUR_REFERENCE_12345\",\n" |
24 | | - + " \"type\": \"individual\",\n" |
25 | | - + " \"individual\": {\n" |
26 | | - + " \"name\": {\n" |
27 | | - + " \"firstName\": \"Jane\",\n" |
28 | | - + " \"lastName\": \"Doe\"\n" |
29 | | - + " },\n" |
30 | | - + " \"residentialAddress\": {\n" |
31 | | - + " \"country\": \"NL\",\n" |
32 | | - + " \"city\": \"Amsterdam\"\n" |
33 | | - + " }\n" |
34 | | - + " },\n" |
35 | | - + " \"capabilities\": {\n" |
36 | | - + " \"issueCard\": {\n" |
37 | | - + " \"allowed\": true,\n" |
38 | | - + " \"requested\": true\n" |
39 | | - + " }\n" |
40 | | - + " }\n" |
41 | | - + "}"; |
42 | | - |
43 | | - LegalEntity legalEntity = LegalEntity.fromJson(json); |
44 | | - |
45 | | - assertNotNull(legalEntity); |
46 | | - assertEquals("LE3227C223222B5BPCMFXD2XG", legalEntity.getId()); |
47 | | - assertEquals("YOUR_REFERENCE_12345", legalEntity.getReference()); |
48 | | - |
49 | | - assertEquals(LegalEntity.TypeEnum.INDIVIDUAL, legalEntity.getType()); |
50 | | - assertNotNull(legalEntity.getIndividual()); |
51 | | - |
52 | | - assertNotNull(legalEntity.getCapabilities()); |
53 | | - assertTrue(legalEntity.getCapabilities().containsKey("issueCard")); |
54 | | - assertNotNull(legalEntity.getCapabilities().get("issueCard")); |
55 | | - } |
56 | | - |
57 | | - @Test |
58 | | - public void testFromJsonLegalEntityWithInvalidEnum() throws JsonProcessingException { |
59 | | - String json = |
60 | | - "{\n" |
61 | | - + " \"id\": \"LE3227C223222B5BPCMFXD2XG\",\n" |
62 | | - + " \"reference\": \"YOUR_REFERENCE_12345\",\n" |
63 | | - + " \"type\": \"UNKNOWN\",\n" |
64 | | - + " \"individual\": {\n" |
65 | | - + " \"name\": {\n" |
66 | | - + " \"firstName\": \"Jane\",\n" |
67 | | - + " \"lastName\": \"Doe\"\n" |
68 | | - + " },\n" |
69 | | - + " \"residentialAddress\": {\n" |
70 | | - + " \"country\": \"NL\",\n" |
71 | | - + " \"city\": \"Amsterdam\"\n" |
72 | | - + " }\n" |
73 | | - + " },\n" |
74 | | - + " \"capabilities\": {\n" |
75 | | - + " \"issueCard\": {\n" |
76 | | - + " \"allowed\": true,\n" |
77 | | - + " \"requested\": true\n" |
78 | | - + " }\n" |
79 | | - + " }\n" |
80 | | - + "}"; |
81 | | - |
82 | | - LegalEntity legalEntity = LegalEntity.fromJson(json); |
83 | | - |
84 | | - assertNotNull(legalEntity); |
85 | | - assertEquals("LE3227C223222B5BPCMFXD2XG", legalEntity.getId()); |
86 | | - assertEquals("YOUR_REFERENCE_12345", legalEntity.getReference()); |
87 | | - // type is null since enum value is invalid |
88 | | - assertNull(legalEntity.getType()); |
89 | | - } |
90 | | - |
91 | | - @Test |
92 | | - public void testToJsonLegalEntity() throws JsonProcessingException { |
93 | | - LegalEntity legalEntity = |
94 | | - new LegalEntity() |
95 | | - .reference("YOUR_REFERENCE_12345") |
96 | | - .type(LegalEntity.TypeEnum.INDIVIDUAL) |
97 | | - .individual( |
98 | | - new Individual() |
99 | | - .name(new Name().firstName("Jane").lastName("Doe")) |
100 | | - .residentialAddress(new Address().country("NL").city("Amsterdam"))) |
101 | | - .putCapabilitiesItem( |
102 | | - "issueCard", |
103 | | - new LegalEntityCapability() |
104 | | - .allowedSettings( |
105 | | - new CapabilitySettings() |
106 | | - .addFundingSourceItem(CapabilitySettings.FundingSourceEnum.CREDIT))); |
107 | | - |
108 | | - String deserialized = legalEntity.toJson(); |
109 | | - |
110 | | - String expected = |
111 | | - "{\"capabilities\":{\"issueCard\":{\"allowedSettings\":{\"fundingSource\":[\"credit\"]}}},\"individual\":{\"name\":{\"firstName\":\"Jane\",\"lastName\":\"Doe\"},\"residentialAddress\":{\"city\":\"Amsterdam\",\"country\":\"NL\"}},\"reference\":\"YOUR_REFERENCE_12345\",\"type\":\"individual\"}"; |
112 | | - |
113 | | - assertEquals(expected, deserialized); |
114 | | - } |
115 | | - |
116 | | - @Test |
117 | | - public void testFromJsonCreateCheckoutSessionResponse() throws JsonProcessingException { |
118 | | - String json = |
119 | | - "{\n" |
120 | | - + " \"amount\": {\n" |
121 | | - + " \"currency\": \"EUR\",\n" |
122 | | - + " \"value\": 1000\n" |
123 | | - + " },\n" |
124 | | - + " \"countryCode\": \"NL\",\n" |
125 | | - + " \"expiresAt\": \"2022-01-11T13:53:18+01:00\",\n" |
126 | | - + " \"id\": \"CS451F2AB1ED897A94\",\n" |
127 | | - + " \"merchantAccount\": \"YOUR_MERCHANT_ACCOUNT\",\n" |
128 | | - + " \"reference\": \"YOUR_PAYMENT_REFERENCE\",\n" |
129 | | - + " \"url\": \"https://checkout-test.adyen.com/checkout/sessions/CS_1234567890ABCDEF\",\n" |
130 | | - + " \"sessionData\": \"Ab02b4c0!BQABAgBfYI29...\"\n" |
131 | | - + "}"; |
132 | | - |
133 | | - CreateCheckoutSessionResponse response = CreateCheckoutSessionResponse.fromJson(json); |
134 | | - |
135 | | - assertNotNull(response); |
136 | | - assertEquals("CS451F2AB1ED897A94", response.getId()); |
137 | | - assertEquals("YOUR_PAYMENT_REFERENCE", response.getReference()); |
138 | | - assertEquals( |
139 | | - "https://checkout-test.adyen.com/checkout/sessions/CS_1234567890ABCDEF", response.getUrl()); |
140 | | - |
141 | | - assertNotNull(response.getAmount()); |
142 | | - assertEquals("EUR", response.getAmount().getCurrency()); |
143 | | - assertEquals(1000L, response.getAmount().getValue().longValue()); |
144 | | - |
145 | | - assertEquals("2022-01-11T13:53:18+01:00", response.getExpiresAt().toString()); |
146 | | - assertEquals(2022, response.getExpiresAt().getYear()); |
147 | | - assertEquals(Month.JANUARY, response.getExpiresAt().getMonth()); |
148 | | - assertEquals(ZoneId.of("+01:00"), response.getExpiresAt().toZonedDateTime().getZone()); |
149 | | - } |
| 25 | + @Test |
| 26 | + public void testFromJsonLegalEntity() throws JsonProcessingException { |
| 27 | + String json = |
| 28 | + "{\n" |
| 29 | + + " \"id\": \"LE3227C223222B5BPCMFXD2XG\",\n" |
| 30 | + + " \"reference\": \"YOUR_REFERENCE_12345\",\n" |
| 31 | + + " \"type\": \"individual\",\n" |
| 32 | + + " \"individual\": {\n" |
| 33 | + + " \"name\": {\n" |
| 34 | + + " \"firstName\": \"Jane\",\n" |
| 35 | + + " \"lastName\": \"Doe\"\n" |
| 36 | + + " },\n" |
| 37 | + + " \"residentialAddress\": {\n" |
| 38 | + + " \"country\": \"NL\",\n" |
| 39 | + + " \"city\": \"Amsterdam\"\n" |
| 40 | + + " }\n" |
| 41 | + + " },\n" |
| 42 | + + " \"capabilities\": {\n" |
| 43 | + + " \"issueCard\": {\n" |
| 44 | + + " \"allowed\": true,\n" |
| 45 | + + " \"requested\": true\n" |
| 46 | + + " }\n" |
| 47 | + + " }\n" |
| 48 | + + "}"; |
| 49 | + |
| 50 | + LegalEntity legalEntity = LegalEntity.fromJson(json); |
| 51 | + |
| 52 | + assertNotNull(legalEntity); |
| 53 | + assertEquals("LE3227C223222B5BPCMFXD2XG", legalEntity.getId()); |
| 54 | + assertEquals("YOUR_REFERENCE_12345", legalEntity.getReference()); |
| 55 | + |
| 56 | + assertEquals(LegalEntity.TypeEnum.INDIVIDUAL, legalEntity.getType()); |
| 57 | + assertNotNull(legalEntity.getIndividual()); |
| 58 | + |
| 59 | + assertNotNull(legalEntity.getCapabilities()); |
| 60 | + assertTrue(legalEntity.getCapabilities().containsKey("issueCard")); |
| 61 | + assertNotNull(legalEntity.getCapabilities().get("issueCard")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testFromJsonLegalEntityWithInvalidEnum() throws JsonProcessingException { |
| 66 | + String json = |
| 67 | + "{\n" |
| 68 | + + " \"id\": \"LE3227C223222B5BPCMFXD2XG\",\n" |
| 69 | + + " \"reference\": \"YOUR_REFERENCE_12345\",\n" |
| 70 | + + " \"type\": \"UNKNOWN\",\n" |
| 71 | + + " \"individual\": {\n" |
| 72 | + + " \"name\": {\n" |
| 73 | + + " \"firstName\": \"Jane\",\n" |
| 74 | + + " \"lastName\": \"Doe\"\n" |
| 75 | + + " },\n" |
| 76 | + + " \"residentialAddress\": {\n" |
| 77 | + + " \"country\": \"NL\",\n" |
| 78 | + + " \"city\": \"Amsterdam\"\n" |
| 79 | + + " }\n" |
| 80 | + + " },\n" |
| 81 | + + " \"capabilities\": {\n" |
| 82 | + + " \"issueCard\": {\n" |
| 83 | + + " \"allowed\": true,\n" |
| 84 | + + " \"requested\": true\n" |
| 85 | + + " }\n" |
| 86 | + + " }\n" |
| 87 | + + "}"; |
| 88 | + |
| 89 | + LegalEntity legalEntity = LegalEntity.fromJson(json); |
| 90 | + |
| 91 | + assertNotNull(legalEntity); |
| 92 | + assertEquals("LE3227C223222B5BPCMFXD2XG", legalEntity.getId()); |
| 93 | + assertEquals("YOUR_REFERENCE_12345", legalEntity.getReference()); |
| 94 | + // type is null since enum value is invalid |
| 95 | + assertNull(legalEntity.getType()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testToJsonLegalEntity() throws JsonProcessingException { |
| 100 | + LegalEntity legalEntity = |
| 101 | + new LegalEntity() |
| 102 | + .reference("YOUR_REFERENCE_12345") |
| 103 | + .type(LegalEntity.TypeEnum.INDIVIDUAL) |
| 104 | + .individual( |
| 105 | + new Individual() |
| 106 | + .name(new Name().firstName("Jane").lastName("Doe")) |
| 107 | + .residentialAddress(new Address().country("NL").city("Amsterdam"))) |
| 108 | + .putCapabilitiesItem( |
| 109 | + "issueCard", |
| 110 | + new LegalEntityCapability() |
| 111 | + .allowedSettings( |
| 112 | + new CapabilitySettings() |
| 113 | + .addFundingSourceItem(CapabilitySettings.FundingSourceEnum.CREDIT))); |
| 114 | + |
| 115 | + String deserialized = legalEntity.toJson(); |
| 116 | + |
| 117 | + String expected = |
| 118 | + "{\"capabilities\":{\"issueCard\":{\"allowedSettings\":{\"fundingSource\":[\"credit\"]}}},\"individual\":{\"name\":{\"firstName\":\"Jane\",\"lastName\":\"Doe\"},\"residentialAddress\":{\"city\":\"Amsterdam\",\"country\":\"NL\"}},\"reference\":\"YOUR_REFERENCE_12345\",\"type\":\"individual\"}"; |
| 119 | + |
| 120 | + assertEquals(expected, deserialized); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testFromJsonCreateCheckoutSessionResponse() throws JsonProcessingException { |
| 125 | + String json = |
| 126 | + "{\n" |
| 127 | + + " \"amount\": {\n" |
| 128 | + + " \"currency\": \"EUR\",\n" |
| 129 | + + " \"value\": 1000\n" |
| 130 | + + " },\n" |
| 131 | + + " \"countryCode\": \"NL\",\n" |
| 132 | + + " \"expiresAt\": \"2022-01-11T13:53:18+01:00\",\n" |
| 133 | + + " \"id\": \"CS451F2AB1ED897A94\",\n" |
| 134 | + + " \"merchantAccount\": \"YOUR_MERCHANT_ACCOUNT\",\n" |
| 135 | + + " \"reference\": \"YOUR_PAYMENT_REFERENCE\",\n" |
| 136 | + + " \"url\": \"https://checkout-test.adyen.com/checkout/sessions/CS_1234567890ABCDEF\",\n" |
| 137 | + + " \"sessionData\": \"Ab02b4c0!BQABAgBfYI29...\"\n" |
| 138 | + + "}"; |
| 139 | + |
| 140 | + CreateCheckoutSessionResponse response = CreateCheckoutSessionResponse.fromJson(json); |
| 141 | + |
| 142 | + assertNotNull(response); |
| 143 | + assertEquals("CS451F2AB1ED897A94", response.getId()); |
| 144 | + assertEquals("YOUR_PAYMENT_REFERENCE", response.getReference()); |
| 145 | + assertEquals( |
| 146 | + "https://checkout-test.adyen.com/checkout/sessions/CS_1234567890ABCDEF", response.getUrl()); |
| 147 | + |
| 148 | + assertNotNull(response.getAmount()); |
| 149 | + assertEquals("EUR", response.getAmount().getCurrency()); |
| 150 | + assertEquals(1000L, response.getAmount().getValue().longValue()); |
| 151 | + |
| 152 | + assertEquals("2022-01-11T13:53:18+01:00", response.getExpiresAt().toString()); |
| 153 | + assertEquals(2022, response.getExpiresAt().getYear()); |
| 154 | + assertEquals(Month.JANUARY, response.getExpiresAt().getMonth()); |
| 155 | + assertEquals(ZoneId.of("+01:00"), response.getExpiresAt().toZonedDateTime().getZone()); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testFromJsonCheckoutPaymentMethodBcmc() throws Exception { |
| 160 | + String json = |
| 161 | + "{\n" |
| 162 | + + " \"type\": \"bcmc\",\n" |
| 163 | + + " \"holderName\": \"Ms Smith\",\n" |
| 164 | + + " \"encryptedCardNumber\": \"...\",\n" |
| 165 | + + " \"encryptedExpiryMonth\": \"...\",\n" |
| 166 | + + " \"encryptedExpiryYear\": \"...\",\n" |
| 167 | + + " \"brand\": \"bcmc\",\n" |
| 168 | + + " \"checkoutAttemptId\": \"...\"\n" |
| 169 | + + "}"; |
| 170 | + |
| 171 | + CheckoutPaymentMethod paymentMethod = CheckoutPaymentMethod.fromJson(json); |
| 172 | + |
| 173 | + assertNotNull(paymentMethod); |
| 174 | + |
| 175 | + CardDetails cardDetails = paymentMethod.getCardDetails(); |
| 176 | + assertNotNull(cardDetails); |
| 177 | + |
| 178 | + assertEquals(CardDetails.TypeEnum.BCMC, cardDetails.getType()); |
| 179 | + assertEquals("bcmc", cardDetails.getBrand()); |
| 180 | + assertEquals("Ms Smith", cardDetails.getHolderName()); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void testFromJsonCheckoutPaymentMethodBcmcMobile() throws Exception { |
| 185 | + String json = |
| 186 | + "{\n" + |
| 187 | + " \"type\":\"bcmc_mobile\",\n" + |
| 188 | + " \"storedPaymentMethodId\":\"7219687191761347\"\n" + |
| 189 | + "}"; |
| 190 | + |
| 191 | + CheckoutPaymentMethod paymentMethod = CheckoutPaymentMethod.fromJson(json); |
| 192 | + assertNotNull(paymentMethod); |
| 193 | + |
| 194 | + StoredPaymentMethodDetails storedPaymentMethodDetails = paymentMethod.getStoredPaymentMethodDetails(); |
| 195 | + assertNotNull(storedPaymentMethodDetails); |
| 196 | + |
| 197 | + assertEquals(StoredPaymentMethodDetails.TypeEnum.BCMC_MOBILE, storedPaymentMethodDetails.getType()); |
| 198 | + assertEquals("7219687191761347", storedPaymentMethodDetails.getStoredPaymentMethodId()); |
| 199 | + } |
150 | 200 | } |
0 commit comments