Skip to content

Commit 8a68c49

Browse files
authored
Convert Json and JsonTest to Kotlin (#1394)
1 parent 350881f commit 8a68c49

File tree

5 files changed

+123
-89
lines changed

5 files changed

+123
-89
lines changed

PayPal/src/main/java/com/braintreepayments/api/paypal/PayPalClient.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ class PayPalClient internal constructor(
251251
val paymentType = Json.optString(metadata, "payment-type", "unknown")
252252
val isBillingAgreement = paymentType.equals("billing-agreement", ignoreCase = true)
253253
val tokenKey = if (isBillingAgreement) "ba_token" else "token"
254-
val switchInitiatedTime = Uri.parse(approvalUrl).getQueryParameter("switch_initiated_time")
254+
val switchInitiatedTime = approvalUrl?.toUri()?.getQueryParameter("switch_initiated_time")
255255
val isAppSwitchFlow = !switchInitiatedTime.isNullOrEmpty()
256256

257-
val contextId = approvalUrl.toUri().getQueryParameter(tokenKey)?.takeIf { it.isNotEmpty() }
257+
val contextId = approvalUrl?.toUri()?.getQueryParameter(tokenKey)?.takeIf { it.isNotEmpty() }
258258
val analyticsEventParams = AnalyticsEventParams(
259259
contextId = contextId,
260260
isVaultRequest = isVaultRequest,
@@ -305,16 +305,16 @@ class PayPalClient internal constructor(
305305
)
306306
private fun parseUrlResponseData(
307307
uri: Uri,
308-
successUrl: String,
308+
successUrl: String?,
309309
approvalUrl: String?,
310310
tokenKey: String
311311
): JSONObject {
312312
val status = uri.lastPathSegment
313-
if (Uri.parse(successUrl).lastPathSegment != status) {
313+
if (successUrl?.toUri()?.lastPathSegment != status) {
314314
throw UserCanceledException("User canceled PayPal.")
315315
}
316316

317-
val requestXoToken = Uri.parse(approvalUrl).getQueryParameter(tokenKey)
317+
val requestXoToken = approvalUrl?.toUri()?.getQueryParameter(tokenKey)
318318
val responseXoToken = uri.getQueryParameter(tokenKey)
319319
if (TextUtils.equals(requestXoToken, responseXoToken)) {
320320
val client = JSONObject().apply {

SharedUtils/src/androidTest/java/com/braintreepayments/api/sharedutils/JsonTest.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.braintreepayments.api.sharedutils
2+
3+
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
4+
import org.json.JSONException
5+
import org.json.JSONObject
6+
import org.junit.Before
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import org.junit.Assert.assertEquals
10+
import org.junit.Assert.assertNull
11+
12+
@RunWith(AndroidJUnit4ClassRunner::class)
13+
class JsonTest {
14+
15+
private lateinit var json: JSONObject
16+
17+
@Before
18+
@Throws(JSONException::class)
19+
fun setup() {
20+
json = JSONObject("{\"key\":null}")
21+
}
22+
23+
@Test
24+
fun android_optString_returnsIncorrectNullValue() {
25+
assertEquals("null", json.optString("key"))
26+
}
27+
28+
@Test
29+
fun optString_returnsCorrectNullValue() {
30+
assertNull(Json.optString(json, "key", null))
31+
}
32+
33+
@Test
34+
fun optString_returnsFallback() {
35+
assertEquals("fallback", Json.optString(json, "key", "fallback"))
36+
}
37+
38+
@Test
39+
@Throws(JSONException::class)
40+
fun optString_returnsValue() {
41+
json = JSONObject("{\"key\":\"value\"}")
42+
43+
assertEquals("value", Json.optString(json, "key", "value"))
44+
}
45+
}

SharedUtils/src/main/java/com/braintreepayments/api/sharedutils/Json.java

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.braintreepayments.api.sharedutils
2+
3+
import androidx.annotation.RestrictTo
4+
import org.json.JSONObject
5+
6+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
7+
object Json {
8+
9+
/**
10+
* Returns the value mapped by `name` from the given `json` object as a [String], or the provided nullable
11+
* `fallback`.
12+
*
13+
* If the `json` object is null, the `name` is null, or the value mapped by `name` is null, the provided nullable
14+
* `fallback` value is returned.
15+
* This works around the issue where `JSONObject.optString` returns the string "null" for null values.
16+
*
17+
* @param json The [JSONObject] to query, or null.
18+
* @param name The key to look up in the [JSONObject], or null.
19+
* @param fallback The nullable fallback value to return if the mapping does not exist or is null.
20+
* @return The mapped [String] value, or the nullable `fallback` if not found.
21+
*/
22+
fun optString(json: JSONObject?, name: String?, fallback: String?): String? {
23+
return if (json == null || name == null || json.isNull(name)) {
24+
fallback
25+
} else {
26+
if (fallback != null) {
27+
json.optString(name, fallback)
28+
} else {
29+
val result = json.optString(name)
30+
if (result.isEmpty() && json.isNull(name)) null else result
31+
}
32+
}
33+
}
34+
35+
/**
36+
* Returns the value mapped by `name` from the given `json` object as a non-null [String].
37+
*
38+
* If the `json` object is null, the `name` is null, or the value mapped by `name` is null,
39+
* the provided non-null `fallback` value is returned.
40+
*
41+
* @param json The [JSONObject] to query, or null.
42+
* @param name The key to look up in the [JSONObject], or null.
43+
* @param fallback The non-null fallback value to return if the mapping does not exist or is null.
44+
* @return The mapped [String] value, or the non-null `fallback` if not found.
45+
*/
46+
@JvmName("optStringNonNull")
47+
fun optString(json: JSONObject?, name: String?, fallback: String): String {
48+
return if (json == null || name == null || json.isNull(name)) {
49+
fallback
50+
} else {
51+
json.optString(name, fallback)
52+
}
53+
}
54+
55+
/**
56+
* Returns the value mapped by `name` from the given `json` object as a [Boolean], or the provided fallback value.
57+
*
58+
* If the `json` object is null, the `name` is null, or the value mapped by `name` is null, the provided fallback
59+
* value is returned.
60+
*
61+
* @param json The [JSONObject] to query, or null.
62+
* @param name The key to look up in the [JSONObject], or null.
63+
* @param fallback The fallback value to return if the mapping does not exist or is null.
64+
* @return The mapped [Boolean] value, or the fallback if not found.
65+
*/
66+
fun optBoolean(json: JSONObject?, name: String?, fallback: Boolean): Boolean {
67+
return if (json == null || name == null || json.isNull(name)) {
68+
fallback
69+
} else {
70+
json.optBoolean(name, fallback)
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)