Skip to content

Commit

Permalink
fix Tests for SpanId, add tests for SentryUUID and UUIDStringUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
lbloder committed Nov 8, 2024
1 parent 45c3cbe commit 5c2c580
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/protocol/SentryId.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SentryId(final @NotNull String sentryIdString) {
+ sentryIdString);
}
if (normalized.length() == 36) {
this.lazyStringValue = new LazyEvaluator<>(() -> normalized.replace("-", ""));
this.lazyStringValue = new LazyEvaluator<>(() -> normalize(normalized));
} else {
this.lazyStringValue = new LazyEvaluator<>(() -> normalized);
}
Expand Down
19 changes: 19 additions & 0 deletions sentry/src/test/java/io/sentry/SentryUUIDTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.sentry

import junit.framework.TestCase.assertEquals
import kotlin.test.Test

class SentryUUIDTest {

@Test
fun `generated SentryID is 32 characters long`() {
val sentryId = SentryUUID.generateSentryId()
assertEquals(32, sentryId.length)
}

@Test
fun `generated SpanID is 16 characters long`() {
val sentryId = SentryUUID.generateSpanId()
assertEquals(16, sentryId.length)
}
}
23 changes: 23 additions & 0 deletions sentry/src/test/java/io/sentry/UUIDStringUtilsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.sentry

import io.sentry.util.UUIDStringUtils
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals

class UUIDStringUtilsTest {

@Test
fun `UUID toString matches UUIDStringUtils to String`() {
val uuid = UUID.randomUUID()
val sentryIdString = uuid.toString().replace("-", "")
assertEquals(sentryIdString, UUIDStringUtils.toSentryIdString(uuid))
}

@Test
fun `UUID toString matches UUIDStringUtils to String for SpanId`() {
val uuid = UUID.randomUUID()
val sentryIdString = uuid.toString().replace("-", "").substring(0, 16)
assertEquals(sentryIdString, UUIDStringUtils.toSentrySpanIdString(uuid))
}
}
37 changes: 11 additions & 26 deletions sentry/src/test/java/io/sentry/protocol/SpanIdTest.kt
Original file line number Diff line number Diff line change
@@ -1,51 +1,36 @@
package io.sentry.protocol

import io.sentry.SentryUUID
import io.sentry.SpanId
import io.sentry.util.StringUtils
import org.mockito.Mockito
import org.mockito.kotlin.any
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import java.util.*
import kotlin.test.Test
import kotlin.test.assertEquals

class SpanIdTest {

@Test
fun `UUID is not generated on initialization`() {
val uuid = UUID.randomUUID()
Mockito.mockStatic(UUID::class.java).use { utils ->
utils.`when`<UUID> { UUID.randomUUID() }.thenReturn(uuid)
fun `ID is not generated on initialization`() {
val uuid = SentryUUID.generateSpanId()
Mockito.mockStatic(SentryUUID::class.java).use { utils ->
utils.`when`<String> { SentryUUID.generateSpanId() }.thenReturn(uuid)
val ignored = SpanId()
utils.verify({ UUID.randomUUID() }, never())
utils.verify({ SentryUUID.generateSpanId() }, never())
}
}

@Test
fun `UUID is generated only once`() {
val uuid = UUID.randomUUID()
Mockito.mockStatic(java.util.UUID::class.java).use { utils ->
utils.`when`<UUID> { UUID.randomUUID() }.thenReturn(uuid)
fun `ID is generated only once`() {
val uuid = SentryUUID.generateSpanId()
Mockito.mockStatic(SentryUUID::class.java).use { utils ->
utils.`when`<String> { SentryUUID.generateSpanId() }.thenReturn(uuid)
val spanId = SpanId()
val uuid1 = spanId.toString()
val uuid2 = spanId.toString()

assertEquals(uuid1, uuid2)
utils.verify({ UUID.randomUUID() }, times(1))
}
}

@Test
fun `normalizeUUID is only called once`() {
Mockito.mockStatic(StringUtils::class.java).use { utils ->
utils.`when`<Any> { StringUtils.normalizeUUID(any()) }.thenReturn("00000000000000000000000000000000")
val spanId = SpanId()
val uuid1 = spanId.toString()
val uuid2 = spanId.toString()

assertEquals(uuid1, uuid2)
utils.verify({ StringUtils.normalizeUUID(any()) }, times(1))
utils.verify({ SentryUUID.generateSpanId() }, times(1))
}
}
}

0 comments on commit 5c2c580

Please sign in to comment.