|
| 1 | +package io.sentry.react |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 5 | +import androidx.test.platform.app.InstrumentationRegistry |
| 6 | +import io.sentry.ILogger |
| 7 | +import io.sentry.Sentry |
| 8 | +import io.sentry.Sentry.OptionsConfiguration |
| 9 | +import io.sentry.android.core.AndroidLogger |
| 10 | +import io.sentry.android.core.SentryAndroidOptions |
| 11 | +import org.junit.After |
| 12 | +import org.junit.Assert.assertEquals |
| 13 | +import org.junit.Assert.assertFalse |
| 14 | +import org.junit.Assert.assertTrue |
| 15 | +import org.junit.Before |
| 16 | +import org.junit.Test |
| 17 | +import org.junit.runner.RunWith |
| 18 | + |
| 19 | +@RunWith(AndroidJUnit4::class) |
| 20 | +class RNSentrySDKTest { |
| 21 | + private val logger: ILogger = AndroidLogger(RNSentrySDKTest::class.java.simpleName) |
| 22 | + private lateinit var context: Context |
| 23 | + |
| 24 | + companion object { |
| 25 | + private const val INITIALISATION_ERROR = "Failed to initialize Sentry's React Native SDK" |
| 26 | + private const val VALID_OPTIONS = "sentry.options.json" |
| 27 | + private const val INVALID_OPTIONS = "invalid.options.json" |
| 28 | + private const val INVALID_JSON = "invalid.options.txt" |
| 29 | + private const val MISSING = "non-existing-file" |
| 30 | + |
| 31 | + private val validConfig = |
| 32 | + OptionsConfiguration<SentryAndroidOptions> { options -> |
| 33 | + options.dsn = "https://[email protected]/123456" |
| 34 | + } |
| 35 | + private val invalidConfig = |
| 36 | + OptionsConfiguration<SentryAndroidOptions> { options -> |
| 37 | + options.dsn = "invalid-dsn" |
| 38 | + } |
| 39 | + private val emptyConfig = OptionsConfiguration<SentryAndroidOptions> {} |
| 40 | + } |
| 41 | + |
| 42 | + @Before |
| 43 | + fun setUp() { |
| 44 | + context = InstrumentationRegistry.getInstrumentation().context |
| 45 | + } |
| 46 | + |
| 47 | + @After |
| 48 | + fun tearDown() { |
| 49 | + Sentry.close() |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun initialisesSuccessfullyWithDefaultValidJsonFile() { // sentry.options.json |
| 54 | + RNSentrySDK.init(context) |
| 55 | + assertTrue(Sentry.isEnabled()) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun initialisesSuccessfullyWithValidConfigurationAndDefaultValidJsonFile() { |
| 60 | + RNSentrySDK.init(context, validConfig) |
| 61 | + assertTrue(Sentry.isEnabled()) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun initialisesSuccessfullyWithValidConfigurationAndInvalidJsonFile() { |
| 66 | + RNSentrySDK.init(context, validConfig, INVALID_OPTIONS, logger) |
| 67 | + assertTrue(Sentry.isEnabled()) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun initialisesSuccessfullyWithValidConfigurationAndMissingJsonFile() { |
| 72 | + RNSentrySDK.init(context, validConfig, MISSING, logger) |
| 73 | + assertTrue(Sentry.isEnabled()) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun initialisesSuccessfullyWithValidConfigurationAndErrorInParsingJsonFile() { |
| 78 | + RNSentrySDK.init(context, validConfig, INVALID_JSON, logger) |
| 79 | + assertTrue(Sentry.isEnabled()) |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun initialisesSuccessfullyWithNoConfigurationAndValidJsonFile() { |
| 84 | + RNSentrySDK.init(context, emptyConfig, VALID_OPTIONS, logger) |
| 85 | + assertTrue(Sentry.isEnabled()) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun failsToInitialiseWithNoConfigurationAndInvalidJsonFile() { |
| 90 | + try { |
| 91 | + RNSentrySDK.init(context, emptyConfig, INVALID_OPTIONS, logger) |
| 92 | + } catch (e: Exception) { |
| 93 | + assertEquals(INITIALISATION_ERROR, e.message) |
| 94 | + } |
| 95 | + assertFalse(Sentry.isEnabled()) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun failsToInitialiseWithInvalidConfigAndInvalidJsonFile() { |
| 100 | + try { |
| 101 | + RNSentrySDK.init(context, invalidConfig, INVALID_OPTIONS, logger) |
| 102 | + } catch (e: Exception) { |
| 103 | + assertEquals(INITIALISATION_ERROR, e.message) |
| 104 | + } |
| 105 | + assertFalse(Sentry.isEnabled()) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun failsToInitialiseWithInvalidConfigAndValidJsonFile() { |
| 110 | + try { |
| 111 | + RNSentrySDK.init(context, invalidConfig, VALID_OPTIONS, logger) |
| 112 | + } catch (e: Exception) { |
| 113 | + assertEquals(INITIALISATION_ERROR, e.message) |
| 114 | + } |
| 115 | + assertFalse(Sentry.isEnabled()) |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + fun failsToInitialiseWithInvalidConfigurationAndDefaultValidJsonFile() { |
| 120 | + try { |
| 121 | + RNSentrySDK.init(context, invalidConfig) |
| 122 | + } catch (e: Exception) { |
| 123 | + assertEquals(INITIALISATION_ERROR, e.message) |
| 124 | + } |
| 125 | + assertFalse(Sentry.isEnabled()) |
| 126 | + } |
| 127 | +} |
0 commit comments