Skip to content

Commit

Permalink
use constants for key strings
Browse files Browse the repository at this point in the history
  • Loading branch information
saperi22 committed Dec 11, 2023
1 parent e2a9320 commit 445f7e3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -35,20 +40,26 @@ 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)
is Phone -> toJson(phone = phone)
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"
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 445f7e3

Please sign in to comment.