From 445f7e36708a37bc4bdf32a8be269d38e47a955f Mon Sep 17 00:00:00 2001 From: Sai Date: Mon, 11 Dec 2023 11:41:03 -0600 Subject: [PATCH] use constants for key strings --- .../api/ShopperInsightsRequest.kt | 33 ++++++++++++------- .../api/ShopperInsightsClientUnitTest.kt | 27 ++++++++------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsRequest.kt b/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsRequest.kt index f22d5c18b9..ae8ab0db61 100644 --- a/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsRequest.kt +++ b/BraintreeCore/src/main/java/com/braintreepayments/api/ShopperInsightsRequest.kt @@ -14,9 +14,14 @@ data class BuyerPhone( var countryCode: String, var nationalNumber: String ) { + companion object { + internal const val KEY_COUNTRY_CODE = "countryCode" + internal const val KEY_NATIONAL_NUMBER = "nationalNumber" + } + fun toJson(): JSONObject = JSONObject().apply { - put("countryCode", countryCode) - put("nationalNumber", nationalNumber) + put(KEY_COUNTRY_CODE, countryCode) + put(KEY_NATIONAL_NUMBER, nationalNumber) } } @@ -35,15 +40,6 @@ sealed class ShopperInsightsRequest { var phone: BuyerPhone ) : ShopperInsightsRequest() - private fun toJson(email: String? = null, phone: BuyerPhone? = null): String { - return JSONObject().apply { - put("customer", JSONObject().apply { - putOpt("email", email) - putOpt("phone", phone?.toJson()) - }) - }.toString() - } - fun toJson(): String { return when (this) { is Email -> toJson(email = email) @@ -51,4 +47,19 @@ sealed class ShopperInsightsRequest { is EmailAndPhone -> toJson(email = email, phone = phone) } } + + private fun toJson(email: String? = null, phone: BuyerPhone? = null): String { + return JSONObject().apply { + put(KEY_CUSTOMER, JSONObject().apply { + putOpt(KEY_EMAIL, email) + putOpt(KEY_PHONE, phone?.toJson()) + }) + }.toString() + } + + companion object { + internal const val KEY_CUSTOMER = "customer" + internal const val KEY_EMAIL = "email" + internal const val KEY_PHONE = "phone" + } } diff --git a/BraintreeCore/src/test/java/com/braintreepayments/api/ShopperInsightsClientUnitTest.kt b/BraintreeCore/src/test/java/com/braintreepayments/api/ShopperInsightsClientUnitTest.kt index 92985e16a9..cdf00d9669 100644 --- a/BraintreeCore/src/test/java/com/braintreepayments/api/ShopperInsightsClientUnitTest.kt +++ b/BraintreeCore/src/test/java/com/braintreepayments/api/ShopperInsightsClientUnitTest.kt @@ -1,5 +1,10 @@ package com.braintreepayments.api +import com.braintreepayments.api.BuyerPhone.Companion.KEY_COUNTRY_CODE +import com.braintreepayments.api.BuyerPhone.Companion.KEY_NATIONAL_NUMBER +import com.braintreepayments.api.ShopperInsightsRequest.Companion.KEY_CUSTOMER +import com.braintreepayments.api.ShopperInsightsRequest.Companion.KEY_EMAIL +import com.braintreepayments.api.ShopperInsightsRequest.Companion.KEY_PHONE import io.mockk.CapturingSlot import io.mockk.mockk import io.mockk.verify @@ -65,10 +70,10 @@ class ShopperInsightsClientUnitTest { } } - val customer = JSONObject(slot.captured).getJSONObject("customer") - val phone = customer.getJSONObject("phone") - val countryCode = phone.getString("countryCode") - val nationalNumber = phone.getString("nationalNumber") + val customer = JSONObject(slot.captured).getJSONObject(KEY_CUSTOMER) + val phone = customer.getJSONObject(KEY_PHONE) + val countryCode = phone.getString(KEY_COUNTRY_CODE) + val nationalNumber = phone.getString(KEY_NATIONAL_NUMBER) assertEquals(testCountryCode, countryCode) assertEquals(testNationalNumber, nationalNumber) @@ -89,8 +94,8 @@ class ShopperInsightsClientUnitTest { } } - val customer = JSONObject(slot.captured).getJSONObject("customer") - val email = customer.getString("email") + val customer = JSONObject(slot.captured).getJSONObject(KEY_CUSTOMER) + val email = customer.getString(KEY_EMAIL) assertEquals(fakeEmail, email) } @@ -120,11 +125,11 @@ class ShopperInsightsClientUnitTest { } } - val customer = JSONObject(slot.captured).getJSONObject("customer") - val email = customer.getString("email") - val phone = customer.getJSONObject("phone") - val countryCode = phone.getString("countryCode") - val nationalNumber = phone.getString("nationalNumber") + val customer = JSONObject(slot.captured).getJSONObject(KEY_CUSTOMER) + val email = customer.getString(KEY_EMAIL) + val phone = customer.getJSONObject(KEY_PHONE) + val countryCode = phone.getString(KEY_COUNTRY_CODE) + val nationalNumber = phone.getString(KEY_NATIONAL_NUMBER) assertEquals(fakeEmail, email) assertEquals(testCountryCode, countryCode)