|
| 1 | +package com.braintreepayments.api.paypal |
| 2 | + |
| 3 | +import android.os.Parcel |
| 4 | +import kotlinx.parcelize.parcelableCreator |
| 5 | +import org.json.JSONException |
| 6 | +import org.json.JSONObject |
| 7 | +import org.junit.Test |
| 8 | +import org.junit.runner.RunWith |
| 9 | +import org.junit.Assert.assertEquals |
| 10 | +import org.junit.Assert.assertNotNull |
| 11 | +import org.junit.Assert.assertNull |
| 12 | +import org.robolectric.RobolectricTestRunner |
| 13 | + |
| 14 | +@RunWith(RobolectricTestRunner::class) |
| 15 | +class PayPalCreditFinancingAmountUnitTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + fun `from JSON returns empty object if null`() { |
| 19 | + val creditFinancingAmount = PayPalCreditFinancingAmount.fromJson(null) |
| 20 | + |
| 21 | + assertNotNull(creditFinancingAmount) |
| 22 | + assertNull(creditFinancingAmount.currency) |
| 23 | + assertNull(creditFinancingAmount.value) |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + @Throws(JSONException::class) |
| 28 | + fun `creates CreditFinancingAmount from standard JSON`() { |
| 29 | + val json = "{\"currency\": \"USD\", \"value\": \"123.45\"}" |
| 30 | + val creditFinancingAmount = PayPalCreditFinancingAmount.fromJson(JSONObject(json)) |
| 31 | + |
| 32 | + assertEquals("USD", creditFinancingAmount.currency) |
| 33 | + assertEquals("123.45", creditFinancingAmount.value) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + @Throws(JSONException::class) |
| 38 | + fun `creates CreditFinancingAmount from JSON with missing currency`() { |
| 39 | + val json = "{\"value\": \"123.45\"}" |
| 40 | + val creditFinancingAmount = PayPalCreditFinancingAmount.fromJson(JSONObject(json)) |
| 41 | + |
| 42 | + assertNull(creditFinancingAmount.currency) |
| 43 | + assertEquals("123.45", creditFinancingAmount.value) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + @Throws(JSONException::class) |
| 48 | + fun `creates CreditFinancingAmount from JSON with missing value`() { |
| 49 | + val json = "{\"currency\": \"USD\"}" |
| 50 | + val creditFinancingAmount = PayPalCreditFinancingAmount.fromJson(JSONObject(json)) |
| 51 | + |
| 52 | + assertNull(creditFinancingAmount.value) |
| 53 | + assertEquals("USD", creditFinancingAmount.currency) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @Throws(JSONException::class) |
| 58 | + fun `write to Parcel serializes correctly`() { |
| 59 | + val json = "{\"currency\": \"USD\", \"value\": \"123.45\"}" |
| 60 | + val preSerialized = PayPalCreditFinancingAmount.fromJson(JSONObject(json)) |
| 61 | + |
| 62 | + val parcel = Parcel.obtain().apply { |
| 63 | + preSerialized.writeToParcel(this, 0) |
| 64 | + setDataPosition(0) |
| 65 | + } |
| 66 | + |
| 67 | + val creditFinancingAmount = parcelableCreator<PayPalCreditFinancingAmount>().createFromParcel(parcel) |
| 68 | + |
| 69 | + assertNotNull(creditFinancingAmount) |
| 70 | + assertEquals("USD", creditFinancingAmount.currency) |
| 71 | + assertEquals("123.45", creditFinancingAmount.value) |
| 72 | + } |
| 73 | +} |
0 commit comments