Skip to content

Commit

Permalink
PRISM-11246: await risk sdk actions
Browse files Browse the repository at this point in the history
chore: update type
  • Loading branch information
precious-ossai-cko committed May 23, 2024
1 parent a829a5a commit 1592d3a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import com.checkout.tokenization.request.GooglePayTokenNetworkRequest
import com.checkout.tokenization.request.TokenRequest
import com.checkout.tokenization.response.CVVTokenDetailsResponse
import com.checkout.tokenization.response.TokenDetailsResponse
import com.checkout.tokenization.usecase.RiskSdkUseCase
import com.checkout.tokenization.utils.TokenizationConstants
import com.checkout.validation.model.ValidationResult
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONException
import org.json.JSONObject

Expand All @@ -45,7 +47,7 @@ internal class TokenRepositoryImpl(
private val logger: TokenizationLogger,
private val publicKey: String,
private val cvvTokenizationNetworkDataMapper: TokenizationNetworkDataMapper<CVVTokenDetails>,
private val riskSdkUseCase: UseCase<TokenResult<String>, Unit>,
private val riskSdkUseCase: RiskSdkUseCase,
) : TokenRepository {
@VisibleForTesting
var networkCoroutineScope =
Expand Down Expand Up @@ -126,8 +128,13 @@ internal class TokenRepositoryImpl(
launch(Dispatchers.Main) {
when (tokenResult) {
is TokenResult.Success -> {
resultHandler(CVVTokenizationResultHandler.Success(tokenResult.result))
riskSdkUseCase.execute(TokenResult.Success(tokenResult.result.token))
try {
withContext(Dispatchers.IO) {
riskSdkUseCase.execute(TokenResult.Success(tokenResult.result.token))
}
} finally {
resultHandler(CVVTokenizationResultHandler.Success(tokenResult.result))
}
}

is TokenResult.Failure -> {
Expand Down Expand Up @@ -192,15 +199,18 @@ internal class TokenRepositoryImpl(
)
}

private fun handleResponse(
private suspend fun handleResponse(
tokenResult: TokenResult<TokenDetails>,
success: (tokenDetails: TokenDetails) -> Unit,
failure: (errorMessage: String) -> Unit,
) {
when (tokenResult) {
is TokenResult.Success -> {
is TokenResult.Success -> try {
withContext(Dispatchers.IO) {
riskSdkUseCase.execute(TokenResult.Success(tokenResult.result.token))
}
} finally {
success(tokenResult.result)
riskSdkUseCase.execute(TokenResult.Success(tokenResult.result.token))
}

is TokenResult.Failure -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@ package com.checkout.tokenization.usecase

import android.content.Context
import com.checkout.base.model.Environment
import com.checkout.base.usecase.UseCase
import com.checkout.tokenization.model.TokenResult
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

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, correlationId)
when (data) {
is TokenResult.Success -> {
riskInstance?.publishData(cardToken = data.result)
}
is TokenResult.Failure -> {}
}
) {
suspend fun execute(data: TokenResult<String>) {
val riskInstance = riskInstanceProvider.provide(context, publicKey, environment, correlationId)
when (data) {
is TokenResult.Success -> riskInstance?.publishData(cardToken = data.result)
is TokenResult.Failure -> {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import com.checkout.tokenization.model.CVVTokenizationResultHandler
import com.checkout.tokenization.model.Card
import com.checkout.tokenization.model.CardTokenRequest
import com.checkout.tokenization.model.GooglePayTokenRequest
import com.checkout.tokenization.model.TokenResult
import com.checkout.tokenization.model.ValidateCVVTokenizationRequest
import com.checkout.tokenization.response.CVVTokenDetailsResponse
import com.checkout.tokenization.response.TokenDetailsResponse
import com.checkout.tokenization.usecase.RiskSdkUseCase
import com.checkout.tokenization.utils.TokenizationConstants
import com.checkout.validation.model.ValidationResult
import io.mockk.coEvery
Expand All @@ -36,6 +36,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.amshove.kluent.internal.assertEquals
Expand All @@ -57,7 +58,7 @@ internal class TokenRepositoryImplTest {
private lateinit var mockValidateTokenizationDataUseCase: UseCase<Card, ValidationResult<Unit>>

@RelaxedMockK
private lateinit var mockRiskSdkUseCase: UseCase<TokenResult<String>, Unit>
private lateinit var mockRiskSdkUseCase: RiskSdkUseCase

@RelaxedMockK
private lateinit var mockValidateCVVTokenizationDataUseCase:
Expand Down Expand Up @@ -197,7 +198,7 @@ internal class TokenRepositoryImplTest {
onFailure = { isSuccess = false },
),
)

advanceUntilIdle()
// Then
launch {
if (successHandlerInvoked) {
Expand Down Expand Up @@ -269,11 +270,33 @@ internal class TokenRepositoryImplTest {
@Nested
inner class GetGooglePayTokenNetworkRequestDetails {
@Test
fun `when sendGooglePayTokenRequest invoked with success response then success handler invoked`() {
testGooglePayTokenResultInvocation(
true,
NetworkApiResponse.Success(TokenizationRequestTestData.tokenDetailsResponse()),
fun `when sendGooglePayTokenRequest invoked with success response then success handler invoked`() = runTest {
// Given
val response = NetworkApiResponse.Success(TokenizationRequestTestData.tokenDetailsResponse())
var isSuccess = false

val testDispatcher = UnconfinedTestDispatcher(testScheduler)
Dispatchers.setMain(testDispatcher)

tokenRepositoryImpl.networkCoroutineScope = CoroutineScope(StandardTestDispatcher(testScheduler))

coEvery { mockTokenNetworkApiClient.sendGooglePayTokenRequest(any()) } returns response
coEvery { mockRiskSdkUseCase.execute(any()) } returns Unit

// When
tokenRepositoryImpl.sendGooglePayTokenRequest(
GooglePayTokenRequest(
"{protocolVersion: ECv1,signature: “test”,signedMessage: testSignedMessage}",
onSuccess = { isSuccess = true },
onFailure = { isSuccess = false },
),
)
advanceUntilIdle()

// Then
launch {
assertEquals(isSuccess, true)
}
}

@Test
Expand Down Expand Up @@ -425,6 +448,7 @@ internal class TokenRepositoryImplTest {

tokenRepositoryImpl.networkCoroutineScope = CoroutineScope(StandardTestDispatcher(testScheduler))

coEvery { mockValidateTokenizationDataUseCase.execute(any()) } returns ValidationResult.Success(Unit)
coEvery { mockTokenNetworkApiClient.sendGooglePayTokenRequest(any()) } returns response
coEvery { mockRiskSdkUseCase.execute(any()) } returns Unit

Expand All @@ -436,10 +460,10 @@ internal class TokenRepositoryImplTest {
onFailure = { isSuccess = false },
),
)

advanceUntilIdle()
// Then
launch {
assertEquals(isSuccess.toString(), successHandlerInvoked.toString())
assertEquals(successHandlerInvoked.toString(), isSuccess.toString())
}
}

Expand Down

0 comments on commit 1592d3a

Please sign in to comment.