11package com.segment.analytics.kotlin.destinations.bugsnag
22
3- import android.os.Bundle
4- import io.mockk.every
5- import io.mockk.mockk
3+ import android.app.Activity
4+ import android.app.Application
5+ import android.content.Context
6+ import android.content.Intent
7+ import com.bugsnag.android.Client
8+ import com.segment.analytics.kotlin.core.*
9+ import com.segment.analytics.kotlin.core.platform.Plugin
10+ import com.segment.analytics.kotlin.core.utilities.LenientJson
11+ import io.mockk.*
12+ import io.mockk.impl.annotations.MockK
13+ import kotlinx.serialization.decodeFromString
14+ import kotlinx.serialization.json.buildJsonObject
15+ import kotlinx.serialization.json.put
16+ import org.junit.Assert.assertEquals
617import org.junit.Before
718import org.junit.Test
19+ import org.junit.jupiter.api.Assertions
820import org.junit.runner.RunWith
921import org.robolectric.RobolectricTestRunner
1022import org.robolectric.annotation.Config
1123
1224@RunWith(RobolectricTestRunner ::class )
1325@Config(manifest = Config .NONE )
14-
1526class BugsnagDestinationTests {
27+
28+ @MockK(relaxUnitFun = true )
29+ lateinit var mockApplication: Application
30+ @MockK(relaxUnitFun = true )
31+ lateinit var mockedClient: Client
32+ @MockK(relaxUnitFun = true )
33+ lateinit var mockedContext: Context
34+ @MockK(relaxUnitFun = true )
35+ lateinit var mockedAnalytics: Analytics
36+ lateinit var mockedBugsnagDestination: BugsnagDestination
37+ init {
38+ MockKAnnotations .init (this )
39+ }
40+
1641 @Before
1742 fun setUp () {
43+ mockedBugsnagDestination = BugsnagDestination ()
44+ mockedBugsnagDestination.analytics = mockedAnalytics
45+ mockedBugsnagDestination.client = mockedClient
46+ every { mockedAnalytics.configuration.application } returns mockedContext
47+ every { mockApplication.applicationContext } returns mockedContext
48+ }
1849
50+ @Test
51+ fun `settings are updated correctly` () {
52+ // An Bugsnag example settings
53+ val sampleBugsnagSettings: Settings = LenientJson .decodeFromString(
54+ """
55+ {
56+ "integrations": {
57+ "Bugsnag": {
58+ "apiKey": "APIKEY1234567890",
59+ "releaseStage": "",
60+ "useSSL": true
61+ }
62+ }
63+ }
64+ """ .trimIndent()
65+ )
66+ val bugsnagSettings: Settings = sampleBugsnagSettings
67+ mockedBugsnagDestination.update(bugsnagSettings, Plugin .UpdateType .Initial )
68+
69+ /* assertions Bugsnag config */
70+ Assertions .assertNotNull(mockedBugsnagDestination.bugsnagSettings)
71+ with (mockedBugsnagDestination.bugsnagSettings!! ) {
72+ assertEquals(apiKey, " APIKEY1234567890" )
73+ assertEquals(releaseStage, " " )
74+ assertEquals(useSSL, true )
75+ }
76+ }
77+
78+ @Test
79+ fun `activity created handled correctly` () {
80+ val activity: Activity = mockkClass(Activity ::class )
81+ every { activity.localClassName } returns " MockActivity"
82+ val intent: Intent = mockkClass(Intent ::class )
83+ every { activity.intent } returns intent
84+ mockedBugsnagDestination.onActivityCreated(activity, null )
85+ verify { mockedClient.context = " MockActivity" }
86+ }
87+
88+ @Test
89+ fun `identify is handled correctly` () {
90+ val sampleEvent = IdentifyEvent (
91+ userId = " User-Id-123" ,
92+ traits = buildJsonObject {
93+ 94+ put(" name" , " First Last" )
95+ }
96+ ).apply { context = emptyJsonObject }
97+ mockedBugsnagDestination.identify(sampleEvent)
98+ verify { mockedClient.setUser(
" User-Id-123" ,
" [email protected] " ,
" First Last" ) }
99+ verify { mockedClient.addMetadata(
" User" ,
" email" ,
" [email protected] " ) }
100+ verify { mockedClient.addMetadata(" User" , " name" , " First Last" ) }
101+ }
102+
103+ @Test
104+ fun `identify with traits is handled correctly` () {
105+ val sampleEvent = IdentifyEvent (
106+ userId = " User-Id-123" ,
107+ traits = buildJsonObject {
108+ 109+ put(" name" , " First Last" )
110+ put(" key1" , " Value 1" )
111+ put(" key2" , " Value 2" )
112+ }
113+ ).apply { context = emptyJsonObject }
114+ mockedBugsnagDestination.identify(sampleEvent)
115+ verify { mockedClient.setUser(
" User-Id-123" ,
" [email protected] " ,
" First Last" ) }
116+ verify { mockedClient.addMetadata(
" User" ,
" email" ,
" [email protected] " ) }
117+ verify { mockedClient.addMetadata(" User" , " name" , " First Last" ) }
118+ verify { mockedClient.addMetadata(" User" , " key1" , " Value 1" ) }
119+ verify { mockedClient.addMetadata(" User" , " key2" , " Value 2" ) }
120+ }
121+
122+ @Test
123+ fun `screen is handled correctly` () {
124+ val sampleEvent = ScreenEvent (
125+ name = " Screen 1" ,
126+ properties = buildJsonObject {
127+ },
128+ category = " "
129+ ).apply {
130+ context = emptyJsonObject
131+ }
132+ mockedBugsnagDestination.screen(sampleEvent)
133+ verify { mockedClient.leaveBreadcrumb(" Viewed Screen 1 Screen" ) }
19134 }
20135
21136 @Test
22- fun `sample mock test` () {
23- val bundle = mockk<Bundle >()
24- every { bundle.get(" key" ) }.returns(" value" )
137+ fun `track is handled correctly` () {
138+ val sampleEvent = TrackEvent (
139+ event = " Track 1" ,
140+ properties = buildJsonObject {
141+ }
142+ ).apply {
143+ context = emptyJsonObject
144+ }
145+ mockedBugsnagDestination.track(sampleEvent)
146+ verify { mockedClient.leaveBreadcrumb(" Track 1" ) }
25147 }
26148
27149}
0 commit comments