Skip to content

Commit 4ef36ef

Browse files
committed
ECWID-144513 isJsonObject null-safety with unit-tests
1 parent 74259b8 commit 4ef36ef

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/main/kotlin/com/ecwid/apiclient/v3/ApiClientHelper.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,10 @@ private fun createAdditionalDataPolymorphicType(): PolymorphicType<FetchedReport
557557
)
558558
}
559559

560-
private fun isJsonObject(input: String): Boolean {
560+
internal fun isJsonObject(input: String?): Boolean {
561561
return try {
562-
JsonParser.parseString(input).isJsonObject
562+
input?.let { JsonParser.parseString(it).isJsonObject } ?: false
563563
} catch (_: JsonSyntaxException) {
564-
return false
564+
false
565565
}
566566
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.ecwid.apiclient.v3
2+
3+
import org.junit.jupiter.api.Test
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.ValueSource
6+
import kotlin.test.assertFalse
7+
import kotlin.test.assertTrue
8+
9+
class ApiClientHelperUnitTest {
10+
11+
@Test
12+
fun `should return true if input string is valid json`() {
13+
val testString = "{\"foo\":\"bar\"}"
14+
assertTrue { isJsonObject(testString) }
15+
}
16+
17+
@ParameterizedTest
18+
@ValueSource(strings = ["some sample text", "\"foo\":\"bar\"}", ""])
19+
fun `should return false if input string is not json`(input: String) {
20+
assertFalse { isJsonObject(input) }
21+
}
22+
23+
@Test
24+
fun `should return false if input string is null`() {
25+
val testString = null
26+
assertFalse { isJsonObject(testString) }
27+
}
28+
}

0 commit comments

Comments
 (0)