Skip to content

Commit aa9d034

Browse files
committed
Update
1 parent 2bfdbb9 commit aa9d034

File tree

4 files changed

+272
-0
lines changed

4 files changed

+272
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package io.sentry.kotlin.multiplatform
2+
3+
import io.sentry.kotlin.multiplatform.extensions.updateFromKmpEventChanges
4+
import io.sentry.kotlin.multiplatform.protocol.Breadcrumb
5+
import io.sentry.kotlin.multiplatform.protocol.Message
6+
import io.sentry.kotlin.multiplatform.protocol.SentryId
7+
import io.sentry.kotlin.multiplatform.protocol.User
8+
import kotlinx.cinterop.convert
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
12+
class UpdateFromKmpEventChangesTest {
13+
@Test
14+
fun `native value is untouched when before and after values are the same`() {
15+
val cocoaEvent = CocoaSentryEvent().apply {
16+
releaseName = "1.0"
17+
dist = "1"
18+
environment = "production"
19+
serverName = "localhost"
20+
platform = "java"
21+
logger = "random logger"
22+
level = 3.convert()
23+
message = CocoaMessage().apply {
24+
message = "My Message"
25+
}
26+
fingerprint = mutableListOf("custom-fingerprints")
27+
user = CocoaUser().apply {
28+
userId = "123"
29+
}
30+
breadcrumbs = mutableListOf(
31+
CocoaBreadcrumb().apply {
32+
message = "My Breadcrumb"
33+
}
34+
)
35+
eventId = CocoaSentryId("2bcc030ccc994de890e058cf5a0527d8")
36+
}
37+
val beforeKmpEvent = SentryEvent()
38+
val afterKmpEvent = SentryEvent()
39+
40+
val updatedCocoaEvent = cocoaEvent.updateFromKmpEventChanges(beforeKmpEvent, afterKmpEvent)
41+
42+
assertEquals("1.0", updatedCocoaEvent.releaseName)
43+
assertEquals("1", updatedCocoaEvent.dist)
44+
assertEquals("production", updatedCocoaEvent.environment)
45+
assertEquals("localhost", updatedCocoaEvent.serverName)
46+
assertEquals("java", updatedCocoaEvent.platform)
47+
assertEquals("random logger", updatedCocoaEvent.logger)
48+
assertEquals(3.convert(), updatedCocoaEvent.level)
49+
assertEquals("My Message", updatedCocoaEvent.message!!.message)
50+
assertEquals(1, updatedCocoaEvent.fingerprint!!.size)
51+
assertEquals("custom-fingerprints", updatedCocoaEvent.fingerprint!!.first())
52+
assertEquals("123", updatedCocoaEvent.user!!.userId)
53+
assertEquals(1, updatedCocoaEvent.breadcrumbs!!.size)
54+
assertEquals("My Breadcrumb", (updatedCocoaEvent.breadcrumbs!!.first() as CocoaBreadcrumb).message)
55+
assertEquals(CocoaSentryId("2bcc030ccc994de890e058cf5a0527d8"), updatedCocoaEvent.eventId)
56+
}
57+
58+
@Test
59+
fun `native value is updated when before and after values are different`() {
60+
val cocoaEvent = CocoaSentryEvent().apply {
61+
releaseName = "1.0"
62+
dist = "1"
63+
environment = "production"
64+
serverName = "localhost"
65+
platform = "java"
66+
logger = "random logger"
67+
level = 1.convert()
68+
message = CocoaMessage().apply {
69+
message = "My Message"
70+
}
71+
fingerprint = mutableListOf("custom-fingerprints")
72+
user = CocoaUser().apply {
73+
userId = "123"
74+
}
75+
breadcrumbs = mutableListOf(
76+
CocoaBreadcrumb().apply {
77+
message = "My Breadcrumb"
78+
}
79+
)
80+
eventId = CocoaSentryId("2bcc030ccc994de890e058cf5a0527d8")
81+
}
82+
val beforeKmpEvent = SentryEvent(cocoaEvent)
83+
val afterKmpEvent = SentryEvent(cocoaEvent).apply {
84+
release = "2.0"
85+
dist = "123"
86+
environment = "staging"
87+
serverName = "www"
88+
platform = "kotlin"
89+
logger = "kmp logger"
90+
level = SentryLevel.DEBUG
91+
message = Message().apply {
92+
message = "Another Message"
93+
}
94+
fingerprint = mutableListOf("another-fingerprints")
95+
user = User().apply {
96+
id = "1234"
97+
}
98+
breadcrumbs = mutableListOf(
99+
Breadcrumb().apply {
100+
message = "My Breadcrumb"
101+
}
102+
)
103+
eventId = SentryId("8bcc030ccc994de890e058cf5a0527d9")
104+
}
105+
106+
val updatedCocoaEvent = cocoaEvent.updateFromKmpEventChanges(beforeKmpEvent, afterKmpEvent)
107+
108+
assertEquals("2.0", updatedCocoaEvent.releaseName)
109+
assertEquals("staging", updatedCocoaEvent.environment)
110+
assertEquals("www", updatedCocoaEvent.serverName)
111+
assertEquals("kotlin", updatedCocoaEvent.platform)
112+
assertEquals("kmp logger", updatedCocoaEvent.logger)
113+
assertEquals(0.convert(), updatedCocoaEvent.level)
114+
assertEquals("Another Message", updatedCocoaEvent.message?.message)
115+
assertEquals(1, updatedCocoaEvent.fingerprint!!.size)
116+
assertEquals("another-fingerprints", updatedCocoaEvent.fingerprint!!.first())
117+
assertEquals("1234", updatedCocoaEvent.user?.userId)
118+
assertEquals(1, updatedCocoaEvent.breadcrumbs?.size)
119+
assertEquals("My Breadcrumb", (updatedCocoaEvent.breadcrumbs?.first() as CocoaBreadcrumb).message)
120+
assertEquals(SentryId("8bcc030ccc994de890e058cf5a0527d9").toString(), updatedCocoaEvent.eventId.toString())
121+
}
122+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package io.sentry.kotlin.multiplatform
2+
3+
import io.sentry.kotlin.multiplatform.extensions.updateFromKmpEventChanges
4+
import io.sentry.kotlin.multiplatform.protocol.Breadcrumb
5+
import io.sentry.kotlin.multiplatform.protocol.Message
6+
import io.sentry.kotlin.multiplatform.protocol.SentryId
7+
import io.sentry.kotlin.multiplatform.protocol.User
8+
import kotlin.test.Test
9+
import kotlin.test.assertEquals
10+
11+
class UpdateFromKmpEventChangesTest {
12+
@Test
13+
fun `native value is untouched when before and after values are the same`() {
14+
val jvmEvent = JvmSentryEvent().apply {
15+
release = "1.0"
16+
dist = "1"
17+
environment = "production"
18+
serverName = "localhost"
19+
platform = "java"
20+
logger = "random logger"
21+
level = JvmSentryLevel.FATAL
22+
message = JvmMessage().apply {
23+
message = "My Message"
24+
}
25+
fingerprints = mutableListOf("custom-fingerprints")
26+
user = io.sentry.protocol.User().apply {
27+
id = "123"
28+
}
29+
breadcrumbs = mutableListOf(
30+
JvmBreadcrumb().apply {
31+
message = "My Breadcrumb"
32+
}
33+
)
34+
eventId = JvmSentryId("2bcc030ccc994de890e058cf5a0527d8")
35+
}
36+
val beforeKmpEvent = SentryEvent()
37+
val afterKmpEvent = SentryEvent()
38+
39+
val updatedJvmEvent = jvmEvent.updateFromKmpEventChanges(beforeKmpEvent, afterKmpEvent)
40+
41+
assertEquals("1.0", updatedJvmEvent.release)
42+
assertEquals("1", updatedJvmEvent.dist)
43+
assertEquals("production", updatedJvmEvent.environment)
44+
assertEquals("localhost", updatedJvmEvent.serverName)
45+
assertEquals("java", updatedJvmEvent.platform)
46+
assertEquals("random logger", updatedJvmEvent.logger)
47+
assertEquals(JvmSentryLevel.FATAL, updatedJvmEvent.level)
48+
assertEquals("My Message", updatedJvmEvent.message!!.message)
49+
assertEquals(1, updatedJvmEvent.fingerprints!!.size)
50+
assertEquals("custom-fingerprints", updatedJvmEvent.fingerprints!!.first())
51+
assertEquals("123", updatedJvmEvent.user!!.id)
52+
assertEquals(1, updatedJvmEvent.breadcrumbs!!.size)
53+
assertEquals("My Breadcrumb", updatedJvmEvent.breadcrumbs!!.first().message)
54+
assertEquals(JvmSentryId("2bcc030ccc994de890e058cf5a0527d8"), updatedJvmEvent.eventId)
55+
}
56+
57+
@Test
58+
fun `native value is updated when before and after values are different`() {
59+
val jvmEvent = JvmSentryEvent().apply {
60+
release = "1.0"
61+
dist = "1"
62+
environment = "production"
63+
serverName = "localhost"
64+
platform = "java"
65+
logger = "random logger"
66+
level = JvmSentryLevel.FATAL
67+
message = JvmMessage().apply {
68+
message = "My Message"
69+
}
70+
fingerprints = mutableListOf("custom-fingerprints")
71+
user = io.sentry.protocol.User().apply {
72+
id = "123"
73+
}
74+
breadcrumbs = mutableListOf(
75+
JvmBreadcrumb().apply {
76+
message = "My Breadcrumb"
77+
}
78+
)
79+
eventId = JvmSentryId("2bcc030ccc994de890e058cf5a0527d8")
80+
}
81+
val beforeKmpEvent = SentryEvent(jvmEvent)
82+
val afterKmpEvent = SentryEvent(jvmEvent).apply {
83+
release = "2.0"
84+
dist = "123"
85+
environment = "staging"
86+
serverName = "www"
87+
platform = "kotlin"
88+
logger = "kmp logger"
89+
level = SentryLevel.DEBUG
90+
message = Message().apply {
91+
message = "Another Message"
92+
}
93+
fingerprint = mutableListOf("another-fingerprints")
94+
user = User().apply {
95+
id = "123"
96+
}
97+
breadcrumbs = mutableListOf(
98+
Breadcrumb().apply {
99+
message = "My Breadcrumb"
100+
}
101+
)
102+
eventId = SentryId("8bcc030ccc994de890e058cf5a0527d9")
103+
}
104+
105+
val updatedJvmEvent = jvmEvent.updateFromKmpEventChanges(beforeKmpEvent, afterKmpEvent)
106+
107+
assertEquals("2.0", updatedJvmEvent.release)
108+
assertEquals("staging", updatedJvmEvent.environment)
109+
assertEquals("www", updatedJvmEvent.serverName)
110+
assertEquals("kotlin", updatedJvmEvent.platform)
111+
assertEquals("kmp logger", updatedJvmEvent.logger)
112+
assertEquals(JvmSentryLevel.DEBUG, updatedJvmEvent.level)
113+
assertEquals("Another Message", updatedJvmEvent.message?.message)
114+
assertEquals(1, updatedJvmEvent.fingerprints!!.size)
115+
assertEquals("another-fingerprints", updatedJvmEvent.fingerprints!!.first())
116+
assertEquals("123", updatedJvmEvent.user?.id)
117+
assertEquals(1, updatedJvmEvent.breadcrumbs?.size)
118+
assertEquals("My Breadcrumb", updatedJvmEvent.breadcrumbs?.first()?.message)
119+
assertEquals(SentryId("8bcc030ccc994de890e058cf5a0527d9").toString(), updatedJvmEvent.eventId.toString())
120+
}
121+
}

sentry-kotlin-multiplatform/src/commonTest/kotlin/io/sentry/kotlin/multiplatform/BeforeSendIntegrationTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ class BeforeSendIntegrationTest {
215215
fun `event release is not modified if KMP beforeSend callback config is not modified`() {
216216
val originalEvent = sentryEventConfigurator.originalEvent
217217
val event = sentryEventConfigurator.applyOptions {
218+
it.release = "1.0.0"
218219
it.dsn = fakeDsn
219220
}
220221
assertNotNull(event)
@@ -236,6 +237,9 @@ class BeforeSendIntegrationTest {
236237
val originalEvent = sentryEventConfigurator.originalEvent
237238
val event = sentryEventConfigurator.applyOptions {
238239
it.dsn = fakeDsn
240+
it.beforeSend = { event ->
241+
event
242+
}
239243
}
240244
assertNotNull(event)
241245
assertEquals(originalEvent.serverName, event.serverName)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.sentry.kotlin.multiplatform.utils
2+
3+
import io.sentry.kotlin.multiplatform.util.applyIfChanged
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
import kotlin.test.fail
7+
8+
class ApplyIfChangedTest {
9+
@Test
10+
fun `applyIfChanged applies change if value changed`() {
11+
var value = "old value"
12+
applyIfChanged(value, "new value") {
13+
value = it
14+
}
15+
assertEquals("new value", value)
16+
}
17+
18+
@Test
19+
fun `applyIfChanged does not apply change if value did not change`() {
20+
applyIfChanged("same value", "same value") {
21+
// This block should not be executed
22+
fail("Change was applied even though the value did not change")
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)