Skip to content

Commit

Permalink
fix: Crash in KitConfiguration due to NumberFormatException (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansi-mParticle authored Aug 9, 2024
1 parent d8c70f3 commit e1a23e7
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -921,12 +921,17 @@ public boolean passesBracketing(int userBucket) {

protected SparseBooleanArray convertToSparseArray(JSONObject json) {
SparseBooleanArray map = new SparseBooleanArray();
if (json == null) {
return map;
}
for (Iterator<String> iterator = json.keys(); iterator.hasNext(); ) {
try {
String key = iterator.next();
map.put(Integer.parseInt(key), json.getInt(key) == 1);
} catch (JSONException jse) {
Logger.error("Issue while parsing kit configuration: " + jse.getMessage());
} catch (Exception e) {
Logger.error("Exception while parsing kit configuration: " + e.getMessage());
}
}
return map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mparticle.kits

import android.util.SparseBooleanArray
import com.mparticle.MParticle
import com.mparticle.commerce.CommerceEvent
import com.mparticle.commerce.Impression
Expand All @@ -19,6 +20,7 @@ import org.junit.Assert
import org.junit.BeforeClass
import org.junit.Test
import org.mockito.Mockito
import java.lang.reflect.Method

class KitConfigurationTest {
private val json =
Expand Down Expand Up @@ -676,6 +678,106 @@ class KitConfigurationTest {
)
}

@Test
fun testConvertToSparseArray() {
val kitConfiguration = MockKitConfiguration()
val jsonData = """
{
"7456529": 0,
"10887805": 0,
"13992010": 0,
"15360852": 0,
"17455322": 0,
"18683141": 0,
"23029959": 0,
"41851400": 0,
"47355425": 0,
"54925556": 0,
"56409892": 0,
"66701264": 0
}
""".trimIndent()
val jsonConfiguration = JSONObject(jsonData)
val method: Method = MockKitConfiguration::class.java.getDeclaredMethod("convertToSparseArray", JSONObject::class.java)
method.isAccessible = true
val result = method.invoke(kitConfiguration, jsonConfiguration) as SparseBooleanArray
Assert.assertEquals(12, result.size())
}

@Test
fun testConvertToSparseArray_When_JSON_OBJECT_IS_NULL() {
val kitConfiguration = MockKitConfiguration()
val jsonData = """
{
"ec": {
}
}
"""
val method: Method = MockKitConfiguration::class.java.getDeclaredMethod("convertToSparseArray", JSONObject::class.java)
method.isAccessible = true
val jsonObject = JSONObject(jsonData)
val ecData = jsonObject.get("ec") as JSONObject
val result = method.invoke(kitConfiguration, ecData) as SparseBooleanArray
Assert.assertEquals(0, result.size())
}

@Test
fun testConvertToSparseArray_When_JSON_IS_NULL() {
val kitConfiguration = MockKitConfiguration()
val method: Method = MockKitConfiguration::class.java.getDeclaredMethod("convertToSparseArray", JSONObject::class.java)
method.isAccessible = true
val result = method.invoke(kitConfiguration, null) as SparseBooleanArray
Assert.assertEquals(0, result.size())
}

@Test
fun testConvertToSparseArray_When_JSON_Data_IS_INVALID() {
val kitConfiguration = MockKitConfiguration()
val jsn = """
{
"7456529": 0,
"10887805": 0,
"-36!037962": 0,
"15360852": 0,
"17455322": 0,
"18683141": 0,
"23029959": 0,
"41851400": 0,
"47355425": 0,
"54925556": 0,
"56409892": 0,
"66701264": 0
}
""".trimIndent()
val jsonConfiguration = JSONObject(jsn)
val method: Method = MockKitConfiguration::class.java.getDeclaredMethod("convertToSparseArray", JSONObject::class.java)
method.isAccessible = true

val result = method.invoke(kitConfiguration, jsonConfiguration) as SparseBooleanArray
Assert.assertEquals(11, result.size())
}

@Test
fun testConvertToSparseArray_When_JSON_OBJECT_IS_INVALID() {
val kitConfiguration = MockKitConfiguration()
val jsn = """
{
"name": "John",
"age": "30",
"18683141": 0
}
""".trimIndent()
val jsonConfiguration = JSONObject(jsn)
val method: Method = MockKitConfiguration::class.java.getDeclaredMethod(
"convertToSparseArray",
JSONObject::class.java
)
method.isAccessible = true

val result = method.invoke(kitConfiguration, jsonConfiguration) as SparseBooleanArray
Assert.assertEquals(1, result.size())
}

@Test
fun testExcludeUser() {
val kitConfiguration = KitConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ public static KitConfiguration createKitConfiguration() throws JSONException {
@Override
protected SparseBooleanArray convertToSparseArray(JSONObject json) {
SparseBooleanArray map = new MockSparseBooleanArray();
if (json == null) {
return map;
}
for (Iterator<String> iterator = json.keys(); iterator.hasNext(); ) {
try {
String key = iterator.next();
map.put(Integer.parseInt(key), json.getInt(key) == 1);
} catch (JSONException jse) {
Logger.error("Issue while parsing kit configuration: " + jse.getMessage());
} catch (Exception e) {
Logger.error("Exception while parsing kit configuration: " + e.getMessage());
}
}
return map;
Expand Down

0 comments on commit e1a23e7

Please sign in to comment.