Skip to content

Commit 11c0bc5

Browse files
authored
Merge 1b0b11d into cc8183b
2 parents cc8183b + 1b0b11d commit 11c0bc5

File tree

5 files changed

+215
-74
lines changed

5 files changed

+215
-74
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package io.sentry.rnsentryandroidtester
2+
3+
import android.content.Context
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
import androidx.test.platform.app.InstrumentationRegistry
6+
import com.facebook.react.bridge.JavaOnlyMap
7+
import com.facebook.soloader.SoLoader
8+
import io.sentry.react.RNSentryJsonUtils
9+
import org.json.JSONArray
10+
import org.json.JSONObject
11+
import org.junit.Assert.assertEquals
12+
import org.junit.Assert.assertNotNull
13+
import org.junit.Assert.assertTrue
14+
import org.junit.Before
15+
import org.junit.Test
16+
import org.junit.runner.RunWith
17+
18+
@RunWith(AndroidJUnit4::class)
19+
class RNSentryJsonUtilsTest {
20+
@Before
21+
fun setUp() {
22+
val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
23+
SoLoader.init(context, false)
24+
}
25+
26+
@Test
27+
fun testJsonObjectToReadableMap() {
28+
val json =
29+
JSONObject().apply {
30+
put("stringKey", "stringValue")
31+
put("booleanKey", true)
32+
put("intKey", 123)
33+
}
34+
35+
val result = RNSentryJsonUtils.jsonObjectToReadableMap(json)
36+
37+
assertNotNull(result)
38+
assertTrue(result is JavaOnlyMap)
39+
assertEquals("stringValue", result?.getString("stringKey"))
40+
assertEquals(true, result?.getBoolean("booleanKey"))
41+
assertEquals(123, result?.getInt("intKey"))
42+
}
43+
44+
@Test
45+
fun testNestedJsonObjectToReadableMap() {
46+
val json =
47+
JSONObject().apply {
48+
put("stringKey", "stringValue")
49+
put("booleanKey", true)
50+
put("intKey", 123)
51+
put(
52+
"nestedKey",
53+
JSONObject().apply {
54+
put("nestedStringKey", "nestedStringValue")
55+
put("nestedBooleanKey", false)
56+
put(
57+
"deepNestedArrayKey",
58+
JSONArray().apply {
59+
put("deepNestedArrayValue")
60+
},
61+
)
62+
},
63+
)
64+
put(
65+
"arrayKey",
66+
JSONArray().apply {
67+
put("arrayStringValue")
68+
put(789)
69+
put(
70+
JSONObject().apply {
71+
put("deepNestedStringKey", "deepNestedStringValue")
72+
put("deepNestedBooleanKey", false)
73+
},
74+
)
75+
},
76+
)
77+
}
78+
79+
val result = RNSentryJsonUtils.jsonObjectToReadableMap(json)
80+
81+
assertNotNull(result)
82+
assertTrue(result is JavaOnlyMap)
83+
assertEquals("stringValue", result?.getString("stringKey"))
84+
assertEquals(true, result?.getBoolean("booleanKey"))
85+
assertEquals(123, result?.getInt("intKey"))
86+
val nested = result?.getMap("nestedKey")
87+
assertNotNull(nested)
88+
assertEquals("nestedStringValue", nested?.getString("nestedStringKey"))
89+
assertEquals(false, nested?.getBoolean("nestedBooleanKey"))
90+
val deepNestedArray = nested?.getArray("deepNestedArrayKey")
91+
assertNotNull(deepNestedArray)
92+
assertEquals("deepNestedArrayValue", deepNestedArray?.getString(0))
93+
val array = result?.getArray("arrayKey")
94+
assertNotNull(array)
95+
assertEquals("arrayStringValue", array?.getString(0))
96+
assertEquals(789, array?.getInt(1))
97+
val deepNested = array?.getMap(2)
98+
assertNotNull(deepNested)
99+
assertEquals("deepNestedStringValue", deepNested?.getString("deepNestedStringKey"))
100+
assertEquals(false, deepNested?.getBoolean("deepNestedBooleanKey"))
101+
}
102+
}

packages/core/RNSentryAndroidTester/app/src/androidTest/java/io/sentry/rnsentryandroidtester/RNSentryMapConverterTest.kt

-21
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ import com.facebook.react.bridge.JavaOnlyArray
88
import com.facebook.react.bridge.JavaOnlyMap
99
import com.facebook.soloader.SoLoader
1010
import io.sentry.react.RNSentryMapConverter
11-
import org.json.JSONObject
1211
import org.junit.Assert.assertEquals
13-
import org.junit.Assert.assertNotNull
1412
import org.junit.Assert.assertNull
15-
import org.junit.Assert.assertTrue
1613
import org.junit.Before
1714
import org.junit.Test
1815
import org.junit.runner.RunWith
@@ -700,22 +697,4 @@ class MapConverterTest {
700697

701698
assertEquals(actual, expectedMap1)
702699
}
703-
704-
@Test
705-
fun testJsonObjectToReadableMap() {
706-
val json =
707-
JSONObject().apply {
708-
put("stringKey", "stringValue")
709-
put("booleanKey", true)
710-
put("intKey", 123)
711-
}
712-
713-
val result = RNSentryMapConverter.jsonObjectToReadableMap(json)
714-
715-
assertNotNull(result)
716-
assertTrue(result is JavaOnlyMap)
717-
assertEquals("stringValue", result.getString("stringKey"))
718-
assertEquals(true, result.getBoolean("booleanKey"))
719-
assertEquals(123, result.getInt("intKey"))
720-
}
721700
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package io.sentry.react;
2+
3+
import android.content.Context;
4+
import com.facebook.react.bridge.ReadableMap;
5+
import com.facebook.react.bridge.WritableMap;
6+
import io.sentry.ILogger;
7+
import io.sentry.SentryLevel;
8+
import java.io.BufferedReader;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.Iterator;
14+
import java.util.List;
15+
import java.util.Map;
16+
import org.jetbrains.annotations.NotNull;
17+
import org.jetbrains.annotations.Nullable;
18+
import org.json.JSONArray;
19+
import org.json.JSONException;
20+
import org.json.JSONObject;
21+
22+
public final class RNSentryJsonUtils {
23+
private RNSentryJsonUtils() {
24+
throw new AssertionError("Utility class should not be instantiated");
25+
}
26+
27+
/**
28+
* Read the configuration file in the Android assets folder and return the options as a
29+
* JSONObject.
30+
*
31+
* @param context Android Context
32+
* @param fileName configuration file name
33+
* @param logger Sentry logger
34+
* @return JSONObject with the configuration options
35+
*/
36+
public static @Nullable JSONObject getOptionsFromConfigurationFile(
37+
@NotNull Context context, @NotNull String fileName, @NotNull ILogger logger) {
38+
try (InputStream inputStream = context.getAssets().open(fileName);
39+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
40+
41+
StringBuilder stringBuilder = new StringBuilder();
42+
String line;
43+
while ((line = reader.readLine()) != null) {
44+
stringBuilder.append(line);
45+
}
46+
String configFileContent = stringBuilder.toString();
47+
return new JSONObject(configFileContent);
48+
49+
} catch (Exception e) {
50+
logger.log(
51+
SentryLevel.ERROR,
52+
"Failed to read configuration file. Please make sure "
53+
+ fileName
54+
+ " exists in the root of your project.",
55+
e);
56+
return null;
57+
}
58+
}
59+
60+
private static @NotNull Map<String, Object> jsonObjectToMap(@NotNull JSONObject jsonObject) {
61+
Map<String, Object> map = new HashMap<>();
62+
Iterator<String> keys = jsonObject.keys();
63+
while (keys.hasNext()) {
64+
String key = keys.next();
65+
Object value = null;
66+
try {
67+
value = jsonObject.get(key);
68+
} catch (JSONException e) {
69+
throw new RuntimeException(e);
70+
}
71+
map.put(key, convertValue(value));
72+
}
73+
return map;
74+
}
75+
76+
private static @NotNull List<Object> jsonArrayToList(@NotNull JSONArray jsonArray) {
77+
List<Object> list = new ArrayList<>();
78+
79+
for (int i = 0; i < jsonArray.length(); i++) {
80+
Object value = jsonArray.opt(i);
81+
list.add(convertValue(value));
82+
}
83+
84+
return list;
85+
}
86+
87+
private static @Nullable Object convertValue(@Nullable Object value) {
88+
if (value instanceof JSONObject) {
89+
return jsonObjectToMap((JSONObject) value);
90+
} else if (value instanceof JSONArray) {
91+
return jsonArrayToList((JSONArray) value);
92+
} else {
93+
return value; // Primitive type or null
94+
}
95+
}
96+
97+
/**
98+
* Convert a JSONObject to a ReadableMap
99+
*
100+
* @param jsonObject JSONObject to convert
101+
* @return ReadableMap with the same data as the JSONObject
102+
*/
103+
public static @Nullable ReadableMap jsonObjectToReadableMap(@Nullable JSONObject jsonObject) {
104+
if (jsonObject == null) {
105+
return null;
106+
}
107+
Map<String, Object> map = jsonObjectToMap(jsonObject);
108+
return (WritableMap) RNSentryMapConverter.convertToJavaWritable(map);
109+
}
110+
}

packages/core/android/src/main/java/io/sentry/react/RNSentryMapConverter.java

-25
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@
1212
import io.sentry.android.core.AndroidLogger;
1313
import java.math.BigDecimal;
1414
import java.math.BigInteger;
15-
import java.util.HashMap;
16-
import java.util.Iterator;
1715
import java.util.List;
1816
import java.util.Map;
1917
import org.jetbrains.annotations.NotNull;
2018
import org.jetbrains.annotations.Nullable;
21-
import org.json.JSONException;
22-
import org.json.JSONObject;
2319

2420
public final class RNSentryMapConverter {
2521
public static final String NAME = "RNSentry.MapConverter";
@@ -202,25 +198,4 @@ private static void addValueToWritableMap(WritableMap writableMap, String key, O
202198
logger.log(SentryLevel.ERROR, "Could not convert object" + value);
203199
}
204200
}
205-
206-
public static ReadableMap jsonObjectToReadableMap(JSONObject jsonObject) {
207-
Map<String, Object> map = jsonObjectToMap(jsonObject);
208-
return (WritableMap) convertToJavaWritable(map);
209-
}
210-
211-
private static Map<String, Object> jsonObjectToMap(JSONObject jsonObject) {
212-
Map<String, Object> map = new HashMap<>();
213-
Iterator<String> keys = jsonObject.keys();
214-
while (keys.hasNext()) {
215-
String key = keys.next();
216-
Object value = null;
217-
try {
218-
value = jsonObject.get(key);
219-
} catch (JSONException e) {
220-
throw new RuntimeException(e);
221-
}
222-
map.put(key, value);
223-
}
224-
return map;
225-
}
226201
}

packages/core/android/src/main/java/io/sentry/react/RNSentrySDK.java

+3-28
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
import io.sentry.SentryLevel;
88
import io.sentry.android.core.AndroidLogger;
99
import io.sentry.android.core.SentryAndroidOptions;
10-
import java.io.BufferedReader;
11-
import java.io.InputStream;
12-
import java.io.InputStreamReader;
1310
import org.jetbrains.annotations.NotNull;
1411
import org.json.JSONObject;
1512

@@ -33,8 +30,9 @@ public static void init(
3330
@NotNull final Context context,
3431
@NotNull Sentry.OptionsConfiguration<SentryAndroidOptions> configuration) {
3532
try {
36-
JSONObject jsonObject = getOptionsFromConfigurationFile(context);
37-
ReadableMap rnOptions = RNSentryMapConverter.jsonObjectToReadableMap(jsonObject);
33+
JSONObject jsonObject =
34+
RNSentryJsonUtils.getOptionsFromConfigurationFile(context, CONFIGURATION_FILE, logger);
35+
ReadableMap rnOptions = RNSentryJsonUtils.jsonObjectToReadableMap(jsonObject);
3836
RNSentryStart.startWithOptions(context, rnOptions, configuration, null, logger);
3937
} catch (Exception e) {
4038
logger.log(
@@ -51,27 +49,4 @@ public static void init(
5149
public static void init(@NotNull final Context context) {
5250
init(context, options -> {});
5351
}
54-
55-
private static JSONObject getOptionsFromConfigurationFile(Context context) {
56-
try (InputStream inputStream = context.getAssets().open(CONFIGURATION_FILE);
57-
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
58-
59-
StringBuilder stringBuilder = new StringBuilder();
60-
String line;
61-
while ((line = reader.readLine()) != null) {
62-
stringBuilder.append(line);
63-
}
64-
String configFileContent = stringBuilder.toString();
65-
return new JSONObject(configFileContent);
66-
67-
} catch (Exception e) {
68-
logger.log(
69-
SentryLevel.ERROR,
70-
"Failed to read configuration file. Please make sure "
71-
+ CONFIGURATION_FILE
72-
+ " exists in the root of your project.",
73-
e);
74-
return null;
75-
}
76-
}
7752
}

0 commit comments

Comments
 (0)