Skip to content

Instantly share code, notes, and snippets.

@CyxouD
Last active April 8, 2019 12:16
Show Gist options
  • Save CyxouD/34ef95274ecfe2450ca18445e1a3a56b to your computer and use it in GitHub Desktop.
Save CyxouD/34ef95274ecfe2450ca18445e1a3a56b to your computer and use it in GitHub Desktop.
Tests to simulate AddEditCardPresenter actions
//... imports
class AddEditCardPresenterTest {
private lateinit var presenter: AddEditCardPresenter
private val view = mock<AddEditCardContract.View>()
private val repository = mock<VoyaRepository>()
private val scheduleProvider = ImmediateSchedulerProvider()
private val creditCardNumbers = mapOf(
CreditCardEnum.AMERICAN_EXPRESS to listOf("371449635398431",
"346221524320194",
"375303789736811",
"349033669366818",
"374449666093965",
"347416425817921"),
CreditCardEnum.DINERS_CLUB_CARD to listOf("36700102000000",
"36148900647913",
"30569309025904",
"30144189586936",
"30078727433633",
"30068783171060",
"30282579749464"),
CreditCardEnum.DISCOVER to listOf("6011111111111117",
"6011066575218560",
"6011914999687276",
"6011240433711146",
"6011863194218975",
"6011502609580526"),
CreditCardEnum.MASTER_CARD to listOf("5555555555554444",
"5454545454545454",
"5242732587924981",
"5418954133015241",
"5413500492163281",
"5416802819055837",
"5127722154119776"),
CreditCardEnum.VISA to listOf("4444333322221111",
"4911830000000",
"4917610000000000",
"4462030000000000",
"4462030000000000",
"4917610000000000003",
"4917300800000000",
"4484070000000000",
"4111111111111111",
"4716058083563507",
"4799171063814632",
"4556025279664244",
"4532354978578698",
"4673370507420425"),
CreditCardEnum.UNKNOWN to listOf("3528000700000000",
"3528206150246080")
)
@Before
fun initTest() {
presenter = AddEditCardPresenter(repository, view, scheduleProvider)
}
@Test
fun createPresenter_setsPresenterToView() {
verify(view).setPresenter(presenter)
}
@Test
fun saveCreditCard_showUnauthorizedErrorIfFailedToSubscribeWithUnauthorizedError() {
Mockito.`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.unauthorizedError))
saveCreditCard()
verify(view).showUnauthorizedError()
}
@Test
fun getCreditCardLogo_showCreditCardLogoWhenAmericanExpress() {
getCreditCardLogo_showCreditCardLogo(CreditCardEnum.AMERICAN_EXPRESS)
}
@Test
fun getCreditCardLogo_showCreditCardLogoWhenDiners() {
getCreditCardLogo_showCreditCardLogo(CreditCardEnum.DINERS_CLUB_CARD)
}
@Test
fun getCreditCardLogo_showCreditCardLogoWhenDiscover() {
getCreditCardLogo_showCreditCardLogo(CreditCardEnum.DISCOVER)
}
@Test
fun getCreditCardLogo_showCreditCardLogoWhenMastercard() {
getCreditCardLogo_showCreditCardLogo(CreditCardEnum.MASTER_CARD)
}
@Test
fun getCreditCardLogo_showCreditCardLogoWhenVisa() {
getCreditCardLogo_showCreditCardLogo(CreditCardEnum.VISA)
}
@Test
fun getCreditCardLogo_showCreditCardLogoWhenUnknown() {
val list = creditCardNumbers[CreditCardEnum.UNKNOWN]!!
list.forEach { presenter.getCreditCardLogo(it) }
verify(view, times(list.size)).showNoCreditCardLogo()
}
@Test
fun validateCreditCardNumber_showCreditCardNumberValidatedSuccessfully() {
val list = creditCardNumbers.filterKeys { it != CreditCardEnum.UNKNOWN }.values.flatten()
list.forEach { presenter.validateCreditCardNumber(it) }
verify(view, times(list.size)).showCreditCardNumberValidatedSuccessfully()
}
@Test
fun validateCreditCardNumber_showCreditCardNumberFailedToValidate() {
val list = creditCardNumbers[CreditCardEnum.UNKNOWN]!!
list.forEach { presenter.validateCreditCardNumber(it) }
verify(view, times(list.size)).showCreditCardNumberFailedToValidate()
}
@Test
fun validateCreditCardHolder_showCreditCardHolderValidatedSuccessfullyWhenOnlyFirstName() {
presenter.validateCreditCardHolder("FirstName")
verify(view).showCreditCardHolderValidatedSuccessfully()
}
@Test
fun validateCreditCardHolder_showCreditCardHolderValidatedSuccessfullyWhenFirstNameAndLastName() {
presenter.validateCreditCardHolder("FirstName LastName")
verify(view).showCreditCardHolderValidatedSuccessfully()
}
@Test
fun validateCreditCardHolder_showCreditCardHolderValidatedSuccessfullyWhenFirstNameAndLastNameAndMiddleName() {
presenter.validateCreditCardHolder("FirstName MiddleName LastName")
verify(view).showCreditCardHolderValidatedSuccessfully()
}
@Test
fun validateCreditCardHolder_showCreditCardHolderFailedToValidateWhenNumbers() {
presenter.validateCreditCardHolder("FirstName1")
presenter.validateCreditCardHolder("FirstName LastName1")
presenter.validateCreditCardHolder("FirstName MiddleName1 LastName")
verify(view, times(3)).showCreditCardHolderFailedToValidate()
}
@Test
fun validateCreditCardHolder_showCreditCardHolderFailedToValidateWhenEmptyString() {
presenter.validateCreditCardHolder("")
verify(view, times(1)).showCreditCardHolderFailedToValidate()
}
@Test
fun validateCreditCardHolder_showCreditCardHolderFailedToValidateWhenMoreThanOneWhitespaceBetweenWords() {
presenter.validateCreditCardHolder("FirstName LastName")
presenter.validateCreditCardHolder("FirstName MiddleName LastName")
verify(view, times(2)).showCreditCardHolderFailedToValidate()
}
@Test
fun validateCreditCardExpiryDate_showCreditCardExpiryDateValidatedSuccessfully() {
val allValidExpiryDates = (1..12).map { if (it / 10.0 >= 1) it.toString() else "0$it" }.map { month ->
(Calendar.getInstance().get(Calendar.YEAR) + 1..99).map { year -> if (year / 10.0 >= 1) "$month/$year" else "$month/0$year" }
}.flatten()
allValidExpiryDates.forEach { expiryDate -> presenter.validateCreditCardExpiryDate(expiryDate) }
verify(view, times(allValidExpiryDates.size)).showCreditCardExpiryDateValidatedSuccessfully()
}
@Test
fun validateCreditCardExpiryDate_showCreditCardExpiryDateFailedToValidate() {
val notValidExpiryDates = listOf("0213",
"022013",
"02/2013",
"02/203",
"02/2",
"02/20322")
notValidExpiryDates.forEach { expiryDate -> presenter.validateCreditCardExpiryDate(expiryDate) }
verify(view, times(notValidExpiryDates.size)).showCreditCardExpiryDateFailedToValidate()
}
@Test
fun validateCreditCardExpiryDate_showCreditCardExpiryDateAlreadyExpired() {
val notValidExpiryDates = listOf("02/17", "01/11", "05/18")
notValidExpiryDates.forEach { expiryDate -> presenter.validateCreditCardExpiryDate(expiryDate) }
verify(view, times(notValidExpiryDates.size)).showCreditCardExpiryDateIsAlreadyExpired()
}
@Test
fun validateCreditCardCVV_showCreditCardCvvValidatedSuccessfullyThreeNumbers() {
presenter.validateCreditCardCVV("123")
verify(view).showCreditCardCvvValidatedSuccessfully()
}
@Test
fun validateCreditCardCVV_showCreditCardCvvValidatedSuccessfullyFourNumbers() {
presenter.validateCreditCardCVV("1234")
verify(view).showCreditCardCvvValidatedSuccessfully()
}
@Test
fun validateCreditCardCVV_showCreditCardCvvFailedToValidateWhenNumberOfNumbersOtherThanThreeOrFour() {
val notValidCvv = listOf("12345",
"12",
"ahbdb",
"abc",
"abcd",
"1",
"",
" ",
" ")
notValidCvv.forEach { presenter.validateCreditCardCVV(it) }
verify(view, times(notValidCvv.size)).showCreditCardCvvFailedToValidate()
}
@Test
fun validateCreditCardTypeAndPriority_showCreditCardPriorityAndTypeValidatedSuccessfully() {
presenter.validateCreditCardTypeAndPriority("notEmpty1", "notEmpty2")
verify(view).showCreditCardPriorityAndTypeValidatedSuccessfully()
}
@Test
fun validateCreditCardTypeAndPriority_showCreditCardPriorityIsEmptyIfPriorityEmptyAndTypeNot() {
presenter.validateCreditCardTypeAndPriority("notEmpty", "")
verify(view).showCreditCardPriorityIsEmpty()
}
@Test
fun validateCreditCardTypeAndPriority_showCreditCardTypeIsEmptyIfTypeEmptyAndPriorityNot() {
presenter.validateCreditCardTypeAndPriority("", "notEmpty")
verify(view).showCreditCardTypeIsEmpty()
}
@Test
fun validateCreditCardTypeAndPriority_showCreditCardPriorityIsEmptyAndshowCreditCardTypeIsEmptyIfBothTypeAndPriorityEmpty() {
presenter.validateCreditCardTypeAndPriority("", "")
verify(view).showCreditCardTypeIsEmpty()
verify(view).showCreditCardPriorityIsEmpty()
}
@Test
fun saveCreditCard_showCreditCardFailedToSaveIncorrectNumber() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.creditCardError))
saveCreditCard()
verify(view).showCreditCardFailedToSave(R.string.invalid_number)
}
@Test
fun saveCreditCard_showCreditCardFailedToSaveBankAccountUnverified() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.creditCardBankAccountUnverifiedError))
saveCreditCard()
verify(view).showCreditCardFailedToSave(R.string.bank_account_unverified)
}
@Test
fun saveCreditCard_showCreditCardFailedToSaveExpiredDate() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.creditCardBankExpiredDateError))
saveCreditCard()
verify(view).showCreditCardFailedToSave(R.string.expired_date)
}
@Test
fun saveCreditCard_showCreditCardFailedToSaveAccountNumberInvalid() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.creditCardAccountNumberInvalidError))
saveCreditCard()
verify(view).showCreditCardFailedToSave(R.string.account_number_invalid)
}
@Test
fun saveCreditCard_showCreditCardFailedToSaveInvalidCVC() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.creditCardInvalidCVCError))
saveCreditCard()
verify(view).showCreditCardFailedToSave(R.string.invalid_cvc)
}
@Test
fun saveCreditCard_showCreditCardFailedToSaveGenericError() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.creditCardGenericError))
saveCreditCard()
verify(view).showCreditCardFailedToSave(R.string.credit_card_failed_to_save)
}
@Test
fun saveCreditCard_showCreditCardFailedToSaveExpiryMonth() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.creditCardInvalidExpiryMonthError))
saveCreditCard()
verify(view).showCreditCardFailedToSave(R.string.invalid_expiry_date)
}
@Test
fun saveCreditCard_showCreditCardFailedToSaveInvalidNumber() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.creditCardInvalidNumberError))
saveCreditCard()
verify(view).showCreditCardFailedToSave(R.string.invalid_number)
}
@Test
fun saveCreditCard_showCreditCardFailedToSaveCardDeclined() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.creditCardDeclinedError))
saveCreditCard()
verify(view).showCreditCardFailedToSave(R.string.card_declined)
}
@Test
fun saveCreditCard_showCreditCardFailedToSaveIncorrectCVC() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.creditCardIncorrectCVCError))
saveCreditCard()
verify(view).showCreditCardFailedToSave(R.string.invalid_cvc)
}
@Test
fun saveCreditCard_showCreditCardFailedToSaveInternalError() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.error(Util.creditCardInternalErrorError))
saveCreditCard()
verify(view).showCreditCardFailedToSave(R.string.internal_error)
}
@Test
fun saveCreditCard_showCreditCardSavedSuccessfully() {
`when`(repository.saveCreditCards(any())).thenReturn(Completable.complete())
saveCreditCard()
verify(view).showCreditCardSavedSuccessfully()
}
private fun saveCreditCard() {
val number = ""
val holderName = ""
val expiryDate = ""
val cvv = ""
val type = AddEditCardContract.CardType.BUSINESS
val isAirplus = true
val isPrimary = true
presenter.saveCreditCard(number, holderName, expiryDate, cvv, type, isAirplus, isPrimary)
}
private fun getCreditCardLogo_showCreditCardLogo(creditCard: CreditCardEnum) {
val list = creditCardNumbers[creditCard]!!
list.forEach { presenter.getCreditCardLogo(it) }
verify(view, times(list.size)).showCreditCardLogo(creditCard)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment