|
| 1 | +package io.sentry.react |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import com.facebook.react.bridge.JavaOnlyMap |
| 5 | +import com.facebook.react.bridge.ReadableMap |
| 6 | +import io.sentry.ILogger |
| 7 | +import io.sentry.Sentry.OptionsConfiguration |
| 8 | +import io.sentry.SentryLevel |
| 9 | +import io.sentry.android.core.SentryAndroidOptions |
| 10 | +import org.json.JSONObject |
| 11 | +import org.junit.After |
| 12 | +import org.junit.Assert.assertEquals |
| 13 | +import org.junit.Assert.assertThrows |
| 14 | +import org.junit.Before |
| 15 | +import org.junit.Test |
| 16 | +import org.junit.runner.RunWith |
| 17 | +import org.junit.runners.JUnit4 |
| 18 | +import org.mockito.ArgumentMatchers.contains |
| 19 | +import org.mockito.ArgumentMatchers.eq |
| 20 | +import org.mockito.MockedStatic |
| 21 | +import org.mockito.Mockito.mock |
| 22 | +import org.mockito.Mockito.mockStatic |
| 23 | +import org.mockito.Mockito.verify |
| 24 | +import org.mockito.MockitoAnnotations |
| 25 | + |
| 26 | +@RunWith(JUnit4::class) |
| 27 | +class RNSentrySDKTest { |
| 28 | + private val configurationFile = "sentry.options.json" |
| 29 | + |
| 30 | + private lateinit var mockLogger: ILogger |
| 31 | + private lateinit var mockContext: Context |
| 32 | + private lateinit var mockConfiguration: OptionsConfiguration<SentryAndroidOptions> |
| 33 | + private lateinit var mockedRNSentryStart: MockedStatic<RNSentryStart> |
| 34 | + private lateinit var mockedRNSentryJsonUtils: MockedStatic<RNSentryJsonUtils> |
| 35 | + |
| 36 | + @Before |
| 37 | + fun setUp() { |
| 38 | + MockitoAnnotations.openMocks(this) |
| 39 | + mockLogger = mock(ILogger::class.java) |
| 40 | + mockContext = mock(Context::class.java) |
| 41 | + mockConfiguration = mock(OptionsConfiguration::class.java) as OptionsConfiguration<SentryAndroidOptions> |
| 42 | + mockedRNSentryStart = mockStatic(RNSentryStart::class.java) |
| 43 | + mockedRNSentryJsonUtils = mockStatic(RNSentryJsonUtils::class.java) |
| 44 | + } |
| 45 | + |
| 46 | + @After |
| 47 | + fun tearDown() { |
| 48 | + mockedRNSentryStart.close() |
| 49 | + mockedRNSentryJsonUtils.close() |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `init with passed configuration callback when no valid json file is provided`() { |
| 54 | + val mockJsonObject = null |
| 55 | + mockedRNSentryJsonUtils |
| 56 | + .`when`<JSONObject> { |
| 57 | + RNSentryJsonUtils.getOptionsFromConfigurationFile(mockContext, configurationFile, mockLogger) |
| 58 | + }.thenReturn(mockJsonObject) |
| 59 | + RNSentrySDK.init(mockContext, mockConfiguration, mockLogger) |
| 60 | + |
| 61 | + verify(mockLogger).log( |
| 62 | + eq(SentryLevel.WARNING), |
| 63 | + contains("Failed to load configuration file(sentry.options.json), starting with configuration callback."), |
| 64 | + ) |
| 65 | + |
| 66 | + mockedRNSentryStart.verify { |
| 67 | + RNSentryStart.startWithConfiguration(mockContext, mockConfiguration) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun `init with passed configuration callback when no valid readable map is created`() { |
| 73 | + val mockJsonObject = JSONObject() |
| 74 | + mockedRNSentryJsonUtils |
| 75 | + .`when`<JSONObject> { |
| 76 | + RNSentryJsonUtils.getOptionsFromConfigurationFile(mockContext, configurationFile, mockLogger) |
| 77 | + }.thenReturn(mockJsonObject) |
| 78 | + val mockReadableMap = null |
| 79 | + mockedRNSentryJsonUtils |
| 80 | + .`when`<ReadableMap> { |
| 81 | + RNSentryJsonUtils.jsonObjectToReadableMap( |
| 82 | + mockJsonObject, |
| 83 | + ) |
| 84 | + }.thenReturn(mockReadableMap) |
| 85 | + |
| 86 | + RNSentrySDK.init(mockContext, mockConfiguration, mockLogger) |
| 87 | + |
| 88 | + verify(mockLogger).log( |
| 89 | + eq(SentryLevel.WARNING), |
| 90 | + contains("Failed to load configuration file(sentry.options.json), starting with configuration callback."), |
| 91 | + ) |
| 92 | + mockedRNSentryStart.verify { |
| 93 | + RNSentryStart.startWithConfiguration(mockContext, mockConfiguration) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun `init with the json file and the passed configuration when a valid json is provided`() { |
| 99 | + val mockJsonObject = JSONObject() |
| 100 | + mockedRNSentryJsonUtils |
| 101 | + .`when`<JSONObject> { |
| 102 | + RNSentryJsonUtils.getOptionsFromConfigurationFile(mockContext, configurationFile, mockLogger) |
| 103 | + }.thenReturn(mockJsonObject) |
| 104 | + val mockReadableMap = |
| 105 | + JavaOnlyMap.of( |
| 106 | + "dsn", |
| 107 | + "https://[email protected]/1234567", |
| 108 | + ) |
| 109 | + |
| 110 | + mockedRNSentryJsonUtils |
| 111 | + .`when`<ReadableMap> { |
| 112 | + RNSentryJsonUtils.jsonObjectToReadableMap( |
| 113 | + mockJsonObject, |
| 114 | + ) |
| 115 | + }.thenReturn(mockReadableMap) |
| 116 | + |
| 117 | + RNSentrySDK.init(mockContext, mockConfiguration, mockLogger) |
| 118 | + |
| 119 | + mockedRNSentryStart.verify { |
| 120 | + RNSentryStart.startWithOptions(mockContext, mockReadableMap, mockConfiguration, null, mockLogger) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun `fails with an error when there is an unhandled exception in initialisation`() { |
| 126 | + mockedRNSentryJsonUtils |
| 127 | + .`when`<JSONObject> { |
| 128 | + RNSentryJsonUtils.getOptionsFromConfigurationFile(mockContext, configurationFile, mockLogger) |
| 129 | + }.thenThrow(RuntimeException("Test exception")) |
| 130 | + |
| 131 | + val exception = |
| 132 | + assertThrows(RuntimeException::class.java) { |
| 133 | + RNSentrySDK.init(mockContext, mockConfiguration, mockLogger) |
| 134 | + } |
| 135 | + |
| 136 | + assertEquals("Failed to initialize Sentry's React Native SDK", exception.message) |
| 137 | + } |
| 138 | +} |
0 commit comments