Skip to content

Commit

Permalink
Merge pull request #32 from fattmerchantorg/release/1.2.2
Browse files Browse the repository at this point in the history
Release/1.2.2
  • Loading branch information
tuliot authored Jan 12, 2021
2 parents 650a8b6 + 821d6db commit c719e15
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.fattmerchant.cpresent

import androidx.test.core.app.ApplicationProvider
import androidx.test.runner.AndroidJUnit4
import com.fattmerchant.android.MobileReaderDriverRepository
import com.fattmerchant.omni.data.MobileReaderDriver
import com.fattmerchant.omni.data.models.Merchant
import com.fattmerchant.omni.data.models.OmniException
import com.fattmerchant.omni.usecase.InitializeDrivers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class InitializeDriversTest {

@Test
fun initializationFailsIfNoCredsProvided() {
// Setup
var error: OmniException? = null
val expectedError = MobileReaderDriver.InitializeMobileReaderDriverException("emv_password not found")
val merchant = Merchant().apply {
// Note that the empty map will cause the ChipDnaDriver to never get the creds it needs to initialize itself
options = mapOf()
}
val args = mapOf(
"appContext" to ApplicationProvider.getApplicationContext(),
"appId" to "123",
"merchant" to merchant
)
val job = InitializeDrivers(MobileReaderDriverRepository(), args, Dispatchers.Default)

// Start the job and capture the error
runBlocking {
job.start {
error = it
}
}

// Assert that the error returned is the expected one
assertEquals(error?.message, expectedError.message)
assertEquals(error?.detail, expectedError.detail)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal class ChipDnaDriver : CoroutineScope, MobileReaderDriver {
?: throw InitializeMobileReaderDriverException("merchant not found")

val apiKey = merchant.emvPassword()
?: throw InitializeMobileReaderDriverException("emvTerminalSecret not found")
?: throw InitializeMobileReaderDriverException("emv_password not found")

val params = Parameters().apply {
add(ParameterKeys.Password, "password")
Expand All @@ -124,6 +124,8 @@ internal class ChipDnaDriver : CoroutineScope, MobileReaderDriver {
val parameters = Parameters().apply {
add(ParameterKeys.SearchConnectionTypeBluetooth, ParameterValues.TRUE)
}
if (!ChipDnaMobile.isInitialized()) { return emptyList() }

ChipDnaMobile.getInstance().clearAllAvailablePinPadsListeners()

val pinPads = suspendCancellableCoroutine<List<SelectablePinPad>> { cont ->
Expand All @@ -144,6 +146,7 @@ internal class ChipDnaDriver : CoroutineScope, MobileReaderDriver {
}
}

@UseExperimental(InternalCoroutinesApi::class)
override suspend fun connectReader(reader: MobileReader): Boolean {

val requestParams = Parameters()
Expand All @@ -163,7 +166,7 @@ internal class ChipDnaDriver : CoroutineScope, MobileReaderDriver {
}

val error = params[ParameterKeys.ErrorDescription]
cont.resumeWithException(ConnectReaderException(error))
cont.tryResumeWithException(ConnectReaderException(error))
}

ChipDnaMobile.getInstance().addConnectAndConfigureFinishedListener(connectAndConfigureListener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,34 @@ internal class InitializeDrivers(
override val coroutineContext: CoroutineContext
) : CoroutineScope {

class InitializeDriversException(message: String? = null) : OmniException("Could not initialize drivers", message)
class InitializeDriversException(message: String? = null) : OmniException("Could not initialize drivers", message) {
companion object {
val NoMobileReadersFound = InitializeDriversException("Couldn't find any mobile readers")
}
}

suspend fun start(onError: (OmniException) -> Unit) {
try {
mobileReaderDriverRepository
.getDrivers()
.firstOrNull()?.initialize(args)
} catch (e: Error) {
onError(InitializeDriversException(e.message))
// Get all the drivers
val drivers = mobileReaderDriverRepository.getDrivers()

var error: OmniException? = null

// One by one, try to initialize them
val initializedDrivers = drivers.map {
try {
it.initialize(args)
} catch (e: Throwable) {
if (e is OmniException) {
error = e
}
false
}
}

when {
initializedDrivers.contains(true) -> return
error != null -> onError(error!!)
else -> onError(InitializeDriversException.NoMobileReadersFound)
}
}
}

0 comments on commit c719e15

Please sign in to comment.