generated from pricelees/issue-pr-template
refactor: MDC 유틸 모듈 이동(service -> common.utils)
This commit is contained in:
parent
ab84b329fd
commit
430630a02b
5
common/utils/build.gradle.kts
Normal file
5
common/utils/build.gradle.kts
Normal file
@ -0,0 +1,5 @@
|
||||
dependencies {
|
||||
implementation("org.slf4j:slf4j-api:2.0.17")
|
||||
|
||||
testImplementation("ch.qos.logback:logback-classic:1.5.18")
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package com.sangdol.common.utils
|
||||
|
||||
import org.slf4j.MDC
|
||||
import java.util.*
|
||||
|
||||
object MdcPrincipalIdUtil {
|
||||
const val MDC_PRINCIPAL_ID_KEY = "principal_id"
|
||||
|
||||
fun extractAsLongOrNull(): Long? {
|
||||
return MDC.get(MDC_PRINCIPAL_ID_KEY)?.toLong()
|
||||
}
|
||||
|
||||
fun extractAsOptionalLongOrEmpty(): Optional<Long> {
|
||||
return MDC.get(MDC_PRINCIPAL_ID_KEY)?.let {
|
||||
Optional.of(it.toLong())
|
||||
} ?: Optional.empty()
|
||||
}
|
||||
|
||||
fun set(id: String) {
|
||||
MDC.put(MDC_PRINCIPAL_ID_KEY, id)
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
MDC.remove(MDC_PRINCIPAL_ID_KEY)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package com.sangdol.common.utils
|
||||
|
||||
import io.kotest.core.spec.style.StringSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
import java.util.*
|
||||
|
||||
class MdcPrincipalIdUtilTest : StringSpec({
|
||||
|
||||
val id = 1872847943L
|
||||
|
||||
"값을 설정한다." {
|
||||
MdcPrincipalIdUtil.set(id.toString()).also {
|
||||
MdcPrincipalIdUtil.extractAsLongOrNull() shouldBe id
|
||||
MdcPrincipalIdUtil.extractAsOptionalLongOrEmpty() shouldBe Optional.of(id)
|
||||
}
|
||||
}
|
||||
|
||||
"값을 제거한다." {
|
||||
MdcPrincipalIdUtil.set(id.toString()).also {
|
||||
MdcPrincipalIdUtil.extractAsLongOrNull() shouldBe id
|
||||
}
|
||||
|
||||
MdcPrincipalIdUtil.clear().also {
|
||||
MdcPrincipalIdUtil.extractAsLongOrNull() shouldBe null
|
||||
MdcPrincipalIdUtil.extractAsOptionalLongOrEmpty() shouldBe Optional.empty()
|
||||
}
|
||||
}
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user