|
| 1 | +package com.braintreepayments.api.paypal |
| 2 | + |
| 3 | +import com.braintreepayments.api.core.IntegrationType |
| 4 | +import com.braintreepayments.api.core.MetadataBuilder |
| 5 | +import org.json.JSONException |
| 6 | +import org.json.JSONObject |
| 7 | +import org.junit.Test |
| 8 | +import org.junit.Assert.assertNull |
| 9 | +import org.junit.Assert.assertEquals |
| 10 | +import org.junit.Assert.assertFalse |
| 11 | +import org.skyscreamer.jsonassert.JSONAssert |
| 12 | +import org.skyscreamer.jsonassert.JSONCompareMode |
| 13 | + |
| 14 | +class PayPalAccountUnitTest { |
| 15 | + val PAYPAL_KEY = "paypalAccount" |
| 16 | + |
| 17 | + @Test |
| 18 | + @Throws(JSONException::class) |
| 19 | + fun `correctly builds a PayPal account`() { |
| 20 | + val sut = PayPalAccount( |
| 21 | + clientMetadataId = "correlation_id", |
| 22 | + urlResponseData = JSONObject(), |
| 23 | + intent = PayPalPaymentIntent.SALE, |
| 24 | + merchantAccountId = "alt_merchant_account_id", |
| 25 | + paymentType = "single-payment", |
| 26 | + sessionId = "session_id", |
| 27 | + source = "paypal-sdk", |
| 28 | + integration = IntegrationType.CUSTOM |
| 29 | + ) |
| 30 | + |
| 31 | + val jsonObject = sut.buildJSON() |
| 32 | + val jsonAccount = jsonObject.getJSONObject(PAYPAL_KEY) |
| 33 | + val jsonMetadata = jsonObject.getJSONObject(MetadataBuilder.META_KEY) |
| 34 | + |
| 35 | + assertNull(jsonAccount.opt("details")) |
| 36 | + assertEquals("correlation_id", jsonAccount.getString("correlationId")) |
| 37 | + assertEquals(PayPalPaymentIntent.SALE, PayPalPaymentIntent.fromString(jsonAccount.getString("intent"))) |
| 38 | + assertEquals("custom", jsonMetadata.getString("integration")) |
| 39 | + assertEquals("paypal-sdk", jsonMetadata.getString("source")) |
| 40 | + assertEquals("alt_merchant_account_id", jsonObject.getString("merchant_account_id")) |
| 41 | + assertFalse(jsonAccount.getJSONObject("options").getBoolean("validate")) |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + @Throws(JSONException::class) |
| 46 | + fun `builds a PayPal account and uses correct metadata`() { |
| 47 | + val sut = PayPalAccount( |
| 48 | + clientMetadataId = "correlation_id", |
| 49 | + urlResponseData = JSONObject(), |
| 50 | + intent = PayPalPaymentIntent.SALE, |
| 51 | + merchantAccountId = "alt_merchant_account_id", |
| 52 | + paymentType = "single-payment", |
| 53 | + sessionId = "session_id", |
| 54 | + source = "paypal-app", |
| 55 | + integration = IntegrationType.CUSTOM |
| 56 | + ) |
| 57 | + |
| 58 | + val jsonObject = sut.buildJSON() |
| 59 | + val jsonMetadata = jsonObject.getJSONObject(MetadataBuilder.META_KEY) |
| 60 | + |
| 61 | + assertEquals("custom", jsonMetadata.getString("integration")) |
| 62 | + assertEquals("paypal-app", jsonMetadata.getString("source")) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @Throws(JSONException::class) |
| 67 | + fun `builds a PayPal account and sets integration method`() { |
| 68 | + val sut = PayPalAccount( |
| 69 | + clientMetadataId = "correlation_id", |
| 70 | + urlResponseData = JSONObject(), |
| 71 | + intent = PayPalPaymentIntent.SALE, |
| 72 | + merchantAccountId = "alt_merchant_account_id", |
| 73 | + paymentType = "single-payment", |
| 74 | + sessionId = "session_id", |
| 75 | + source = "form" |
| 76 | + ) |
| 77 | + sut.integration = IntegrationType.CUSTOM |
| 78 | + |
| 79 | + val jsonObject = sut.buildJSON() |
| 80 | + val jsonMetadata = jsonObject.getJSONObject(MetadataBuilder.META_KEY) |
| 81 | + |
| 82 | + assertEquals(IntegrationType.CUSTOM.stringValue, jsonMetadata.getString("integration")) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @Throws(JSONException::class) |
| 87 | + fun `builds a PayPal account with single payment and sets options validate as false`() { |
| 88 | + val sut = PayPalAccount( |
| 89 | + clientMetadataId = "correlation_id", |
| 90 | + urlResponseData = JSONObject(), |
| 91 | + intent = PayPalPaymentIntent.SALE, |
| 92 | + merchantAccountId = "alt_merchant_account_id", |
| 93 | + paymentType = "single-payment", |
| 94 | + sessionId = "session_id", |
| 95 | + source = "form", |
| 96 | + integration = IntegrationType.CUSTOM |
| 97 | + ) |
| 98 | + |
| 99 | + val jsonObject = sut.buildJSON() |
| 100 | + val jsonAccount = jsonObject.getJSONObject(PAYPAL_KEY) |
| 101 | + |
| 102 | + assertFalse(jsonAccount.getJSONObject("options").getBoolean("validate")) |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + @Throws(JSONException::class) |
| 107 | + fun `builds a PayPal account with billing agreement and does not set options validate`() { |
| 108 | + val sut = PayPalAccount( |
| 109 | + clientMetadataId = "correlation_id", |
| 110 | + urlResponseData = JSONObject(), |
| 111 | + intent = PayPalPaymentIntent.SALE, |
| 112 | + merchantAccountId = "alt_merchant_account_id", |
| 113 | + paymentType = "billing-agreement", |
| 114 | + sessionId = "session_id", |
| 115 | + source = "form", |
| 116 | + integration = IntegrationType.CUSTOM |
| 117 | + ) |
| 118 | + |
| 119 | + val jsonObject = sut.buildJSON() |
| 120 | + val jsonAccount = jsonObject.getJSONObject(PAYPAL_KEY) |
| 121 | + |
| 122 | + assertFalse(jsonAccount.has("options")) |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + @Throws(JSONException::class) |
| 127 | + fun `builds a PayPal account as an empty object and does not include it when serializing`() { |
| 128 | + val sut = PayPalAccount( |
| 129 | + clientMetadataId = null, |
| 130 | + urlResponseData = JSONObject(), |
| 131 | + intent = null, |
| 132 | + merchantAccountId = null, |
| 133 | + paymentType = null, |
| 134 | + sessionId = null, |
| 135 | + source = null, |
| 136 | + integration = null |
| 137 | + ) |
| 138 | + |
| 139 | + val jsonObject = sut.buildJSON() |
| 140 | + val jsonAccount = jsonObject.getJSONObject(PAYPAL_KEY) |
| 141 | + |
| 142 | + assertEquals(0, jsonAccount.length()) |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + @Throws(JSONException::class) |
| 147 | + fun `builds a PayPal account and adds URL response data`() { |
| 148 | + val urlResponseData = JSONObject() |
| 149 | + .put("data1", "data1") |
| 150 | + .put("data2", "data2") |
| 151 | + .put("data3", "data3") |
| 152 | + |
| 153 | + val sut = PayPalAccount( |
| 154 | + clientMetadataId = null, |
| 155 | + urlResponseData = urlResponseData, |
| 156 | + intent = null, |
| 157 | + merchantAccountId = null, |
| 158 | + paymentType = null, |
| 159 | + sessionId = null, |
| 160 | + source = null, |
| 161 | + integration = null |
| 162 | + ) |
| 163 | + |
| 164 | + val jsonObject = sut.buildJSON() |
| 165 | + val jsonAccount = jsonObject.getJSONObject(PAYPAL_KEY) |
| 166 | + |
| 167 | + val expectedPaymentMethodNonceJSON = JSONObject() |
| 168 | + .put("data1", "data1") |
| 169 | + .put("data2", "data2") |
| 170 | + .put("data3", "data3") |
| 171 | + |
| 172 | + JSONAssert.assertEquals(expectedPaymentMethodNonceJSON, jsonAccount, JSONCompareMode.NON_EXTENSIBLE) |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + @Throws(JSONException::class) |
| 177 | + fun `builds a PayPal account with an empty intent and does not include it when serializing`() { |
| 178 | + val sut = PayPalAccount( |
| 179 | + clientMetadataId = "correlation_id", |
| 180 | + urlResponseData = JSONObject(), |
| 181 | + intent = null, |
| 182 | + merchantAccountId = "alt_merchant_account_id", |
| 183 | + paymentType = "billing-agreement", |
| 184 | + sessionId = "session_id", |
| 185 | + source = "form", |
| 186 | + integration = IntegrationType.CUSTOM |
| 187 | + ) |
| 188 | + |
| 189 | + val jsonObject = sut.buildJSON() |
| 190 | + val jsonAccount = jsonObject.getJSONObject(PAYPAL_KEY) |
| 191 | + |
| 192 | + assertFalse(jsonAccount.has("intent")) |
| 193 | + } |
| 194 | +} |
0 commit comments