|
| 1 | +package com.braintreepayments.api.googlepay |
| 2 | + |
| 3 | +import android.os.Parcel |
| 4 | +import com.braintreepayments.api.core.PostalAddress |
| 5 | +import com.braintreepayments.api.sharedutils.Json |
| 6 | +import com.braintreepayments.api.testutils.Assertions.assertBinDataEqual |
| 7 | +import com.braintreepayments.api.testutils.Fixtures |
| 8 | +import kotlinx.parcelize.parcelableCreator |
| 9 | +import org.json.JSONException |
| 10 | +import org.json.JSONObject |
| 11 | +import org.junit.Test |
| 12 | +import org.junit.runner.RunWith |
| 13 | +import org.robolectric.RobolectricTestRunner |
| 14 | +import kotlin.test.assertEquals |
| 15 | +import kotlin.test.assertTrue |
| 16 | + |
| 17 | +@RunWith(RobolectricTestRunner::class) |
| 18 | +class GooglePayCardNonceUnitTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + @Throws(Exception::class) |
| 22 | + fun `parses JSON object and creates GooglePayCardNonce`() { |
| 23 | + val response = Fixtures.PAYMENT_METHODS_GOOGLE_PAY_CARD_RESPONSE |
| 24 | + val billing = JSONObject(response) |
| 25 | + .getJSONObject("paymentMethodData") |
| 26 | + .getJSONObject("info") |
| 27 | + .getJSONObject("billingAddress") |
| 28 | + val shipping = JSONObject(response).getJSONObject("shippingAddress") |
| 29 | + |
| 30 | + val billingPostalAddress = getPostalAddressObjectFromJson(billing) |
| 31 | + val shippingPostalAddress = getPostalAddressObjectFromJson(shipping) |
| 32 | + |
| 33 | + val googlePayCardNonce = GooglePayCardNonce.fromJSON(JSONObject(response)) as GooglePayCardNonce |
| 34 | + |
| 35 | + assertEquals("fake-google-pay-nonce", googlePayCardNonce.string) |
| 36 | + assertEquals("Visa", googlePayCardNonce.cardType) |
| 37 | + assertEquals("123456", googlePayCardNonce.bin) |
| 38 | + assertEquals("11", googlePayCardNonce.lastTwo) |
| 39 | + assertEquals("1234", googlePayCardNonce.lastFour) |
| 40 | + assertEquals("android-user@example.com", googlePayCardNonce.email) |
| 41 | + assertPostalAddress(billingPostalAddress, googlePayCardNonce.billingAddress) |
| 42 | + assertPostalAddress(shippingPostalAddress, googlePayCardNonce.shippingAddress) |
| 43 | + assertTrue(googlePayCardNonce.isNetworkTokenized) |
| 44 | + assertEquals("VISA", googlePayCardNonce.cardNetwork) |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @Throws(Exception::class) |
| 49 | + fun `parses JSON object without BillingAddress and creates GooglePayCardNonce`() { |
| 50 | + var response = Fixtures.PAYMENT_METHODS_GOOGLE_PAY_CARD_RESPONSE |
| 51 | + val responseNoBillingAddress = JSONObject(response).apply { |
| 52 | + getJSONObject("paymentMethodData") |
| 53 | + .getJSONObject("info").remove("billingAddress") |
| 54 | + } |
| 55 | + response = responseNoBillingAddress.toString() |
| 56 | + |
| 57 | + val billing = JSONObject() |
| 58 | + val billingPostalAddress = getPostalAddressObjectFromJson(billing) |
| 59 | + val googlePayCardNonce = GooglePayCardNonce.fromJSON(JSONObject(response)) as GooglePayCardNonce |
| 60 | + |
| 61 | + assertPostalAddress(billingPostalAddress, googlePayCardNonce.billingAddress) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @Throws(Exception::class) |
| 66 | + fun `parses JSON object without ShippingAddress and creates GooglePayCardNonce`() { |
| 67 | + var response = Fixtures.PAYMENT_METHODS_GOOGLE_PAY_CARD_RESPONSE |
| 68 | + val responseNoShippingAddress = JSONObject(response).apply { |
| 69 | + remove("shippingAddress") |
| 70 | + } |
| 71 | + response = responseNoShippingAddress.toString() |
| 72 | + |
| 73 | + val shipping = JSONObject() |
| 74 | + val shippingPostalAddress = getPostalAddressObjectFromJson(shipping) |
| 75 | + val googlePayCardNonce = GooglePayCardNonce.fromJSON(JSONObject(response)) as GooglePayCardNonce |
| 76 | + |
| 77 | + assertPostalAddress(shippingPostalAddress, googlePayCardNonce.shippingAddress) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + @Throws(JSONException::class) |
| 82 | + fun `parses JSON object without email and creates GooglePayCardNonce`() { |
| 83 | + var response = Fixtures.PAYMENT_METHODS_GOOGLE_PAY_CARD_RESPONSE |
| 84 | + val responseNoEmail = JSONObject(response).apply { |
| 85 | + remove("email") |
| 86 | + } |
| 87 | + response = responseNoEmail.toString() |
| 88 | + |
| 89 | + val googlePayCardNonce = GooglePayCardNonce.fromJSON(JSONObject(response)) as GooglePayCardNonce |
| 90 | + |
| 91 | + assertEquals("", googlePayCardNonce.email) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + @Throws(Exception::class) |
| 96 | + fun `parcels and returns all properties correctly`() { |
| 97 | + val response = Fixtures.PAYMENT_METHODS_GOOGLE_PAY_CARD_RESPONSE |
| 98 | + val billing = JSONObject(response) |
| 99 | + .getJSONObject("paymentMethodData") |
| 100 | + .getJSONObject("info") |
| 101 | + .getJSONObject("billingAddress") |
| 102 | + val shipping = JSONObject(response).getJSONObject("shippingAddress") |
| 103 | + |
| 104 | + val billingPostalAddress = getPostalAddressObjectFromJson(billing) |
| 105 | + val shippingPostalAddress = getPostalAddressObjectFromJson(shipping) |
| 106 | + |
| 107 | + val googlePayCardNonce = GooglePayCardNonce.fromJSON(JSONObject(response)) as GooglePayCardNonce |
| 108 | + val parcel = Parcel.obtain().apply { |
| 109 | + googlePayCardNonce.writeToParcel(this, 0) |
| 110 | + setDataPosition(0) |
| 111 | + } |
| 112 | + val parceled = parcelableCreator<GooglePayCardNonce>().createFromParcel(parcel) |
| 113 | + assertEquals("fake-google-pay-nonce", googlePayCardNonce.string) |
| 114 | + assertEquals("Visa", googlePayCardNonce.cardType) |
| 115 | + assertEquals("11", googlePayCardNonce.lastTwo) |
| 116 | + assertEquals("1234", googlePayCardNonce.lastFour) |
| 117 | + assertEquals("android-user@example.com", googlePayCardNonce.email) |
| 118 | + assertPostalAddress(billingPostalAddress, googlePayCardNonce.billingAddress) |
| 119 | + assertPostalAddress(shippingPostalAddress, googlePayCardNonce.shippingAddress) |
| 120 | + assertTrue(googlePayCardNonce.isNetworkTokenized) |
| 121 | + assertEquals("VISA", googlePayCardNonce.cardNetwork) |
| 122 | + |
| 123 | + assertBinDataEqual(googlePayCardNonce.binData, parceled.binData) |
| 124 | + } |
| 125 | + |
| 126 | + private fun getPostalAddressObjectFromJson(address: JSONObject): PostalAddress { |
| 127 | + val result = PostalAddress( |
| 128 | + recipientName = Json.optString(address, "name", ""), |
| 129 | + streetAddress = Json.optString(address, "address1", ""), |
| 130 | + extendedAddress = listOf( |
| 131 | + Json.optString(address, "address2", ""), |
| 132 | + Json.optString(address, "address3", "") |
| 133 | + ).joinToString(separator = "\n").trim(), |
| 134 | + locality = Json.optString(address, "locality", ""), |
| 135 | + region = Json.optString(address, "administrativeArea", ""), |
| 136 | + countryCodeAlpha2 = Json.optString(address, "countryCode", ""), |
| 137 | + postalCode = Json.optString(address, "postalCode", "") |
| 138 | + ) |
| 139 | + return result |
| 140 | + } |
| 141 | + |
| 142 | + private fun assertPostalAddress(expected: PostalAddress, actual: PostalAddress) { |
| 143 | + assertEquals(expected.toString(), actual.toString()) |
| 144 | + } |
| 145 | +} |
0 commit comments