Skip to content

Commit

Permalink
PRISM-11149 - Pass correlationID to Risk for logging (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
precious-ossai-cko authored Apr 2, 2024
1 parent 6b87c41 commit 1479b9f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object Versions {
const val moshi = "1.15.1"

// Risk SDK Dependencies
const val riskSdk = "1.0.4"
const val riskSdk = "1.0.6"

// Unit Testing Dependencies
const val junit5Jupiter = "5.8.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import com.checkout.validation.validator.PhoneValidator
import com.squareup.moshi.Moshi

public object CheckoutApiServiceFactory {
private lateinit var correlationId: String

@JvmStatic
public fun create(
publicKey: String,
Expand All @@ -40,6 +42,7 @@ public object CheckoutApiServiceFactory {
val logger = EventLoggerProvider.provide()

logger.setup(context, environment)
correlationId = logger.correlationId

return CheckoutApiClient(
provideTokenRepository(context, publicKey, environment),
Expand Down Expand Up @@ -68,7 +71,7 @@ public object CheckoutApiServiceFactory {
logger = TokenizationEventLogger(EventLoggerProvider.provide()),
publicKey = publicKey,
cvvTokenizationNetworkDataMapper = CVVTokenizationNetworkDataMapper(),
riskSdkUseCase = RiskSdkUseCase(environment, context, publicKey, RiskInstanceProvider),
riskSdkUseCase = RiskSdkUseCase(environment, context, publicKey, correlationId, RiskInstanceProvider),
)

private fun provideNetworkApiClient(
Expand Down
5 changes: 4 additions & 1 deletion checkout/src/main/java/com/checkout/logging/EventLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ internal class EventLogger(private val logger: CheckoutEventLogger) : Logger<Log
@VisibleForTesting
var needToSetup = true

@VisibleForTesting
override lateinit var correlationId: String

override fun setup(
context: Context,
environment: Environment,
Expand All @@ -35,7 +38,7 @@ internal class EventLogger(private val logger: CheckoutEventLogger) : Logger<Log
}

override fun resetSession() {
val correlationId = UUID.randomUUID().toString()
correlationId = UUID.randomUUID().toString()
logger.addMetadata(METADATA_CORRELATION_ID, correlationId)
sentLogs.clear()
}
Expand Down
5 changes: 5 additions & 0 deletions checkout/src/main/java/com/checkout/logging/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public interface Logger<T> {
version: String = BuildConfig.PRODUCT_VERSION,
)

/**
* correlationId for metadata logs
*/
public var correlationId: String

/**
* Resets logger session.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal object RiskInstanceProvider {
context: Context,
publicKey: String,
environment: Environment,
correlationId: String,
): Risk? {
if (riskInstance != null) {
return riskInstance
Expand All @@ -31,6 +32,7 @@ internal object RiskInstanceProvider {
publicKey = publicKey,
environment = riskEnvironment,
framesMode = true,
correlationId = correlationId,
),
)
return riskInstance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ internal class RiskSdkUseCase(
private val environment: Environment,
private val context: Context,
private val publicKey: String,
private val correlationId: String,
private val riskInstanceProvider: RiskInstanceProvider,
) : UseCase<TokenResult<String>, Unit> {
override fun execute(data: TokenResult<String>) {
CoroutineScope(Dispatchers.IO).launch {
val riskInstance = riskInstanceProvider.provide(context, publicKey, environment)
val riskInstance = riskInstanceProvider.provide(context, publicKey, environment, correlationId)
when (data) {
is TokenResult.Success -> {
riskInstance?.publishData(cardToken = data.result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

public class RiskSdkUseCaseTest {
private val correlationId: String = "testCorrelationId"
private val environment: Environment = Environment.SANDBOX
private val riskInstanceProvider: RiskInstanceProvider = mockk()
private val riskInstance: Risk = mockk()
Expand All @@ -27,22 +28,23 @@ public class RiskSdkUseCaseTest {
@BeforeEach
public fun setup() {
coEvery { tokenDetails.token } returns TOKEN
coEvery { riskInstanceProvider.provide(context, PUBLIC_KEY, environment) } returns riskInstance
coEvery { riskInstanceProvider.provide(context, PUBLIC_KEY, environment, correlationId) } returns riskInstance
coEvery { riskInstance.publishData(any()) } returns mockk()
useCase =
RiskSdkUseCase(
environment = environment,
context = context,
publicKey = PUBLIC_KEY,
riskInstanceProvider = riskInstanceProvider,
correlationId = correlationId,
)
}

@Test
public fun `Success result should trigger publishData`() {
runBlocking {
useCase.execute(TokenResult.Success(tokenDetails.token))
coVerify { riskInstanceProvider.provide(context, PUBLIC_KEY, environment) }
coVerify { riskInstanceProvider.provide(context, PUBLIC_KEY, environment, correlationId) }
coVerify { riskInstance.publishData(TOKEN) }
}
}
Expand Down

0 comments on commit 1479b9f

Please sign in to comment.